#get dump of all NSE instruments instrument_dump = kite.instruments("NSE") instrument_df = pd.DataFrame(instrument_dump)
def tokenLookup(instrument_df,symbol_list): """Looks up instrument token for a given script from instrument dump""" token_list = [] for symbol in symbol_list: token_list.append(int(instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0])) return token_list
def tickerLookup(token): global instrument_df return instrument_df[instrument_df.instrument_token==token].tradingsymbol.values[0]
tickers = ["INFY", "ACC", "ICICIBANK"]
data = pd.DataFrame(columns=['datetime','ticker', 'ltp', 'oi']) def on_ticks(ws,ticks): global data for tick in ticks: s = pd.DataFrame([[tick['last_trade_time'],tickerLookup(tick['instrument_token']),float(tick['last_price']),int(tick['oi'])]], columns = ["datetime","ticker","ltp","oi"]) data = data.append(s, ignore_index = True)
def on_connect(ws,response): ws.subscribe(tokens) ws.set_mode(ws.MODE_FULL,tokens) # Set all token tick in `full` mode.
https://youtu.be/FCshuy9gTOQ
from kiteconnect import KiteTicker
import pandas as pd
#generate trading session here
.........
#get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)
def tokenLookup(instrument_df,symbol_list):
"""Looks up instrument token for a given script from instrument dump"""
token_list = []
for symbol in symbol_list:
token_list.append(int(instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]))
return token_list
def tickerLookup(token):
global instrument_df
return instrument_df[instrument_df.instrument_token==token].tradingsymbol.values[0]
tickers = ["INFY", "ACC", "ICICIBANK"]
data = pd.DataFrame(columns=['datetime','ticker', 'ltp', 'oi'])
def on_ticks(ws,ticks):
global data
for tick in ticks:
s = pd.DataFrame([[tick['last_trade_time'],tickerLookup(tick['instrument_token']),float(tick['last_price']),int(tick['oi'])]], columns = ["datetime","ticker","ltp","oi"])
data = data.append(s, ignore_index = True)
def on_connect(ws,response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL,tokens) # Set all token tick in `full` mode.
kws.on_ticks=on_ticks
kws.on_connect=on_connect
kws.connect()