Why Order Failed in API but actually Trigered

babansfolio
Hello Zerodha,

I had observed one abnormal behavioral in last few weeks,
Market order has been executed using API and response got that Order is Failed.
When we checked in KITE application found order actually executed.

Here is one latest example:

IN GET_ORDER_STATUS :: 2019-12-17 15:15:00 NFO BANKNIFTY19DEC32600CE SELL 20
ORDER_FAILED :: NFO BANKNIFTY19DEC32600CE SELL Couldn't find that `order_id`.


Relevant Code Used:

except Exception as e:
logging.info("Order placement failed: {}".format(e))
print("ORDER_FAILED :: ",exchange_input, tradingsymbol_input, transaction_type_input,e)
  • sujith
    @babansfolio,
    It can happen when Kite trade doesn't receive a response from the OMS. In such cases, you need to catch the exception and check orderbook before you retry placing the order.
  • babansfolio
    @sujith : Is not this weakness of SW in your end ? . order was actually placed but API return errors?
    Could you please help more to get rid of this kind of problem?
    Which API calls need to check order book before Retry?
    Please share order book check syntax.
  • sujith
    Kite Connect APIs didn't receive a response from the OMS vendor when you placed an order. Hence it throws an exception.

    When a read request times out, you can just directly retry but in case of a write request time out, the system doesn't really know if the write is success or failure. Hence you need to check if it was actually written before you retry. If the write was already successful then it would create a duplicate.
    Please share order book check syntax.
    You need to check if the order you just placed exists in the orderbook or not. You will have to fetch orderbook and check for the order with the same tradingsymbol, price, trigger price and product.
  • babansfolio
    @sujith : Understood and still think there should some improvement mechanism in API to avoid this kind of scenario.

    About Order Book check, are you suggest to use following?
    "Retrieving all trades"
    "Retrieving an order's trades"
  • babansfolio
    @sujith : Understood and still think there should some improvement mechanism in API to avoid this kind of scenario.

    About
  • sujith
    sujith edited December 2019
    I am not sure what you are referring to. It is not a specific issue to go fix it. It can happen because of many reasons, it could be the client's connection with Kite trade, Kite trade's cloud connection to OMS data center or OMS overload, or OMS internal sytem connection issue. It could be because of the physical exchange lease lines issue.
  • RishiS
    @babansfolio There will always be some response issues occasionally in APIs. The way I tackle your issue is by having a tiny delay of 0.5secs, then getting the order book to check if an order with the desired parameters is placed. You can place the commands to do all the above in a function and call it after the order is placed, regardless of if you get an exception or not. I know that this is slightly inefficient, but, it's fool proof.
  • sudipto
    @sujith In case of a 400-Timed out exception while placing a new order, I will be checking the orderbook for a maximum of 60 seconds to ascertain whether the order actually went through.
    Since I don't have an order number to check against in the order book, can I reliably use the "tag" parameter that I send in my request parameter to check against? Otherwise I will have to guess since I have no other reference ID, and there might be multiple orders from multiple threads in the orderbook.
  • sujith
    You can use it if you are going to place a unique tag for every order.
  • sagaranilganu
    @sujith

    Q1] To check the order's current state what can be used?
    a) Retrieving an order's history or
    b) Retrieving an order's trades ?
    (What is the difference between them?)

    Q2] Say I have MIS BUY Options contract and now need to sell it. How will I be able to sell the same options contract (Tracking issue)?
    a) Use fresh "place_order" with same "tradingsymbol" of above contract or else what?? Am a little confused?
  • sujith
    Order history is the different states of an order in our system. You can know more here.

    Trade is where the buyer and seller actually make the transaction. Once you place an order on the exchange then it can be filled in one trade or multiple trades. Let us say I placed a buy order for 100 quantity it can be executed in one or two or any number of trades. For example, in one trade only 50 can be filled and another 50 in another trade.

    If an order is partially filled then the status of the order still remains open. It won't change the order status.
    You need to either fetch orderbook and check the status of an order or you may fetch order's history.

    If you place a buy order for options instrument, once it is executed, even a partial fill will create a position for that instrument, which you can fetch using the positions API. If the net quantity of a position is positive then you have a long position, if it is negative then you have a short position, if it is 0 then you have closed the position.
    If you have a long position then you need to sell (place a fresh sell order with the same product type) the same quantity to close the position and vice versa for a short position.
  • sagaranilganu
    sagaranilganu edited March 12
    [All below is regarding options intraday buying only]
    I would be tracking the order's status, average_price, filled & pending, cancelled qty by the "order_history" api.

    Since I would be calculating the pnl (using charges list + stamp duty calculation) and margins internally; I am not in requirement of "positions API" thus minimize api usage.

    (pnl and margins internally:: This way I plan to keep sufficient margins for physical stock trading/invest in parallel through kite web!!. Option's contract strike price would be selected at run time and also I do not want to limit the qty hard coded with approximations-assumptions).

    Imp :: Can the profits (hope so) from previous trade be used to buy option for next trade. Does it get added to "net (Net cash balance available for trading)"

    Your opinion is most desirable.
  • sagaranilganu
    Your opinion is most desirable.
  • themohammedfaisal
    Imp :: Can the profits (hope so) from previous trade be used to buy option for next trade. Does it get added to "net (Net cash balance available for trading)"
    Yes, you can use the profits from the previous trade for the next trade. However, if you carry the position overnight, there will be a margin penalty by the exchange as the profits from the previous trade are only settled on T+1 day.
  • ravishastry
    Is there any specific exception that we have to catch ?
  • sujith
    We recommend you check orderbook and retry for all non-400 write API responses. Otherwise, you might end up placing multiple orders.
Sign In or Register to comment.