Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    Best Practice for Squaring Off F&O Position with Pending SL Order via SmartAPI?

    Python SDK
    1
    1
    5
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      Konda last edited by

      I'm building an automated trading system using the Angel One SmartAPI for F&O, and I have a critical question about the safest and
      most reliable way to programmatically square off a position.

      The Scenario:

      My system has an open long position (e.g., 10 lots of a BANKNIFTY Call Option). At the same time, it also has a corresponding
      pending STOPLOSS_MARKET order for those 10 lots, which is active at the broker.

      When my internal logic decides it's time to exit the trade (e.g., a take-profit signal is generated), I need to send the correct
      sequence of API calls to close the position cleanly.

      The Core Question:

      What is the community's recommended, "battle-tested" sequence of API calls to guarantee a safe exit and avoid errors like accidental
      short positions or race conditions?

      I see two main possibilities:

      • Option A: CANCEL then SELL

        1. First, call cancelOrder() on the pending STOPLOSS_MARKET order.
        2. Wait for a success confirmation from the API.
        3. Then, call placeOrder() with a MARKET SELL order to close the position.
          (My concern with this is the small time gap between the successful cancel and the sell order, where the position is live in the
          market but has no stop-loss protection).
      • Option B: SELL then CANCEL

        1. First, call placeOrder() with a MARKET SELL order, relying on the broker's system to net out the position.
        2. Then, call cancelOrder() on the now "orphaned" STOPLOSS_MARKET order as a cleanup step.
          (My concern here is whether relying on netting is 100% safe and if this could cause issues with the orphaned stop-loss order).

      Follow-up Questions for High-Volume Traders:

      1. Large Orders: How does this logic change when you need to exit a large position that was "sliced" into multiple orders (e.g.,
        exiting a 1000-lot position that was entered via two separate 500-lot orders)? Do you have a loop to cancel all stop-losses first?

      2. Rate Limiting: Have you run into the "10 orders per second" API rate limit during these rapid exit events? How do you manage it?

      I am looking for advice on the most robust industry practice for this specific workflow. Thank you in advance for your help!

      1 Reply Last reply Reply Quote 0
      • First post
        Last post