¿ªÔÆÌåÓý

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

Re: goodAfterTime attribute not supported for combo orders

 

Works!
Thanks a lot ´³¨¹°ù²µ±ð²Ô!


Re: Get movers in the last 1 (say) minute

 

Hi Gordon,
thanks for your reply.

A) Yes, actually the examples you gave are normalized according to IB.

Hot Contracts by Price

Contracts where:

(lastTradePrice-prevClose)/avgDailyChange is highest in absolute value (positive or negative).

The avgDailyChange is defined as an exponential moving average of the contract's (dailyClose-dailyOpen)

Exponential Moving Average calculation is: (price - close) / close / historicalVolatility30

Hot Contracts by Volume

Contracts where:

today'sVolume/avgDailyVolume is highest.

avgDailyVolume is a 30-day exponential moving average of the contract's daily volume.


So what I want is essentially is hot contracts by price but over say 1, or (5, 10 min) etc. scaled by their expected volatility over respective time period.

B) Thanks, that how i will proceed. Will start with 'Hot contracts by price' and then go from there, at least it is normalized by volatility already.


Re: goodAfterTime attribute not supported for combo orders

 

You could try a instead.

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


On Fri, Oct 13, 2023 at 06:13 PM, Ace wrote:
HI,

I want to attach a?"Good After" market order to an option combo order to close the position at a predeterrmined time using this attibute:
time_order.goodAfterTime = expiration_day +?' '?+?'15:40:00 US/Eastern'

But I get this error message:
349 Generic combo does not support "Good After" attribute.

Is it just not possible to do it for a combo or there is another way?

Thank you!


goodAfterTime attribute not supported for combo orders

 

HI,

I want to attach a?"Good After" market order to an option combo order to close the position at a predeterrmined time using this attibute:
time_order.goodAfterTime = expiration_day +?' '?+?'15:40:00 US/Eastern'

But I get this error message:
349 Generic combo does not support "Good After" attribute.

Is it just not possible to do it for a combo or there is another way?

Thank you!


Re: Get movers in the last 1 (say) minute

 

A>> Can you give a definition of "biggest movers" ?
Because if you look at TradeRate or VolumeRate, they can be very high on a big MarketCap while simultaneously the PERC_GAIN stay rather small, however this instrument maybe the top mover
a contrario: a small MktCap with a 10% TOP Gain is most probably a very dangerous bet, could even be an exception like during a short squeeze.

B>> The IB Scanner is a wonderful tools and nearly fully accessible trough the API but it is also the most complex (IMHO) request to setup.
I am not surprised that IB doesn't give more info about it because it's fluctuating, and it is tedious to explain the way it can be parametrized.

Your question is a Pandora box it require preliminary that:
1- You are really familiar with it on TWS and very aware of what "filters" allow you to do.
2- You are familiar with XML format to parse and use it.
IBK API send on request the whole available rules as a XML,? see attached XML fetch with a as of today.

For now you can peep in it and guessimate what can be used to patch existing coarse example IB supplied for scanner subscription.
Do search with the UI exact TWS text name and you will find a <displayName> rubric? within a <ScanType> section this give you the <ScanCode> to use.

But best is to parsed it to get the <ScanCode> that you wish to use and then re-index trough <instruments> to see what <RangeFilter>? to use and adjust, in order to match the combination that you can setup in TWS which fit your bill.


Get movers in the last 1 (say) minute

 

Dear all,

I was looking for custom scanner to see biggest movers in the last 1 minute say using IB API.?
Couldn't find any documentation on the scan params, is the best way to proceed to look for 'PERC_GAIN' scans?

Thanks


Re: questions about the two thread design (TWS c++ api)

 

It proves that callbacks CANNOT be process anywhere else.
It proves that your callbacks will be processed in same thread and sequentially. (if you have a single eReader which is the typical and the default implementation)

Not sure I follow your concern or understand the case you mention, "isn't done in the other thread*. " ? Which other thread ? Launched by what ?
Message are thrown away once processed and 'callbacked'. Even if a dormant thread exist somewhere (how was it launched ?) it won't catch anything.

Note:
Don't try to create multiple eReader, it's touchy and not the smartest way to parallelize processing.
Massive but blind multi-threading is not the solution. You are serialized by the recv from the connection.
If speed is of the essence you will rapidly see that you need to sort urgent processing from lazy one, and this factored by long processing vs short.
So prioritizing will most likely lead to a message queue dispatching implementation to a thread pool of your own.? Make sense but this is NOT something to start with when "onboarding" the API and anyway it better be piggybacked on existing IB code
Trust a simple implementation, the IB Client codes sample are safe and well designed to make your life simple.

