¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io
Date

Re: Gateway loses historical market data every day

 

Ok, thanks. Today I observed that it behaves like described. But yesterday it did not reactivate. The yesterdays client log contains this two times:
id: 416904 | Error Code: 366 | Error Msg: No historical data query found for ticker id:41690

And this over 300 times:
?id: 416904 | Error Code: 162 | Error Msg: Historical Market Data Service error message:API historical data query cancelled: 416904

What can I do to analyze/ avoid that? Due to that the trades couldn't be created.


Re: C++ TWS API w/ Cmake

 

I haven't worked with Windows in a while but I don't think using a library changes too much from platform to platform. So, anyway...

I'd probably give a shot until you decide to dive into the depths of any build system.


Re: Gateway loses historical market data every day

 

@Gonzalo
Yes
IMHO that's same issue (or non issue) a first call reactivate the connection as the message explain it.
Once done and depending upon your TZ/Instrument TZ and RTH and ... your 'live' between ~3 min to ~15 min.
What you show as a 'strange' sequence may not be correlated,
Like you didn't use GW for 10 min (Hence the 2nd message), but in parallel IB did disconnect and reconnect your connection for other reasons (Hence the first message, what is interesting is the previous one too)
Anyway
- If you can spent 10 min doing nothing unlikely you need less than 100 millisecond response time for Historical data. (you can! just setup a heart beat)
- I get this also and it doesn't bother me or have any operational impact, it's just HMDS (Historical)

@Greg
Yes TWS does implicitly a 'heart beat'
As every instrument displayed may require a price update. While GW does only what you ask it (nearly).
But TWS is more hungry in memory and resources, and is not very server friendly.

You should think GW.
If an implementation doesn't work properly with GW, one day it may not work with TWS too.
Or the opposite TWS give you wrong impression that your code is self sufficient.
So that your fix using TWS to keep up tick-by-tick may hide another issue.


C++ TWS API w/ Cmake

 

Hi,

I am trying to leverage Pytorch's C++ API with the TWS API in a single project. I have an existing project in Visual Studio that uses the TWS API. PyTorch for C++ really only supports a build using Cmake. So, I may be forced to make a new project with Cmake so I can use PyTorch. If that is the case, has anyone successfully built the C++ TWS API in a Cmake project? Because I need both of these APIs, desperately. If so, any tips on how I can go about this? I am a noob at Cmake...

Thanks,
Brendan


Re: Gateway loses historical market data every day

 

Hi,

I¡¯m not sure if it¡¯s related. But I keep seen this warnings

INFO:2023-08-17 15:38:03,286:Warning 2106, reqId -1: HMDS data farm connection is OK:ushmds.nj
INFO:2023-08-17 15:39:26,379:Warning 2107, reqId -1: HMDS data farm connection is inactive but should be available upon demand.ushmds
The first one when I do a request, and the second line after I get the results

On 17 Aug 2023, at 19:04, Greg Angelow <twsapi@...> wrote:

I have a similar problem with losing IBGW connections at tick-by-tick data.
The easiest way I found is to use the TWS API instead of the IBGW API.
TWS seems to take care of a continuous connection, because the GUI cannot know if someone is sitting in front of it watching the data or not.





Re: Gateway loses historical market data every day

 

I have a similar problem with losing IBGW connections at tick-by-tick data.
The easiest way I found is to use the TWS API instead of the IBGW API.
TWS seems to take care of a continuous connection, because the GUI cannot know if someone is sitting in front of it watching the data or not.


Re: Gateway loses historical market data every day

 

1- no need, it happens on first call. Make a dummy APPL historical and 'voila'

2- see above.
Understand that a service provider dislike having open connection doing nothing, so after a certain inactivity time (seems ~10 min) IB server does sever the connection, socket are a precious resources. The penalty to reconnect is very small (a couple of 100ms and on historical it hadrly hurt anything)
IB somewhere give too much, they show this idle state to you (many other platform only state connection status when it fail)

3- there are a lot a talks about this on this forum, with various philosophy and principles and limitations (you need to allow upgrades, you may want to take control of it , etc ...)
Personally I like restart everyday, if IB proceed that way that's because there are a couple of good reasons. So I won't be of help advising pro/con.


Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

On Thu, Aug 17, 2023 at 01:29 AM, <omgwtfsalty@...> wrote:
"All MOC orders must be received at NYSE markets by 15:50 ET, unless entered to offset a published imbalance. NYSE markets' rules also prohibit the cancellation or reduction in size of any market-on-close (MOC) order after 15:45 ET."
I guess this quote must be from IBKR's website, where it's a bit outdated.

NYSE 2023 opening and closing auction rules:?
Nasdaq 2023 closing cross rules & faq:?https://nasdaqtrader.com/content/ETFs/closing_cross_faqs.pdf
?
--
Best,
DS


Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

MOC/LOC orders require that TIF is set to "DAY".

To put MOC order in the bracket, the following settings must be used for the bracket order peers:

