开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: what time EST is it safe to assume that IB have printed the final close for all symbols EOD

 

开云体育

You may want to check with the various exchanges as IB is just reporting.? The exchanges have pools that don't print during the day and only hit the tape after the close.? I'm not sure what time frame each of the exchanges have for final posting if trades.
Mel?





-------- Original message --------
From: "comicpilsen via groups.io" <comicpilsen@...>
Date: 2024-12-05 7:25 a.m. (GMT-08:00)
Subject: [TWS API] what time EST is it safe to assume that IB have printed the final close for all symbols EOD

Hi all In the groups experience what is the earliest time , eastern standard time, when it's reasonable to say that IB have printed ( immutable) the close for all symbols that will be the number of record in perpetuity. So market close at 16:00 but there's going to be a period where the numbers are still being populated. I'm certain that someone has figured out what that "indeterminate" period is to a margin of safety. I asked IB sometime back but the support desk has never responded, I asked 3 time. thank you for any help OR thoughts. cp


Re: Purebasic and TWS

 

I am sorry to be a bit stubborn but I prefer to start in Purebasic. Might change later eventually.
?
However I am struggling using the TWSlib.dll. I can connect to this lib but I am having problems to calling a function.
What I would like to know is if TWSlib is the best option or is it better to use the windows socket solution ?
I am not sure but it seems that the source code suppplied with TWS API of the other languages use the socket version ?
?
And I also have the impression that the socket version is more generic and makes the program independent of changes made by IB to TWSlib.
What do you think ?
?
Please help me, I really would like to start making TWSAPI calls and start working on the interesting part of the program.
?


Re: reqRealTimeBars: data stops receiving for after-hour session

 

1- Not every stocks are traded after RTH even if traded premarket (unusual though)
2- Even if market is still open doesn't mean there are any trade done.
3- Look at your market data subscriptions
?
1- do a reqContractDetails and look for TradingHours
2- use TWS and look at Booktrader
?


what time EST is it safe to assume that IB have printed the final close for all symbols EOD

 

Hi all In the groups experience what is the earliest time , eastern standard time, when it's reasonable to say that IB have printed ( immutable) the close for all symbols that will be the number of record in perpetuity. So market close at 16:00 but there's going to be a period where the numbers are still being populated. I'm certain that someone has figured out what that "indeterminate" period is to a margin of safety. I asked IB sometime back but the support desk has never responded, I asked 3 time. thank you for any help OR thoughts. cp


Re: reqRealTimeBars: data stops receiving for after-hour session

 

isn't this exactly what useRTH=False supposed to mean?


TWS API - TICK Historical Candles issue AFTER November 25th

 

Hi,
?
I am trying to retrieve historical price data of a few FOREX currency pairs: mainly AUDCAD, EURUSD and EURGBP.
?
Since November 25th, I am unable to retrieve historical TICK data via API. Before that date, I was able to download any price even going back to 2022.
?
When trying to download TICK data, I am getting the following error:
Historical Market Data Service error message:HMDS query returned no data: <SYMBOL@TYPE OF PRICE>
?
I get the error regardles if it's:
CASH or CFD
IDEAPRO or SMART
ASK BID BID_ASK_MIDPOINT price
?
What I tried:
1. I tried and I CAN download 30 SEC candles meaning that it is not a problem with the code, connection or ports.
2. I used another account and another IP.
3. I tried different DATE formats, e.g. yyyyMMdd-HH:mm:ss
?
I am running out of options.
?
Can you please help me understand what might be the problem?


Re: Best Practices for Pulling Stock Data Efficiently with APIs

 