Note:
VisualStudio Code (work Mac/Linux) is pretty efficient to track calls of methods/procs, goes way beyond plain old 'grep', with a pretty developer display of results (clickable) Not tried on Linux.


Re: questions about the two thread design (TWS c++ api)

 

Yes, I was able to find those callback invocations in all?EDecoder.cpp as well. However, this just proves that calling *is* done in the main thread, not that it *isn't done in the other thread*. Apologies for being pedantic.

The reasons I ask are? a.) some threads on this forum mention calls can be done from either thread, and b.) it's difficult to grep every single callback of my EWrapper implementation in the source code.

I only grepped the ones I'm interested in and indeed they're all *only* being called in EDecoder.cpp, so you're probably right.


How does IB calculate option prices when ex-dividend date is during the lifetime of the option?

 

I have a specific query about how the TWS API adjusts option price calculations in the presence of an ex-dividend date. Below is the full context and what I've tried so far.

IB offers as part of the API IBApi.EClient.calculateOptionPrice. These return the option price as well as the greeks. I've been able to replicate the calculations they do using an off the shelf option black scholes python module ( for those interested). It returns the same greek values to within 4 decimal places of accuracy, e.g. say the delta is 0.2542 from the TWS API, pyvollib returns 0.2541. However, this changes when there's an ex-dividend date during the lifetime of the option.

Black scholes doesn't take early exercise or the impact of dividends causing a price drop into account, so this requires so modifications to the calculations. There's a couple of ways to account for the dividend price drop but none of them seem to match up with the TWS API calculations. Approaches that I've tried:

  • reducing the underlying price by the present value of the dividend
  • include a term for the annualized dividend rate in the bs model

There was a third approach that I tried but can't find the details of it in my notes.

When I messaged the TWS API team, they got back saying it was the TWS team proper that was responsible for the calculations. And then after following up with the other team I still didn't get any response. I'm hoping someone here has insight into IB's methods.

So, to the experts in this group, do you have any insights into how IB modifies their option price calculations when an ex-dividend date is within the lifetime of the option?


Re: TWS api multiple similar orders submission delays

 

On Thu, Oct 5, 2023 at 01:47 PM, @tomaszk wrote:
I do not wait for anything, the only thing I do is my order send function is synchronized:
Does that mean you do not send your second Limit order till you get a status update for the first order? If so, this will delay your entire process of placing multiple Limit orders. If I understand your prints correctly, you are measuring delay between each print and not when the real execution happens on the server? I am not sure if there's a good way of measuring delay between when you place an order and when the exchange server gets it. The "execDetails" callback's 'execution.time' tells a little about the server side (liely the IB server and not the exchange server) execution time but unfortunately only has seconds granularity. Also, this time cannot be really compared with your computer time, as they are not in sync.
It's somewhat difficult to know where exactly the delay might be occurring. The OS we typically use (Windows, MAC, Linux) are not real-time systems and may have it's own delays. I make sure to not run any other programs when running TWS and limit my background tasks, but delays could be introduced at several other places along the way:?TWS UI/gateway delays, internet delays reaching IB server (if you are east coast vs west coast vs out of country will also affect this), IB server delays, IB server sending order to exchange server delay and round trip delay back to your computer for printing status.


Re: questions about the two thread design (TWS c++ api)

 

To add to Gordon's answer:
One thread does I/O, parsing of network messages and formatting of network messages.? The other thread gets callbacks, does your application logic and makes requests to the API.

Hunter


On Tuesday, October 10, 2023 at 12:11:09 PM PDT, Gordon Eldest via groups.io <hymagik@...> wrote:


No
Assuming you use a basic implementation, as in the supplied example. You do not have more default processing thread than your number of eReader, typically a single one.
So then all callbacks are processed in same thread as the one that process messages. IB hide for you the multithread queuing of messages in your processing thread, you don't have to worry about it.

Hence they are processed sequentially, This could be in a very different order than request messages, typically "orders" messages are processed faster than "data request" messages)

Typically the reader loop is made of
?? ???? while( client.isConnected()) {
?? ???? ??? client.processMessages();
?? ???? }

and zooming in client.processMessages(); you typically find
??? m_osSignal.waitForSignal();
?? ?errno = 0;
?? ?m_pReader->processMsgs();

How it work:
In same thread (and in the supplied example in main() thread )