moc.ocaType = 3;
trailingStopLoss.ocaType = 3;
This is to allow the MOC order to work in an OCA group of the bracket order children, countering the concern that Juergen raised. Although "3" for this setting is generally used to reduce the mutually exclusive orders by the size of either order's fill without overfill protection, with MOC/LOC order it just means "cancel the other order completely" because IBKR does not offer reduction of MOC/LOC orders while any other?ocaType?setting will resulting in the MOC/LOC order rejection. The result would look like this:



You do not need any time condition for the MOC order as it will activate automatically once the exchange transitions from regular trading to the closing auction (and IBKR won't allow order conditions on MOC orders, though if you need to enable the order at a certain time for any reason, you can always use Good-After-Time (order.goodAfterTime) property, which, in addition to working with more order types than time conditions, is also easier to use as it takes a single line of code to configure).

Referring to Juergen's concern again, since MOC orders indeed cannot be activated and cancelled after a few minutes before close (10 minutes for submission and 2 minutes for cancellation on NYSE, and 5 minutes for submission and 10 minutes for cancellation on Nasdaq), it might be a good idea to cancel or suspend the other orders at those cutoffs so as to avoid accidentally running into an overfill, although these guards would certainly complicate the code and kind of defeat the simplicity advantage of the MOC order in that it requires zero timing efforts. Today's liquid hours closing time can be learned from ContractDetails for the respective stock or from a Python package such as exchange_calendars. If you choose the MOC path, and seeing that you already use GTC TIFs, I believe temporary suspension of trading is enabled for GTC orders with?order.activeStopTime?property (vis a vis?order.activeStartTime, though I'll admit I never used these properties myself yet).

--
Best,
DS


Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

Hi ´³¨¹°ù²µ±ð²Ô sorry for the double post, I just re read what you said and looked it up.

"All MOC orders must be received at NYSE markets by 15:50 ET, unless entered to offset a published imbalance. NYSE markets' rules also prohibit the cancellation or reduction in size of any market-on-close (MOC) order after 15:45 ET."

Now I understand what you mean. I probably can't use it in this case then. Thanks for the advice.


Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

Hi ´³¨¹°ù²µ±ð²Ô, I tried on paper and it rejected the order with time conditions.

However I did this instead without the time condition and it seemed to go through yesterday a few mins before market close, will try it on paper today hopefully it works as intended.

Please let me know if I missed anything out.

  1. My opening order should be cancelled at 3:45.

  2. If the opening order fills, either the take profit, stop loss or MOC order should execute, any of them should cancel the rest.

Not sure if the tif for the MOC order should be "DAY" or "GTC" in this case.

    #1st part Opening
    opening = Order()
    opening.orderId = parentOrderId
    opening.action = "BUY" if quantity > 0 else "SELL"
    opening.orderType = "LMT"
    ....
    opening.conditionsCancelOrder = True 
    opening.conditionsIgnoreRth = True
    algo.FillAdaptiveParams(opening, adaptive_type)

    timeCondition = TimeCondition()
    timeCondition.time = timeCancel #15:40:00 cancel order
    timeCondition.isMore = True
    opening.conditions.append(timeCondition)

    #2nd part take profit or stoploss, if order filled above.
    takeProfit = Order()
    takeProfit.orderId = opening.orderId + 2
    takeProfit.action =  "SELL" if quantity > 0 else "BUY"
    takeProfit.orderType = "MIT"
    .....
    takeProfit.parentId = parentOrderId
    takeProfit.tif = "GTC"
    takeProfit.transmit = False

    stopLoss = Order()
    stopLoss.orderId = opening.orderId + 3
    stopLoss.action =  "SELL" if quantity > 0 else "BUY"
    stopLoss.orderType = "STP"
    .....
    stopLoss.parentId = parentOrderId
    stopLoss.tif = "GTC"
    stopLoss.transmit = False

    # Exit at closing time if take profit or stop loss not hit
    closing = Order()
    closing.orderId = opening.orderId + 4
    closing.action = "SELL" if quantity > 0 else "BUY"
    closing.orderType = "MOC"
    closing.totalQuantity = abs(quantity)
    closing.parentId = parentOrderId
    closing.tif = "DAY" 

    closing.transmit = True
    closing.conditionsCancelOrder = False
    closing.conditionsIgnoreRth = True
    closing.OutsideRth = True

    bracketOrder = [opening, takeProfit, stopLoss, closing]


Gateway loses historical market data every day

 

Hello,

my IB Gateway is configured for automatic restart every night. Each day the historical market data is yellow. See pic.. After close and relogin it is green.?

1. Can I manual reconnect the market data without closing the gateway?
2. Can I trigger reconnect market data per API?
3. Do you have other ideas how I can avoid manual restart?



Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

You probably can't, but why don't you give it a quick try in TWS or via the API to get a better feel for how things work?

When you place a bracket order, IBKR wraps the attached orders (profit, loss, others) into a group for you. Only one of those attached orders should ever execute in case the parent fills.

MOC orders (and MOO for that matter) are not compatible with groups since they cannot be cancelled after a certain point in time before market close (MOC) or market open (MOO)..

My expectation is that IBKR rejects bracket orders when:
  • the order has more than one attached order (profit, loss, others)
  • and one or more attached orders is of type MOC or MOO

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


On Wed, Aug 16, 2023 at 02:41 PM, <omgwtfsalty@...> wrote:

how would I put this MOC order within my bracket order, would this work?

?


Re: Is it possible to place order at 15:59 or 16:00 and get a fill?

 

Thanks for the suggestion, how would I put this MOC order within my bracket order, would this work?

Or do I not need to add a timecondition to an MOC order within this bracket?

# previous code replacing last part in bracket
# 3rd part: Sell Market order
moc = Order()
moc.orderId = opening.orderId + 3
moc.action = "SELL" if quantity > 0 else "BUY"
moc.orderType = "MOC"
moc.totalQuantity = abs(quantity)
moc.parentId = parentOrderId
moc.tif = "DAY" 

timeCondition = TimeCondition()
timeCondition.time = timeswitch
timeCondition.isMore = True
moc.conditions.append(timeCondition)

bracketOrder = [opening, trailingStopLoss, moc]
return bracketOrder


Re: Historical Market Data Service error

 

I guess the name of your subscription gives it away. Your market data permissions for CME futures is "for Display on Trading Platform" not for consumption in machine readable form via TWS API.

Market data is owned by the exchanges and licensed to companies such as IBKR so that they can offer subscriptions to their customers. Subscriptions for non-professional traders are generally less expensive than those for professionals, and subscriptions for "display to humans only" (raw, in the form of charts, for order entry, etc) is less expensive than subscriptions that provide access to raw real-time or historical data.

You'll need a market data subscription that permits access from a client application via TWS API.

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


On Tue, Aug 15, 2023 at 07:11 AM, <ohrmich207@...> wrote:
Error 162, reqId 3: Historical Market Data Service error message:No market data permissions for CME FUT, ...

when i run the code i get this error although i am subscribed to "CME Futures Level 1 for Display on Trading Platform. Examples - ES, NQ and HE"

not sure what is causing the issue, any help would be much appreciated!


Re: Pairs trading: Parent vs Child order placement

 

If you need different quantities for the same security in a cadence where the execution of one order should enable the other order (trading in opposite direction), you can use pair trades mode for the child order with the same security and respective ratio of quantities:

order.HedgeType = "P";
order.HedgeParam = quantityRatio.ToString("F3", CultureInfo.InvariantCulture);
--
Best,
DS


Historical Market Data Service error

 

def get_historical_data(contract):
ib = IB()
ib.connect('127.0.0.1', 4002, clientId=1)

bars = ib.reqHistoricalData(
contract,
endDateTime='',
durationStr='30 D',
barSizeSetting='1 min',
whatToShow='TRADES',
useRTH=False,
formatDate=1
)

nq_contract = Contract(
symbol="NQ",
secType="FUT",
exchange="CME",
currency="USD",
lastTradeDateOrContractMonth="20230915"
)

Error 162, reqId 3: Historical Market Data Service error message:No market data permissions for CME FUT, contract: Contract(secType='FUT', symbol='NQ', lastTradeDateOrContractMonth='20230915', exchange='CME', currency='USD')

when i run the code i get this error although i am subscribed to "
CME Futures Level 1 for Display on Trading Platform. Examples - ES, NQ and HE"

not sure what is causing the issue, any help would be much appreciated!


Re: Pairs trading: Parent vs Child order placement

 

Thanks again for the very informative message.

This highlights a couple of points of confusion with the TWS API.

  • The API doc describes the order attaching mechanism of bracket and hedge orders. It's implied they are just two order types that happen to make use of the parent-child relationship made available through attaching.
  • When an order is marked as a hedge order quantity must be 0. However, as you say a bracket order also should default to 0 since it matches the parent quantity, so specifying the quantity is irrelevant. Yet they do not show any errors or warnings about setting quantity like they do with hedging.

This implies there's a mechanism by which an order can just be "attached" without being a bracket or a hedge order, and without having its quantities automatically modified.

Maybe there's some way to tell the TWS API that an order is not a bracket order but rather an attached order that should simply execute when the parent executes.

Edit: Actually, I originally misread your screenshot, I mistook different prices on each order to be quantities but now see that the quantity on all child orders is the same, 10 units.

That's too bad anyway if that really is the case. Back to building a function that responds appropriately to incoming events.


Re: Retrieving live data for demo account via Python API?

 

contract vs eurusd_contract

Could that be a possibility or did you copy and paste incorrectly? Have you considered using a more strongly typed language?


Retrieving live data for demo account via Python API?

 

I am trying to get live tick data (or at least data every second) through?TWS API Python.

I use the?
self.reqTickByTickData(reqId, contract, "MidPoint", 0, True)
method to retrieve tick by tick data where contract is specified
as EURUSD as the following:

eurusd_contract = Contract()
eurusd_contract.symbol = 'EUR'
eurusd_contract.secType = 'CASH'
eurusd_contract.exchange = 'IDEALPRO'
eurusd_contract.currency = 'USD'

but data stream does not start.

Someone else used this exact code to get data stream for equities and I wonder if this works differently to Forex or perhaps due to a demo account?

Everything else in my code works as expected, I create the necessary class, can load historical data successfully
using the contract above etc.