¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 ¿ªÔÆÌåÓý

Duplicate Order ID


 

Hello guys,

I'm new here and I'm looking forward that someone might be possible to help me. I'm using Excel for my trading and I'm struggling with the error: Duplicate order id.

1. In my algorithm i place a order to get all the margin information.
2. First i make a request to get the next valid order id
3. I'm waiting 1 sec for the answer and place then an order with this order id

At normal (99%) there is no problem with this but sometimes i get the failure Duplicate order id.
Since yesterday i can't mange this anymore and i was looking in the TWS-logfile and found this:

My request to get the Order id:

2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:INFO] Handling incoming ReqNextValidId(8) message.

2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started reading message:
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished reading message:
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] ReqNextValidId(8)::[version=1,numIds=1]
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Start validating message:
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished validating message:
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started processing message:

Get the next valid order id
11535:

2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:9:1:INFO] Sending next valid order id.

2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:9:1:DET] [9;1;11535]
2022-07-28 07:00:02.971 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished processing message:
2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:INFO] Handling incoming PlaceOrder(3) message.
2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started reading message:
2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished reading message:

Place an order with id
11536 (plus 1 increment: I did this only by hoping that it could help to add one more increment...its not necessary and bullshit i know):

2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] PlaceOrder(3)::[version=151,ID=11536,reqDesc=Symbol=DAX? Type=FUT? Expiry=202209? Strike=0.0? Put/Call=?? Exchange=EUREX? CompExch=null? Currency=EUR? Multiplier=5? IbLocalSymbol=null? IbTradingClass=null? SecIdType=NULL? SecId=null? includeExpired=false? needLeadFutureMonth=false? needContinuousLeadFutureOnly=false? newsSource=null? Legs=null? Special Info=null,transmit=true,parent=0,combo=null]

2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] [151;11536;0;DAX;FUT;202209;0;2;5;EUREX;null;EUR;null;null;NULL;null;1;1;STP;Default;1.7976931348623157E308;Value;1.0;1.7976931348623157E308;1.0;0;U7646634.;true;0;null;ExpireDateTime [null];Reduce on Fill without Block;null;true;null;null;None;1.7976931348623157E308;1.7976931348623157E308;Infinity;DarkPoolOnly=false;AllOrNone=false;Volatility=1.7976931348623157E308;SoftDollars=;NbboPriceCap=1.7976931348623157E308;OptionAcctAttrib=c;StockRefPrice=1.7976931348623157E308;VolatilityType=None;ETradeOnly=false;AdjustableTrailingUnit=amt;MinimumQuantity=2147483647;DeltaNeutralOrderType=None;DeltaNeutralAuxPrice=1.7976931348623157E308;Clearing=/IB;SweepToFill=false;OutsideRth=false;ContinuousUpdate=false;StockRangeUpper=1.7976931348623157E308;OpenCloseAttrib=O;TriggerMethod=Default;OptOutSmartRouting=false;NotHeld=false;Hidden=false;StockRangeLower=1.7976931348623157E308;StartingPrice=1.7976931348623157E308;DisplaySize=2147483647;RelativeDiscretionary=false;PercentOffset=1.7976931348623157E308;FirmQuoteOnly=false;Delta=1.7976931348623157E308;BlockOrder=false;ReferencePriceType=None;{}]
2022-07-28 07:00:05.895 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Start validating message:
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished validating message:
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started processing message:
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Continue processing message
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:INFO] Placing orderId - 11536
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:INFO] Processing of order {11536} completed.
2022-07-28 07:00:05.896 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished processing message:

From here on i do not understand what's going on and why it try's to place a second order. I definitely do no request from excel again. I do the request only one time after i get the valid order id. Then i wait for the answer but the log show me that he place this order again:

2022-07-28 07:00:06.017 [KK] INFO? [JTS-CCPDispatcherS2-35] - Calling doAsSoonAsPossible for preset request

