Available translations

Checking Exchange Events

foundations.png
Summary: The trading bot monitors exchange orders to check their status analyzing multiple possible scenarios.
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.
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
 Body: 
average:12930.92
clientOrderId:"eLAkNrEoMw3erEp8AeaiTT"
cost:1293.24717104
datetime:"2020-10-24T08:47:43.104Z"
fee:undefined
filled:0.100012
id:"3420506109"
lastTradeTimestamp:undefined
price:12930.92
remaining:0
side:"sell"
status:"closed"
symbol:"BTC/USDT"
timestamp:1603529263104
trades:undefined
type:"market"

Info: 
orderId:3420506109
orderListId:-1
origQty:"0.10001200"
origQuoteOrderQty:"1293.25000000"
price:"0.00000000"
side:"SELL"
status:"FILLED"
stopPrice:"0.00000000"
symbol:"BTCUSDT"
time:1603529263104
timeInForce:"GTC"
type:"MARKET"
updateTime:1603529263104
Limit sell order for 0.1 BTC
 Body: 
amount:0.1
average:12976.74
clientOrderId:"iZbiUF9W1WiKmtMfhA8hIr"
cost:1297.674
datetime:"2020-10-24T10:43:47.944Z"
fee:undefined
filled:0.1
id:"3420762036"
lastTradeTimestamp:undefined
price:12969.83
remaining:0
side:"sell"
status:"closed"
symbol:"BTC/USDT"
timestamp:1603536227944
trades:undefined
type:"limit"

Info: 
executedQty:"0.10000000"
icebergQty:"0.00000000"
isWorking:true
orderId:3420762036
orderListId:-1
origQty:"0.10000000"
origQuoteOrderQty:"0.00000000"
price:"12969.83000000"
side:"SELL"
status:"FILLED"
stopPrice:"0.00000000"
symbol:"BTCUSDT"
time:1603536227944
timeInForce:"GTC"
type:"LIMIT"
updateTime:1603536227944
Status checks
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 Syncing 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 Syncing With the Exchange).
Cancelled
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'
Canceled orders go through the same processes of synchronization (see Syncing With the Exchange) and the recalculation of the size of the stage (see Accounting).
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 Syncing With the Exchange).
Previous
Maintaining Live Orders
Next
Syncing With the Exchange