jugaad-py/jugaad-data

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

vgaurav3011 opened this issue · 3 comments

Issue description

The error comes up only when one makes two or more calls to the fetch_stock_data() function. This has been coming at a higher frequency for longer periods of time. Tried it multiple times after creating a thread like in the Issue in October 2020 but not resolving.

Example Code

from jugaad_data.nse import stock_csv, stock_df

def fetch_stock_data(symbol, start_year, start_month, start_day, end_year, end_month, end_day, series):
    """
        Function to fetch historical stock data of specific symbols for a given range of dates
        Input: Stock Symbol and the date range
        Output: Stock Historical Data in form of a data frame
    """
    df = stock_df(symbol=str(symbol), 
    from_date=date(start_year,start_day,start_month),to_date=date(end_year,end_month,end_day), 
    series=str(series))
    return df

techm_df = fetch_stock_data("TECHM", 2010, 1, 1, 2021, 12, 31, "EQ")

Error snippet

\local\programs\python\python39\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    344             parse_int is None and parse_float is None and
    345             parse_constant is None and object_pairs_hook is None and not kw):
--> 346         return _default_decoder.decode(s)
    347     if cls is None:
    348         cls = JSONDecoder

The stock_df() function is not working for many symbols even when the NSE server is up.
It is not working for "TECHM", "WIPRO", "HCLTECH" and many more. However, for some reason it is responding for "INFY", and "TCS". I was not able to understand why it is not working for certain symbols specifically, and cross checked with the server and also tried using another module named nsepy to check if the NSE server was not responding, but turns out it was working.

Hi @vgaurav3011

I'm sharing the internals of jugaad-data where we receive the raw response. With this we will be sure there is no other component that is causing error.

Request you to check for date and symbol combination where it is failing.

from datetime import date
from jugaad_data.nse import NSEHistory 

n = NSEHistory()
symbol = "TECHM"
from_date = date(2022,1,1)
to_date = date(2022,1,31)
series = "EQ" 
params = {
    'symbol': symbol,
    'from': from_date.strftime('%d-%m-%Y'),
    'to': to_date.strftime('%d-%m-%Y'),
    'series': '["{}"]'.format(series),
}
r = n._get("stock_history", params)
print(r.text)
j = n.r.json()
print(j)

Please ensure that there gap between from_date and to_date is not more than one month

If it fails at r = n._get("stock_history", params) then there is a bad response from NSE. If it fails at j = n.r.json() then NSE responded but it was not JSON.

Please observe the symbol, from_date, to_date combination where it is failing.

Also regarding NSEPy, it is still using the old website which might shut down anytime hence as a design choice we decided to go with the new API.