I am having problem in real time trading when using ibc.ModifyOrder
I read the documentation and it says that if order id passed does not exist then this will create a new order itself. Is there a way to make sure that ibc.modifyOrder fails rather than create a new order if order id is not found by it.
What about checking if the order exists before trying to modify it !
I am first checking if the order status == Submitted and only then going forward with modifying it. In 99% cases this works fine. However, a very minute number of times, it so happens that the modifyOrder creates a new order if the original order gets filled. I think what is happening is...
99% times - Either of these..(both expected and wanted behaviour)
Order is submitted and not filled so modifyOrder modifies it
Order is submitted but by the time code calls modifyOrder it has already filled and ibController also has got to know that so modifyOrder fails with error Order filled
1% times - (GUESSING) Order is submitted and when modifyOrder is called, the order actually got filled but ibController doesn't know it yet and the request gets passed to IB and new order with new order id gets created (UNWANTED AND RISKY)
I can code to control it by checking that when order id returned by modifyOrder is different from the one passed, the code will know there is a duplicate order in system. But I am afraid if I do handle this in code by squaring off the new unwanted position, I might get into a avalanche loop of multiple orders of BUY SELL (if the 1% keeps happening in a short interval time) and that WILL wipe out the account as Ami is fast and the number of orders even might run into 50 per second before anyone notices and stops it
However, this is not how modifyOrder should work. Ideally it should fail if it cannot modify the order id passed and NOT create a new duplicate order in any case
You don't understand the statuses in IBC
function f_OrderCanBeModified( id )
{
global ibc;
st = ibc.GetStatus( id );
return ( st == "Submitted" OR st == "PreSubmitted" OR st == "ApiPending" );
}
,,,,
if (f_OrderCanBeModified( id ))
ModifyOrder(Id,........);
The thing is that this is the way how TWS API works. It has only PlaceOrder method and it is common for both placing and modifying orders. It is your task to make sure that order that you modify actually exists: