It seems the API is not correctly handling replacing of partially filled sell orders (buy orders work as expected).
When a buy order is partially filled and then gets replaced with an updated price, it will eventually fill the total quantity that was set initially. This shows that the API is internally keeping track of the partially filled quantity and only applying the new price to the rest of the quantity needed to fill the initially targeted qty.
However, when a sell order that was opened with qty=N is partially filled and then gets replaced with an updated price, the API is not correctly keeping track of the partially filled quantity. If updating the price, the API returns an error insufficient qty available for order (requested: N, available: N-partially_filled_qty)
, showing that it thinks that the updated order which still has the initial qty set is meant to be executed for qty = N instead of qty = N - partially_filled_qty. It seems that whatever logic is checking whether enough qty is available for a regular sell order doesn’t account for fact the qty set in the replaced order may have already been partially filled and does not need to be <= to existing position.
On the other hand, if the client reduces the quantity in the replaced order request by the partially filled quantity the final filled qty (including the initial partial fill) will be less than the initially set qty, i.e. it will equal the newly set quantity. This shows that the actual execution of a replaced order does keep track of the partial fill quantity and only executes the difference between the newly set quantity and the initial partial fill for the replaced order.
This internal inconsistency in the way the API handles qty seems to be unintended behavior and prevents the client from replacing a partially filled sell order correctly.