我当前收到以下错误:
Traceback (most recent call last):
File "/Users/Stephen/Desktop/projects/Option_History/snapshot.py", line 44, in <module>
data = (current_timestamp, option['underlying'], option['symbol'], option['description'], option['strike'], option['bid'], option['ask'], option['volume'], option['greeks']['delta'], option['greeks']['gamma'], option['greeks']['theta'], option['greeks']['vega'], option['greeks']['rho'], option['greeks']['phi'], option['greeks']['mid_iv'], option['greeks']['smv_vol'])
KeyError: 'greeks'
我的代码如下所示:
import config, requests, pprint, sqlite3
from datetime import datetime
connection = sqlite3.connect('option_history.db')
cursor = connection.cursor()
try:
cursor.execute("""
CREATE TABLE option_history (
timestamp text,
underlying text,
symbol text,
description text,
strike real,
bid real,
ask real,
volume real,
delta real,
gamma real,
theta real,
vega real,
rho real,
phi real,
mid_iv real,
smv_vol real
)
""")
except:
pass
response = requests.get(config.OPTION_CHAIN_URL,
params={'symbol': 'SPY', 'expiration': '2020-12-04', 'greeks': 'true'},
headers=config.HEADERS
)
json_response = response.json()
options = json_response['options']['option']
current_timestamp = datetime.now().replace(second=0, microsecond=0).isoformat()
print(options)
for option in options:
data = (current_timestamp, option['underlying'], option['symbol'], option['description'], option['strike'], option['bid'], option['ask'], option['volume'], option['greeks']['delta'], option['greeks']['gamma'], option['greeks']['theta'], option['greeks']['vega'], option['greeks']['rho'], option['greeks']['phi'], option['greeks']['mid_iv'], option['greeks']['smv_vol'])
print(",".join(map(str, data)))
cursor.execute("""
INSERT INTO option_history (
timestamp, underlying, symbol, description, strike, bid, ask, volume, delta, gamma, theta, vega, rho, phi, mid_iv, smv_vol
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", data)
connection.commit()
connection.close()
我使用cron每5分钟运行一整天的代码,它正确地插入了我需要的所有数据,没有错误。现在,当今天尝试使用crontab再次运行它时,数据库中没有填充任何新数据
有趣的是,即使我得到了KeyError
消息,data
变量仍然正确填充,因为
print(",".join(map(str, data))
line正在使用新的API数据调用进行正确更新,并打印到控制台,没有问题
目前没有回答
相关问题 更多 >
编程相关推荐