startdate = datetime.date(2018,1,1)
expirydate = datetime.date(2018,1,4)
data = dict()
for x in range(0,3):
for y in range(1,8):
data [y] = get_history(symbol="BANKNIFTY",
start= startdate,
end= startdate,
index=True,
option_type='CE',
strike_price= 27000,
expiry_date=expirydate)
startdate += datetime.timedelta(days=1)
expirydate += datetime.timedelta(days=7)
该循环效果很好,但只给了我最后一组值,即x = 3时。其余所有先前值都将被覆盖。
答案 0 :(得分:0)
您将不得不将不同的y
值与之前的x
值循环中的值区分开。例如,您可以设置data[(x, y)]
而不是data[y]
。为避免添加空值,只需在添加到字典之前添加条件以检查值:
for x in range(0,3):
for y in range(1,8):
val = get_history(symbol="BANKNIFTY",
start= startdate,
end= startdate,
index=True,
option_type='CE',
strike_price= 27000,
expiry_date=expirydate)
if val:
data[(x, y)] = val
...