zerodha / pykiteconnect

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

Every two minute i got this error - Connection error: 1006 - connection was closed uncleanly (None) #88

Closed karthickbala123 closed 3 years ago

karthickbala123 commented 4 years ago

I read previous discussions about this error but am not resolve this issue am also not using any computation inside on_ticks(). kindly help me

susbcription_list = [int(x) for x in trd_portfolio]
def getticks(ticks):
for tick in ticks:
ticks_token = tick['instrument_token']
push_tick_values_to_trd_portfolio(tick, ticks_token)

push_value_to_excel()
def on_ticks(ws, ticks):
# Todo: "On_ticks"
getticks(ticks)

def on_connect(ws, response):
ws.subscribe(susbcription_list)
ws.set_mode(ws.MODE_FULL, susbcription_list)

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect

kws.connect(threaded=True)

count = 0
while True:
count += 1
if count % 2 == 0:
if kws.is_connected():
kws.set_mode(kws.MODE_FULL, susbcription_list)
else:
if kws.is_connected():
kws.set_mode(kws.MODE_FULL, susbcription_list)

time.sleep(5)

image

vijaykz commented 4 years ago
push_tick_values_to_trd_portfolio(tick, ticks_token)
push_value_to_excel()

By looks, these seem to be time consuming operations. Try doing only in-memory operations in on_ticks() and offload other functionalities to a separate thread.

karthickbala123 commented 4 years ago

Thank you for your reply @vijaykz can you send some exapmle code for memory operations bcoz am basic python user only. kindly help me

vijaykz commented 4 years ago

Refer to this example. This shows how to fetch & store ticks. Instead of database, you can simply maintain candles in list of list, for example.

sachin2404 commented 3 years ago

Hi Vijaykz , In your example, you said you are sending ticks in celery taske queue where ticks are inserted into DB. But what if I want to do operation on latest ticks not on previous ticks. I want to compute on instant basis while getting ticks.

vijaykz commented 3 years ago

I do not have a need to save in DB. I create candlesticks in a list of lists like this:

ticks = [ [Date, O, H, L, C, V], [Date, O, H, L, C, V], .... [Date, O, H, L, C, V] ]

In order to perform operations on the latest bar, you can fetch it just by using ticks[-1]