2022-07-28 07:00:06.017 [KK] INFO? [JTS-CCPDispatcherS2-35] - Got Synced preset response
2022-07-28 07:00:06.018 [KK] INFO? [JTS-CCPDispatcherS2-35] - Checking with faConfig for synced presets
2022-07-28 07:00:06.018 [KK] INFO? [JTS-CCPDispatcherS2-35] - Apply clearing defaults to order. Order account: U7646634.. Rule group: []
2022-07-28 07:00:06.020 [KK] INFO? [JTS-CCPDispatcherS2-35] - ENCODING STOP PRICE 1.0
2022-07-28 07:00:06.187 [KK] INFO? [JTS-CCPDispatcherS2-35] - what-if? id=650257091? mod=0? liq=false? initMargin=1.7976931348623157E308? maintMargin=1.7976931348623157E308? equityWithLoan=1.7976931348623157E308 commission=1.7976931348623157E308 min_comm=1.7976931348623157E308 max_comm=1.7976931348623157E308 currentPrice=1.7976931348623157E308 childCurrentPrice=1.7976931348623157E308
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:INFO] Handling incoming PlaceOrder(3) message.
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started reading message:
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished reading message:
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] PlaceOrder(3)::[version=151,ID=11536,reqDesc=Symbol=DAX? Type=FUT? Expiry=202209? Strike=0.0? Put/Call=?? Exchange=EUREX? CompExch=null? Currency=EUR? Multiplier=5? IbLocalSymbol=null? IbTradingClass=null? SecIdType=NULL? SecId=null? includeExpired=false? needLeadFutureMonth=false? needContinuousLeadFutureOnly=false? newsSource=null? Legs=null? Special Info=null,transmit=true,parent=0,combo=null]
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] [151;11536;0;DAX;FUT;202209;0;2;5;EUREX;null;EUR;null;null;NULL;null;1;1;STP;Default;1.7976931348623157E308;Value;1.0;1.7976931348623157E308;1.0;0;U7646634.;true;0;null;ExpireDateTime [null];Reduce on Fill without Block;null;true;null;null;None;1.7976931348623157E308;1.7976931348623157E308;Infinity;DarkPoolOnly=false;AllOrNone=false;Volatility=1.7976931348623157E308;SoftDollars=;NbboPriceCap=1.7976931348623157E308;OptionAcctAttrib=c;StockRefPrice=1.7976931348623157E308;VolatilityType=None;ETradeOnly=false;AdjustableTrailingUnit=amt;MinimumQuantity=2147483647;DeltaNeutralOrderType=None;DeltaNeutralAuxPrice=1.7976931348623157E308;Clearing=/IB;SweepToFill=false;OutsideRth=false;ContinuousUpdate=false;StockRangeUpper=1.7976931348623157E308;OpenCloseAttrib=O;TriggerMethod=Default;OptOutSmartRouting=false;NotHeld=false;Hidden=false;StockRangeLower=1.7976931348623157E308;StartingPrice=1.7976931348623157E308;DisplaySize=2147483647;RelativeDiscretionary=false;PercentOffset=1.7976931348623157E308;FirmQuoteOnly=false;Delta=1.7976931348623157E308;BlockOrder=false;ReferencePriceType=None;{}]
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Start validating message:
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished validating message:
2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Started processing message:

Of course the answer is the duplicate order id:


2022-07-28 07:00:06.965 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:ERR]? OrderId {11536} is < 11537

2022-07-28 07:00:06.966 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:4:2:DET] Sending error.
2022-07-28 07:00:06.966 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:4:2:DET] [4;2;11536;103;Duplicate order id]
2022-07-28 07:00:06.966 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:4:2:DET] Error sent.
2022-07-28 07:00:06.966 [KK] INFO? [JTS-EServerSocket-159] - [1:151:151:1:0:0:0:DET] Finished processing message:

This is the logfile from the API aand this time stamp: It shows two requests too:

07:00:05:223 <- 3-11536-0-DAX-FUT-202209-0--5-EUREX--EUR-----BUY-1-STP--1-DAY---O-0--1-0-0-0-0-0-0-0--0--------0---1-0---0---0-0--0------0-----0-----------0---0-0---1--0-0-0-0--1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-0---------0-0-0--
07:00:06:293 <- 3-11536-0-DAX-FUT-202209-0--5-EUREX--EUR-----BUY-1-STP--1-DAY---O-0--1-0-0-0-0-0-0-0--0--------0---1-0---0---0-0--0------0-----0-----------0---0-0---1--0-0-0-0--1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-1.79769313486232E+308-0---------0-0-0--
07:00:06:294 -> ---!4-2-11536-103-Duplicate order id-
07:00:07:418 <- 8-1-1-
07:00:07:418 -> ---

Could someone understand this log or has the same issue and might be possible to help me. I could also publish my VBA-code here with the order id- and the place order- request.

Best wishes and thx for help

Robert


 

The group cannot fix large chunks of code for you, but I hope we can give you pointers so you can do that yourself. In that spirit a few observations.

TWS log is always very helpful since it tells you what is going on from the TWS/IBGW point of view. But it does not tell you why your client makes these requests so you will need to find a way to add logging to your code. It is very unlikely that the source of and reason for the second order request is anywhere other than in your VBA code.

One area I'd review is the code that determines the next order ID. As the chapter of the API Reference Guide suggests, clients generally maintain their own sequence of order IDs and do not make API calls for the nextOrderId unless they somehow lose the sequence. During the initial connection request, clients are automatically informed of the next valid order ID and, from that point on, simply increment an internal nextOrderId field as needed. Doing so would reduce your order placement complexity (and increase the speed) since you eliminate the request-wait-response cycle through the API and all the way to TWS/IBGW. According to the logs it currently takes nearly three seconds from when TWS sends the nextValidOrderId to when it sees the first placeOrder request.

