1
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 时。其余所有先前的值都被覆盖。

4

1 回答 1

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
        ...
于 2019-02-23T18:48:12.207 回答