¿ªÔÆÌåÓý

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

Re: Could this happen: stp and trail in oca group both getting triggered?

 

I think it is not guaranteed even with the block option of the ocatype flag. It can execute two orders of the OCA at the exact same time. Check? ?

"Please note that because the OCA procedure is an automated process, there is no guarantee that requested cancellations and modifications will reach the specific exchange before an order has been executed."

El lun, 21 ago 2023 a las 18:11, ´³¨¹°ù²µ±ð²Ô Reinold via (<TwsApiOnGroupsIo=[email protected]>) escribi¨®:

We just had a different post where OCA overfill protection was discussed, Raoul. When you create the OCA group, do you set the attribute for each order or do you let IBKR use a default?

Chances are, that your OCA was of type 3 (see table at the bottom of the chapter) which does not protect against overfill. So you might need to set ocaType 1 or type 2 for your orders explicitly.

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

On Mon, Aug 21, 2023 at 10:51 AM, Raoul Suurmeijer wrote:
Hello all,
?
Just minutes ago, something strange happened in my algo trading. My algo always submits orders as an oca order. Parent being a limit order, connected are 3 child orders: stp, trail and mkt with time control. The trail is set much wider than the stp but I want to limit the max loss with the fixed stp.
?
In the call back after placing the MNQ order, size 1, I can clearly see all the order id's being used, all child orders in the same oca group. Everything perfect as expected and I am positioned +1 MNQ. The positions goes green, and the trail is going along.
?
At some point the stp and trail are at the same level. Then price drops and hits that level. You would expect that either of the two (stp or trail) to get executed and not the others. But afterwards I am left with -1 MNQ position. In the logging I see callbacks for executed orders for both the stp and trail causing a double closing sell. Again, all the order id's match with the initial orders as part of the same parent.
?
Is this behavior to be expected when using both a stp and trail in the same oca group and are at the same price level? If so, my algo should probably change stp level as soon as trail gets to the same level. Or remove it from the oca group (if that's possible).
?
Thanks in advance for sharing your insights and own experience, greetings,
?
Raoul
?
?
?


Re: Could this happen: stp and trail in oca group both getting triggered?

 

We just had a different post where OCA overfill protection was discussed, Raoul. When you create the OCA group, do you set the attribute for each order or do you let IBKR use a default?

Chances are, that your OCA was of type 3 (see table at the bottom of the chapter) which does not protect against overfill. So you might need to set ocaType 1 or type 2 for your orders explicitly.

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


On Mon, Aug 21, 2023 at 10:51 AM, Raoul Suurmeijer wrote:
Hello all,
?
Just minutes ago, something strange happened in my algo trading. My algo always submits orders as an oca order. Parent being a limit order, connected are 3 child orders: stp, trail and mkt with time control. The trail is set much wider than the stp but I want to limit the max loss with the fixed stp.
?
In the call back after placing the MNQ order, size 1, I can clearly see all the order id's being used, all child orders in the same oca group. Everything perfect as expected and I am positioned +1 MNQ. The positions goes green, and the trail is going along.
?
At some point the stp and trail are at the same level. Then price drops and hits that level. You would expect that either of the two (stp or trail) to get executed and not the others. But afterwards I am left with -1 MNQ position. In the logging I see callbacks for executed orders for both the stp and trail causing a double closing sell. Again, all the order id's match with the initial orders as part of the same parent.
?
Is this behavior to be expected when using both a stp and trail in the same oca group and are at the same price level? If so, my algo should probably change stp level as soon as trail gets to the same level. Or remove it from the oca group (if that's possible).
?
Thanks in advance for sharing your insights and own experience, greetings,
?
Raoul
?
?
?


Could this happen: stp and trail in oca group both getting triggered?

 

Hello all,

Just minutes ago, something strange happened in my algo trading. My algo always submits orders as an oca order. Parent being a limit order, connected are 3 child orders: stp, trail and mkt with time control. The trail is set much wider than the stp but I want to limit the max loss with the fixed stp.

In the call back after placing the MNQ order, size 1, I can clearly see all the order id's being used, all child orders in the same oca group. Everything perfect as expected and I am positioned +1 MNQ. The positions goes green, and the trail is going along.

At some point the stp and trail are at the same level. Then price drops and hits that level. You would expect that either of the two (stp or trail) to get executed and not the others. But afterwards I am left with -1 MNQ position. In the logging I see callbacks for executed orders for both the stp and trail causing a double closing sell. Again, all the order id's match with the initial orders as part of the same parent.

Is this behavior to be expected when using both a stp and trail in the same oca group and are at the same price level? If so, my algo should probably change stp level as soon as trail gets to the same level. Or remove it from the oca group (if that's possible).

Thanks in advance for sharing your insights and own experience, greetings,

Raoul




Re: Can I place orders asyncronously like this without causing issues with the twsapi?

 

I am not familiar enough with Python but a couple of things maybe be generic

1- you have a side 'thread' that deliver you a condition that look like a HTTP return code, right ?
aiohttp.ClientSession() which will use same credentials, hence same session as your own main thread.
2- your own thread will place a bracket order (and look like you understood the purpose of 'transmit')
Your concern is that your order id will be messed up, but with what ?

Your code should better take some precaution as you seems to me to mix 2 different technologies, the SDK and the REST.
You better call self.reqIds(-1) at least once yourself and track the call back of nextValidId before you use app.nextOrderId().?
Read the sequence (init+operation+report) in
and consider the 2 call back openOrder and orderStatus. you SHOULD listen to it.
Start keeping thing simple, there is no harm to call self.reqIds(-1) and it is fast


Can I place orders asyncronously like this without causing issues with the twsapi?

 

I want to check if this is possible.

I am using the following code which queries a price using certain conditions from an api, the prices are returned to me asynchronously.

I want to place the order as soon as the query has been returned from the api. I put the app.placeorder into the async loop as shown below.

def BracketOrder(self, .....):


    # 1st part: Buy Adaptive Algo order
    opening = Order()
    opening.orderId = parentOrderId
    ./....
    opening.conditions.append(timeCondition)


    # 2nd part take profit or stop loss
    takeProfit = Order()
    takeProfit.orderId = opening.orderId + 2
    ....
    takeProfit.transmit = False

    stopLoss = Order()
    stopLoss.orderId = opening.orderId + 3
    ....
    stopLoss.transmit = False

    # # 3rd part: Sell Adaptive Algo order
    closing = Order()
    closing.orderId = opening.orderId + 4
    ...
    bracketOrder = [opening, takeProfit, stopLoss, closing]
    return bracketOrder


async def get_data(session, ticker):
    while True:
        try:
            async with session.get(url=f'api.exec.com') as response:
                if response.status == 200:
                    data = await response.json()
                    try:
                        df = pd.DataFrame(data)
                    except ValueError:
                        print("Data is not a valid float. Retrying...")
                else:
                    print("Received non-200 status code. Retrying...")
            await asyncio.sleep(1)  
        except Exception as e:
            print("Unable to get url {} due to {}. Retrying...".format(ticker, e.__class__))
            await asyncio.sleep(1)


    bracket = app.BracketOrder(parentOrderId=app.nextOrderId(), .........)

    for o in bracket:
        app.placeOrder(o.orderId, app.EQ_order_MKT(ticker), o)
        app.nextOrderId() 


async def main_loop(ticker_list):
    async with aiohttp.ClientSession() as session:
        ret = await asyncio.gather(*[get_data(session, tick) for tick in ticker_list])
        #return {ticker_list[i]: ret[i] for i in range(len(ticker_list))}
        return ret

def run_loop():
    app.run()

app = ib_class("127.0.0.1", 4002, 999)

#app.nextorderId = None
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

asyncio.run(main_loop(new_portfolio.keys()))

I am worried that I will place to orders at the exact same time and the order id's will get messed up. Is there a way around this or does twsapi automatically mitigate this?


(Paid) Need a bot written in c# (with code) to place option trades based on criteria

 

I would like to have a bot that can place complex option strategies along with profit taker and stop loss orders. I would want to have it place?trades based on specific times, volatility, margin and maybe technicals.

If a fully developed solution is not available, I am fine if the application is able to get account particulars, place trades and provides extensibility to build strategies on my own.?

You can reach out to me at lvsniper@...

Thanks


Re: C++ TWS API w/ Cmake

 

I would like to use pytorch because I trained my models in python using pytorch, and are all serialized to .pt files. Just would be easiest if I could get pytorch working in C++ so I can load the the .pt files using their load functionality rather than using ONNX or something. And yes, I like the extra ability to configure everything. I really have next to no experience using Cmake for reference.?


Re: C++ TWS API w/ Cmake

 

Oh wow, so this CMake builds a project with both pytorch and the TWS API in C++? Thank you so much

Yes. You're very welcome.

Just bear in mind that the process will be a little different for you since you're on Windows. For example, in order for me to get my stack ready I only needed to download the TWS API and the rest was set up using aptitude install cmake libtorch-dev libintelrdfpmath-dev.

I also don't know if WIndows supports genuine symbolic links so you may have to copy more stuff around than otherwise necessary.

In addition, you'll need to fiddle w/ the target_link_libraries for your specific needs. The bullet-points I mentioned should be most, if not everything, you need to "wrap your mind around" though.

-Good luck


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

 

Juergen, your concern is certainly valid, I just meant to show how some limitations can be worked around. Admittedly, while LOC/MOC orders have their strong advantages and constitute perhaps some 30-40% of my own volumes, and about twice that amount of my orders use oca groups and/or bracket/pair-trading modes allowing to almost completely avoid real-time trading, when recommending them I kind of tend to overlook the fact that my process involves routinely pre-programming orders to avoid overfills with tightly timed cancellations and activations. In the end of the day (pun not pre-programmed), the choice of placing orders into the auctions or simulating them with timed activations during regular trading is a problem of concrete implementation's design, and both approaches have their merits, some of which go beyond mere technical discussion.
--
Best,
DS


Re: C++ TWS API w/ Cmake

 

Oh wow, so this CMake builds a project with both pytorch and the TWS API in C++? Thank you so much, if so. I will probably take the approach of making a whole new project with cmake then. I need to get better with using it anyways...


Re: WAP value provided by 'realtimeBar' vs. that shown in the TWS quote panel

 

Thanks Gordon for your detailed response.. I went off to test a few more things. Thought I post it here as it might be useful for others:

1) Getting VWAP from subscription of "reqRealTimeBars" (updates every 5 secs) depends on when you subscribe. Not sure how exactly it's calculated but the first VWAP value is always the closing price of the first 5 sec bar after the subscription was made and ofcourse the subsequent vwap calcs are made of the first value, i.e. this value can be very different than what you see on TWS charts.
2) The VWAP value on TWS chart remains consistent regardless of the candle size you choose (1 minute, 5min, 15min, hourly, daily)
3) VWAP value coming from the subscription of "reqHistoricalData" with "KeepUpToDate=true" is closer to the what's shown in TWS charts.
4) As you mentioned, volume and HLC price between different vendors maybe slightly different, but with TWS API all data comes from TWS, that's why the fixed difference between VWAP values of TWS and it's API?(when using "reqHistoricalData" with "KeepUpToDate=true"), even of a few cents, does not make sense to me. I imagine IB software engg. may have an answer to this but I will leave it be.