And finally (frequent readers of my posts will know where this is going - so hold on to your seats) a big red flag is when you say "First i make a request ... [then]? I'm waiting 1 sec for the answer ". That is wrong and I would not be surprised if this contributes to your code placing two identical orders. The TWS API is event driven and completely asynchronous with no guarantee as to the time it takes between a request and the corresponding answer nor to the order in which answers arrive in case there are multiple outstanding requests or subscriptions to data feeds. Your code needs to be structured event oriented as well and needs to wait for the arrival of a callback (or the corresponding error) and not sleep for a fix period of time after the request.

When sleep is used to synchronize requests and responses you'd expect to get exactly what you see: 99% of the time it works correctly and some times it does not. That is when the "sleep" time was too short and the response had not come in yet. And the fix is not to simply increase the sleep time. The fix is to restructure the code and to eliminate the "sleep".

By maintaining the nextOrderId in your client you will naturally avoid this issue, but it would still be interesting for you to review your current code to see what happens if the "sleep 1 second" expires before the nextOrderId callback has come in. Will your code place two orders? One right after the sleep and one triggered by when the nextOrderId callback comes later?

Hope this helps,

´³¨¹°ù²µ±ð²Ô


 

Hello ´³¨¹°ù²µ±ð²Ô,

first of all I thank you very much for your fast reply and your time! I'm absolutely with you that my kind of solution is not perfect but at the time when i started it was the best what i could manage with my knowledge.?

1. "One area I'd review is the code that determines the next order ID. As the Placing Orders chapter of the API Reference Guide suggests, clients generally maintain their own sequence of order IDs and do not make API calls for the nextOrderId unless they somehow lose the sequence. During the initial connection request, clients are automatically informed of the next valid order ID and, from that point on, simply increment an internal nextOrderId field as needed. "

So for my understanding now, it is not necessary to make always a request for a validOrderid and it save process time to increment the order id in a own sub. I get the first valid id with the first connection in the morning. I implemented this and of course the problem with the duplicate order id is gone :)

2. "
our code needs to be structured event oriented as well and needs to wait for the arrival of a callback (or the corresponding error) and not sleep for a fix period of time after the request."

The Code really wait and not sleep. There is a timer in the backround who started the code again and then it follows a validation phase to check the answer from the API. I'm use unique strings that the program always know the correct sequence entry point. IMO there is no way to use multithread with excel so i have to do everything step by step. Of course this need time but still now it is not relevant for my trading.

3. Unfortunately now the wrapper send no response for my DAX contract. If i change the contract to EUR.USD i get the response without any problem. So is there a way if you can make a order request to look if you get an answer?

Kontraktinformationen:
With objTWSControl.m_contractInfo
???? .Symbol ="DAX"
???? .secType = "FUT"
???? .lastTradeDateOrContractMonth = "202209"
???? .multiplier =5
???? .exchange = "EUREX" changed from DTB on monday this week and this was the time when my problem with duplicate Order id started
???? .currency ="EUR"
End With

Orderinformationen:
With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

When I make the request with EUR.USD I get the response without any problem:

Kontraktinformationen:
With objTWSControl.m_contractInfo
???? .Symbol ="EUR"
???? .secType = "CFD"
???? .lastTradeDateOrContractMonth = ""
???? .multiplier =1
???? .exchange = "SMART"
???? .currency ="USD"
End With

Orderinformationen:
With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

My question is now if i have to change anything in the contractinfo object in the way to get a response from TWS via API. IMO it is strange that the problem started for me with the change of the exchange from DTB to EUREX. Do you or anyone know something about this or have the same issue with requests on EUREX-Exchange via API and TWS?

Thanks again for your help!!!

Robert




 

Glad the duplicate order is gone now, Robert.

I was not suggesting a multi-threaded design to avoid "sleep" between requests and responses. Just an event-oriented design. I am not a VBA/Excel macro practitioner, but I believe they do have an event model that supports an event oriented/reactive application design that entirely removes the need for blocking/wait/sleep between request and response.

Not sure what to tell you about the DAX order not working other than the standard checklist. Are you sure your code properly catches and reports all API errors? If you can't get an error to display, this would be a good case for the TWS log, since you'd want to know what TWS thinks you have sent and what the response is.
Not sure whether the DAX contract needs additional fields initialized. We avoid the guess work, as a matter of practice, since we never create our own Contract objects. We rather make . queries when the client starts (with the fewest fields that uniquely describe the instrument, usually just with the local symbol such as "FDXM SEP 22", type, and maybe exchange) and then use the returned fully populated contract object for all requests that require one.

´³¨¹°ù²µ±ð²Ô


On Fri, Jul 29, 2022 at 07:26 AM, Metrodus79 wrote:

1. "One area I'd review is the code that determines the next order ID. As the Placing Orders chapter of the API Reference Guide suggests, clients generally maintain their own sequence of order IDs and do not make API calls for the nextOrderId unless they somehow lose the sequence. During the initial connection request, clients are automatically informed of the next valid order ID and, from that point on, simply increment an internal nextOrderId field as needed. "

So for my understanding now, it is not necessary to make always a request for a validOrderid and it save process time to increment the order id in a own sub. I get the first valid id with the first connection in the morning. I implemented this and of course the problem with the duplicate order id is gone :)

