1

I am trying to retrieve historical stock information using NSEpy but I am unable to do so because it raises a ValueError everytime I pass a date to it and I cannot seem to figure out the problem and would appreciate any help. This is my code:

from datetime import date, timedelta

ticker = input("Please enter symbol: ")
present_date = date.today()
prev_date = date.today() - timedelta(days = 5457)
get_stock_data(ticker,start_date = present_date, end_date = prev_date)

Here is the get_stock_data() function which is in another file:

import nsepy as nse

def get_stock_data(ticker, start_date, end_date):
    data = nse.get_history(symbol=ticker, start=start_date,
                           end= end_date)

This is the Error I'm getting everytime i run the file:

Traceback (most recent call last):
  File "ConvLSTM.py", line 79, in <module>
    get_stock_data(ticker,start_date = present_date, end_date = prev_date)
  File "G:\Stocks\get_data.py", line 11, in get_stock_data
    end= end_date)
  File "C:\Users\shiva\AppData\Local\Programs\Python\Python37\lib\site-packages\nsepy\history.py", line 138, in get_hist
ory
    return get_history_quanta(**kwargs)
  File "C:\Users\shiva\AppData\Local\Programs\Python\Python37\lib\site-packages\nsepy\history.py", line 142, in get_hist
ory_quanta
    url, params, schema, headers, scaling = validate_params(**kwargs)
  File "C:\Users\shiva\AppData\Local\Programs\Python\Python37\lib\site-packages\nsepy\history.py", line 179, in validate
_params
    raise ValueError('Please check start and end dates')
ValueError: Please check start and end dates

Sorry if this is a stupid question but I tried to look for answers but couldn't find one. Thank you in advance.

4

1 回答 1

0

start date has to be less than the end date. Code below shall work:

from datetime import date, timedelta
ticker = input("Please enter symbol: ")
present_date = date.today()
prev_date = date.today() - timedelta(days = 5457)
get_stock_data(ticker,start_date = prev_date, end_date = present_date)
于 2020-04-27T10:56:37.477 回答