kiteconnect.exceptions.InputException: Order cannot be cancelled as it is being processed. Try later

seshareddy
Grateful if someone can suggest the solution for the above error. The above error is being thrown when I am trying to cancel SLM order when LIMT order is executed. i.e. I am trying to cancel the SLM order after profit is booked.

Following is the part of the code I am using.Not sure if this is the way. But orders are getting cancelled sometimes as per the code. Sometimes the exception is being thrown.
Apologies for the large post.

order_id = kite.place_order(tradingsymbol=symbol,exchange=kite.EXCHANGE_NSE,
transaction_type=kite.TRANSACTION_TYPE_SELL,
quantity=quantity,order_type=kite.ORDER_TYPE_SLM,
trigger_price=pivot_high-0.3,
product=kite.PRODUCT_MIS,variety=kite.VARIETY_REGULAR)
order_id_slm=order_id
order_id = kite.place_order(tradingsymbol=symbol,exchange=kite.EXCHANGE_NSE,
transaction_type=kite.TRANSACTION_TYPE_SELL,
quantity=quantity,order_type=kite.ORDER_TYPE_LIMIT,
price=pivot_high+0.3,
product=kite.PRODUCT_MIS,variety=kite.VARIETY_REGULAR)
order_id_limit=order_id

received=kite.orders()
for individual_order in received:
if (individual_order['order_id'])==order_id_limit:
if (individual_order['status'])=="COMPLETE":
kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_slm) ------> exception is pointing to this line.

And the exception is:

