Trying to fetch NIFTY 50 historical data (5m timeframe), but nothing comes from before 09/01/2015

AJ2610
AJ2610 edited December 2023 in API clients
I am trying to fetch historical data of nifty 50 on 5 min timeframe from 01/01/2001 to 06/12/2023 by using the following codes:

# Function to fetch historical data for a given instrument token
def fetch_historical_data(instrument_token, from_date, to_date):
url = f"https://api.kite.trade/instruments/historical/{instrument_token}/5minute"
params = {
'from': from_date.strftime('%Y-%m-%d %H:%M:%S'),
'to': to_date.strftime('%Y-%m-%d %H:%M:%S')
}
headers = {
'X-Kite-Version': '3',
'Authorization': f'token {api_key}:{access_token}'
}

response = requests.get(url, params=params, headers=headers)

if response.status_code == 200:
return response.json()
else:
print(f"Failed to fetch data for instrument token {instrument_token}. Status code: {response.status_code}, Response: {response.text}")
return None

# Specify the date range
start_date = datetime(2001, 1, 1)
end_date = datetime(2023, 12, 6)
instrument_token = 256265

# Calculate the number of 200-day intervals
interval_days = 100
num_intervals = (end_date - start_date).days // interval_days
# List to store individual instrument DataFrames with instrument_token column

nifty_df = []
instrument_data = []

for interval in range(num_intervals):
# Calculate the start and end dates for each interval
interval_start_date = start_date + timedelta(days=interval * interval_days)
interval_end_date = interval_start_date + timedelta(days=interval_days)

# Fetch historical data for the current interval
historical_data = fetch_historical_data(instrument_token, interval_start_date, interval_end_date)

# Append the data to the instrument_data list with an additional 'instrument_token' column
if historical_data:
candles = historical_data['data']['candles']
candles_with_token = [{'timestamp': candle[0], 'open': candle[1], 'high': candle[2], 'low': candle[3], 'close': candle[4], 'volume': candle[5], 'instrument_token': instrument_token} for candle in candles]
instrument_data.extend(candles_with_token)

# Create a DataFrame from the combined data
df = pd.DataFrame(instrument_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'instrument_token'])

# Convert timestamp to datetime format
df['timestamp'] = pd.to_datetime(df['timestamp'])

print(df.head())
print(df.tail())


but it is just fetching me data from 09/01/2015 to 22/09/2023. Why can I not get data from before 09/01/2015 and also after 22/09/2023.
Verify from the first 5 lines & the last 5 lines of the output smile:

timestamp open high low close \
0 2015-01-09 09:15:00+05:30 8285.45 8301.30 8285.45 8301.20
1 2015-01-09 09:20:00+05:30 8300.50 8303.00 8293.25 8301.00
2 2015-01-09 09:25:00+05:30 8301.65 8302.55 8286.80 8294.15
3 2015-01-09 09:30:00+05:30 8294.10 8295.75 8280.65 8288.50
4 2015-01-09 09:35:00+05:30 8289.10 8290.45 8278.00 8283.45 0


timestamp open high low close \
161049 2023-09-22 15:05:00+05:30 19681.65 19682.40 19661.5 19675.95
161050 2023-09-22 15:10:00+05:30 19677.25 19682.20 19664.7 19664.70
161051 2023-09-22 15:15:00+05:30 19664.35 19672.45 19658.5 19663.90
161052 2023-09-22 15:20:00+05:30 19664.50 19684.90 19664.5 19682.65
161053 2023-09-22 15:25:00+05:30 19683.60 19690.95 19675.8 19684.60

for other instruments too, zerodha API doesn't provide the latest data. Why

Tagged:
  • rakeshr
    I just checked for 256265, and it's showing the latest 5minute data.
    {'date': datetime.datetime(2023, 12, 6, 10, 45, tzinfo=tzoffset(None, 19800)),
    'open': 20904.25, 'high': 20921.2, 'low': 20904.25, 'close': 20920.6, 'volume': 0}

    Are you sure about requesting the correct to-period .... 'to': to_date.strftime('%Y-%m-%d %H:%M:%S')? Maybe print/debug it once at your end.
  • AJ2610
    I shall check again.
Sign In or Register to comment.