2. "
our code needs to be structured event oriented as well and needs to wait for the arrival of a callback (or the corresponding error) and not sleep for a fix period of time after the request."

The Code really wait and not sleep. There is a timer in the backround who started the code again and then it follows a validation phase to check the answer from the API. I'm use unique strings that the program always know the correct sequence entry point. IMO there is no way to use multithread with excel so i have to do everything step by step. Of course this need time but still now it is not relevant for my trading.

...
My question is now if i have to change anything in the contractinfo object in the way to get a response from TWS via API. IMO it is strange that the problem started for me with the change of the exchange from DTB to EUREX. Do you or anyone know something about this or have the same issue with requests on EUREX-Exchange via API and TWS?


 

¿ªÔÆÌåÓý

Robert

?

You have set .auxPrice = 1 for the DAX contract. The auxPrice field is the trigger price for your stop order. For EUR.USD that is a possible value since the current price for this contract is around 1.019. But the current DAX price is around 13486, so your trigger price of 1 is completely unreasonable and is almost certainly being rejected with an error message.

?

You are handling errorMsg events, presumably? If not, you stand no chance of getting the API to do anything useful¡­

?

Richard

?

?

From: [email protected] <[email protected]> On Behalf Of Metrodus79
Sent: 29 July 2022 13:27
To: [email protected]
Subject: Re: [TWS API] Duplicate Order ID

?

Hello ´³¨¹°ù²µ±ð²Ô,

first of all I thank you very much for your fast reply and your time! I'm absolutely with you that my kind of solution is not perfect but at the time when i started it was the best what i could manage with my knowledge.?

1. "One area I'd review is the code that determines the next order ID. As the Placing Orders chapter of the API Reference Guide suggests, clients generally maintain their own sequence of order IDs and do not make API calls for the nextOrderId unless they somehow lose the sequence. During the initial connection request, clients are automatically informed of the next valid order ID and, from that point on, simply increment an internal nextOrderId field as needed. "

So for my understanding now, it is not necessary to make always a request for a validOrderid and it save process time to increment the order id in a own sub. I get the first valid id with the first connection in the morning. I implemented this and of course the problem with the duplicate order id is gone :)

2. "our code needs to be structured event oriented as well and needs to wait for the arrival of a callback (or the corresponding error) and not sleep for a fix period of time after the request."

The Code really wait and not sleep. There is a timer in the backround who started the code again and then it follows a validation phase to check the answer from the API. I'm use unique strings that the program always know the correct sequence entry point. IMO there is no way to use multithread with excel so i have to do everything step by step. Of course this need time but still now it is not relevant for my trading.

3. Unfortunately now the wrapper send no response for my DAX contract. If i change the contract to EUR.USD i get the response without any problem. So is there a way if you can make a order request to look if you get an answer?

Kontraktinformationen:
With objTWSControl.m_contractInfo
???? .Symbol ="DAX"
???? .secType = "FUT"
???? .lastTradeDateOrContractMonth = "202209"
???? .multiplier =5
???? .exchange = "EUREX" changed from DTB on monday this week and this was the time when my problem with duplicate Order id started
???? .currency ="EUR"
End With

Orderinformationen:
With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

When I make the request with EUR.USD I get the response without any problem:

Kontraktinformationen:
With objTWSControl.m_contractInfo
???? .Symbol ="EUR"
???? .secType = "CFD"
???? .lastTradeDateOrContractMonth = ""
???? .multiplier =1
???? .exchange = "SMART"
???? .currency ="USD"
End With

Orderinformationen:
With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

My question is now if i have to change anything in the contractinfo object in the way to get a response from TWS via API. IMO it is strange that the problem started for me with the change of the exchange from DTB to EUREX. Do you or anyone know something about this or have the same issue with requests on EUREX-Exchange via API and TWS?

Thanks again for your help!!!

Robert



 

Hello ´³¨¹°ù²µ±ð²Ô,

thanks for the fast reply again. I will test this at the weekend to get the contract infos from IBApi.EClient.reqContractDetails and give my result here.

Thanks a lot again for this hind!

VG Robert


 

Hello Richard,

you can use this object with this parameters without any problems when you set the ".whatIf = 1" because it gives you only information back like your margin conditions and your equityWithLoan before and after this order will be set. I had never a problem with this. Of course you cant use this parameters to set an real order.

With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

Public Sub M_REAL_ORDER_MARGIN_AKTUALISIERUNG(orderid As Long, contract As TWSLib.IContract, order As TWSLib.IOrder, orderState As TWSLib.IOrderState)

????????? =orderState.warningText
??????? ? =orderState.equityWithLoanAfter
????????? =orderState.initMarginChange
????????? =orderState.maintMarginChange
????????? =orderState.equityWithLoanAfter

end sub

Do you maybe trade FDXM from Eurex?

Thanks a lot too for your reply. Everything could help me to figure out that problem.


 

