taylor - 1 year ago 162

Python Question

This is my python code in ipython notebook

`import sys`

sys.path.append('C:/Users/dell/.ipynb_checkpoints/bsm_functions.py')

tol=0.5

for option in options_data.index:

forward=futures_data[futures_data['MATURITY']==\

options_data.loc[option]['MATURITY']]['PRICE'].values[0]

if(forward*(1-tol)<options_data.loc[option]['STRIKE']

<forward*(1+tol)):

imp_vol=bsm_call_imp_vol(v0,

options_data.loc[option]['STRIKE'],

options_data.loc[option]['TTM'],r,

options_data.loc[option]['PRICE'],

sigma_est=2,it=100)

options_data['IMP_VOL'].loc[option]=imp_vol

this the module I wrote:

`def bsm_call_value(S0,K,T,r,sigma):`

from math import log,sqrt,exp

from scipy import stats

S0=float(S0)

d1=(log(S0/K)+(r+0.5*sigma**2)*T)/(sigma*sqrt(T))

d2=(log(S0/K)+(r-0.5*sigma**2)*T)/(sigma*sqrt(T))

value=(S0*stats.norm.cdf(d1,0.0,1.0)-K*exp(-r- T)*stats.norm.cdf(d2,0.0,1.0))

return value

def bsm_vega(S0,K,T,r,sigma):

from math import log,sqrt

from scipy import stats

S0=float(S0)

d1 = (log(S0 / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * sqrt(T))

vega=S0*stats.norm.cdf(d1,0.0,1.0)*sqrt(T)

return vega

def bsm_call_imp_vol(S0,K,T,r,C0,sigma_est,it=100):

for i in range(it):

sigma_est-=((bsm_call_value(S0,K,T,r,sigma_est)-C0)/bsm_vega(S0,K,T,r,sigma_est))

return sigma_est

my error is:

`name 'bsm_call_imp_vol' is not defined`

I do not know the reason of this error

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You need to **import** your module. You add the *parent directory* of a module to your `sys.path`

, then import names from the module:

```
sys.path.append('C:/Users/dell/.ipynb_checkpoints')
from bsm_functions import bsm_call_imp_vol
```

Adding the path to a `.py`

file itself to `sys.path`

does not import the module.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**