Keyboard Shortcuts
Likes
- Twsapi
- Messages
Search
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, J¨¹rgen Reinold wrote:
J¨¹rgen, 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, J¨¹rgen Reinold wrote:
J¨¹rgen, 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? |
Re: Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
Hi there, If I'm reading your responses right the trouble is caused by one of your oca orders getting filled. There's a couple of ways that could happen. The obvious way is through price action, the ltd obvious way is if the oca group are not uniquely named, this can be tricky to spot, particularly if the order is part of a complex set of orders. Are you monitoring for executions? Best wishes, M On Tue, 4 Apr 2023, 19:04 , <omgwtfsalty@...> wrote: I meant to say I am using the latest Stable, not the latest latest. |
Weird behaviour from IB gateway / tws when using mac vs windows vs linux.
I am getting really weird behaviour from the IBAPI. I am going to try to keep my message as short as possible but I am getting different behaviour on my macos and on my windows and linux distros.
I run it in Mac OS, Ubuntu and Windows using the latest twsapi and ib gateways using the correct port numbers and addresses. I am using python. My steps: I Long / Buy 5 shares of AAPL. I Short / Sell 5 shares of ORCL. I Place an Order to Close the Longs and Shorts at 3:45pm or Trailing stop loss of 5%, one cancels all for each stock. Here is my code for buying the longs and shorts. This works fine on all platforms. As you can see the code does not continue until the orders are filled. ``` order = order_maker(action='SELL',type='MKT', quantity=5, tif='GTC')
app.placeOrder(app.nextorderId, EQ_order_MKT('ORCL'), order)
app.nextorderId += 1
while True:
orders = read_open_orders(app)
if len(orders[orders['Status'] == 'Filled']) == len(orders):
break
else:
time.sleep(3)
print('.', end = '')
order = order_maker(action='BUY',type='MKT', quantity=5, tif='GTC')
app.placeOrder(app.nextorderId, EQ_order_MKT('AAPL'), order)
app.nextorderId += 1
while True:
orders = read_open_orders(app)
if len(orders[orders['Status'] == 'Filled']) == len(orders):
break
else:
time.sleep(3)
print('.', end = '')
```This next part is the weird behaviour I don't understand. I run this code right after the above orders are filled to close out the Long appl shares either at 3:45pm or if theres a stop loss of 5%. Either way the position will be closed out at 3:45pm. ``` tradedate = (datetime.datetime.now(pytz.timezone('US/Eastern'))).strftime('%Y%m%d')
orderts = trailing_stop_order_maker(action='SELL',type='TRAIL', quantity=5, trailpercent=5.00, gtd=f'{tradedate} 15:45:00 US/Eastern', tif='GTD')
orderc = order_maker_delayed(action='SELL',type='MKT', quantity=5, gat=f'{tradedate} 15:45:00 US/Eastern', tif='GAT')
ocaOrders = [orderts, orderc]
ocaOrders = os.OneCancelsAll('OCA_' + str(app.nextorderId), ocaOrders, 1)
for o in ocaOrders:
app.placeOrder(app.nextorderId, EQ_order_MKT('AAPL'), o)
app.nextorderId += 1
``` Here is the weird behaviour, On my mac, the orders are filled, and I get the message: ``` OpenOrder. PermId: 1238129788 ClientId: 0 OrderId: 31 Account: DU123 Symbol: AAPL SecType: STK Exchange: SMART Action: SELL OrderType: MKT TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 0.0 Status: PreSubmitted 31,0,1238129788: MKT SELL 5@0 DAY OrderStatus. Id: 31 Status: PreSubmitted Filled: 0 Remaining: 5 AvgFillPrice: 0.0 PermId: 1238129788 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld: MktCapPrice: 0.0 OpenOrder. PermId: 1238129787 ClientId: 0 OrderId: 30 Account: DU123 Symbol: AAPL SecType: STK Exchange: SMART Action: SELL OrderType: TRAIL TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 1.7976931348623157e+308 Status: PreSubmitted 30,0,1238129787: TRAIL SELL 5@0 GTD OrderStatus. Id: 30 Status: PreSubmitted Filled: 0 Remaining: 5 AvgFillPrice: 0.0 PermId: 1238129787 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld: trigger MktCapPrice: 0.0 OpenOrder. PermId: 1238129790 ClientId: 0 OrderId: 33 Account: DU123 Symbol: ORCL SecType: STK Exchange: SMART Action: BUY OrderType: MKT TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 0.0 Status: PreSubmitted 33,0,1238129790: MKT BUY 5@0 DAY ``` Everything works as it should. But on UBUNTU and WINDOWS I get the following message, ``` penOrder. PermId: 1591978271 ClientId: 0 OrderId: 33 Account: DU123 Symbol: AAPL SecType: STK Exchange: SMART Action: SELL OrderType: TRAIL TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 1.7976931348623157e+308 Status: Inactive 33,0,1591978271: TRAIL SELL 5@0 GTD OrderStatus. Id: 33 Status: Inactive Filled: 0 Remaining: 5 AvgFillPrice: 0.0 PermId: 1591978271 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld: MktCapPrice: 0.0 ERROR 33 201 Order rejected - reason:OCA group is already filled Error. Id: 33 Code: 201 Msg: Order rejected - reason:OCA group is already filled OpenOrder. PermId: 1591978271 ClientId: 0 OrderId: 33 Account: DU123 Symbol: AAPL SecType: STK Exchange: SMART Action: SELL OrderType: TRAIL TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 1.7976931348623157e+308 Status: Inactive 33,0,1591978271: TRAIL SELL 5@0 GTD OrderStatus. Id: 33 Status: Inactive Filled: 0 Remaining: 5 AvgFillPrice: 0.0 PermId: 1591978271 ParentId: 0 LastFillPrice: 0.0 ClientId: 0 WhyHeld: MktCapPrice: 0.0 OpenOrder. PermId: 1591978272 ClientId: 0 OrderId: 34 Account: DU123 Symbol: AAPL SecType: STK Exchange: SMART Action: SELL OrderType: MKT TotalQty: 5 CashQty: 0.0 LmtPrice: 0.0 AuxPrice: 0.0 Status: Inactive ``` |