[Tutor] urgent help required! invalid syntax

James Reynolds eire1130 at gmail.com
Fri Feb 18 15:27:59 CET 2011


There's a few things I've noticed:

1. I would recommend using an IDE of some sort. I copy and pasted this into
eclipse, and it told me straight away that you had a parenthesis problem on
this line: d1=(log(s/x)+((r+v**2/2)*t)/(v*sqrt(t))

2. Your function "dividend" isn't returning a value.

3. Unless there is more to the program that you haven't shown us, all of the
math stuff that I can see, such as sqrt, pi, log, exp and possibly others
I'm not thinking of should be prefixed with math.xxxx.

4. The function BS isn't returning anything.

5. Lastly, if you are a familiar with object oriented programing, the black
scholes is better suited towards OOP.

2011/2/18 lim chee siong <doggy_lollipop at hotmail.com>

>
>
> Hi,
>
> I was writing a module for the black-scholes pricing model in python, but I
> keep getting this error message:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "C:\Python26\lib\blackscholes.py", line 25
>     d2=d1-v*sqrt(t)
>
>
> This is the code in my blackscholes.py file:
> #Black-Scholes model
>
> import math
> import numpy
> import scipy
>
> #Cumulative normal distribution
>
> def CND(x):
>
>   (a1,a2,a3,a4,a5)=(0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429)
>   L=abs(x)
>   K=1.0/(1.0+0.2316419*L)
>   w=1.0-1.0/sqrt(2*pi)*exp(-L*L/2)*(a1*K+a2*K*K+a3*pow(K,3)+a4*pow(K,4)+a5*pow(K,
> 5))
>   if x<0:
>     w=1.0-w
>   return w
>
> #s: price of underlying stock    x:strike price
> #r: continuously compounded risk free interest rate
> #t: time in years until expiration of option
> #v: implied volatility of underlying stock
>
> def dividend(s,x,r,t,v):
>   d1=(log(s/x)+((r+v**2/2)*t)/(v*sqrt(t))
>   d2=d1-v*sqrt(t)
>
> def BS(s,x,r,t,v):
>   c=s*CND(d1)-x*exp(-r*t)*CND(d2)
>   p=x*exp(-r*t)*CND(-d2)-s*CND(-d1)
>   delta=CND(d1)-1
>   gamma=CND(d1)/(s*v*sqrt(t))
>   vega=s*CND(d1)*sqrt(t)
>   theta=-((s*CND(d1)*v)/(2*sqrt(t))+r*x*exp(-r*t)*CND(-d2)
>   rho=-x*t*exp(-r*t)*CND(-d2)
>
>
> What is wrong here? What do I need to change? Thanks! Quick reply will be
> much appreciated because the deadline is tomorrow!
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110218/8bce1450/attachment.html>


More information about the Tutor mailing list