sir, Due to same resion I am not able to trade from last one year before that my py programme was working fine. i am trying to place an order error Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
This error generally happens due to some internal error in your code.
-----------------------------------------------------------------
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
api_k = "" #api key
api_s = "" #api secret
access_token = ""
def get_login(api_k, api_s):
global kws, kite, Trade_count
kite = KiteConnect(api_key=api_k)
kite.set_access_token(access_token)
kws = KiteTicker(api_k, access_token)
get_login(api_k, api_s)
inst_token = [895745]
Stocks = {895745: {'name': 'TATASTEEL', 'Qty' : 1, 'Prev_Price' : 0, 'Order_Price' : 0}}
def on_ticks(ws, ticks):
for Script in ticks:
ticks_token = Script['instrument_token']
if Script['last_price'] >= Stocks[ticks_token]['Prev_Price'] :
Stocks[ticks_token]['Order_Price'] = round((Script['last_price'] - Script['last_price'] * 0.1 / 100), 1)
order_id = kite.place_order(variety='regular', exchange='NSE', tradingsymbol=Stocks[ticks_token]
['name'],transaction_type='BUY', quantity=Stocks[ticks_token]['Qty'], product='MIS', order_type='LIMIT',
price=Stocks[ticks_token]['Order_Price'], validity='DAY', disclosed_quantity=None, trigger_price=None,
squareoff=None, stoploss=None, trailing_stoploss=None, tag=None)
elif Script['last_price'] <= Stocks[ticks_token]['Prev_Price'] :
Stocks[ticks_token]['Order_Price'] = round((Script['last_price'] + Script['last_price'] * 0.1 / 100), 1)
order_id = kite.place_order(variety='regular', exchange='NSE', tradingsymbol=Stocks[ticks_token]['name'], transaction_type='SELL', quantity=Stocks[ticks_token]['Qty'], product='MIS', order_type='LIMIT',
price=Stocks[ticks_token]['Order_Price'], validity='DAY', disclosed_quantity=None, trigger_price=None,
squareoff=None, stoploss=None, trailing_stoploss=None, tag=None)
def on_connect(ws, response): # noqa
ws.subscribe(inst_token)
ws.set_mode(ws.MODE_QUOTE, inst_token)
def on_close(ws, code, reason):
print('Connection close')
def on_error(ws, code, reason):
print('Connection Error')
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
kws.connect()
Try threaded ticker,it might solve the issue:
https://kite.trade/forum/discussion/comment/25535/#Comment_25535