******************
Unhandled Error
Traceback (most recent call last):
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\python\log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
return func(*args,**kw)
--- ---
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
why = getattr(selectable, method)()
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\internet\tcp.py", line 243, in doRead
return self._dataReceived(data)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\internet\tcp.py", line 249, in _dataReceived
rval = self.protocol.dataReceived(data)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\protocols\tls.py", line 330, in dataReceived
self._flushReceiveBIO()
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\protocols\tls.py", line 295, in _flushReceiveBIO
ProtocolWrapper.dataReceived(self, bytes)
File "c:\users\dell\desktop\python\env\lib\site-packages\twisted\protocols\policies.py", line 120, in dataReceived
self.wrappedProtocol.dataReceived(data)
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\twisted\websocket.py", line 290, in dataReceived
self._dataReceived(data)
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\websocket\protocol.py", line 1206, in _dataReceived
self.consumeData()
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\websocket\protocol.py", line 1218, in consumeData
while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\websocket\protocol.py", line 1578, in processData
fr = self.onFrameEnd()
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\websocket\protocol.py", line 1703, in onFrameEnd
self._onMessageEnd()
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\twisted\websocket.py", line 318, in _onMessageEnd
self.onMessageEnd()
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\websocket\protocol.py", line 627, in onMessageEnd
self._onMessage(payload, self.message_is_binary)
File "c:\users\dell\desktop\python\env\lib\site-packages\autobahn\twisted\websocket.py", line 321, in _onMessage
self.onMessage(payload, isBinary)
File "c:\users\dell\desktop\python\env\lib\site-packages\kiteconnect\ticker.py", line 71, in onMessage
self.factory.on_message(self, payload, is_binary)
File "c:\users\dell\desktop\python\env\lib\site-packages\kiteconnect\ticker.py", line 670, in _on_message
self.on_ticks(self, self._parse_binary(payload))
File "", line 231, in on_ticks
kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_slm)
File "c:\users\dell\desktop\python\env\lib\site-packages\kiteconnect\connect.py", line 370, in cancel_order
return self._delete("order.cancel", {
File "c:\users\dell\desktop\python\env\lib\site-packages\kiteconnect\connect.py", line 829, in _delete
return self._request(route, "DELETE", params)
File "c:\users\dell\desktop\python\env\lib\site-packages\kiteconnect\connect.py", line 886, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.InputException: Order cannot be cancelled as it is being processed. Try later.

Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

************

  • sujith
    Are you trying to close a position or canceling a pending order?
  • sujith
    If an order is already completed then one can't cancel it. Since execution is already done.
  • sujith
    Only a pending order can be canceled.
  • seshareddy
    Dear Sujith, It is open order (TRIGGER PENDING status). It is working first time for the first position taken. I mean when the first LIMIT order is executed, the corresponding SLM order is cancelled today. But for the subsequent orders the error is coming. It is behaving strangely.
  • seshareddy
    For your clarity I am attaching the screen shot of the orders executed today with the logic. In the screenshot, the market order is executed by the logic and when the profit is booked (LIMIT order is executed), the SLM order automatically got can celled. But there was another scenario where the buy market order should have been exe
    cuted just like this, but due to these errors, the trade didn't happen. Attaching the logic of the code oalso for your perusal and if you have time
  • rakeshr
    @seshareddy
    kiteconnect.exceptions.InputException: Order cannot be cancelled as it is being processed. Try later.
    Can you re-check if you are cancelling correct order_id(which is trigger_pending)?
    Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    Don't perform any conditional computation inside Websocket Streaming, as it will block the main Ticker thread. You can go through python Websocket Streaming FAQs.
  • seshareddy
    I have a doubt in the following code , after the PENDING order is cancelled first time, for every tick, the cancelled order is getting cancelled again. That is why first time there is no error and second time onwards the error is thrown. I will look into it.
    @rakeshr
    I have gone through the link above to separate the code from the on_ticks function and use threading. I will restructure my code accordingly. This is a good code practice. Thanks very much for pointing me to the right link
  • seshareddy
    I have modified the code as below. So it is working. My above observation was correct. Cancelled order was getting multiple times so the error was thrown. In the following code that has been taken care of. May not be a good code. but somehow working.

    received=kite.orders()
    for individual_order in received:
    if (individual_order['order_id'])==order_id_limit:
    if (individual_order['status'])=="COMPLETE":
    for i in received:
    if (i['order_id'])==order_id_slm:
    if (i['status'])=="TRIGGER PENDING":
    kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_slm)
    if (individual_order['order_id'])==order_id_slm:
    if (individual_order['status'])=="COMPLETE":
    for i in received:
    if (i['order_id'])==order_id_limit:
    if (i['status'])=="OPEN":
    kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_limit)
  • rakeshr
    @seshareddy
    Cancelled order was getting multiple times so the error was thrown.
    Yeah, as said above, you can always re-check this at your end by required order_id.
  • seshareddy
    @rakeshr
    Dear Rakesh, Kindly advise. Sorry to bother you with large posts.
    I tried putting the code outside on_ticks function, but facing issues. I also ran the same logic with code inside on_ticks function. Following are the results. Code and log outputs attached.Unable to make out where the problem is.
    1) Code within on_ticks function:
    a) Getting error after every few seconds: Connection error: 1006 - connection was closed uncleanly (None)
    b) Getting OHLC data. But there are some candles missing. See 19:23 and 19:29 candles missing.
    DateTime Open High Low Close PH PL
    6 19:22:59.460813 65005.0 65046.0 64920.0 64929.0 65150.0 0.0
    7 19:24:00.007276 64928.0 64932.0 64819.0 64918.0 65150.0 0.0
    8 19:25:00.016708 64918.0 65021.0 64915.0 64995.0 65150.0 64819.0
    9 19:26:00.615449 64999.0 65069.0 64990.0 65056.0 65150.0 64819.0
    10 19:27:01.225971 65056.0 65130.0 65055.0 65055.0 65150.0 64819.0
    11 19:28:01.806436 65055.0 65124.0 65055.0 65104.0 65130.0 64819.0
    12 19:30:07.808843 65100.0 65123.0 65021.0 65053.0 65130.0 64819.0

    2) Code outside on_ticks function:
    a) Getting error after every few seconds: Connection error: 1006 - connection was closed uncleanly (None)
    b)Not getting OHLC data
    c) print statement in on_ticks function 'print("inside main on_ticks")' not getting printed in output. I doubt whether main on_ticks function is getting called or not.
  • seshareddy
    In the scenario where the code is witin on_ticks Whenever I get this error: 1006 - connection was closed uncleanly (None). I can see that tick data is not getting printed in output for 7 to 8 seconds and then the tick data starts as I have commented out the following code
    # def on_close(ws, code, reason):
    # ws.stop()
  • rakeshr
    @seshareddy
    Historical data APIs are high resource consumable APIs. So, fetching historical data for every tick is not recommended and it shouldn't be soo time-constrained as in of the above ticker case.
    Maybe you can try the Queue method, as stated here.
    You can push tick data to Queue(use celery,rq,etc) as task queue
    manager and have another method that reads these Queue data and
    perform the tasks.

  • seshareddy
    @rakeshr
    I have modified the code as per the link advised by you earlier itself. The code is now outside the on_ticks function. Still the same error. Now I observed the problem is with the following code
    received=kite.orders().
    If I comment out this part of the code there is no error whether the code is within on_ticks or outside on_ticks function.
    For testing purpose, if i include the statement print(kite.orders()), then also the error is coming. Not sure how to get rid of this error. At any point of time I will have one SLM order in 'TRIGGER PENDING" status and one LIMIT order in OPEN status. If one is executed (status changes to COMPLETE), then the other should be cancelled.
    Is there a better way to handle. Presently I am using the following code. It works but I need to use received=kite.orders() statement which is throwing error.
  • seshareddy
    Sorry the code is :smile:
    print("before received")
    received=kite.orders()
    print("after received")
    # for individual_order in received:
    # if (individual_order['order_id'])==order_id_limit:
    # print("inside for and inside limit if after received orders")
    # print("order_id_limit",order_id_limit)
    # print("order_id_slm",order_id_slm)
    # print("order_id_limit_status outside if",individual_order['status'])
    # if (individual_order['status'])=="COMPLETE":
    # print("order_id_limit_status inside if",individual_order['status'])
    # for i in received:
    # if (i['order_id'])==order_id_slm:
    # if (i['status'])=="TRIGGER PENDING":
    # kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_slm)
    # if (individual_order['order_id'])==order_id_slm:
    # print("inside for and inside second if after received orders")
    # print("order_id_slm",order_id_slm)
    # print("order_id_limit",order_id_limit)
    # print("order_id_slm_status outside if",individual_order['status'])
    # if (individual_order['status'])=="COMPLETE":
    # print("order_id_slm_status inside if",individual_order['status'])
    # for i in received:
    # if (i['order_id'])==order_id_limit:
    # if (i['status'])=="OPEN":
    # scrip[instrument]['buystatus'] = "null"
    # scrip[instrument]['sellstatus'] = "null"
    # kite.cancel_order(variety=kite.VARIETY_REGULAR,order_id=order_id_limit)
  • rakeshr
    rakeshr edited July 30
    @seshareddy
    Kite Connect is purely an execution platform. We won't be able to provide you any kind of support or solution for writing strategies. You need to sort those at your end.
Sign In or Register to comment.