Fixing USER_SUSPEND method
Not yet finished
WORK IN PROGRESS
I have to figure out the way accounts balance are updated when an order is cancelled.
Initial idea of users removal was:
- External system (gateway) lock trading for specific client, stop accepting new orders. We assume client's orders and positions (if margin trading is enabled) - are known externally (should be tracked by events or journal). That is needed for steps 2 and 3.
- Cancel requests sent for each of them, one by one. Some of them may not succeed because orders can get executed before cancel command arrive, but this is tracked by the external anyway.
- If margin trading is enabled: After orders cancelled (received all corresponding events) - close positions by issuing corresponding closing orders, one by one. Because all limit orders were previously removed (and confirmed), resulting positions guaranteed to be closed completely.
- Balance adjustment operations (withdrowals) issued to zero all balances.
- Suspend command is issued to the core. User record completely removed from the risk control (if possible) - so it not affecting peformance in any way. However for finantial accounting cosistency, if all steps above were not executed correctly - exchange core can re-create user account in SUSPENDED state. For such account external system can try remove it again - by applying same steps. Fixing positions (if margin trading is enabled) would require RESUME user first (if we apply fix from this PR).
Straight forward removal with just a single command of caurse looks much easier for beginners. It has potential latency penalty. But i think we can add it if can proce consistency gurantees for such commands.
Initial idea of users removal was:
1. External system (gateway) lock trading for specific client, stop accepting new orders. We assume client's orders and positions (if margin trading is enabled) - are known externally (should be tracked by events or journal). That is needed for steps 2 and 3. 2. Cancel requests sent for each of them, one by one. Some of them may not succeed because orders can get executed before cancel command arrive, but this is tracked by the external anyway. 3. If margin trading is enabled: After orders cancelled (received all corresponding events) - close positions by issuing corresponding closing orders, one by one. Because all limit orders were previously removed (and confirmed), resulting positions guaranteed to be closed completely. 4. Balance adjustment operations (withdrowals) issued to zero all balances. 5. Suspend command is issued to the core. User record completely removed from the risk control (if possible) - so it not affecting peformance in any way. However for finantial accounting cosistency, if all steps above were not executed correctly - exchange core can re-create user account in SUSPENDED state. For such account external system can try remove it again - by applying same steps. Fixing positions (if margin trading is enabled) would require RESUME user first (if we apply fix from this PR).Straight forward removal with just a single command of caurse looks much easier for beginners. It has potential latency penalty. But i think we can add it if can proce consistency gurantees for such commands.
Thank you for your review of the code and all infos you've shared and the way it can be improved.
What if we can allow the admin to choose between issuing a SUSPEND user request to: 1- Only suspend user without cancelling all orders 2- Suspend both: user and related (spot) orders (CURRENCY_EXCHANGE_PAIR)
The command will be sent as orderId (0 or 1), 1 to allow removing orders.