¿ªÔÆÌåÓý

Ok, I¡¯ll take your word for the ¡®whatif¡¯ aspect.

?

If I use the contract parameters you specify in my platform, it works fine. If it doesn¡¯t work for you, you must get an error message (assuming your request is actually being actioned by the API). So what is the error message?

?

I don¡¯t normally use FDXM but I just pushed an order through on the paper trading system and it executed immediately. For what it¡¯s worth, here¡¯s the log entry that shows the order API message (I have my own API implementation that can log all API messages, similar to the TWS API logfile but rather easier to use). It may not be much use to you in this context:

?

2022-07-29 19:35:37.819 S [IBAPIV100.BufferedWriter:logSocketOutputMessage] OUT: Msg Id (PLACE_ORDER)=3;Version=45;Order id=268445460;ConId=534005999;Symbol=DAX;Sec type=FUT;Expiry=20220916;Strike=0;Multiplier=5;Exchange=EUREX;Currency=EUR;Local Symbol=FDXM SEP 22;Action=BUY;Quantity=1;Order type=MKT;TIF=DAY;OpenClose=O;Origin=0;Transmit=1;Parent id=0;Block Order=0;Sweep to fill=0;Display Size=0;Trigger method=0;Outside RTH=0;Hidden=0;Discretionary amount=0.0; Short Sale Slot=0;Exempt Code=-1;Oca type=0;All or none=0;E-trade only=0;Firm quote only=0;Auction strategy=0;Override percentage constraints=0;Continuous update=0;Opt Out Smart Routing=0;Not held=0;Delta Neutral=0;WhatIf=0;Solicited=0;Randomize size=0;Randomize price=0;Conditions Count=0;AdjustableTrailingUnit=0;DontUseAutoPriceForHedge=0;

?

And here¡¯s the resulting execution:

?

2022-07-29 19:35:38.187 S [IBAPIV100.BufferedReader:EndLogMessage] IN: Msg id (EXECUTIONDATA)=11;ReqId=-1;OrderId=268445460;Con Id=534005999;Symbol=DAX;Sec type=FUT;Expiry=20220916;Strike=0.0;Right=;Multiplier=5;Exchange=EUREX;Currency=EUR;Local Symbol=FDXM SEP 22;TradingClass=FDXM;Exec id=0000e9b5.62e36263.01.01;Exec Time=20220729? 19:35:38;Account id=DU5263;Exchange=EUREX;Side=BOT;Shares=1;Price=13508.00;Perm Id=2048695160;Client id=194362192;Liquidation=0;Cum Qty=1;Avg Price=13508.00;OrderRef=;EvRule=;EvMultiplier=;Model Code=;Last Liquidity=1;

?

Richard

?

From: [email protected] <[email protected]> On Behalf Of Metrodus79
Sent: 29 July 2022 19:29
To: [email protected]
Subject: Re: [TWS API] Duplicate Order ID

?

Hello Richard,

you can use this object with this parameters without any problems when you set the ".whatIf = 1" because it gives you only information back like your margin conditions and your equityWithLoan before and after this order will be set. I had never a problem with this. Of course you cant use this parameters to set an real order.

With objTWSControl.m_orderInfo
???? .Action =? "BUY"
???? .totalQuantity = 1
???? .orderType = "STP"
???? .auxPrice = 1
???? .timeInForce = "DAY"
???? .whatIf = 1
End With

Public Sub M_REAL_ORDER_MARGIN_AKTUALISIERUNG(orderid As Long, contract As TWSLib.IContract, order As TWSLib.IOrder, orderState As TWSLib.IOrderState)

????????? =orderState.warningText
??????? ? =orderState.equityWithLoanAfter
????????? =orderState.initMarginChange
????????? =orderState.maintMarginChange
????????? =orderState.equityWithLoanAfter

end sub

Do you maybe trade FDXM from Eurex?

Thanks a lot too for your reply. Everything could help me to figure out that problem.


 

Hello Folks,

summarized briefly I could figure out that the problem was a confirmation what i had to do in my account. After that i could set the order placement.

@Richard: Unfortunately I forgot to say that i had no problem on my paper account. Only on my live account and only for the future contracts. There absolutely no failure log were i could figure out why the order was not accepted. But i thank you for your time and that you tried this for me!

@´³¨¹°ù²µ±ð²Ô: The hind with the order-ID is great and i implemented this in my program. I tried to implement the too but still now i couldn't manage this. Maybe you can give me a hind again.

1. I made the request for the contract details and i get a object called contract: m_TWSControl_contractDetailsEx(ByVal lngreqId As Long, ByVal contract As ComContractDetails)
2. In the next step i tried to use this object in the request for order placement
3. I create the object with the order information out of the class m_orderCom. This object comes for the class ComOrder and is defined as? Public m_orderCom As TWSLib.ComOrder:
Set objTWSControl.m_orderCom = objTWSControl.m_TWSControl.createOrder
4. I fill that object with the significant order information

