Exiting a trade to free up margin when Available Margin is negative gives error

sudipto
Hi,
When my account goes into negative Available margin (usually happens on Expiry day), and I try to exit some short options positions to free up margin using the API, the order is rejected with message saying "insufficient funds". This puts me in a catch-22. To make sufficient funds I have to exit a position and to exit a position I have insufficient funds!
If I use the Zerodha Kite site though the exact same transaction is allowed and margins get freed up. However because the transaction happens outside my program using API, the program data goes into inconsistent state and starts behaving erratically.
How can I execute exit trade transactions using API to free up margin when the account balance Available margin has already gone into negative?
  • salim_chisty
    The margin requirements and risk checks for order placement are the same whether the order is placed through the Kite web platform or via the Kite Connect API.

    You may raise a ticket on the support portal with the complete details for further assistance.
  • sudipto
    Hi Salim,
    Are you sure that the risk checks for the API and the Kite Web are the same? According to this document below cited by Gemini, the risk checks for the two endpoints are not the same and its description is exactly what I have experienced in my use. I have been using Kite api for 7 years now and I am very familiar with the system and the API. I would not raise a frivolous query without checking thoroughly on my side. Pasted below is part of the document that gives the reason for the failure...
    ------------------------

    This is a known architectural behavior (and frustration) when working with the Kite Connect API.
    While it feels counterintuitive that the official Kite UI lets you exit or hedge a position to reduce margin while the API gives you an Insufficient Margin error for the exact same order, there is a clear technical reason for it.

    Why the API Fails (While the UI Succeeds)
    1. The API Checks Margins Sequentially (Independently)
    When your account has a negative available margin, Zerodha’s Risk Management System (RMS) treats any incoming order via the API as a new request.
    The API's validation engine evaluates the order on an absolute basis: “Does this account have the margin to execute this BUY or SELL instruction right now?”
    Because your margin is negative, the validation check immediately throws an error and rejects the order before realizing that executing it would actually credit margin back to your account or reduce your exposure.

    2. The Kite UI Uses Basket/Contextual Logic
    The official web and mobile applications are built with structural awareness. When you click "Exit" or execute a counter-trade directly from your positions page:
    The UI explicitly flags the transaction as an unwinding/reversing order of an existing position.
    The internal RMS bypasses the standard upfront margin check for these specific manual actions because it recognizes it is a risk-reducing transaction. The Kite Connect API does not automatically map a generic order placement to an "intent to reduce risk" in the same way during a margin debit block.
Sign In or Register to comment.