For forward testing and live trading sessions, the state of orders at the exchange is checked on every execution of the trading bot.
It’s important to note that, when an order is placed, no relevant information is obtained from the exchange other than a confirmation that the order was received. The trading bot must ask the exchange for the status of the order some time after placing it.
01-mintime frame, the order is checked roughly one minute after it is placed.
When the trading bot fetches an order from the exchange, it gets the order information in a text message. What does the trading bot obtain from the exchange?
This is an important question, as the answer conditions what the trading bot may or may not do with each order, and how it may keep a reliable internal accounting system.
Orders according to the exchange
What follows are several examples including a market sell order, a market buy order, a limit sell order, and a limit buy order, all of them in the BTC/USDT market in Binance.
|Market sell order for 0.1 BTC||Market buy order for 0.1 BTC|
|Limit sell order for 0.1 BTC||Limit buy order for 0.1 BTC|
Once the status of the order is received as in one of the above examples, the trading bot performs multiple checks, each leading to particular actions. The following are the particular cases analyzed and the actions taken.
Filled and closed
If the order is filled and closed, the corresponding data structures in the trading engine are updated to indicate so.
tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Filled'
But, most importantly, the details in the order are processed to synchronize the information in the trading engine—at this point consisting mostly of information derived from the definitions in the trading system—with the information obtained from the exchange.
The data structures to be updated are mostly those related to the actual rate at which the order was filled, the actual size that was filled, the actual fees that were charged, and how those variables affect balances and the overall accounts in various contexts (order, stage, position, episode, etc.).
The calculations involved in the synchronization process are covered in Syncying with the exchange.
Partially filled and closed
If the order is partially filled and closed, the corresponding data structures in the trading engine are updated to indicate so.
tradingEngineOrder.status.value = 'Closed' tradingEngineOrder.exitType.value = 'Closed at the Exchange'
For a limit order to be closed while partially filled, it must be closed manually by the user directly at the exchange, or forcefully closed by the exchange itself, for whatever reason. The system handles such occurrences so that they do not disrupt the trading session or the accounts at Superalgos.
The first thing to do with such an order is to update the size placed on the stage so that the stage accounts for the amount that was filled only, instead of the original size of the order (see Accounting).
And then, the same synchronization process described earlier must be applied as well (see Syncying with the exchange).
These are limit orders canceled by the trading bot upon the triggering of the cancel order event. In such cases, the exit type is set as follows:
tradingEngineOrder.exitType.value = 'Cancelled at the Exchange'
All other instances
The cases described above refer to orders with particular characteristics. Orders that do not match any of the above criteria trigger the synchronization process described earlier (see Syncying with the exchange).