??????????????? With objTWSControl.m_orderCom
?? ?
??????????????????? .Action = "BUY" 'strOrderAktion? 'BUY, SELL, SSHORT"
??????????????????? .totalQuantity = 1 'lngOrderAnzahl 'ORDER ANZAHL
??????????????????? .orderType = "STP" 'strOrderTyp? 'MKT, MKTCLS, LMT, LMTCLS, PEGMKT, SCALE, STP, STPLMT, TRAIL, REL, VWAP, TRAILLIMIT
??????????????????? .auxPrice = 1 'dblOrderStoppPreis 'Stopp price
??????????????????? .timeInForce = "DAY" 'Order ist nur f¨¹r den Tag der Orderer?ffnung aktiv
??????????????????? .whatIf = 1 'bolwhatIf
?????????????? ?
??????????????? End With
5. I put the two objects (contract and objTWSControl.m_orderCom) in the request for the order placement and it doesn't work
?? ???????????
?????????????? Set objTWSControl.m_orderCom.orderMiscOptions = objTWSControl.m_TWSControl.createTagValueList()
?????????????? Call objTWSControl.m_TWSControl.placeOrderEx(lngOrderId, contract, objTWSControl.m_orderCom)

I think the object "contract" out of the is not correct used. I tried different ways but I'm not sure if i can use the complete object or if i have to take all the information out and put this in a new object.

You helped me a lot to solve my problem. If this need to much time its absolute ok for me. i will figure out how it works by time.

THANKS A LOT AGAIN!

Robert
???


 

Glad that worked, Robert. On the surface your approach looks fine. It is the contract object you receive with your object that you'd use for the order. There is a little more detail at

Couple of thoughts:

  • What error message do you get when the order placement fails?
  • Did you get exactly one object from your request and is it the one you are trying to order? reqContractDetails returns a list of results and depending on what you ask for, you can get 0, 1, or several (even many) .
  • I am not familiar with the Excel/VBA details. But in your earlier post it looked like you used "With objTWSControl.m_contractInfo" to specify the contract information. Is it possible that you have to set objTWSControl.m_contractInfo to the contract object you received in

I guess you have to do a little more digging.

´³¨¹°ù²µ±ð²Ô


 

As you mentioned: " I think the object "contract" out of the??is not correct used. I tried different ways but I'm not sure if i can use the complete object or if i have to take all the information out and put this in a new object.?"

I do think the same as you.?
You need to "extract" the information received to fill the new object for the order to be sent.?

I think?the most important is this one, in my implementation which may differ from yours:
(The object conDetails comes from the Contract detail request)

? ? Dim contract As TWSLib.IContract
? ? Set contract = conDetails.contract
? ? Debug.print "Api contractDetails " & contract.localSymbol & "-" & contract.Exchange & "-" & contract.secType
? ? Debug.print " ? ? ?-----------> " & conDetails.lastTradeTime & ";" & conDetails.contractMonth & ";" & contract.lastTradeDateOrContractMonth & ";" & conDetails.timeZoneId & ";" & contract.conId & ";" & conDetails.priceMagnifier

This above piece of code will display on your excel debugger log some of the information which sends your the TWS once the request of contract details is received. Hope that helps you to fill the proper information for the new order request

Hope that helps

El dom, 31 jul 2022 a las 18:58, Metrodus79 (<robert.metasch@...>) escribi¨®:
Hello Folks,

summarized briefly I could figure out that the problem was a confirmation what i had to do in my account. After that i could set the order placement.

@Richard: Unfortunately I forgot to say that i had no problem on my paper account. Only on my live account and only for the future contracts. There absolutely no failure log were i could figure out why the order was not accepted. But i thank you for your time and that you tried this for me!

@´³¨¹°ù²µ±ð²Ô: The hind with the order-ID is great and i implemented this in my program. I tried to implement the too but still now i couldn't manage this. Maybe you can give me a hind again.

1. I made the request for the contract details and i get a object called contract: m_TWSControl_contractDetailsEx(ByVal lngreqId As Long, ByVal contract As ComContractDetails)
2. In the next step i tried to use this object in the request for order placement
3. I create the object with the order information out of the class m_orderCom. This object comes for the class ComOrder and is defined as? Public m_orderCom As TWSLib.ComOrder:
Set objTWSControl.m_orderCom = objTWSControl.m_TWSControl.createOrder
4. I fill that object with the significant order information

??????????????? With objTWSControl.m_orderCom
?? ?
??????????????????? .Action = "BUY" 'strOrderAktion? 'BUY, SELL, SSHORT"
??????????????????? .totalQuantity = 1 'lngOrderAnzahl 'ORDER ANZAHL
??????????????????? .orderType = "STP" 'strOrderTyp? 'MKT, MKTCLS, LMT, LMTCLS, PEGMKT, SCALE, STP, STPLMT, TRAIL, REL, VWAP, TRAILLIMIT
??????????????????? .auxPrice = 1 'dblOrderStoppPreis 'Stopp price
??????????????????? .timeInForce = "DAY" 'Order ist nur f¨¹r den Tag der Orderer?ffnung aktiv
??????????????????? .whatIf = 1 'bolwhatIf
?????????????? ?
??????????????? End With
5. I put the two objects (contract and objTWSControl.m_orderCom) in the request for the order placement and it doesn't work
?? ???????????
?????????????? Set objTWSControl.m_orderCom.orderMiscOptions = objTWSControl.m_TWSControl.createTagValueList()
?????????????? Call objTWSControl.m_TWSControl.placeOrderEx(lngOrderId, contract, objTWSControl.m_orderCom)

