Can this code buy a duplicated number of contracts?

The following code shows a very simple algorithm that I think it might improve execution price. It works as follows:

  • Inmediately places a BUY order to the Bid
  • After 5 seconds If it is not filled (Still "Submitted") it changes the order to BUY the Ask
  • After 10 seconds if it is not filled (Still "Submitted") it changes the order to BUY at Market price

But I am not sure what will happen if I see "Submitted" on my side but at the same time the order is being executed at the exchange (due to some delay). I know that ModifyOrder places new orders when the ID is empty so I wonder if I can have a duplicated number of contracts from time to time. Any thoughts? Thank you!

//seconds to change order
Counter = DateTimeDiff( Now(5), StaticVarGet("OrderTime") ); 

if ( ibc.IsConnected() )	
		if( LastValue(Buy) AND StaticVarGetText("BuyID") == "" )
			BuyID = ibc.PlaceOrder( tib, "BUY", Ctos, "LMT", GetRTData("Bid") , 0, "DAY", transmit, 1);
			StaticVarSetText("BuyID", BuyID, True);
			//vars to modify the order later
			StaticVarSet("OrderTime", Now(5) );
			StaticVarSet("BuyPending", 1);					
		if ( Counter == 5 AND ibc.GetStatus(StaticVarGetText("BuyID"), True) == "Submitted" AND StaticVarGet("BuyPending") == 1 )
			ibc.ModifyOrder( StaticVarGetText("BuyID"), tib, "BUY", Ctos, "LMT", GetRTData("Ask"), 0, "DAY", transmit, 1);
			StaticVarSet("BuyPending", 2);
		if ( Counter == 10 AND ibc.GetStatus(StaticVarGetText("BuyID"), True) == "Submitted" AND StaticVarGet("BuyPending") == 2 )		
			ibc.ModifyOrder( StaticVarGetText("BuyID"), tib, "BUY", Ctos, "MKT", 0 , 0, "DAY", transmit, 1);
			StaticVarSet("BuyPending", 0);	

I think you are using IB.... If you submit a buy order at the ask, it should be executed. Only thing that can happen is that not your full order size is executed. You would have a partial fill but there would be a new "ask" at which you could place your order. You could then also check if that ask is not a redicolous price.

So I am not sure what you intend to do... I have no clue about the code anyway, sorry for not being able to answer that, but I do think your logic is somewhat "strange" for your order placement.

Also, take into consideration that bid and ask are not the only two prices. You are better off improving the bid instead of getting last in line for the bid. Execution within 5 seconds will hardly ever happen then... (depends on the stock you are placing your order for but I think 99% of the cases you will not be filled at the bid)

You can also think about placing orders at mid price.


Thanks Henri for your answer. I plan to use the code for liquid futures (SP500 mini, hence the subject "number of contracts") so usually bid and ask are separated only by ticksize, there is no middle price and partial fills are not a problem. I don't always get a fill buying at ASK because the price moves very fast, so for that reason I need to finish with a market order, to ensure execution.

My question has to do with the behaviour of the IBController together with Interactive Brokers, when I am modifiying an order in my computer that may being executed in the exchange at the same time; if that duplicates the order.

The function ibc.ModifyOrder() places new orders if it doesn't find the ID to modify. Can someone explain what happens when an OrderID exists, I call ibc.ModifyOrder() and that order is filled already? Thanks

If you try to modify a filled order it'll be rejected (error). As a general rule, use your paper account to test your scripts.

Reading your scripts, it's clear you don't know/understand well enough how your orders are managed and you won't achieve what you want this way. I suggest you try to achieve your goal, efficient execution, using a simpler solution. For instance you could use a standard order (pegged to market, if you trade stocks) or a standard algo (Midprice looks fine for you). Read the TWS API doc for a list of available orders & how to use them.

Good luck.


Thank you. Yes, I understand that trying to modify a filled order is not possible. But my concern has to do with the delay between my computer and the market.

Let's assume that delay between my computer and the exchange is 1 second. When the order is getting filled at the exchange it still has a status of "Submitted" for 1 full second in my computer. If I try to modify it (with ibc.ModifyOrder()) during that second maybe I create another order. Is that correct?

@Armin_Tamzarian - I confirm that all my orders are filled and in synced to my local system prior to the system taking further action. If it is not, I have code to alert me that there might be an issue due to an unforeseen bug, communications delay, outage or whatever. Then I can investigate to see what is happening and manually re-sync if there was an issue.

Also - even in the ES there can be a temporary spread due to liquidity issues in a fast moving market. I would write the code to assume that a spread is possible and can/does happen. You may also want to start on the MES which is still pretty liquid after you have tested on the paper account.

1 Like