It looks like you're new here. If you want to get involved, click one of these buttons!
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import pandas as pd
import xlwings as xw
import datetime
import numpy as np
kws = ""
kite = ""
api_k = "REMOVED" #api_key
api_s = "REMOVED"
df_cols = ["Tstamp","Token","LTP","BQ","SQ"] #defining coloumn name for pandas dataframe
df = pd.DataFrame(data=[], columns=df_cols, index=[]) #dataframe definition
tradedic = {3050241:"YESBANK",895745:"TATASTEEL",1887745:"STAR",779521:"SBIN",738561:"RELIANCE",3834113:"POWERGRID",2905857:"PETRONET",633601:"ONGC",4464129:"OIL",2815745:"MARUTI",519937:"M&M",2939649:"LT",2061825:"KTKBANK",492033:"KOTAKBANK",3001089:"JSWSTEEL",424961:"ITC",415745:"IOC",1346049:"INDUSINDBK",2883073:"IGL",1270529:"ICICIBANK",356865:"HINDUNILVR",345089:"HEROMOTOCO",341249:"HDFCBANK",340481:"HDFC",1207553:"GAIL",261889:"FEDERALBNK",3513601:"DCBBANK",197633:"DABUR",3876097:"COLPAL",5215745:"COALINDIA",177665:"CIPLA",3905025:"CEATLTD",320001:"CASTROLIND",2763265:"CANBK",134657:"BPCL",558337:"BOSCHLTD",108033:"BHARATFORG",94977:"BATAINDIA",1195009:"BANKBARODA",4267265:"BAJAJ-AUTO",1510401:"AXISBANK",60417:"ASIANPAINT",54273:"ASHOKLEY",49409:"ARVIND",41729:"APOLLOTYRE",40193:"APOLLOHOSP",325121:"AMBUJACEM",25601:"AMARAJABAT",5633:"ACC"
}
pd.set_option('display.max_rows', 1000000)
pd.set_option('display.max_columns', 15)
pd.set_option('display.width',300)
wb = xw.Book('LiveTradeData.xlsx')
sht = wb.sheets['Sheet1']
def get_login(api_k,api_s): #login to zerodha api panel
global kws,kite,j
kite = KiteConnect(api_key=api_k)
print("[*] Generate access Token :", kite.login_url())
request_tkn = input("[*] Enter your request token here:")
data = kite.generate_session(request_tkn,api_secret=api_s)
kite.set_access_token(data["access_token"])
kws = KiteTicker(api_k, data["access_token"])
get_login(api_k,api_s) #function that is used to get connected to api
def on_ticks(ws,ticks):
global j, df, df_cols, tradedic
#keeps extracted tick data
for current_tick in ticks:
sht.range('A' + str(j)).value = datetime.datetime.now()
sht.range('B' + str(j)).value = current_tick['instrument_token']
sht.range('C' + str(j)).value = current_tick['last_price']
sht.range('D' + str(j)).value = current_tick['buy_quantity']
sht.range('E' + str(j)).value = current_tick['sell_quantity']
sht.range('F' + str(j)).value = current_tick['volume']
j = j + 1
def on_connect(ws,response):
ws.subscribe([3050241,895745,1887745,779521,738561,3834113,2905857,633601,4464129,2815745,519937,2939649,2061825,492033,3001089,424961,415745,1346049,2883073,1270529,356865,345089,341249,340481,1207553,261889,3513601,197633,3876097,5215745,177665,3905025,320001,2763265,134657,558337,108033,94977,1195009,4267265,1510401,60417,54273,49409,41729,40193,325121,25601,5633]) #subscribing to instruments with maximum margins respectively
ws.set_mode(ws.MODE_QUOTE,[3050241,895745,1887745,779521,738561,3834113,2905857,633601,4464129,2815745,519937,2939649,2061825,492033,3001089,424961,415745,1346049,2883073,1270529,356865,345089,341249,340481,1207553,261889,3513601,197633,3876097,5215745,177665,3905025,320001,2763265,134657,558337,108033,94977,1195009,4267265,1510401,60417,54273,49409,41729,40193,325121,25601,5633])
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()
Connection error: 1006 - connection was closed uncleanly (None)
Connection closed: 1006 - connection was closed uncleanly (None)
This keeps on repeating in a few seconds and my program keeps on running. Apart from this no error message is displayed. However, i dont get all the ticks. Number of ticks are reduced drastically. When i tried running it yesterday, i got around 7000 tick data for these 50 instruments during 2.5 hours of live market.
We checked your above code(except removing part of on_tick method of extracting tick data in excel).It's working fine.Can you re-check extracting tick data into excel part , it shouldn't be blocking main tick thread?
The on_ticks function is getting ticks every second. However, my logic (which i have not shown in the above code) which is basically operation on pandas dataframe is taking more and more time as its size is getting increased.
This operation on dataframe might be delaying the on_ticks function call thereby causing this error.
Now how do i solve this?
My on_ticks function looks like this-
One solution could be to separate my dataframe logic from on_ticks function. But i need live analysis of my data without which i will lose my triggers.
Please help.
Yes, the main tick thread is getting blocked. Basically, the idea is to not block the thread which is receiving the ticks,transfer calculation to different thread.
The right solution here would be to offload all your tasks to another thread and not do it on the thread which is listening to the ticks.