on an Inbound Message=>Free m_osSignal.waitForSignal(),
then typical next step is to run a m_pReader->processMsgs(); => which loop on all existing messages (Normally one but could be more that arrived since you get there, this depend upon how slow you process a previous salvo of messages) ,
Messages? are process in the order they arrived using a EDecoder processMsgsDecoder_;

Look code in
void EReader::processMsgs(void)

and? in
int EDecoder::parseAndProcessMsg(const char*& beginPtr, const char* endPtr)

No mystery.

If you want to see how message queue is thread safe, look at EReader::getMsg(void)? you will see a scope lock method using m_csMsgQueue as lock


Re: questions about the two thread design (TWS c++ api)

 

No
Assuming you use a basic implementation, as in the supplied example. You do not have more default processing thread than your number of eReader, typically a single one.
So then all callbacks are processed in same thread as the one that process messages. IB hide for you the multithread queuing of messages in your processing thread, you don't have to worry about it.

Hence they are processed sequentially, This could be in a very different order than request messages, typically "orders" messages are processed faster than "data request" messages)

Typically the reader loop is made of
?? ???? while( client.isConnected()) {
?? ???? ??? client.processMessages();
?? ???? }

and zooming in client.processMessages(); you typically find
??? m_osSignal.waitForSignal();
?? ?errno = 0;
?? ?m_pReader->processMsgs();

How it work:
In same thread (and in the supplied example in main() thread )

on an Inbound Message=>Free m_osSignal.waitForSignal(),
then typical next step is to run a m_pReader->processMsgs(); => which loop on all existing messages (Normally one but could be more that arrived since you get there, this depend upon how slow you process a previous salvo of messages) ,
Messages? are process in the order they arrived using a EDecoder processMsgsDecoder_;

Look code in
void EReader::processMsgs(void)

and? in
int EDecoder::parseAndProcessMsg(const char*& beginPtr, const char* endPtr)

No mystery.

If you want to see how message queue is thread safe, look at EReader::getMsg(void)? you will see a scope lock method using m_csMsgQueue as lock


Re: questions about the two thread design (TWS c++ api)

 

Thank you for following up because I actually did *not* understand.

¡±It is this [the main] thread that dispatches to your EWrapperImpl callbacks.¡± This makes it sound like all reading and writing to state variables I care about is done in one thread, the main thread.?


¡°
So in C++ the message reading and the callbacks are on separate threads.¡± This makes it sound like two threads are calling functions that read/write important state variables, which means I have to be more careful.?

so to confirm: callbacks like execDetails and orderStatus and tickByTickLast can get called by a separate thread, possibly leading to race conditions, which necessitates synchronization of state variables such as current position, etc.


Change ACCOUNT in user interface

 

Hello community - i hope you are doing well.
Is there any API command that letting me change account in the TWS interface?
something like : def changeToAccount('DU001') ?


Re: questions about the two thread design (TWS c++ api)

 

You don't need to worry about contention between the message reader and sender but you do have to care about possible race conditions between your message dispatch loop and your main thread.

i.e. The callbacks return data in one thread which you will likely use in your main thread.

However that type of contention is related to your own app design rather than the API.

On Sun, 8 Oct 2023, 22:14 , <tbrown122387@...> wrote:
Oh, so I don't need to worry about race conditions at all do I? I'm just going through TestCppClient::processMessages() over and over again? Do something based on m_state, then receive information from callbacks. Got it.



Re: strange execution price for a stop loss

 

The data that was attached to your post seems to be for 08:32:57 US/Central, one minute later than when the STOP order happened at 08:31:57 US/Central. Prices during the STOP order second were much jumpier.

Just looking at changes of Bid and Ask price does not give you a good picture of what the order book looks like and whether there is "great activity". Level II can help since it is a snapshot of the sizes for all prices in the order book at and above Ask as well as at and below Bid. At the end of the post is a snapshot for NQZ3 just after trading started at 17:00 US/Central on Sunday. If you have subscribed to Level II data for CME, you can use the TWS "Level II" window (the top window below) and the "Book Trader" (bottom window) to look at 10 levels for the buy and sell side each and your client can request the data as well:

  • NQZ3 had only ~40 contracts available for trades within $2.50 of Bid and Ask respectively ($5 range). With such a thin order book, even relatively small institutional orders can push prices around considerably. And the order book cannot have been much fuller in the 08:31:57 second since Bid and Ask sizes were smaller than this.

  • Just for comparison, ESZ3 at the same time had ~400 contracts each

  • and the ZN futures front month had ~3,200

