exit_order from thread or on_tick returns exceptions

y_sravan
y_sravan edited November 2019 in Python client
Hi All,

I have Kite ticker registered to
kws.on_ticks = on_ticks
kws.on_close = on_close
kws.on_error = on_error
kws.on_connect = on_connect
kws.on_reconnect = on_reconnect
kws.on_noreconnect = on_noreconnect
kws.on_order_update = on_order_update
kws.connect(threaded=True)
ticker running in threaded model

in on_ticks I'm checking for profit/loss based on entry price.
when profit/loss reached the target, I want to exit the order ( mostly Cover Order)

I'm calling
kite.exit_order(data['product'], order_id=data['order_id'], parent_order_id=data['parent_order_id'])

this is returning me
   rval = self.protocol.dataReceived(data)
File "/usr/lib/python3/dist-packages/twisted/protocols/tls.py", line 330, in dataReceived
self._flushReceiveBIO()
File "/usr/lib/python3/dist-packages/twisted/protocols/tls.py", line 295, in _flushReceiveBIO
ProtocolWrapper.dataReceived(self, bytes)
File "/usr/lib/python3/dist-packages/twisted/protocols/policies.py", line 120, in dataReceived
self.wrappedProtocol.dataReceived(data)
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/twisted/websocket.py", line 291, in dataReceived
self._dataReceived(data)
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/websocket/protocol.py", line 1213, in _dataReceived
self.consumeData()
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/websocket/protocol.py", line 1225, in consumeData
while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/websocket/protocol.py", line 1591, in processData
fr = self.onFrameEnd()
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/websocket/protocol.py", line 1713, in onFrameEnd
self._onMessageEnd()
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/twisted/websocket.py", line 319, in _onMessageEnd
self.onMessageEnd()
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/websocket/protocol.py", line 634, in onMessageEnd
self._onMessage(payload, self.message_is_binary)
File "/home/mr/.local/lib/python3.7/site-packages/autobahn/twisted/websocket.py", line 322, in _onMessage
self.onMessage(payload, isBinary)
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/ticker.py", line 71, in onMessage

File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/ticker.py", line 677, in _on_message

File "pyroteleupstox.py", line 407, in on_ticks
kite.exit_order(data['product'], order_id=data['order_id'], parent_order_id=data['parent_order_id'])
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 358, in exit_order

File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 353, in cancel_order

File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 709, in _delete

File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 766, in _request

kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.

ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "pyroteleupstox.py", line 373, in threadFun
kt.exit_order(data['product'], order_id=data['order_id'], parent_order_id=data['parent_order_id'])
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 358, in exit_order
self.cancel_order(variety, order_id, parent_order_id=parent_order_id)
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 353, in cancel_order
"parent_order_id": parent_order_id
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 709, in _delete
return self._request(route, "DELETE", params)
File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 766, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
in the main thread I can still place orders, but unable to call exit_order

any help is appreciated
  • y_sravan
    further more to test, I tried with thread

    it's also returning same kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.

    Exception in thread Thread-3:
    Traceback (most recent call last):
    File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
    File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
    File "pyroteleupstox.py", line 373, in threadFun
    kite.exit_order(data['product'], order_id=data['order_id'], parent_order_id=data['parent_order_id'])
    File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 358, in exit_order
    self.cancel_order(variety, order_id, parent_order_id=parent_order_id)
    File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 353, in cancel_order
    "parent_order_id": parent_order_id
    File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 709, in _delete
    return self._request(route, "DELETE", params)
    File "/home/mr/.local/lib/python3.7/site-packages/kiteconnect-3.7.7-py3.7.egg/kiteconnect/connect.py", line 766, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.InputException: Invalid `api_key` or `access_token`.
  • sujith
    Can you check if you are passing the right api_key and access token?
    You may also run it in debug mode to print all the request and response logs.
  • y_sravan
    I'm using the same kite object, which I used to place an order ( which is successful)
    yes, api_key and access token are correct.

    is it something to do with the thread?
  • rakeshr
    @y_sravan
    kite.exit_order(data['product'], order_id=data['order_id'], parent_order_id=data['parent_order_id'])
    Can you print all param you are sending to exit_order method, and manually check, if you are sending it correct?
  • y_sravan
    @rakeshr
    it should have been data['variety'] instead of data['product']
    I didn't pay much attention since both shows cover order.
    but the problem is
    data['variety'] = "co", whereas
    data['product'] = "CO" (in caps)

    I didn't thought, it would be case sensitive. and the error message didn't convey anything

    problem is solved, with "co"

    this thread can be closed now.
This discussion has been closed.