For now, since I have been using the 5 sec bars and wanting to avoid any new tick based subscriptions, I calculate vwap locally. As you pointed out, because of the aggregated data provided, the calc is not precise but serves my purpose.


Re: C++ TWS API w/ Cmake

 

BTW, besides the symbolic links, here are the changes I needed to make to their CMakeLists.txt:

--- orig        2023-08-18 12:41:21.180000000 -0400                                                            
+++ CMakeLists.txt      2023-08-18 12:39:07.488000000 -0400                                                    
@@ -1,11 +1,18 @@                                                                                              
 cmake_minimum_required(VERSION 3.18 FATAL_ERROR)                                                              
+include(CMakePrintHelpers)                                                                                    
 project(example-app)                                                                                          
                                                                                                               
 find_package(Torch REQUIRED)                                                                                  
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")                                                  
                                                                                                               
-add_executable(example-app example-app.cpp)                                                                   
-target_link_libraries(example-app "${TORCH_LIBRARIES}")                                                       
+include_directories("~/tmp/IBJts/source/cppclient/client/")                                                   
+include_directories("~/tmp/IBJts/source/cppclient/")                                                          
+file(GLOB SOURCES *.cpp ./client/*.cpp)                                                                       
+                                                                                                              
+cmake_print_variables(SOURCES)                                                                                
+                                                                                                              
+add_executable(example-app ${SOURCES})                                                                        
+target_link_libraries(example-app "${TORCH_LIBRARIES}" bidgcc000)                                             
 set_property(TARGET example-app PROPERTY CXX_STANDARD 17)                                                     
                                                                                                               
 # The following code block is suggested to be used on Windows.

Hope this helps a little.


Re: C++ TWS API w/ Cmake

 

On Fri, Aug 18, 2023 at 02:57 PM, blydon12@... wrote:

I don't want to use Cmake if I don't have to

I think you're probably making a mountain out of a molehill. As Gordon suggests CMake offers a ton of options and LibTorch needs that power since it can be configured to use disparate and exotic hardware. Otherwise you're better off (IMHO) just using Python and opencv + scikit or something.

But, if you decided to stick with LibTorch there must be a reason. And that reason, presumably, is you want the extra ability to configure everything. It shouldn't be that hard to merge your stuff in though. Especially if you're willing to take some [temporary?] shortcuts.

Gordon's suggestion to experiment on a small project is definitely spot on. I don't actually see why you can't experiment with your own TWS application though... just use the minimal CMakeLists.txt provided by LibTorch and leverage the following:

  • symbolic links
  • . EG: include_directories("~/tmp/IBJts/source/cppclient/client/")
  • . EG: file(GLOB SOURCES *.cpp ./client/*.cpp)
  • (to double check things are as you believe)
  • . EG: target_link_libraries(example-app "${TORCH_LIBRARIES}" bidgcc000)

Then do your regular: cmake . and cmake --build .

I had the TWS sample going in short order and so I'm sure you'll be up and running in no time flat.


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

 

There are three ocaType options and only one of them allows MOC/MOO orders in the group. So I guess my concern is 67% valid :-)

We don't use OCA groups a lot but when we do, (more) guaranteed overfill protection is key so that we tend to use type 1 ("with block"). As you said, the two "with block" types reject MOC/MOO orders in the group but I guess the overfill risk for type 3 OCAs can be mitigated if triggers for the orders in the OCA are "far enough" apart. In other words, even in crazy market situation no two orders in the group would be eligible at the same time.

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



On Thu, Aug 17, 2023 at 08:16 AM, ds-avatar wrote:

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.


Re: Gateway loses historical market data every day

 

You might want to check this post and the topic for more information about the various market data connection messages. I don't want to repeat that post but here some thoughts:
  • There is a difference between "connection inactive" (when the farm is no longer needed) and "connection lost" (when there is a network failure or IBKR resets the farm)
  • If there is any active subscription to real-time data, market data farms never become "inactive". They may be "lost" but in our experience are re-established within milliseconds without any impact on your client (unless you have a serious and ongoing network issue between you and IBKR).
  • HDMS farms provide more general information services and are not limited to just historical market data download. If you dig into the TWS/IBGW logs you will see that internally, HDMS queries are made even for TWS API calls such as and and many (if not all) calls that have a object parameter. Apparently, HDMS farms carry and provide certain instrument related meta information for the instruments they are responsible for.
  • There is absolute no need to "heartbeat" any of these servers and it actually may backfire on you since you may eat into you "pacing" budget. As I said before, market farms become inactive when they are no longer needed and get connected instantaneously and transparently when any of your API requests require them. If a connection that is needed is lost, it is also reconnected automatically without service interruption.
You say you find over 300 error code 162 messages in your log. That suggests to me that your client is causing pacing violations and I suggest you revisit the section and other documentation on the limits IBKR places on the rate of historical market data requests. Error code 162 is often related to pacing violations and does not always use the words "pacing violation" in the error message. When you request historical data too quickly, IBKR sometimes:
  • slows down the response time fore requests
  • fails requests with error 162 and message "no such data" or other errors
  • fails requests with error 162 and some pacing violation message
  • disconnects your client all together when too many pacing violations occur
You can dig into the TWS/IBGW logs to learn more about what is going on but that may be cumbersome and time consuming. You can also enable additional (and very verbose) HDMS activity logging where you can see exactly which of your client API calls cause HDMS server XML data exchange. You can also see farm management in action (such as "inactive/active" and "lost/reconnect").

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



On Fri, Aug 18, 2023 at 02:26 AM, Matthias Kurz wrote:
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

 

Yes Out of scope of this forum
Yes Cmake sound crazy as another language to generate a language to generate a language to interpret a language.
But don't fight Cmake, it does exist because it's a pain to support multiple platform makefile and is now part of the landscape and for long.

Try to get a working makefile (or solution) for VS from somebody that have a working make PyTorch, it surely exist.? That's enough to deal with and give an example of a target file (to target with Cmake during weekend)
'roger'


Re: C++ TWS API w/ Cmake

 

Ya I don't want to use Cmake if I don't have to. Especially, since I already have a fairly large project I built without it, which correctly implements the TWS C++ API. So, my problem is probably more suited towards a PyTorch forum to see how to link without Cmake.

Thanks anyways,
Brendan


Re: C++ TWS API w/ Cmake

 

About Cmake: unfortunately allows to specify tons of option at generation time.
You should look at linker error message and guess what could make Cmake happy.
Cmake is only a "makefile" generator (or Solution generator) , in VS it make it compatible with nmake (MS implementation of make)
Once done your are free to modify the makefile (or the project file) but be careful that all your modification will be overwritten the next time Cmake run (which can be automatic as part of consistency check done during make 'thanks' to Cmake invocation during make)
Be carefull also about any Release version opf lib supplied by thrid party and invocation of nmake in Debug mode
Look to me you need first to master Cmake.? Take a small project like a ziplib to experiment it. Sorry to not be more helpful, wasn't a specific issue I ran trough


Re: Gateway loses historical market data every day

 

I can only speculate.
What was the file path of the log you show extract here ?

Diag:
There are various log avail (including the GW display windows but require a lot of time to be analyzed (Best is to massage it as a CSV then open in Excel and use Excel to sort by kind or date or keyword or ...)
Generally people log everything themselves (you should), search on this forum about Log there are various thread on the subject.

Issue:
your report look to me like it should be interpreted as what messages say.
Looks like you look for a non existing instrument or no data exist for this instrument (Which one ?) Then you may have a logic that send a cancel (which is good in most cases )
but the request didn't behave as expected (was non longer answering something you expect) then you repeat the cancel.
Is it exactly 300 times ? if so then look for "300" in your code.

To be noticed: The difference between the error 366 (id 41690) and the error 162 cancel id (416904)? which is the same as the id: entry of your log.
Remember that many request have a "requestXXend" like historicalDataEnd, once done, your served and no need to trigger anything else aside of code housekeeping.
Cancel in is supposed to be used on a pending subscription. Over-Cancel is harmless as long as you know how to filter back messages.

?


Re: C++ TWS API w/ Cmake

 

I've tried that section briefly to no avail, perhaps I will have to try again. I could try just mirroring the template project that link populates and copy the includes and linkers to my current TWS Project. It's really a linking issue I am having with PyTorch. Knowing exactly which libraries to build, etc. If I knew that, which, to my knowledge, they do not seem to outline explicitly, I believe I could figure out how to get the build into my project. Building a whole new project for the TWS C++ API in Cmake seems like overkill. It may be necessary though because PyTorch has detailed information on building this way, and they plan to only support this going forward.