Syntax errors while running this code

What am I doing wrong in the code below:  I have tried but still get an error. any help would be appreciated.  #Fetchhing Historical data 







!pip install nsepy

from nsepy import get_history

import datetime

from datetime import date

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

from nsepy.derivatives import get_expiry_date





eexpiry = max(get_expiry_date(year=2019, month=12))

print (expiry)





nifty_fut = get_history(symbol="RELIANCE",

        start=expiry,

        end=expiry,

        futures=True,

        expiry_date=expiry)



niftydata = nifty_fut



nifty_opt = get_history(symbol="RELIANCE",

        start=expiry,

        end=expiry,

        option_type='CE',

        strike_price=1200,

        expiry_date=expiry)



optiondata = nifty_opt



y = expiry + datetime.timedelta(days=1)-----##Successfully implemented




Errors


for x in range (4,13):
    expiry = max(get_expiry_date(year=2018, month=x))
    nifty_fut = get_history (symbol="RELIANCE",
                             start=y,
                             end=expiry,
                             futures=True,
                             expiry_date=expiry)
    niftydata = niftydata.append(nifty_fut)
        
    high = nifty_fut[['Close']].max()
    low = nifty_fut[['Close']].min()
    Diff = high - low
    high = int((round(high/20)*20)+100)
    low = int((round(low/20)*20)-100)
    
    for z in range (low, high, 20):
        nifty_opt = get_history(symbol="RELIANCE",
        start=y,
        end=expiry,
        option_type='CE',
        strike_price=z,
        expiry_date=expiry)
        optiondata = optiondata.append(nifty_opt)
        nifty_opt = get_history(symbol="RELIANCE",
        start=y,
        end=expiry,
        option_type='PE',
        strike_price=z,
        expiry_date=expiry)
        optiondata = optiondata.append(nifty_opt)
    y = expiry + datetime.timedelta(days=1)


 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-b042809173f2> in <module>
      5                              end=expiry,
      6                              futures=True,
----> 7                              expiry_date=expiry)
      8     niftydata = niftydata.append(nifty_fut)
      9 



 

Hi Mukul, 



You have stored expiry dates in eexpiry and calling expiry everywhere.



for x in range (4,13):

    expiry = max(get_expiry_date(year=2018, month=x))



Try changing the year in the above code. It might solve your issue.

Thanks for your support, Vibhu…







for x in range (4,13):

    expiry = max(get_expiry_date(year=2020, month=x)) 



issue resolved for 2020 expiry, can you pls share how I can add 2021 or recent months data.

You have to change the range to get the year 2021 data.



Try this:

for x in range (1,4):

    expiry = max(get_expiry_date(year=2021, month=x))