Paper trading is a simulation, and as such, "always wrong but mostly useful". So you cannot expect that executions exactly match those that would have taken place in a live account at the same time. But as @fordfrog indicated, executions in the paper account closely follow those in the real exchange and in thin order books (such as NQZ3 at start of trading today and on the morning of October 5) even in live trading prices can briefly take place $10 or more away from Bid and Ask.

I can't think of anything more you can analyze for this historical case. Going forward, you could subscribe to Level II data, record the 20 levels of order book details IBKR provides, and use them for analysis for case where you feel simulated trade prices were "too far away" from Bid/Ask. But keep in mind that not all orders make it into the order book and that some (even large) trades are not reporable or reported. So there will always be a some uncertainty.

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




On Sun, Oct 8, 2023 at 04:57 AM, FrenchFlorent wrote:
Thank you for your answers.
I totally agree that the stop order is a MKT type order and offer no garanty on the price effectively executed.
But I've checked order book at this time (8:31:57 ECT), via reqhistoricalTicks and?whatToShow=BID_ASK, and it seems to have great activity (not surprising, at the market opening). So I am quite sceptical that there were such a huge gap on the priceBid between?14908.50 and?14893.75.

I'd like to go further in the analyze, what is?
Level II data and how I can I get it ?

Thank's a lot?


Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder

 

None of that invalidates anything I said about the code you posted.

Hunter


On Sunday, October 8, 2023 at 06:42:57 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


threading.Event is a method to communicate between threads. I find it useful when waiting for a server to respond versus time.sleep() - when we get a server response the flag on the response is .set() (true) and the other threads waiting for it are released.?





On Sun, Oct 8, 2023 at 6:50?PM Hunter C Payne via <hunterpayne2001=[email protected]> wrote:
In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder

 

threading.Event is a method to communicate between threads. I find it useful when waiting for a server to respond versus time.sleep() - when we get a server response the flag on the response is .set() (true) and the other threads waiting for it are released.?





On Sun, Oct 8, 2023 at 6:50?PM Hunter C Payne via <hunterpayne2001=[email protected]> wrote:
In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder

 

In the interest of helping, Andrew's suggestion is for an interrupt style.? It also has a synchronization bug in it (the blocks of code that reads/writes the flag needs to be in a synch block, them being thread-safe isn't enough because the logic depends on multiple accesses).? And that's my point about doing this in an interrupt style way, its really (really really really) hard to get right.? As in most professional programmers don't attempt this stuff and instead use frameworks that don't require threading or give this type of work to a specialist (read old and expensive) programmer in most situations.? Just do a while loop as I suggested and if you don't want the blocking behavior, launch a thread to send the request and wait on the response (but you probably don't need to do that).? IBApi is really designed to be used in a single-threaded design.

Hunter

On Sunday, October 8, 2023 at 03:33:36 PM PDT, Andrew Bannerman <bannerman1985@...> wrote:


Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?


Re: Avoiding time.sleep while waiting for openOrder / orderStatus callback after placeOrder

 

Python if it's python your using - threading.event() module. Can assign different threads to events?

as eg:?

import threading

# event threads
event = threading.Event()
hist_event = threading.Event()
open_order_event = threading.Event()

in the call back functions:?

you may hist_event.set()

? ? def historicalDataEnd(self, reqId, start, end):
? ? ? ? super().historicalDataEnd(reqId, start, end)
? ? ? ? print("HistoricalDataEnd. ReqId:", reqId, "from", start, "to", end)
? ? ? ? hist_event.set()

hist_event.clear()
histData(9, FUTcontract(symbol), '3 D', '1 min')
hist_event.wait()

so you could do something in the ordercall back function i believe.....

This is what I do at least seems to work pretty well.?
I run main() in its own thread which is all my strategy logic which is a while loop?
and the TradeApp() in its own thread?

like this:

def websocket_con(symbol):
? ? app.run()

app = TradeApp()
app.connect(host='127.0.0.1', port=7497,
? ? ? ? ? ? clientId=3)? # port 4002 for ib gateway paper trading/7497 for TWS paper trading
con_thread = threading.Thread(target=websocket_con, args=(symbol,))? # , daemon=True
con_thread.start()
time.sleep(5)? # some lag added to ensure that streaming has started

MainThread = threading.Thread(target=main, args=(app,))
MainThread.start()

On Sun, Oct 8, 2023, 9:02 AM Lipp F. <flipp31a@...> wrote:
All code?examples I can see are like this:

placeOrder(orderId, contract, order)
time.sleep(10)
Is there a better way of doing this? Maybe using?threading.Lock() with release() in callback functions? Looking for an example. TIA.
?