Issue in Socket Connection : Frequent Disconnection after 1st order copy to client accounts.

theankitdabas
import logging
from helper.orders_helper import *
from helper.token_helper import *
from kiteconnect import KiteTicker
from threading import Thread

class ZerodhaSocketClient:
def __init__(self, zerodha_user_id, zerodha_user_password, zerodha_api_key, zerodha_api_secret, zerodha_totp_secret, user):
self.zerodha_user_id = zerodha_user_id
self.zerodha_user_password = zerodha_user_password
self.zerodha_api_key = zerodha_api_key
self.zerodha_api_secret = zerodha_api_secret
self.zerodha_totp_secret = zerodha_totp_secret
self.user = user

def start(self):
self.z_token = ZerodhaAccessToken(self.zerodha_user_id, self.zerodha_user_password, self.zerodha_api_key, self.zerodha_api_secret, self.zerodha_totp_secret)
self.kite = self.z_token.gen_access_token()
self.data = self.kite.profile()
self.access_token = self.kite.access_token
self.kws = KiteTicker(self.zerodha_api_key, self.access_token)

self.kws.on_connect = self.on_connect
self.kws.on_close = self.on_close
self.kws.on_order_update = self.on_order_update

print("[I] CONNECTING TO THE SOCKET CLIENT")
self.kws.connect()

def on_connect(self, ws, response):
print("[+] CONNECTED TO THE SOCKET CLIENT")

def on_order_update(self, ws, data):
# Handle order updates
# TODO: Here we need to add code to place the order
print(f"[+] NEQ ORDER UPDATE: {data}")
Thread(target=place_orders, args=(data,), daemon=True).start()

def on_close(self, ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
print(f"[!] SOCKET CLOSED REASON:{reason} | CODE: {code}")
ws.stop()

def connect_to_socket(user):
zerodha_user_id = user["username"]
zerodha_user_password = user["password"]
zerodha_api_key = user["api_key"]
zerodha_api_secret = user["api_secret"]
zerodha_totp_secret = user["totp"]

socket_client = ZerodhaSocketClient(zerodha_user_id, zerodha_user_password, zerodha_api_key, zerodha_api_secret, zerodha_totp_secret, user)
socket_client.start()
  • rakeshr
    Can you paste here the complete error stack trace?
  • theankitdabas
    it was due to multiple order being passed in one go for clients... was more than 15 in one go...
    how can we get copy trade in all cclients in one go with not breaking zerodha api order limit ...can you suggest ?
Sign In or Register to comment.