Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
Re: Best order type to assure getting filled at a specific future price
I agree with @fordfrog. The stop-limit order is probably the best fit for what you want to do. If you have not done so yet, you can read up on a lot of the fine print at or browse details of all order types and algos at There are other options as well, such as the a conditional order as @John suggested. When we design order constructs for an instrument, we take into consideration where the order is held until execution and when exactly it is submitted to the exchange:
Obviously, speed of execution (with possibly better prices), resilience and tolerance against a long list of failures increases the closer your order is held to the exchange. The order types that execute directly at the exchange are a little different from exchange to exchange, but you can quickly find that out from the "Order Types" tab for each exchange listed at In most of our cases, for example, we prefer bracket orders over OCA orders (assuming we can achieve the same goal) since brackets are generally native and OCAs are generally simulated. In your case, the stop-limit order is native on many exchanges while conditional orders are simulated by IBKR. There is nothing wrong with simulated order types or strategies where your client application does all the heavy lifting (we do that a;ll the time), but you probably want to make sure you are aware of possibly failure modes when you make design and trade-off decisions. Hope that helps, ´³¨¹°ù²µ±ð²Ô |
Re: Best order type to assure getting filled at a specific future price
your right stop limit order thanks! On Wed, Apr 5, 2023 at 9:07?AM fordfrog via <fordfrog=[email protected]> wrote: if i got it right, you want??order. |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
I have another update (my problem is fixed, but I am worried about how it was fixed), I was hoping someone else can confirm this about OCA orders. |
Re: Running multiple algorithms in parallel
On Wed, Apr 5, 2023 at 10:56 PM, Richard L King wrote:
I'm just trying to help, and get help, working through a tricky problem. Sorry to have made you feel slighted. |
Re: Running multiple algorithms in parallel
¿ªÔÆÌåÓýBuddy ? I¡¯m perfectly happy for you to ¡®harp on this¡¯ because what you discovered is an important issue. It was you who took your ball home, not me¡ ? By the way, it¡¯s not necessary to treat me as if I¡¯m a junior programmer that knows nothing about debugging, race conditions, etc. Look up the expression ¡®teach your grandmother to suck eggs¡¯ ¨C I¡¯m your grandmother!¡ ? You don¡¯t seem to have picked up on the fact that I have been running your script, and with the call to sleep it hasn¡¯t caused any problems. ? But I¡¯m perfectly willing to admit that I haven¡¯t run the script repeatedly in a loop as you suggest, and I note with interest what you say. So I¡¯ll do ?this tomorrow. ? I also want to mention something that you may or may nor be aware of. When clients make marketdata requests for a given contract, they all end up receiving exactly the same ticks (after the first few that just establish the current high, low, volume, previous close, etc).? So no matter how many clients are receiving data for that contract, there is only a single market data stream coming from IB¡¯s market data servers to TWS, and TWS just passes this data out via the individual API socket connections to the clients. That single stream represents 1 count off your ticker allowance. Moreover it¡¯s actually even better than this, because if both the live and the paper TWS are supplying data for particular contract, that again is just the identical data for both, and just one ticker count. ? So clearly there is some co-ordination going on between the TWSs and the market data servers, and I suspect that the fault lies in here somewhere. ? ? ? From: [email protected] <[email protected]> On Behalf Of buddy
Sent: Wednesday, April 5, 2023 6:24 PM To: [email protected] Subject: Re: [TWS API] Running multiple algorithms in parallel ? On Tue, Apr 4, 2023 at 05:38 PM, Richard L King wrote:
Richard, I hate to harp on this but I think your understanding of the bug is coming along and so I would like to point out, again, that cancelling the market data stream does not prevent the eventual deadlock. In fact it is very easy to refute that it does... just apply the proposed fix to the example and then run the example in a loop. In other words, apply this:
to the example and then run in a forever loop, e.g:
The deadlock may not occur immediately but will happen in somewhat short order. When I tried this the loop would hang by the 12th iteration with a very high likelihood; usually earlier. This behavior is certainly indicative of a race condition; be it in a destructor or reference counter as you suggest. This is why I was encouraging you to forego "issuing API commands discretely" and test the example programmatically and thoroughly. Race conditions often disappear in a debugger or when you "step" through code because the code timing is affected. Additionally, in a real-world application, attempting to "fix" the race condition by introducing an artificial delay wouldn't help since the program would be running in parallel at random overlapping times and therefore any artificial delay would be canceled out by the start times. Anyway, I'm glad you're feeling better. On Sat, Apr 1, 2023 at 10:14 PM, ´³¨¹°ù²µ±ð²Ô Reinold wrote:
´³¨¹°ù²µ±ð²Ô, I'll be the first to admit that the bug I've been droning on about is a corner case. However... it is a very good example of why serious consideration should be given to the decision of implementing a multi-threading/processing design. Multi-threading/processing can sometimes be difficult to implement and often creates very tricky problems that even experienced folks have problems troubleshooting and understanding. And, even if the application design is solid, sometimes the cause is poor vendor support which can be out of the developers control. |
Re: non-simultaneous clients w/ different ID#s won't receive price ticks
On Wed, Apr 5, 2023 at 02:02 PM, J G wrote:
Well, IDK if "not allowed" is the right term but yes... there appears to be a race condition problem introduced in some of the 10.x branch's clean-up code that complicates this. Off the top of my head I have two recommendations:
If there are some long running computations that are necessary, off-load them to separate processes or threads and manage the IPC on your own. If you don't rely on shared data and keep things on the stack... thread-safety should be easy to guarantee. The later option of keeping all data collection on a single thread is my personal preference. And, I've previously mentioned that using makes this pretty easy. But, it's been a long time since I've used Java and so, although there may be an analogue, you'll have to research that solution yourself. Perhaps someone else can help there. I'm sure there are other solutions that achieve a more fine-grained, perhaps higher-throughput, solution but IMHO it likely won't be worth the trouble. -Good luck |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
I don't know if this is progress but I got another mac system running in a VM. At first I got an OCA error on it saying the OCA was filled. I think the OCA code might have existed before like you mentioned.
toggle quoted message
Show quoted text
So I changed how the OCA string from OCA_orderID to OCA_datetime_orderid. Now it runs perfectly on both my macs systems, and on ubuntu and windows it's doing something different altogether. Instead of doing the sell orders, buy orders then skipping over the OCA orders, it just executes the order and closes all my positions on the spot. I tried it with another stock this time just to rule that out. Initially it was a short for MU, so the exit position should be long. I am getting this message. OrderStatus. Id: 16 Status: PreSubmitted Filled: 0 Remaining: 82 AvgFillPrice: 0.0 PermId: 103665766 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld:? MktCapPrice: 0.0OpenOrder. PermId:? 103665767 ClientId: 0? OrderId: 17 Account: DU5632812 Symbol: MU SecType: STK Exchange: SMART Action: BUY OrderType: TRAIL TotalQty: 6 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 1.7976931348623157e+308 Status: PreSubmittedOrderStatus. Id: 17 Status: PreSubmitted Filled: 0 Remaining: 6 AvgFillPrice: 0.0 PermId: 103665767 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld: trigger MktCapPrice: 0.017,0,103665767: TRAIL BUY 6@0 GTDOpenOrder. PermId:? 103665768 ClientId: 0? OrderId: 18 Account: DU5632812 Symbol: MU SecType: STK Exchange: SMART Action: BUY OrderType: MKT TotalQty: 6 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 0.0 Status: PreSubmitted18,0,103665768: MKT BUY 6@0 DAYOrderStatus. Id: 18 Status: PreSubmitted Filled: 0 Remaining: 6 AvgFillPrice: 0.0 PermId: 103665768 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld:? MktCapPrice: 0.0disconnecting |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
Do let us know if there's any logic behind the mystery.? Best wishes, M On Wed, 5 Apr 2023, 11:28 , <omgwtfsalty@...> wrote:
|
Re: Running multiple algorithms in parallel
On Tue, Apr 4, 2023 at 05:38 PM, Richard L King wrote:
Richard, I hate to harp on this but I think your understanding of the bug is coming along and so I would like to point out, again, that cancelling the market data stream does not prevent the eventual deadlock. In fact it is very easy to refute that it does... just apply the proposed fix to the example and then run the example in a loop. In other words, apply this:
to the example and then run in a forever loop, e.g:
The deadlock may not occur immediately but will happen in somewhat short order. When I tried this the loop would hang by the 12th iteration with a very high likelihood; usually earlier. This behavior is certainly indicative of a race condition; be it in a destructor or reference counter as you suggest. This is why I was encouraging you to forego "issuing API commands discretely" and test the example programmatically and thoroughly. Race conditions often disappear in a debugger or when you "step" through code because the code timing is affected. Additionally, in a real-world application, attempting to "fix" the race condition by introducing an artificial delay wouldn't help since the program would be running in parallel at random overlapping times and therefore any artificial delay would be canceled out by the start times. Anyway, I'm glad you're feeling better. On Sat, Apr 1, 2023 at 10:14 PM, ´³¨¹°ù²µ±ð²Ô Reinold wrote:
´³¨¹°ù²µ±ð²Ô, I'll be the first to admit that the bug I've been droning on about is a corner case. However... it is a very good example of why serious consideration should be given to the decision of implementing a multi-threading/processing design. Multi-threading/processing can sometimes be difficult to implement and often creates very tricky problems that even experienced folks have problems troubleshooting and understanding. And, even if the application design is solid, sometimes the cause is poor vendor support which can be out of the developers control. |
Re: non-simultaneous clients w/ different ID#s won't receive price ticks
Unfortunately I'm late to the party. I am working on upgrading from version 9.x to API 10.19 and Gateway 10.22. I have just spent two days figuring out why I got such strange behavior from reqMktData() after upgrading to version 10.x. A reference in another, more recent, thread pointed me to here. What is happening in my case:
Program A connects to Gateway, using clientId1. One of its activities is getting streaming data for an instrument, using reqMktData(). After it has completed its activities it sends cancelMktData() and finally disconnects clientId1. Some seconds later program B connects to Gateway, using clientId2. After a while it tries to use reqMktData() for the same instrument. It does not receive any data. It then sends cancelMktData() and disconnects clientId2. All my programs are coded in Java. My understanding from this thread is that I am not allowed to use two different numbers clientId1 and clientId2, but that both programs must use and re-use the same clientId number. |
Re: Requested market data is not subscribed. Displaying delayed market data
Thank you for your quick reply. In my case it is not a market data subscription issue. I am finding that I can subscribe to market data once and receive streaming data. When I then cancel the subscription, cancel the API client, open a new API client and subscribe to the same ticker I don't receive data any more.
|
Re: Requested market data is not subscribed. Displaying delayed market data
No reply, but I believe it is a subscription issue, because I subscribed and it now works,
I was (wrongly) assuming that you could make snapshots without subscription but that's apparently not true for all exchanges, specifically not ASX and TSEJ. Now to your point, tickPrice should get called, especially on EUR/USD where it ticks and returns data regardless of subscriptions or time of the day. Can you post your call code? |
Re: Requested market data is not subscribed. Displaying delayed market data
John, did you receive any reply or help regarding your question?
The reason why I'm asking is that today I am experimenting with reqMktData() streaming data using API 10.19 and Gateway 10.22. I am using various contracts to experiment with but none of them return any data. Nothing at all. I have used the EUR/USD forex contract, the ES futures contract and some stocks contracts. I do have market data subscriptions for all of these. There are no error messages. Callback tickPrice() is not being called at all. I am wondering what is going wrong. |
Re: Best order type to assure getting filled at a specific future price
Limit if touched where trigger price set at 2060 then it activates the limit order.? On Tue, Apr 4, 2023, 9:55 PM John <serorjb@...> wrote: Not sure I understand, the market is now at 2050 and you want to send a BUY order now, to buy later at a higher price of 2060? why not buy at 2050? |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
I contacted IB support and pressed the number to talk to the "API helpdesk". They asked me to send them the code so they can test it on the different platforms. I also tried running the code in amazon cloud on another apple silicon mac, I can confirm the code works as it should on another apple silicon mac but it doesn't work on linux and windows. |
Re: Best order type to assure getting filled at a specific future price
Not sure I understand, the market is now at 2050 and you want to send a BUY order now, to buy later at a higher price of 2060? why not buy at 2050?
Not sure if limit if touched would work, because the current price would be below the limit threshold, i.e. "better" and I assume would likely get executed immediately. IMHO price condition would be the go to approach for that. |
Best order type to assure getting filled at a specific future price
As price is unfolding at some point I wish to buy at a future (higher) price. Lets say market is 2050.25, lets say i now want to send an order and looking to get a fill at 2060.50. The order will sit there and wait for the market to come to it.?
I would be looking to get filled right on 2060.50.? What would be the best strategy to achieve this, what order type? So far I am looking at a Limit if touched order and also a conditional order, with the price being the condition.? Is this about the right approach or anything else to suggest getting filled at a future price, and directly at the price.? Note this is ES also.? |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
Hi,
I have monitored the prices and there is no way they are getting filled. I have tried multiple times on each platform and monitored the movement of the prices. Should I be using the transmit=False flag for these orders? Would that be the solve for this? I saw them for bracket orders in the documentation but I dont really see anything about OCA. Also, naming wise, I chose OCA_ + orderid, shouldn't that be unique enough? Any idea why it would say inactive filled? |