I experimented some time ago with downloading daily data for 200 US stocks to see what worked best for me. I tried sequential versus parallel.
With sequential I mean I submit a reqHistoricalData() for one ticker and wait until I have received the data and stored it in a csv file. Then I do the same for the next ticker on the list.
I repeated the experiment with parallel downloads. With parallel I mean that I assign a request ID to each ticker. Then I send all reqHistoricalData() requests for all of these, taking the pacing limit of 50 messages per second into account. All received data was stored with the particular request ID and then matched with the ticker symbol. Again, all data was stored in a csv file, one file per ticker.
This parallel approach was a lot lot faster. Since then I don't do sequential data requests any more.
Be aware though that if you request too much data you might encounter a "soft limit" imposed by IBKR. As it is soft, there are no real criteria provided by them. When exceeding these soft limits you will encounter connectivity issues (IBKR will disconnect your app). So what I do is, after I once downloaded the initial historical data, to download daily only the recent historical data and merge the new data with the existing data. Getting one month of historical data for around 200 tickers takes around 15 seconds with this parallel approach. I don't know whether this is the most efficient solution, but it is good enough for me.


Re: Best Practices for Pulling Stock Data Efficiently with APIs

 

Let's start with setting expectations. IBKR is a brokerage that handles your trades and not a specialized historical market data provider. While there is a lot of good historical data you can download, there will be limits, and their real-time data offerings are much richer and more reliable.

There are at least three recent posts that you should look at, but you should also spend a little effort and search the archive yourselves:
The trick will be to create a "pipeline" so that you have a certain number of outstanding requests all the time:
  • You'd start a certain number of requests up front without delays or pacing (say 25 requests).
  • Each time an outstanding request finishes and before you process the provided data, you submit a request for the next symbol.
  • In my case (server processing speed, latency, network bandwidth, distance from IBKR ....) that pipeline of 25 outstanding requests "self adjusted" the request rate to about 20 requests per second over the 26 seconds it took to download the data for 500 members of the S&P 500. Lager window sizes resulted in similar results and you can experiment with the exact number of outstanding requests to find the optimum for your setup.

Hope this helps,

闯ü谤驳别苍


Re: Best Practices for Pulling Stock Data Efficiently with APIs

 

