zerodha / pykiteconnect

The official Python client library for the Kite Connect trading APIs
MIT License
981 stars 469 forks source link

How to get historical data of indices? #78

Closed hemangjoshi37a closed 2 years ago

hemangjoshi37a commented 4 years ago

Dear sir,

There is absolute no documentation on how to read indices like NIFTY from historical_data() function or by any other way.

Please help me. Thank you in advance sir.

rahulmr commented 3 years ago

Not sure about the documentation but I think below code might help you.

...
...
import pandas as pd
from datetime import datetime as dt
from datetime import timedelta as td
...
...
... 
# do all kite object creation steps
sbin_token = kite.ltp("NSE:SBIN")['NSE:SBIN']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    sbin_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)

nf_token = kite.ltp("NSE:NIFTY 50")['NSE:NIFTY 50']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    nf_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)
bnf_token = kite.ltp("NSE:NIFTY BANK")['NSE:NIFTY BANK']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    bnf_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)
ranjanrak commented 2 years ago

The response structure section of all APIs explains the fields.

hemangjoshi37a commented 2 years ago

Not sure about the documentation but I think below code might help you.

...
...
import pandas as pd
from datetime import datetime as dt
from datetime import timedelta as td
...
...
... 
# do all kite object creation steps
sbin_token = kite.ltp("NSE:SBIN")['NSE:SBIN']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    sbin_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)

nf_token = kite.ltp("NSE:NIFTY 50")['NSE:NIFTY 50']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    nf_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)
bnf_token = kite.ltp("NSE:NIFTY BANK")['NSE:NIFTY BANK']['instrument_token']
df = pd.DataFrame(kite.historical_data(
    bnf_token, dt.today() - td(days=7), dt.today(), '15minute'))
print(df)

Thanks. Your solution works. https://hjlabs.in