I think the object "contract" out of the is not correct used. I tried different ways but I'm not sure if i can use the complete object or if i have to take all the information out and put this in a new object.

You helped me a lot to solve my problem. If this need to much time its absolute ok for me. i will figure out how it works by time.

THANKS A LOT AGAIN!

Robert
???


 

¿ªÔÆÌåÓý

There is no point in deleting it ¨C it has already been sent out to all the members (the vast majority) who receive posts via email.

?

Just post again with the complete message, and you can add a comment explaining that the previous post was incomplete.

?

Richard King

Moderator

?

?

From: [email protected] <[email protected]> On Behalf Of Metrodus79
Sent: 01 August 2022 11:08
To: [email protected]
Subject: Re: [TWS API] Duplicate Order ID

?

How i can delete the last message...i was not finish with this ;)


 

Hello joanmarcel119, hello ´³¨¹°ù²µ±ð²Ô

I'm not anymore sure if the the request for the is valuable for me. To make the request i need a contract object and fill this with all the information that also need to place an order. I'm not sure if the contract object need all the information that i put in.

Request for?

Sub M_REAL_ORDER_KONTRAKT_DETAILS_ANFORDERN()

Call M_GLOB_DEKLARATIONEN_EXCEC
?? ?
??? If Not (objTWSControl Is Nothing) Then
????? ?
??????? If objTWSControl.m_isConnected Then
?????? ?
??????????? 'Create object contract
??????????? Set contract = objTWSControl.contract

??????????? 'Fill this object it with contract information
??????????? With objTWSControl.contract

??????????????? .Symbol = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SYMBOL")))
??????????????? .secType = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SECTYP")))
??????????????? .lastTradeDateOrContractMonth = wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("VERFALLSDATUM"))
??????????????? .multiplier = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("MULTIPLIKATOR")))
??????????????? .exchange = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("B?RSE")))
??????????????? .currency = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("W?HRUNG")))
??????????? ?
??????????? End With
?????????? ?
??????????? Dim lngreqId As Long
?????????? ?
??????????? lngreqId = 1
???????????
?????????? 'Call for detailed contract information.
??????????? Call objTWSControl.m_TWSControl.reqContractDetailsEx(lngreqId, objTWSControl.m_contractInfo)
?????? ?
??????? End If
??
?? End if

end Sub

Public Event send me the contract object in class modul

Public WithEvents m_TWSControl As Tws
Public contract As TWSLib.IContract

Private Sub m_TWSControl_contractDetailsEx(ByVal lngreqId As Long, ByVal contract As ComContractDetails)

Call M_REAL_ORDER_KONTRAKT_OBJEKT(lngreqId, contract)

End Sub

Do i get the same object back what i declared before? is This my problem here?

Thanks again but I'm struggling with this.




 

I think the best for you is to get the excel file which comes with the IB API and do some tests in their code.?

What you have now developed I can not check if it is valid or it will work. I started the same way, by copying first the excel file of IB and then doing some minor changes and debugging. So if you already did this, then I recommend starting again. You should also debug the code to see how it works, like for example, setting a breakpoint?into the procedures called by the TWS usually?placed in the API module after your request has been sent.

Hope that helps

El lun, 1 ago 2022 a las 12:37, Metrodus79 (<robert.metasch@...>) escribi¨®:
Hello joanmarcel119, hello ´³¨¹°ù²µ±ð²Ô

I'm not anymore sure if the the request for the is valuable for me. To make the request i need a contract object and fill this with all the information that also need to place an order. I'm not sure if the contract object need all the information that i put in.

Request for?

Sub M_REAL_ORDER_KONTRAKT_DETAILS_ANFORDERN()

Call M_GLOB_DEKLARATIONEN_EXCEC
?? ?
??? If Not (objTWSControl Is Nothing) Then
????? ?
??????? If objTWSControl.m_isConnected Then
?????? ?
??????????? 'Create object contract
??????????? Set contract = objTWSControl.contract

??????????? 'Fill this object it with contract information
??????????? With objTWSControl.contract

