Python – Binomial Option Pricing Code [closed]-Collection of common programming errors

I am trying to finish this code, but I keep getting an unknown error message. I don’t understand what I am doing wrong. Sorry, I am new to python!

I would appreciate any help!!

import math

def nCr(n,r):
    f = math.factorial
    return f(n) / f(r) / f(n-r)

class Option(object):

    def __init__(self,s0,u,d,r,t,strike):
        self.s0=s0
        self.u=u
        self.d=d
        self.r=r
        self.t=t
        self.strike=strike

    def price(self):
        q = (self.r - self.d) / (self.u - self.d)
        prc = 0
        temp_stock = 0
        temp_payout = 0
        for x in range(1,self.t+1):
            temp_stock = self.strike*(self.u**(4-x))*(self.d**(x-1))
            temp_payout = max(temp_stock-self.strike,0)
            prc += nCr(self.t,x-1)*(q**(4-x))*((1-q)**(x-1))*temp_payout
        prc = prc / (self.r**self.t)
        return prc


newOption = Option(100,1.07,0.93458,1.01,3,100)
print newOption.price()
  1. There are at least the following problems with your code:

    • you have to include the math module at the beginning of your program:

      import math

    • you have to replace the “^” operator by “**” operator.

    (If that isn’t enough check the indentation, as your indentation in this message has a small problem: the function price() is shifted to the left)

    With these corrections your program produces an output: 6.77357485848.

    Check if that’s your desired output.

Originally posted 2013-11-09 22:22:41.