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)

The loop works well but only gives me last set of values i.e. when x = 3. Rest all previous values gets overridden.

lczapski
  • 4,026
  • 3
  • 16
  • 32
  • You are going to have to differentiate the different `y` values from the ones in the previous loops of `x` values. For instance you can set `data[(x, y)]` instead of `data[y]`. – busybear Feb 22 '19 at 20:19
  • Thanks it worked, One more thing I need to ask. How can I delete empty values in dictionary as it throws empty value on Saturday and Sundays – Puneet Tewani Feb 22 '19 at 20:33
  • I would add a conditional to avoid ever adding it to your dictionary. – busybear Feb 22 '19 at 21:29
  • What could be the condition. Tried ifelse but it's not working the way it should. Please suggest. – Puneet Tewani Feb 23 '19 at 16:08

1 Answers1

0

You are going to have to differentiate the different y values from the ones in the previous loops of x values. For instance you can set data[(x, y)] instead of data[y]. To avoid adding empty values, just add the conditional to check for a value before setting it to your dictionary:

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
        ...
busybear
  • 10,194
  • 1
  • 25
  • 42