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 fairly complex, we have simplified it here so that it is easy to understand.
- AT Web gets a request to square-off a position
- AT-Web pulls latest positions and orders from your trading platform
- It tries to find the position, if it cannot find it; the system will throw an error (Position not found)
- It checks if the position is OPEN. How does the system determine an OPEN position?
- Normal positions are checked with net quantity non-zero
- 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:
- User places a BUY bracket order with 1 quantity for SBIN
- It gets executed & he has a BO position open with net quantity 1
- User places a SELL bracket order with 1 quantity for SBIN
- It gets executed & he has a BO position still open with net quantity 0
- 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
- If the position is NOT OPEN, AT Web stops
- If the position is OPEN, then it proceeds to do the square-off
- 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).
- Position is locked by AT Web to avoid parallel square-off as it will result in unexpected results.
- Parallel square-off can happen if sqaure-off request is submitted multiple times by user or automated strategies
- 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.
- The system will decide on square-off action
- For simple positions, a reverse order is placed with quantity as absolute net quantity
- 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)
- The system will do following checks 10 times (if the position remains OPEN)
- The system will then fetch latest orders and positions from your trading platform
- It will verify that position is CLOSED
- If the position is CLOSED, the system stops with SUCCESS
- 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)
- If the position is STILL OPEN after 10 retries, then the system considers it as FAILURE. And you will 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 is done to safeguard our users, as it can result in multiple square-off orders being generated.
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.
How to handle failures?
Most common causes of failures are:
- Broker’s server under heavy load
- Error from broker, while placing square-off order
- Broker’s server rejecting the square-off order (for various reasons like market orders not allowed for illiquid options etc.)
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.
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.