我如何应用此函数并在python的数据框架的新列中分配计算值?



我想计算选项希腊的增量值,并在数据框的新列中分配计算值。下面是我的代码_

#!pip install mibian
import requests
import json
import pandas as pd
import mibian
import time
session = requests.Session() # Create request session object
url1 = "https://www.nseindia.com/option-chain?type=currency"
url = "https://www.nseindia.com/api/option-chain-currency?symbol=USDINR"
headers = {
"user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/99.0.4844.74 Mobile Safari/537.36",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9"
}
request = session.get(url1, headers=headers)
cookies = dict(request.cookies)
response = session.get(url, headers=headers, cookies=cookies).text
data = json.loads(response)
exp_list = data['records']['expiryDates']
# print(exp_list)
exp_date = exp_list[0]
# print("Expiry Date: " +exp_date)
ce = {}
pe = {}
n = 0
m = 0
for i in data['records']['data']:
if i['expiryDate'] == exp_date:
try:
ce[n] = i['CE']
n = n+1
except:
pass
try:
pe[m] = i['PE']
m = n+1
except:
pass

intrestRate = 10
daysToExpiry = 1
def call_delta(a, b, c, d, e):
# BS([underlyingPrice, strikePrice, interestRate, daysToExpiration], volatility=x, 
callPrice=y, putPrice=z)
c = mibian.BS([a, b, c, d], volatility=e)
return c.callDelta
def call_theta(a, b, c, d, e):
# BS([underlyingPrice, strikePrice, interestRate, daysToExpiration], volatility=x, 
callPrice=y, putPrice=z)
c = mibian.BS([a, b, c, d], volatility=e)
return c.callTheta
def call_vega(a, b, c, d, e):
# BS([underlyingPrice, strikePrice, interestRate, daysToExpiration], volatility=x, 
callPrice=y, putPrice=z)
c = mibian.BS([a, b, c, d], volatility=e)
return c.vega

ce_df = pd.DataFrame.from_dict(ce).transpose()
ce_df = ce_df.drop(["underlying", "identifier", "openInterest", "changeinOpenInterest", 
"pchangeinOpenInterest", "totalTradedVolume", "change", "pChange", "totalBuyQuantity", 
"totalSellQuantity", "bidQty", "bidprice", "askQty", "askPrice"], axis=1)
ce_df = ce_df.rename(columns = {'strikePrice':'strike', 'expiryDate':'ce_expiryDate', 
'impliedVolatility':'ce_impliedVolatility', 'lastPrice':'ce_lastPrice', 
'underlyingValue':'ce_underlyingValue'})
ce_df["strike"] = ce_df["strike"].astype(float) # Change strike column dataType of ce_df from 
object to float
ce_df["ce_impliedVolatility"] = ce_df["ce_impliedVolatility"].astype(float) # Change 
ce_impliedVolatility column dataType of ce_df from object to float
ce_df["ce_lastPrice"] = ce_df["ce_lastPrice"].astype(float) # Change ce_lastPrice column 
dataType of ce_df from object to float
ce_df["ce_underlyingValue"] = ce_df["ce_underlyingValue"].astype(float) # Change 
ce_underlyingValue column dataType of ce_df from object to float
ce_df['ce_expiryDate'] = pd.to_datetime(ce_df['ce_expiryDate']) # Change dataType of 
ce_expiryDate column into datetime format
ce_df["ce_delta"] = ce_df.apply(call_delta(ce_df["ce_underlyingValue"], ce_df["strike"], 
int(intrestRate), int(daysToExpiry), ce_df["ce_impliedVolatility"]))
print(ce_df.head())
print(ce_df.info())

当我运行这段代码时,会抛出类似

这样的错误
TypeError: cannot convert the series to <class 'float'>

有没有人能告诉我,我怎么能解决这个问题,并在一个新的数据框架中分配计算值。还有其他方法吗?

更改以下代码行,

ce_df["ce_delta"] = ce_df.apply(call_delta(ce_df["ce_underlyingValue"], ce_df["strike"], 
int(intrestRate), int(daysToExpiry), ce_df["ce_impliedVolatility"]))

ce_df["ce_delta"] = ce_df.apply(call_delta, axis=1)

def call_delta(a, b, c, d, e):
# BS([underlyingPrice, strikePrice, interestRate, daysToExpiration], volatility=x, 
callPrice=y, putPrice=z)
c = mibian.BS([a, b, c, d], volatility=e)
return c.callDelta

def _call_delta(a, b, c, d, e):
# BS([underlyingPrice, strikePrice, interestRate, daysToExpiration], volatility=x, callPrice=y, putPrice=z)
c = mibian.BS([a, b, c, d], volatility=e)
return c.callDelta
def call_delta(row):
return _call_delta(row['ce_underlyingValue'], row['strike'], int(intrestRate), int(daysToExpiry), row['ce_impliedVolatility'])

最新更新