Reputation: 313
Extreme Python newbie here. I'm trying to write some functions for the Black-Scholes option/greek formulas for LibreOffice Calc. I wanted to make one big module with various functions I will need to use in a few spreadsheets. I have them saved in a file called TradingUtilities.py. The first two functions look like,
def BSCall(S, K, r, sig, T):
import scipy.stats as sp
import numpy as np
d1 = (np.log(S/K) - (r - 0.5 * sig * sig) * T)/(sig*np.sqrt(T))
d2 = d1 - sig*np.sqrt(T)
P1 = sp.norm.cdf(d1)
P2 = sp.norm.cdf(d2);
call = S*P1 - K * np.exp(-r*T)*P2;
return call
def BSPUt(S, K, r, sig, T):
import scipy.stats as sp
import numpy as np
d1 = (np.log(S/K) - (r-0.5*sig*sig)*T)/(sig*np.sqrt(T))
d2 = d1 - sig*np.sqrt(T)
P1 = sp.norm.cdf(-d1)
P2 = sp.norm.cdf(-d2)
put = K*exp(-r*t)*P2 - S*P1
return put
When I run the script from the command line, the first function works fine. But I get the following error when I try to run the second,
>>> import TradingUtilities as tp
>>> tp.BSCall(238, 238.5, 0.05, 0.09, 0.09)
2.9860730330243541
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'TradingUtilities' has no attribute 'BSPut'
I am trying to figure out what's wrong, but no luck so far. If anyone can see what I am doing wrong, or point me in the right direction, I'd greatly appreciate it.
Thanks!
Upvotes: 0
Views: 484
Reputation: 59
There is typo in your function name, compared to what you wrote in command line. Python is case sensitive.
It should be:
def BSPut(S, K, r, sig, T):
Upvotes: 0
Reputation: 22885
There is typo in your code
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
should be
>>> tp.BSPUt(238, 238, 0.05, 0.09, 0.09)
Or you can change BSPUt
to BSPut
in main code.
Upvotes: 1