My definition of batch-trading: Given $N$ BUY orders, $M$ SELL orders and $O$ ($O < N$) as the max number of open positions to be held. Batch-trading should monitor the orders and when $O$ BUY orders are filled it would cancel the $N-O$ BUY Orders.
To perform this, below is the pseudo-code:
for every PositionMessage that comes in:
if number_of_filled_openpositions >= Max:
// cancel is performed only ONCE, I set a "flag" to make sure
// cancel is called only once during this entire process
cancel_unfilled_BUY_orders_one_by_one()
// OpenPositions have a TimeUpdated field,
// Sell order will be generated starting from the last filled orders
// Sell order is generated only after the position is fully filled
Sell_excess_filled_orders()
The problem I am facing now is, depending on the day, I might get correct number of cancellations and correct number of excess_positions_sold. But on other days, I sometimes run into max number of messages exceeds limit..
or remote host forcibly disconnected your connection
or incorrect number of cancellation or excess_filled_sold.
I have tried introducing a time-delay
within cancel_unfilled_BUY_orders_one_by_one()
so that each cancel order is sent after a time-delay.
I am finding it hard to debug because I need the market-to-be-open to test exhaustively, which is 00:00 (midnight) for me and on weekends the markets are not open (this is a hobby project).
What else should I consider? what am I missing? Has anyone else tried doing something similar?
Happy to elaborate more if required.