Fetching data just for a few names. Error in most of the codes / names
deshwalmahesh opened this issue · 1 comments
deshwalmahesh commented
UPDATE: It's not working for official names from NSE either
You can reproduce the code as below:
current_date = date.today()
stock_df(symbol='INFY', from_date = current_date - timedelta(days = 365), to_date = current_date, series="EQ")
When you get the list from this website or by another as
# ! pip install nsetools
from nsetools import Nse
nse = Nse()
all_stocks = nse.get_stock_codes()
stocks = list(all_stocks.keys())[1:]
it won't give you for all. just a few, also sometimes it fails for HDFC too.
The exact error is
JSONDecodeError Traceback (most recent call last)
/tmp/ipykernel_2736/1058062146.py in <module>
1 current_date = date.today()
----> 2 stock_df(symbol='INFY', from_date = current_date - timedelta(days = 365), to_date = current_date, series="EQ").drop(drop,axis=1)
3
~/anaconda3/envs/finance/lib/python3.7/site-packages/jugaad_data/nse/history.py in stock_df(symbol, from_date, to_date, series)
168 if not pd:
169 raise ModuleNotFoundError("Please install pandas using \n pip install pandas")
--> 170 raw = stock_raw(symbol, from_date, to_date, series)
171 df = pd.DataFrame(raw)[stock_select_headers]
172 df.columns = stock_final_headers
~/anaconda3/envs/finance/lib/python3.7/site-packages/jugaad_data/nse/history.py in stock_raw(self, symbol, from_date, to_date, series)
106 chunks = ut.pool(self._stock, params)
107
--> 108 return list(itertools.chain.from_iterable(chunks))
109
110 def derivatives_raw(self, symbol, from_date, to_date, expiry_date, instrument_type, strike_price, option_type):
~/anaconda3/envs/finance/lib/python3.7/concurrent/futures/_base.py in result_iterator()
596 # Careful not to keep a reference to the popped future
597 if timeout is None:
--> 598 yield fs.pop().result()
599 else:
600 yield fs.pop().result(end_time - time.monotonic())
~/anaconda3/envs/finance/lib/python3.7/concurrent/futures/_base.py in result(self, timeout)
426 raise CancelledError()
427 elif self._state == FINISHED:
--> 428 return self.__get_result()
429
430 self._condition.wait(timeout)
~/anaconda3/envs/finance/lib/python3.7/concurrent/futures/_base.py in __get_result(self)
382 def __get_result(self):
383 if self._exception:
--> 384 raise self._exception
385 else:
386 return self._result
~/anaconda3/envs/finance/lib/python3.7/concurrent/futures/thread.py in run(self)
55
56 try:
---> 57 result = self.fn(*self.args, **self.kwargs)
58 except BaseException as exc:
59 self.future.set_exception(exc)
~/anaconda3/envs/finance/lib/python3.7/site-packages/jugaad_data/util.py in wrapper(*args, **kw)
107 if not os.path.exists(cache_dir):
108 os.makedirs(cache_dir)
--> 109 j = function(**kw)
110 with open(path, 'wb') as fp:
111 pickle.dump(j, fp)
~/anaconda3/envs/finance/lib/python3.7/site-packages/jugaad_data/nse/history.py in _stock(self, symbol, from_date, to_date, series)
73 }
74 self.r = self._get("stock_history", params)
---> 75 j = self.r.json()
76 return j['data']
77
~/anaconda3/envs/finance/lib/python3.7/site-packages/requests/models.py in json(self, **kwargs)
908 # used.
909 pass
--> 910 return complexjson.loads(self.text, **kwargs)
911
912 @property
~/anaconda3/envs/finance/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
~/anaconda3/envs/finance/lib/python3.7/json/decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
~/anaconda3/envs/finance/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
sevakram commented
This is working for me, I'm not able to reproduce the issue.
Can you please share the full code snippet.
Possible reasons-
- You might be trying to download too much data and hence NSE might have blocked you
- The link you shared is from BSE, now there might be stocks which are listed in BSE but not in NSE or they may have different symbol