请您帮我在下面的Python代码中添加strikePrice、expiryDate和instrumentType的过滤器。
例如:如果我在strikePrice=8500、expiryDate=29-Dec-2022和instrumentType=PE上给出了过滤器,那么应该在变量中得到underlyingValue列的值。
import requests
import pandas as pd
class NseIndia2:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
self.session = requests.Session()
self.session.get("http://nseindia.com", headers=self.headers)
def get_option_chain(self, symbol):
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=' + symbol
data = self.session.get(url,headers=self.headers).json()["records"]["data"]
my_df = []
for i in data:
for k, v in i.items():
if k == "CE" or k == "PE":
info = v
info["instrumentType"] = k
my_df.append(info)
return pd.DataFrame(my_df)
nse = NseIndia2()
print(nse.get_option_chain("NIFTY"))发布于 2022-09-27 19:45:44
试试这个:
class NseIndia2:
def __init__(self):
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
self.session = requests.Session()
self.session.get("http://nseindia.com", headers=self.headers)
def get_option_chain(self, symbol):
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=' + symbol
data = self.session.get(url,headers=self.headers).json()["records"]["data"]
df = pd.DataFrame(data)
cond_sk = df.strikePrice == 8500
cond_ed = df.expiryDate == "29-Dec-2022"
cond_all = cond_sk & cond_ed
uv = df[cond_all].PE.apply(lambda x: x["underlyingValue"]).values[0]
return uv
nse = NseIndia2()
symbol = "Nifty"
print(f"The underlying value for {symbol} is: {nse.get_option_chain(symbol)}")输出:
The underlying value for NIFTY is: 17007.4https://stackoverflow.com/questions/73872478
复制相似问题