AutoTrader Web Square-off Process

How AutoTrader Web squares off a position safely, and what to do when it fails.

Square-offPositionsSafety checks
In short

When you square off a position, AutoTrader Web pulls the latest positions and orders, confirms the position is open, locks it so two requests cannot run at once, then places a reverse order (or cancels child orders for BO/CO positions). It checks the result up to 10 times for about a minute. If the square-off fails once, the system will not accept that position for square-off again, which protects you from building an accidental reverse position. In that case, you square off manually.

In this post, we explain how the square-off process of AutoTrader Web works. It helps to understand this so you can take the right action if something goes wrong.

The process is designed to protect you from building a reverse position when a square-off is requested or clicked many times by mistake. The sections below explain how.

Internal Working

The square-off process has several steps. Here is the full flow, step by step.

  1. AT Web gets a request to square off a position.
  2. AT Web pulls the latest positions and orders from your trading platform.
  3. It tries to find the position. If it cannot find it, the system throws an error: Position not found.
  4. It checks if the position is open. The system decides an open position like this:
    1. Normal positions are checked using net quantity non-zero.
    2. Complex positions (Bracket and Cover order positions) are checked using open legs (open child orders, also known as target or stoploss orders). This is needed because of the following scenario:
      1. User places a BUY bracket order with 1 quantity for SBIN.
      2. It gets executed and he has a BO position open with net quantity 1.
      3. User places a SELL bracket order with 1 quantity for SBIN.
      4. It gets executed and he has a BO position still open with net quantity 0.
      5. So we cannot use net quantity for BO and CO positions to decide whether they are open. In this case the user still has both the BUY and SELL BO positions open, because their target and stoploss orders are open.
  5. If the position is not open, AT Web stops.
  6. If the position is open, it proceeds to do the square-off:
    1. If the position square-off failed earlier in the day, the system will not proceed, for safety. It throws an error: Square-off has already failed X times.
      • A user or his program can square off many times when he sees the position is not squared off (in case of failures). In the worst case, this can lead to a reverse position being generated, which may keep increasing with every square-off request and lead to heavy losses. So the system is designed not to accept a square-off-market request for a position that has already failed once. In such a case, a user can do a manual square-off by placing a reverse order.
    2. The position is locked by AT Web to avoid parallel square-off, which would cause unexpected results.
      • Parallel square-off can happen if the square-off request is submitted many times by the user or by automated strategies.
    3. If it cannot acquire the lock, an error is generated: Square-off is already running. This happens when multiple square-off requests come for the same position while a square-off is in progress.
    4. Cancellation of open orders:
      • If this option is set to true (the default), it automatically cancels any open orders for the selected symbol and product type.
      • This helps prevent a common mistake where a trader forgets to cancel an existing open order. If not cancelled, the broker may reject the square-off order due to insufficient margin, because it treats it like a second exit order.
    5. The system decides the square-off action:
      • For simple positions, a reverse order is placed with quantity as the absolute net quantity.
      • For complex (BO and CO) positions, all of their open child (target and stoploss) orders are cancelled. This causes the position to square off, which is how BO and CO positions are squared off on your trading platform too. If the system cannot find any open child orders, it throws an error: No open child (target or stoploss) orders found.
    6. The system does the following checks 10 times (if the position remains open):
      • It fetches the latest orders and positions from your trading platform.
      • It verifies that the position is closed:
        • If the position is closed, the system stops with success.
        • If the position is not closed, the system waits for some time and verifies again. However, if it sees that the square-off order was rejected by the broker, it throws an error: Square-off order rejected by broker.
    7. If the position is still open after 10 checks, the system considers it a failure. You get an error: Waited long enough, but the position is still open.
      • A position that has failed once will never be accepted for square-off again. This protects you, because retrying could result in multiple square-off orders being generated.

Safety Features

Locking to safeguard from parallel requests

Imagine you, or your automated trading program, send back-to-back square-off requests by mistake for the same position. If the requests reach the server within milliseconds of each other, both will see the position as open. This would result in multiple square-offs for the same position, and you would most likely end with a reverse position.

The system protects you here by locking the position before proceeding with the square-off. In such cases, only one request is allowed to proceed and the others are rejected.

Broker server slowness safety

Imagine you send a request to square off a position. The system places a reverse order to square it off, but the broker’s server is slow. The order takes longer than normal to execute. You, or your automated trading system, see the position as still open and decide to square off again. This places another reverse order, and you would most likely end with a reverse position.

The system protects you against this. It waits for up to a minute and keeps checking the status of the reverse order placed during square-off. It will not accept any new square-off request for the same position. If the position still remains open, the system marks it as a failure, and this position will never be accepted for square-off again.

Safety from broker server errors

As explained above, a square-off order is sometimes rejected by a broker. There can be many causes, but the most common one is that market orders are restricted by brokers for certain illiquid stocks or derivatives, and a square-off order is a market order. In such cases, a user’s automated trading system would just keep retrying forever.

To protect you, the system marks the position as a square-off failure and notifies you. Most such cases need manual user intervention. The system will not accept that position for square-off again.

Common causes of failure

  1. Error from the broker while placing the square-off order.
  2. Broker’s server rejecting the square-off order. This happens for various reasons, such as market orders not being allowed for illiquid options, or a margin shortfall. A margin shortfall should ideally not happen during square-off, but we have mostly seen it when users have placed another target or stoploss order that they forgot about, and that order is blocking the margin.
  3. Another order was placed by the user or his algo during the square-off operation in the same symbol, affecting net quantity. So the system sees net quantity as non-zero after square-off (meaning the position is open) and gives a failure message.
  4. Broker’s server sending stale position data. The position is squared off, but the broker’s server is not sending the updated net quantity (which should now be zero). As a result, the system thinks the position is still open even after square-off and gives a failure message.

How to handle failures

If there is a failure, you must manually square off the position. A manual square-off can be done as follows:

  • For BO or CO positions: Cancel all of their target and stoploss orders from AT Web’s Trade screen.
  • For Normal positions: Place a reverse order from AT Web’s Trade screen. A quick way to do this is to use the Custom Square-off Button on the positions screen (see screenshot below).

In the worst case, you can log in to your trading platform and do a manual square-off.

Square-off position custom button

Square-off position custom

Investigation

Failures are rare. Whenever they happen, something did not work as expected. You must find the cause and take the necessary action. Our systems are fully transparent and give you the insights to investigate any issue.

Use activity logs to figure out what went wrong.

Note: Once a square-off fails for a position, it is designed (to protect you) to error for that position on further square-off attempts. In such cases, you must investigate the activity logs to find the failure cause. Check the logs of the very first square-off request.

Next steps

Was this page helpful?

Last updated 20 June 2026