开云体育

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

Re: Purebasic and TWS

 

Thank you all very much for your responses.
?
I will carefully study all the information received and try not to bother too much in the future.


Re: Purebasic and TWS

 

You received a lot of valuable feedback, the task you are trying to take on is massive, it will be a nightmare to maintain as a single person over long periods of time, and will possibly be a massive waste of time. I had not heard about PureBasic until your post, so that I am sure there is nobody to help you. And the code ChatGPT generated will not get you anywhere either, But go ahead, if that is what you want to do.

HOWEVER this is not the group to look for help for a task that you have been advised against. We are exclusively focused on the API implementations that IBKR provides. You may not expose our 6,000++ members to the long struggle you are going to have, That will create too much noise and I will have to lock the topic.

So here my advise:

  • Don't do it. Stay with one of the many IBKR API implementations (C++, C#, VB, Java, Python) or and independent implementation with a large enough existing community (ib_async., Ruby, ...)
  • At the heart, all IBKR TWS API implementations simply serialize the requests your client makes into messages that are sent to TWS/IBGW via the socket and, in return, a deserialize messages from TWS/IBGW into callbacks. Follow Mark Collins's advice and read and understand the EClient (requests) and EDecoder (responses) classes for a language you are most familiar with. You would have to create the equivalent to those two classes in PureBasic if you want to connect at the socket level.
  • The wire-line message protocol is not documented, subject to change without notice, and does change with every major TWS/IBGW release. But several of our members and the ib_insync/ib_async community do maintain their own implementations. It can be done.
  • If that is the route you want to go, implement a program that simply connects to TWS/IBGW via the socket and handles the few very basic messages required for the "HELLO" phase. But there are will be many additiona? messages with complex objects zou would have to implement eventually even if you'd only use a subset of TWS API. But I have attached (once more) the basic message flow you would have to implement:
    • Create a socket with TWS/IBGW
    • Send the FIRST_CLIENT_MESSAGE
    • Wait for and receive the FIRST_SERVER_MESSAGE
    • Send the API_START_API_MESSAGE
    • Wait for and receive the MANAGED_ACCOUNTS_MESSAGE and NEXT_VALID_ID_MESSAGE
    • Plus be prepared for ERROR_MESSAGEs since they can occur at any time after API_START
  • IBKR takes care of all the little details by providing you with TWS API implementations for various languages. Making and maintaining a wrapper around TWSlib.dll for PureBasic might be a much more manageable task as long as TWSlib.dll and their threading code plays nicely with PureBasic. You might have to make minor adjustments to method signatures over time, but you can stay on the same TWSlib.dll version for months if not years unless you need features of newer versions.

But using an existing TWS API implementation will be by far your most productive path. And the only path for our group to provide you with any ongoing help.

One last thought. Assuming PureBasic provides a rich set of WEB/REST API tools, (formerly called Client Portal API) might be the simplest path for you to interact with your IBKR account from PureBasic.

闯ü谤驳别苍

?

?

?

?
On Thu, Dec 5, 2024 at 11:12 AM, Tradiator wrote:

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: Purebasic and TWS

 

Sorry if blunt, no intent
but IMHO and using my limited experience of IBKR I feel the need to share the feeling that you are "swimming against the flow".
?
1- Development should start easy and gradually increase in complexity,
Here it look to me you have a first mandatory daunting task to overcome, to create a non existing ABI lib for another language, able to do the telecom with the IBKR ABI,
And extend this later taking control and experiment the API itself (not a minor task and requiring a lot of trials)
All of this without any examples or help or support.
Then and only then will you be able to focus yourself on your algo.
?
2- Although this business require well designed and safe code, a loosely type language like Basic is not a way to make your future life simple.
You also increase the odd of uncontrolled polymorphic calls that could create hard to diagnose side effect.
(I already get bad feeling with your code that morph "clientid" to a string)
If your aren't familiar with C++ from your embedded system experience, go for Java, surely a strongly type language and also the best portable solution (I wonder why do you need to design a cross platform, solution)
?
3- I don't understand your terminology, what IBKR codes are you calling the "windows socket solution" ?
hints: the IBKR Client code sample ?
Where did you get the "TWSlib.dll" ? I just don't remember if IBKR does supply a precompiled binary.
note: this may show how difficult it could be to give help if you don't comply with a standardized terminology, by default the IBKR API convention
?
Last:
Why don't you consider a mix with another REST data oriented vendor and using IBKR Web API (a REST API) ?
It is surely more easy to implement and debug.
?
?
?


Re: Purebasic and TWS

 

I'm with the rest. Please consider using python. I can use julia but python is more fun.

so in purebasic you'd

; PureBasic example for connecting to IB API and requesting account details

#PORT = 7497 ; TWS or Gateway default port
#HOST = "127.0.0.1"

; Establish a socket connection
If OpenNetworkConnection(#HOST, #PORT)
  Debug "Connected to IB TWS/Gateway"
  
  ; Send handshake message (TWS Protocol version)
  SendNetworkString(0, "API\0")
  Delay(100)
  
  ; Request account details (e.g., account summary request)
  requestID = 1
  version = 1
  message = "9" + Chr(0) + Str(version) + Chr(0) + Str(requestID) + Chr(0) + "All" + Chr(0)
  SendNetworkString(0, message)
  Delay(200)
  
  ; Receive and parse response
  Repeat
    buffer = ReceiveNetworkData(0, *memory, 4096)
    If buffer > 0
      result$ = PeekS(*memory, buffer)
      Debug result$
    EndIf
  Until buffer <= 0
  
  CloseNetworkConnection(0)
Else
  Debug "Failed to connect to IB TWS/Gateway"
EndIf

and in python you'd


from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract


class IBApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId, errorCode, errorString):
        print(f"Error: {reqId}, {errorCode}, {errorString}")

    def accountSummary(self, reqId, account, tag, value, currency):
        print(f"Account Summary. ID: {reqId}, Account: {account}, {tag}: {value} {currency}")

    def accountSummaryEnd(self, reqId):
        print(f"Account Summary End for request ID: {reqId}")
        self.disconnect()


def main():
    app = IBApp()
    app.connect("127.0.0.1", 7497, clientId=1)

    # Wait for the connection to establish
    app.run()

    # Request account summary
    app.reqAccountSummary(9001, "All", "NetLiquidation,AvailableFunds")
    

if __name__ == "__main__":
    main()


Re: Purebasic and TWS

 

I'll be completely honest, if you find you stumble here, then it is very likely that your journey will be a hard one and you will likely waste time getting going when you should be working on something much more interesting.
Take one of the example code interfaces, and learn to read that language. It does not matter if it's c#, python or even java. All programming languages handle the same basic concepts you'll find you have transferable skills that map across. I would not be surprised if GPT or Replit couldn't generate the interface automatically. Honestly, learning new languages will save you a lot of time and provide you skills at the same time.?
If your attachment to Basic is because you have coded decision algorithms in that language, then perhaps you could drop the output to a database and simplify the execution side to simply running the positions according to what the DB says. Just an idea.
The reason I suggest all this is that if you go a direction that is traveled by you and you alone, then there will be no one here who can help you when the inevitable hiccups occur.

Best of luck,

M


On Thu, 5 Dec 2024 at 17:12, Tradiator via <yahoo=[email protected]> wrote:
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.
?



--
+44 (0) 7528 551604?


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

 

Makes sense but the group would know, through experience, when it's reasonable to see that the US exchanges are printed. I am only interested in the PRINT after the close. It would be nice to think all is done by 16:00 EST but is that reality? Is it 16:05, 16:15... you get the idea. Thanks for the heads up though


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