dfelton / kobens-gemini

Application for facilitating trades on the Gemini exchange.
9 stars 1 forks source link

Handle when Exchange Server performs MarketClosed on an order #235

Open dfelton opened 2 years ago

dfelton commented 2 years ago

At times when gemini performs maintenance on their servers they will close all open limit orders an a trading pair. When this happens, the application does not recognize that the order is not live anymore and will not re-place the original buy / sell order.

Example order status after this event has occurred:

Order Id            71173822663
Id                  71173822663
Symbol              batusd
Exchange            gemini
Avg Execution Price 0.00
Side                sell
Type                exchange limit
Timestamp           1637985141
Timestampms         1637985141681
Is Live             false
Is Cancelled        true
Is Hidden           false
Was Forced          false
Executed Amount     0
Client Order Id     repeater_42249_sell_1637985141.1653
Reason              MarketClosed
Options             
    0: maker-or-cancel
Price               2.24251
Original Amount     1.1725
Remaining Amount    1.1725
dfelton commented 2 years ago

Note: the below verbiage is written under the context that we are only trading on "USD" quoted pairs. However, problem is quote currency agnostic.

After today's 9 hour service outage Gemini encountered which resulted in the cancellation of all limit orders on the exchange it brought to light the importance of handling this properly for partially filled orders. Up until now I have been resolving this scenario by performing the following manual steps:

This worked well enough for the time being. It was already known that there was the possibility that some orders had been partially filled. For these situations, the following was accepted:

What had not been considered was the following:

In today's MarketClosed of all Limit Orders on the exchange, the bot put itself into a situation where it was $40 away from running out of USD funds for more buy orders. Furthermore, because all shibusd records had been configured to never hodl any of the profits of coin's sales into profit buckets, the last record it attempted to re-place a sell order for, encountered an insufficient funds error (Attempted to place a sell order of 100,000 SHIB, however only 65983.79777000 was available. Indicating that roughly 34,000 SHIB had already been sold in prior partially filled sell orders).