AutoTrader Web Square-off Process

In this post, we will understand how the square-off process of AutoTrader Web works. It is extremely important to understand this, so that you are prepared to make the correct decision if something goes wrong.

Square-off process is intelligently designed to safeguard users from building a reverse position, when user requests/clicks square-off multiple times “by mistake”. Please read the process explanation given below to know more.


Internal Working

Square-off process is fairly complex, we have simplified it here so that it is easy to understand.

  1. AT Web gets a request to square-off a position
  2. AT-Web pulls latest positions and orders from your trading platform
  3. It tries to find the position, if it cannot find it; the system will throw an error (Position not found)
  4. It checks if the position is OPEN.How does the system determine an OPEN position?
    1. Normal positions are checked with net quantity non-zero
    2. Complex positions (Bracket & Cover order positions) are checked with open legs (or open child orders also known as target or stoploss orders). Why? Consider following scenario:
      1. User places a BUY bracket order with 1 quantity for SBIN
      2. It gets executed & 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 & he has a BO position still open with net quantity 0
      5. Hence, we cannot use net quantity for BO & CO positions to determine whether they are open. Because in this case the user still has both BUY & SELL BO positions open as their target & s/l orders are open
  5. If the position is NOT OPEN, AT Web stops
  6. If the position is OPEN, then it proceeds to do the square-off
    1. If the position square-off failed earlier in the day, then for safety purposes system will not proceed with square-off. It will throw an error (Square-off has already failed X times).
      1. A user or his program can square-off multiple times, when he sees that the position is not square-off (in case of failures). In worst case scenario, this can lead to reverse position being generated. And it may go on increasing every time user sends square-off request, which may lead to heavy losses. Hence, the system is designed NOT to accept “sqaure-off-market” request for the position which has already failed once. A user can do manual square-off by placing reverse order in such a case.
    2. Position is locked by AT Web to avoid parallel square-off as it will result in unexpected results.
      1. Parallel square-off can happen if sqaure-off request is submitted multiple times by user or automated strategies
    3. If it cannot acquire the lock, an error (Square-off is already running) will be generated. This will happen if multiple square-off requests come for the same position & the position square-off is in-progress.
    4. The system will decide on square-off action
      1. For simple positions, a reverse order is placed with quantity as absolute net quantity
      2. For complex (BO & CO) positions, all of it’s open child (target & s/l) orders are cancelled. As this will cause the position to square-off, this is how BO & CO are square-off on your trading platform as well. If the system cannot find any open child orders then it will throw an error (No open child (target or stoploss) orders found)
    5. The system will do following checks 10 times (if the position remains OPEN)
      1. The system will then fetch latest orders and positions from your trading platform
      2. It will verify that position is CLOSED
        1. If the position is CLOSED, the system stops with SUCCESS
        2. If the position is not CLOSED, then the system waits for some time and verifies again. However if it sees that the square-off order was rejected by broker, then it will throw an error (Square-off order rejected by broker)
    6. If the position is STILL OPEN after checking 10 times, then the system considers it as FAILURE. And you will get an error (Waited long enough, but the position is still open)
      1. A position that has failed once will never be accepted for square-off again. This is done to safeguard our users, as it can 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. Remember that if the requests reach the server within milliseconds of each other, they both will see the position as being open. This will result in multiple square-offs for the same position. And the consequences would most likely be you would end with a reverse position.

Our system provides built in safety in such scenarios by locking the position before proceeding with the square-off. So in such cases only one request will be allowed to proceed & others will be rejected.

Broker server slowness safety

Imagine you sent a request to square-off a position. Our system placed a reverse order to square-off the position. But the broker’s server is having slowness. This slowness means order is taking longer than normal to execute. You as a user or your automated trading system sees that the position is still open even after sending a square-off request. You or your automated trading system decides to square-off again. Now this will result in multiple square-off requests & another reverse order will be placed. And the consequences would most likely be you would end with a reverse position.

Our system helps to safeguard you against such issues. The system waits for up to a minute & 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 for some reason, the position remains open. The system will mark it as a failure & this position will never be accepted for square-off again.

Safety from broker server errors

As explained in the previous section, sometimes a square-off order is rejected by a broker. There could be many causes for this, but the most common cause is 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 will just keep on retrying forever.

To safeguard our users, our system marks the position as a failure in square-off & notifies the user. This is because in most of such cases, a manual user intervention is required. The system will not accept that position for square-off again.

Common causes of failure

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

How to handle failures?

If there is a failure, then 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 & s/l 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 available on the positions screen (see screenshot below)

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

Square-off position custom button
Square-off position custom


Failures are rare & whenever they happen; there is something that did not work as expected. A user must find the cause and take necessary actions. Our systems are completely transparent & they provide you insights to help you investigate any issues.

Use activity logs & figure out what went wrong.

Note: Once a sq. off fails for a position, then (to safeguard the user) it is designed to error for the given position on multiple attempts of sq. off. In such cases, a user must investigate the activity logs to determine the failure cause. He should check logs of the “VERY FIRST SQ. OFF REQUEST”.

I have around 15 years of experience developing Financial Software. I took a break from my career to start my own algorithmic trading setup. This new journey started well and along with my own proprietary trading systems, I also started building automation systems for retail traders, fund managers and HNIs. I was employed with firms like Morgan Stanley, HSBC & SunGard. I work on various technologies, but my strengths are building server side cloud based algorithmic trading systems built primarily using Java programming language. Apart from all of this, I am a big fan of Formula 1 racing :-)

4 thoughts on “AutoTrader Web Square-off Process”
  • Abhishek Pendse says:

    Your creativity and courage have eventually led you to open your own business. I have every reason to believe that your hard work and perseverance will lead to the success of this business.
    Congratulations and good luck bro

    December 11, 2020 at 11:31 am
    • Pritesh says:

      Thanks mate, stay in touch!

      December 11, 2020 at 7:55 pm
  • nitin says:

    firstly its a wonderful platform and im using it from past 3 days and no errors yet. and one suggestion is we cannot see our live pnl in positions so please check that part, and apart from that i love the platform. keep rocking and updating.
    thank you

    December 25, 2020 at 1:52 am
    • Pritesh says:

      Live pnl was only missing for Upstox. We have added a change in our system to show live pnl.

      December 25, 2020 at 10:44 am

Comments are closed.