¿ªÔÆÌåÓý

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

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.


Re: Level2 data falls behind actual market

 

You're not using wifi by any chance, are you?

Anyway, if you're ever wondering which end of your system can't keep up I suggest playing with dd progress=status for a few minutes.

Good luck!


No realized profit and loss for Forex ?

 

Hi,
if i execute m_pClient->reqPnLSingle(...) for the forex pairs i get 0.0 as pnl even if there were trades executed with a result different from 0. Why is that ? does this have something to do with virtual FX positions ? If i do exact the same for a stock i can get the correct pnl.


Re: Order entry time via the API?

 

Thanks, I will take a look at the flex reports, although I suspect this data is not available.

I'll report back here if I find anything.


Re: Level2 data falls behind actual market

 

Yes
No need to upgrade, especially this have nothing to do (or not enough) with GPU. It's only CPU based computation. Better invest in SSD than GPU
I guess your testing method (Still unclear to me how you proceed)? is not suited to what you want to check, and as @buddy suggest it, write a dumb CSV and check it later to get confidence.
'rogers'


Re: Order entry time via the API?

 

I don't know how. Generally manual and automated are not very easy to run simultaneously. Trend is to add Manual capabilities UI directly on your automate so that your automate DB can store info
If this is for auditing and not millisecond oriented, maybe take a look at info that would match your desire using


Re: Order entry time via the API?

 

Thanks for the reply.

My application is listening for the openOrder callback, but I was expecting to see an order creation/submit time field on the order itself. For example, my application may not be running when an order is submitted manually via TWS, it is then not possible to find out via the API later what time it was submitted.