Issue fetching historical data
-
Hi, I used the below code earlier to fetch historical data for a range of dates and since day historical data is not allowed beyond 2000 bars in a single request, I loop through a list of dates before concats the list that contains data frames with the historical data.
My problem is that now when I use the same code, I end up getting arbitrary data i.e. lots of date ranges are missing in between the way I have shown in the e.g. below. Can someone share why this may be happening.
def hist_data(): dfs = [] frm = ['2010-07-12 09:15', '2015-01-01 09:15', '2020-01-01 09:15'] until = ['2014-12-31 15:30', '2019-12-31 15:30', '2022-11-17 15:30'] for x, y in zip(frm, until): try: historicParam = { "exchange": "NSE", "symboltoken": tokens, "interval": 'ONE_DAY', "fromdate": x, "todate": y } dfs.append(pd.DataFrame(obj.getCandleData(historicParam)['data'], columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])) except Exception as e: print(f'Historic Api failed: {e}') time.sleep(1) df = pd.concat(x for x in dfs) df['Date'] = df['Date'].str.split('T').str[0] return df for tokens, symbols in zip(nifty_50['token'], nifty_50['symbol']): stock_data = hist_data() stock_data.to_csv(f'/Users/varadjoshi/Documents/Markets Data/Price Volume Data/Nifty 500 Stocks Data/{symbols}.csv', index=False) time.sleep(1) print(symbols)
Output:
 code_text ```15/12/14 70.4 72 67.9 68.6 129777 16/12/14 68 68.05 63.7 67 193654 17/12/14 66.7 67.45 60.6 64.8 172924 18/12/14 66.45 69.35 66 67.8 328438 19/12/14 69 80 68 68.4 987759 22/12/14 68.95 71.9 68 68.8 79850 23/12/14 69.1 69.5 66.3 67.25 48193 24/12/14 66.7 68.05 65 65.45 104596 26/12/14 66.7 68 66.25 67.25 83206 29/12/14 67.65 68.4 66.8 67.2 76121 30/12/14 67.65 69.55 66 68.2 139656 31/12/14 69.4 69.7 68.2 68.95 89187 18/12/17 131 135.35 127.7 131.85 714035 19/12/17 132.8 135.9 132.05 134.8 306865 20/12/17 134 135.5 133 134.15 197805 21/12/17 134.9 139.35 133.3 136.55 550289 22/12/17 138.7 138.75 136 136.65 292362 26/12/17 151 152 140.3 141.95 2135648 27/12/17 142.9 143.5 138 138.85 694039 28/12/17 139 140.75 136.1 137.6 448845 29/12/17 137 138.6 135.1 135.75 268609 01/01/18 136 139.5 135 138.75 350672 02/01/18 139.65 139.65 135 135.95 203144 03/01/18 137 143.5 135.1 142.3 917062 04/01/18 143.8 146 143.6 144.8 569818 05/01/18 145.85 154.95 145.15 152.2 1655604 08/01/18 153 158.4 150.5 154.6 1024843 09/01/18 155.1 156 151.25 155.3 420061 10/01/18 152.2 158.9 152.2 156.8 685480 11/01/18 154.15 161.6 154.15 158.25 943735 12/01/18 158.95 174 158.3 170.5 2949954 15/01/18 173.95 190.3 173.55 185.75 2968256 16/01/18 189 189.25 170.25 172.5 1963995 17/01/18 172.9 187.9 164.25 185.15 1988863 18/01/18 187 194.9 176.7 179 2481861