Batch request
Never heard of it on IBKR,
- Look to me against the design pattern used by the API. Data callbacks do refer to a reqid, not to a symbol, so I don't see a way to resolve OHLC Bar for multiple symbol
- as you can asynchronously launch multiple requests, batch mode is in batching requests.
- IMHO as it's a also a Trading API, provision for batch would also induce some dramatic coding errors.
?
API limitation
yes there are, read:
optionally study what IBKR call "quote boost" (you shouldn't need it if I understood what you look for)
?
Reality is that it depends also on the popularity of the instrument, like if IBKR does not cache every data and you may need to wake-up old storage
Try to stay under 10 request/second if not intra-day, (you say "includes" so I don't know what else than Daily you may be looking for.)
?
Caching Strategies
None I heard of, and again I doubt anything exist you can manage yourself, this would be against API pattern (how to manage the cache size ? Purge ? Cache synchronization is a huge burden on IBKR shoulders, etc ...)
Tradition is that you handle the local storage is a DB of any form, including CSV file
?
Advices:
1.0- Unless you are dealing with crypto (where precision is paramount), consider another specialized data feed for your data, one that support batch request
like Polygone or FinancialModelingPrep or ActiveTicks,
IBKR primary purpose is to be a broker, not a data feed.
?
1.1- Other feed benefit of alternate feed is that they report more popularly used data.
IBKR seems to tradeoff with some data they fudge themselves, which reflect another kind of reality, somewhere closer to the efficient reality needed when you go manage orders.
I won't discuss who is right or wrong, what could be important if doing decision is frequently to use same data used by other algo
If you just look for OHLC, discard this advice IBKR one's are same, IBKR VWAP is different, probably more accurate, as other feed seems to base their VWAP on 1min hlc3 as AP.
?
2- Loop with 10 request in parallel, don't limit yourself to 1.
You will need a "rendez vous" method to synchronize all historicalDataEnd.? More advice involve knowing that language you expect to use.
?
?


Best Practices for Pulling Stock Data Efficiently with APIs

 

Hi everyone,

I’m working on a project that requires pulling historical data on hundreds of stocks using the Interactive Brokers TWS API. The data includes daily OHLC (Open, High, Low, Close) values and trading volume. My current method involves sequential API calls in a loop, which is slow and not efficient for a large dataset.

Here’s my current setup:

  1. Sending requests for individual stocks sequentially using the TWS API.
  2. Saving the returned data locally after each request for further analysis.

I’d like to optimize this process to handle large batches of stocks more efficiently.I’m interested in advice on:

  • Batch Requests: Does the TWS API support batch requests, and if so, how can I implement them?
  • API Limitations: Are there known rate limits or best practices with the TWS API to avoid throttling or data loss?
  • Caching Strategies: Are there effective ways to cache or save data to avoid redundant requests during retries or updates?

If anyone has experience optimizing the Interactive Brokers API or similar APIs for high-volume data retrieval, I’d appreciate your insights. Also, if this topic has been covered before, I’d be grateful if you could point me to the relevant discussions in the archives.

Thanks in advance!


Re: How do I find out the margin requirements for a US stock through the TWS API?

 

Margin by itself is not additive as it is calculated under the maximum norm. The closest thing that works in real-time is CME's SPAN mechanism. There's some documentation on the client systems wiki if you want to implement something similar, although it would require simulating all scenarios (up, down, sideways, high vol, low vol).
?
Margin(A) + Margin(B) != Margin (A+B)
?
With the SPAN methodology, the risk vectors are additive:
Risk(A+B)[k] = Risk(A)[k] + Risk(B)[k]
?
So margin can always be deduced:
Margin(A+B) = max ( Risk(A+B)[k] for k in [1..16] ) = max ( Risk(A)[k]+Risk(B)[k] for k=1..16 )
?
IB is doing something similar on their side.


Re: Purebasic and TWS

 

I am under the impression that IBKR have a decent REST API implementation.
Interesting! I didn't know that. I am going to check that out!


Re: Purebasic and TWS

 

Richard wrote almost exactly what I wanted to reply.
In a nutshell: go with python. Lots of support available, lots of sample code.?
If you have any kind of programming experience, the learning curve to python will be a pleasant and a short one.


Re: upper and lower limit

 

开云体育

No it won’t object to that. (But why don’t you just try it yourself with your paper-trading account?)

?

But there IS a problem here: because current price is below the trigger price of that first order, it will be executed immediately – not what you want.

?

To fix this, use a ‘market if touched’ (MIT) order for the first one.

?

By the way, few members have much knowledge of the socket protocol, so when asking this sort of question it’s best to express it in terms of API calls.

?

Richard


upper and lower limit

 

Hello all,
?
I would like to set an upper and lower limit for selling stocks I hold. For example, let's say that I hold 120 shares of SMCI and the current price is 32.50. I would like to SELL those 120 shares if it reaches 34.71 OR if it went down and reached 31.20:
?
SMCI-STK--0.0---SMART-Nasdaq-USD-----SELL-120-STP-34.71
SMCI-STK--0.0---SMART-Nasdaq-USD-----SELL-120-STP-31.2
?
The question is, if I only hold 120 shares, can I still do that since I sent 2 calls of 120 shares each (240 in total)? Will it be rejected with "you are trying to sell 240 SMCI shares but you only hold 120"?
?
Thank you


Re: How to get position info in "points"

 

As 闯ü谤驳别苍 mentioned, you need to keep track of PnL yourself based on the executions.
Reconciliating against reqPnl should be a governance / secondary check function and not be based on for trading.
?
As such it's not directly a TWS related question. For example, more advanced PnL engines will keep track of the following also:
- arrival time
- arrival price (what is current market price when you decide to go long)
- fill times
- fill prices
- average of fill prices
- slippage (fill price - arrival price)
- commisions
- market cost (fill price - mid price on each fill)
- price impact (mid price 3 minutes later vs fill price)
- MAE (in-trade)
- MFE (in-trade)
- draw-down across trades
- profit
- %win
- avg. win
etc...
?
You would need to pay alot of attention to the number handling (especially where you might have zeroes and a division - for example with 0 trades you would return something for %win even if the division by zero number of trades is undefined).
Depending on what you're doing you might want to optimize secondarily also for different parameters (like minimize price impact) or just use one of the existing TWS algos.


Re: Purebasic and TWS

 

I am going to have a look on Python, but I am affraid that mastering all the subtleties of a new language could take a lot of time? (a bit lazy too...).

?

---------------------------------------------------------------------------------------------------------

Here is the code generated by chatGPT for the "TWSLib.dll" VERSION. This code is only for getting started.

---------------------------------------------------------------------------------------------------------

; Load the TWSLib.dll (make sure it's in the same folder as the executable or in the system PATH)

If OpenLibrary(0, "TWSLib.dll")

??? Debug "TWSLib.dll loaded successfully"

???

??? ; Declare the functions from the DLL

??? ; The function signatures are derived from TWSLib documentation and can be different

??? ; Depending on what you're doing, the function names and arguments might change

???

??? ; Connect to TWS

??? Define ConnectToTWS.i

??? ConnectToTWS = CallFunction(0, "Connect", 7496, "127.0.0.1", "clientId") ; The Connect function is for establishing a connection

???

??? If ConnectToTWS = 1 ; Check if connected successfully (depends on how TWSLib.dll works)

??????? Debug "Connected to TWS"

???????

??????? ; Now you can send a request. For example, let's request account data

??????? ; You would need to call the appropriate methods for account data or market data

???????

??????? ; Example of account request (you'd need to look up exact method and params in TWSLib)

??????? Define requestAccountData.i

??????? requestAccountData = CallFunction(0, "reqAccountSummary", 9001, "All", "NetLiquidation") ; Example parameters

?

??????? ; Process response (usually you need a callback handler to get updates,

??????? ; but you can poll the data periodically)

??????? ; Placeholder code for data reception

?

??????? Debug "Requested account summary data"

?

??? Else

??????? Debug "Failed to connect to TWS"

??? EndIf

?

??? ; Don't forget to disconnect when done

??? CallFunction(0, "Disconnect") ; Disconnect from TWS

???

??? ; Release the library when done

??? CloseLibrary(0)

Else

??? Debug "Failed to load TWSLib.dll"

EndIf
?
---------------------------------------------------------------------------------------------------------

Here is the code generated by chatGPT for the SOCKET version.

---------------------------------------------------------------------------------------------------------

; PureBasic code to connect to the TWS API using socket

?

; Define TWS API socket port and host

#TWS_PORT = 7496? ; Default TWS socket port

#TWS_HOST = "127.0.0.1"? ; Default local host

?

; Initialize the socket connection

If OpenNetworkConnection(#TWS_HOST, #TWS_PORT)

??? Debug "Connected to TWS API."

???

??? ; Send a request to TWS (example: request account summary)

??? ; Example request string to get account summary

??? ; The following message is the format for sending API requests to TWS

??? ; This particular message is an Account Summary request

???

??? Define requestString.s

??? requestString = "10|1|1|1|4|0|1"

???

??? ; Send the message to TWS

??? WriteStringN(requestString)

???

??? ; Wait for response from TWS (you can use a loop here to read the data)

??? Define response.s

??? response = ""

??? While AvailableNetworkData()

??????? response + ReadString(AvailableNetworkData())

??? Wend

???

??? ; Output the response

??? If Len(response) > 0

??????? Debug "Received response from TWS: " + response

??? Else

??????? Debug "No response received."

??? EndIf

???

??? ; Close the network connection

??? CloseNetworkConnection()

Else

??? Debug "Failed to connect to TWS API."

EndIf
---------------------------------------------------------------------------------------------------------


Re: Purebasic and TWS

 

As Richard suggest with reasons, Python is most probably the best choice for a programmer who need to switch to another language
You can find a lot of support here in Python, and Java (and C/C++)
Python is one of supported language from IBKR (you need a reference point)
?
While IBKR is Asynchronous (that's the beauty of it), Python users seems happy with it and there are some decent provision for multi-threading in Python.
Just fight using any "sleep", there are always a good trigger or cv to listen to.
I never heard of anybody complaining about python performance for algo trading, before you experience a difference you will have to deal with a lot of unrelated issues (coloc, venues selection)
As per portability, switching from one platform to another involve so many details that code refactoring is not the top one of the list (even in C++ if you use C20 and standard RTL call, porting from/to Windows/Linux is a matter of days on first iteration)
?
As per code generated by ChatGPT, able to deal with IBKR TWS API, I am presuming here, but it could be a layer that obstruct understanding of some detail and make your debugging life a misery.
Out of curiosity I wonder what it looks like, can you post it as an attachement?
I am really intrigued!?
Knowing the IBKR API,how can GPT generate a working lib out of nowhere but specs ?
?
Last though:
I am under the impression that IBKR have a decent REST API implementation.
This could be a simpler path for you if you don't want to use a lib. REST is now universally implemented you should not have issue in Basic
?


Re: Purebasic and TWS

 

Thank you for your detailed response.
?
My first idea was to use VB6 that I have used in the past but it is no longer supported.
Switching to VB.NET seems to be a step as large as to any other language.
?
The trading algorithm is very simple. It uses only one instrument (future). I do not expect to use a large variety of TWS function calls.
But things always get much more complicated as expected...
?
Programming also involves psychology. I have some aversion for (semi)interpreted stuff.
I have worked professionally in embedded computing which means designing compact electronic boards and compact programs that fit into small microcontrollers.
My trading configuration will use a tiny PC (working 24 hours a day) that consumes 16W. Power will be backed up directly with a battery (no inverter at all).
Thats the KISS approach. The simpler, the more reliable it should be: a professional "obsession" ;-)
?
With Purebasic the only thing that I am affraid of is that with TWS everything seems to be asynchronous.
So some kind of multitasking or multithreading handling could be required.
?
By the way, I just made a request to chatGPT and in just a few microseconds it generated source code for both socket and TWSLib.dll choices.
So both options seem to work. Although the second seems to be simpler.
?


Re: Purebasic and TWS

 

开云体育

You need to decide what your priorities are. If you just want a large challenge, by all means push ahead with PureBasic, but it will be difficult to say the least because you’ll be pretty much entirely on your own, working on a topic that you have no experience of. And for what benefit? ‘compact’, ‘efficient’, ‘no dependencies’ etc are simply irrelevant distractions.

?

But if you want to actually get something useful (for trading) working in the shortest possible time, then just forget about PureBasic.

?

If you’re already familiar with some form of Basic, then you could use Visual Basic .Net, which will cost you nothing if you use the free Community Edition of Microsoft Visual Studio: if you haven’t used Visual Studio before you’ll be blown away by its massive capabilities, and ‘free’ doesn’t mean ‘cut down’, it just means you can’t use it for commercial software.

?

Visual Basic .Net works perfectly well with IBKR’s C# API (as does any other .Net language). But it’s still a significant learning curve to make good use of the API, as it also is in other languages like Java.

?

But by far the fastest way to be productive is to use Python (and especially the ib-async API from , which is a third-party API implementation that makes life a lot simpler in many ways).

?

Don’t be fooled into thinking that Python, being interpreted rather than compiled, is a poor choice: for the kind of programs we write using the API, that difference is entirely irrelevant. While it’s true that a compiled API might get your order out of the door a couple of microseconds sooner than an interpreted one, that advantage is totally buried by the several milliseconds, or more likely several tens of milliseconds, that it takes for your order to reach the exchange.

?

By the way, I’m not a Python user (there was no Python API when I started on this back in 2003), but I’m very much aware of the considerable productivity benefits it offers. I’m just too hugely invested in more ‘traditional’ languages to make the switch.

?

If you decide to go the Visual Basic (or C#) route, I can provide you with a simple (but meaningful) sample program that shows how to get started. Actually I’ll link to it here – it’s a couple of years out of date and uses the 10.19 API version, but it still works fine:

?

?

You’ll find the executable in the bin/debug subfolder, so you can run it to see what it does. If you load the project into Visual Studio, you won’t be able to compile it without first updating the reference to the C# API.

?

I hope this isn’t all too confusing…

?

Richard