I am trying to create a new column that will perform either 1 function or another depending on the value in the row in the df['Call/Put'] column. I am having difficulties performing a calculation based on certain row values as well as it determining which function to perform. Below is the last function I tried but it appears to not execute the formula properly. I have tried several ways to no avail but this is the last one I tried.
I am trying to create a new column called 'Black Scholes' and perform either bs_call if df['Call/Put']=='Call' in that row or perform bs_put if df['Call/Put']=='Put' in that row.
for index, value in df.iterrows():
df['Black Scholes'][index]=np.where((df['Call/Put']=='Call')|(df['Call/Put']==' Put'),bs_call(df['Close'][index],df['Strike Price'][index],df['Days to Expiry'][index],rf,df['Volatility'][index]),bs_put(df['Close'][index],df['Strike Price'][index],df['Days to Expiry'][index],rf,df['Volatility'][index]))
Below are the functions I use for the calculation along with a dataframe that contains 3 rows
def bs_call(S,K,T,r,sigma):
T=T/365
d1=(log(S/K)+(r+sigma**2/2)*T)/(sigma*sqrt(T))
d2= d1-sigma*sqrt(T)
ans = S*norm.cdf(d1)-K*exp(-r*T)*norm.cdf(d2)
return ans
def bs_put(S,K,T,r,sigma):
T=T/365
d1=(log(S/K)+(r+sigma**2/2)*T)/(sigma*sqrt(T))
d2= d1-sigma*sqrt(T)
ans = S*norm.cdf(d1)-K*exp(-r*T)*norm.cdf(d2)
return K*exp(-r*T)-S+ans
df = [{'Close': 27.3,
'Company': 'Barrick Gold Corporation (ABX)',
'Ticker': 'ABX',
'Yahoo Ticker': 'ABX.TO',
'Expiry Date': Timestamp('2020-03-01 00:00:00'),
'Strike Price': 19.5,
'Call/Put': 'Put',
'Days to Expiry': 2,
'Volume': 1,
'Bid Price': 0.0,
'Ask Price': 0.11,
'Open Interest': 24,
'Implied Volatility': 2.4757,
'Spread %': 100.0,
'Volatility': 0.41140252083455864},
{'Close': 27.3,
'Company': 'Barrick Gold Corporation (ABX)',
'Ticker': 'ABX',
'Yahoo Ticker': 'ABX.TO',
'Expiry Date': Timestamp('2020-03-01 00:00:00'),
'Strike Price': 23.0,
'Call/Put': 'Call',
'Days to Expiry': 2,
'Volume': 5,
'Bid Price': 4.1,
'Ask Price': 5.9,
'Open Interest': 5,
'Implied Volatility': 3.0017,
'Spread %': 30.508474576271194,
'Volatility': 0.41140252083455864},
{'Close': 27.3,
'Company': 'Barrick Gold Corporation (ABX)',
'Ticker': 'ABX',
'Yahoo Ticker': 'ABX.TO',
'Expiry Date': Timestamp('2020-03-01 00:00:00'),
'Strike Price': 24.0,
'Call/Put': 'Put',
'Days to Expiry': 2,
'Volume': 5,
'Bid Price': 0.06,
'Ask Price': 0.17,
'Open Interest': 5,
'Implied Volatility': 1.3371,
'Spread %': 64.70588235294117,
'Volatility': 0.41140252083455864}]
I think you could go with apply :
df["Black Scholes"] = df.apply(lambda r : bs_call(r) if r["Call/Put"] == "Call" else bs_put(r),axis=1)
That would work fine if you only have two possible value for the column "Call/Put" but if you're planning on having more, you should define a function that does this :
def foo(row) :
if row["Call/put"] == "value_1" :
return func_1(r)
elif ...
df["Black Scholes"] = df.apply(foo,axis=1)
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.