python - 如何获得实时期权链?

标签 python python-3.x

如何获取实时期权链(每分钟或如果可能的话,每秒)?另外,我想每次刷新包含此数据(从Python导出)的excel,以便刷新图表?

我编写了以下代码来获取日终数据:

#spot market inputs
s_ticker = "ITC"
start_date = date(2020,12,7)    
end_date = date(2021,1,15)    
expiry_date = date(2021,1,28)

#option market inputs    
option_tick = 5    
coverage = 100    
atm_strike = 215    
ce_type = "CE"    
pe_type = "PE"

#special attributes    
ce_strike = atm_strike    
ce_strike_min = ce_strike - coverage    
ce_strike_max = ce_strike + coverage    
pe_strike = atm_strike    
pe_strike_min = pe_strike - coverage    
pe_strike_max = pe_strike + coverage

#data fetching    
ce_df_exp = pd.DataFrame()    
ce_strike_temp = ce_strike_min    
while (ce_strike_temp < ce_strike_max):    
    ce_df = get_history(symbol=s_ticker,    
                            start=start_date,    
                            end=end_date,    
                            option_type=ce_type,
                            strike_price=ce_strike_temp,
                            expiry_date=expiry_date)
    temp1 = ce_df
    ce_df_exp = ce_df_exp.append(temp1.tail(1))    
    temp1 = pd.DataFrame()    
    ce_strike_temp = ce_strike_temp + option_tick    
ce_df_exp

最佳答案

尝试用这种方式编码。

import requests
import json
import pandas as pd
from pandas.io.json import json_normalize

urlheader = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
    "authority": "www.nseindia.com",
    "scheme":"https"
}

expiry_dt = '28-Jan-2021'
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
data = requests.get(url, headers=urlheader).content
data2 = data.decode('utf-8')
df = json.loads(data2)
json_ce = eval("[data['CE'] for data in df['records']['data'] if 'CE' in data and data['expiryDate'] == '" + expiry_dt + "']")
df_ce = json_normalize(json_ce)
print('*** NIFTY Call Options Data with Expiry Date: '+ expiry_dt + ' *** \n', df_ce)
json_pe = eval("[data['CE'] for data in df['records']['data'] if 'PE' in data and data['expiryDate'] == '" + expiry_dt + "']")
df_pe = json_normalize(json_pe)
print('*** NIFTY Put Options Data with Expiry Date: '+ expiry_dt + ' *** \n', df_pe)

关于python - 如何获得实时期权链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65757973/

相关文章:

python - PyInstaller 可执行文件无法获取 TorchScript 的源代码

python - 灰度图像不是 jpeg

python - 为什么我从 pandas/matplotlib 收到错误 "OverflowError: Python int too large to convert to C long"?

python - 设置类组成对象的属性的功能

Python没有抛出缩进错误,但没有执行代码

python - python中的BFS算法

python - 如何使用python迭代scrapy中的节点

python-3.x - 如何替换 Tkinter 应用程序中的图标

python - 当我们安装了较新版本的Python时,如何安装旧版本的Python

python-3.x - PyQt5 : How to delete widget once video gets over and put a picture at the place of video player