??????????????? .Symbol = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SYMBOL")))
??????????????? .secType = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SECTYP")))
??????????????? .lastTradeDateOrContractMonth = wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("VERFALLSDATUM"))
??????????????? .multiplier = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("MULTIPLIKATOR")))
??????????????? .exchange = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("B?RSE")))
??????????????? .currency = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("W?HRUNG")))
??????????? ?
??????????? End With
?????????? ?
??????????? Dim lngreqId As Long
?????????? ?
??????????? lngreqId = 1
???????????
?????????? 'Call for detailed contract information.
??????????? Call objTWSControl.m_TWSControl.reqContractDetailsEx(lngreqId, objTWSControl.m_contractInfo)
?????? ?
??????? End If
??
?? End if

end Sub

Public Event send me the contract object in class modul

Public WithEvents m_TWSControl As Tws
Public contract As TWSLib.IContract

Private Sub m_TWSControl_contractDetailsEx(ByVal lngreqId As Long, ByVal contract As ComContractDetails)

Call M_REAL_ORDER_KONTRAKT_OBJEKT(lngreqId, contract)

End Sub

Do i get the same object back what i declared before? is This my problem here?

Thanks again but I'm struggling with this.




 

You should not have to create all that code in order to use a contract object you received from reqContractDetails. And you should be able to use the entire object without having to copy individual fields. That defeats the purpose. Unfortunately, I don't have enough experience with Excel/VB (nor do we use MS/Office programs, LibreOffice/OpenOffice works much better for us) and I cannot give you additional pointers here.

Maybe, for now, you should create contracts from scratch again.

Using reqContractDetails to retrieve contract objects that IBKR creates for you with all fields they care about initialized is a "best practice" (but clearly not the only approach):

  • It facilitates a code structure that "separates concerns". Meaning that you have a small specialized module that knows everything that needs to be known about instruments, trading hours, exchanges, and other contract object details. It knows the many things that can go wrong and what to do in case something does, and applies in a central location all validations and safety checks you care about. You don't want to realize during an urgent position close attempt that something has changed and the contract is no good for trading.
  • You are correct that reqContractDetails also needs a contract object for the request, but it has a different function, is more human oriented, and you can provide fewer details than what you'd need for placing orders or other API requests (machine usable). You can specify the instrument in the most convenient way for you (local symbol, CUSIP/ISIN, conid, ...) and, if that makes sense, you can even ask for all contracts of a certain type (say all active futures of a certain type, or the contracts for all currencies of a stock, ...) and you get trade hour details for the current session and the next week or so. Are you really sure you know all rules for trading around holidays and other, possibly short notice, trade hour changes?
  • take a look at Richard King's ContractInspector that you can get at . The ContractInspector allows to explore what reqContractDetails can do for you without having to create code first.

But again, this only makes sense if you can convince the Excel/VB environment that it should use the contract object from reqContractDetails without any extra coding or field copying.

´³¨¹°ù²µ±ð²Ô

On Mon, Aug 1, 2022 at 05:37 AM, Metrodus79 wrote:

Hello joanmarcel119, hello ´³¨¹°ù²µ±ð²Ô

I'm not anymore sure if the the request for the is valuable for me. To make the request i need a contract object and fill this with all the information that also need to place an order. I'm not sure if the contract object need all the information that i put in.

Request for?

Sub M_REAL_ORDER_KONTRAKT_DETAILS_ANFORDERN()

Call M_GLOB_DEKLARATIONEN_EXCEC
?? ?
??? If Not (objTWSControl Is Nothing) Then
????? ?
??????? If objTWSControl.m_isConnected Then
?????? ?
??????????? 'Create object contract
??????????? Set contract = objTWSControl.contract

??????????? 'Fill this object it with contract information
??????????? With objTWSControl.contract

??????????????? .Symbol = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SYMBOL")))
??????????????? .secType = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("SECTYP")))
??????????????? .lastTradeDateOrContractMonth = wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("VERFALLSDATUM"))
??????????????? .multiplier = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("MULTIPLIKATOR")))
??????????????? .exchange = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("B?RSE")))
??????????????? .currency = UCase(wksRealImport.Cells(lngZ, flngFindeSpaltewksRealImport("W?HRUNG")))
??????????? ?
??????????? End With
?????????? ?
??????????? Dim lngreqId As Long
?????????? ?
??????????? lngreqId = 1
???????????
?????????? 'Call for detailed contract information.
??????????? Call objTWSControl.m_TWSControl.reqContractDetailsEx(lngreqId, objTWSControl.m_contractInfo)
?????? ?
??????? End If
??
?? End if

end Sub

Public Event send me the contract object in class modul

Public WithEvents m_TWSControl As Tws
Public contract As TWSLib.IContract

Private Sub m_TWSControl_contractDetailsEx(ByVal lngreqId As Long, ByVal contract As ComContractDetails)

Call M_REAL_ORDER_KONTRAKT_OBJEKT(lngreqId, contract)

End Sub

Do i get the same object back what i declared before? is This my problem here?

Thanks again but I'm struggling with this.




 

Hello Folks,

thanks a lot for your help again. I don't want to use more of your time. I have a workaround that works for me with creating a contract object. Its time for me now to figure out how i can use the contractdetails object and if i manage this i will publish this here. So thanks again for all the information and have a good time!

Best wishes Robert