简体   繁体   中英

Python seems to be messing up a calculation

I am trying to calculate d1 for options (stocks). This is my formula:

d1_calc = (math.log(spot / strikeprice) + (timedelta/365) * ((vol**2)/2) / (vol * (math.sqrt(timedelta/365)))

when I individually print each variable it is correct, but when I do the entire calculation the resulting d1s are absolutely not correct. I have no idea what I have done wrong. I use the same numbers and formula in my excel spreadsheet and it works just fine. Please help!!

The code you posted isn't correct. You're missing a bracket. You either need to add a bracket right on the end to pair with the initial bracket like this:

d1_calc = (math.log(spot / strikeprice) + (timedelta/365) * ((vol**2)/2) / (vol * (math.sqrt(timedelta/365))))

Or more likely you need to add a bracket before the division like this:

d1_calc = (math.log(spot / strikeprice) + (timedelta/365) * ((vol**2)/2)) / (vol * (math.sqrt(timedelta/365)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM