Keyboard Shortcuts
Likes
Search
Purebasic and TWS
Hi,
?
I am looking for Purebasic source code that uses TWS API. I am a begineer in Purebasic and TWS but not in programming but dont really know where to start.
?
For example do I have to:
1 - Load a DLL such as TWSAPI.DLL or TWSLib.dll or something else, and then call the included functions? 2 - Open a windows socket connection and then send command strings ? ?
I tryed to find out exploring source code in other languages, but nothing appeared clearly to me.
?
My feeling is that Purebasic is a good language for this use as it generates a very compact and efficient executable file with no depencies.
?
I just need to get started, then I can manage the rest. However if I could find Purebasic source code I might save a lot of time.
?
Thanking you in advance for any help.
|
开云体育I haven't seen an API version for basic or purebasic.? There are versions for C++, C#, Python, java and others.? But I'm not sure if that would work well and you wouldn't have much for support as you progress. Perrhaps someone else knows more on this... -------- Original message -------- From: "Tradiator via groups.io" <yahoo@...> Date: 2024-12-02 7:02 a.m. (GMT-08:00) Subject: [TWS API] Purebasic and TWS Hi,
?
I am looking for Purebasic source code that uses TWS API. I am a begineer in Purebasic and TWS but not in programming but dont really know where to start.
?
For example do I have to:
1 - Load a DLL such as TWSAPI.DLL or TWSLib.dll or something else, and then call the included functions? 2 - Open a windows socket connection and then send command strings ? ?
I tryed to find out exploring source code in other languages, but nothing appeared clearly to me.
?
My feeling is that Purebasic is a good language for this use as it generates a very compact and efficient executable file with no depencies.
?
I just need to get started, then I can manage the rest. However if I could find Purebasic source code I might save a lot of time.
?
Thanking you in advance for any help.
|
开云体育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 |
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. ?
|
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
? |
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--------------------------------------------------------------------------------------------------------- |
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.
? |
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:
--
+44 (0) 7528 551604? |
I'm with the rest. Please consider using python. I can use julia but python is more fun. so in purebasic you'd
and in python you'd
|
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. ?
?
?
|
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:
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:
? |
开云体育Can I just say that @Tradiator’s earlier post today has actually opened up a possibility to move forward with PureBasic. I’m in the middle of drafting a response to it that will indicate a way to proceed. It’ll be a while before I’m ready to post it though. ? So if no-one objects, I’ll post that response here in due course (in case it might be useful for someone else in future), and I can then follow up privately with @Tradiator if he wants to try my suggestion. ? 闯ü谤驳别苍. If you’d rather I didn’t, let me know and I’ll just send it direct to him. ? Richard ? ? From: [email protected] <[email protected]> On Behalf Of Tradiator
Sent: 05 December 2024 18:38 To: [email protected] Subject: Re: [TWS API] 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. |
I have no objections at all, Richard, and documenting such a path may even be helpful for people who want to integrate other languages in the future. I just want to avoid that the entire group will be exposed to the long and painful TWS API re-implementation process from scratch in PureBasic. Looking forward to seeing what you came up with. 闯ü谤驳别苍 ? On Thu, Dec 5, 2024 at 12:54 PM, Richard L King wrote:
|
开云体育@TRadiator ? This is in response to your post earlier today. Your statements are quoted in blue, followed by my replies. ? “I am sorry to be a bit stubborn but I prefer to start in Purebasic. Might change later eventually.” ? Well, all the advice you’ve received so far is DON’T. And that’s good advice. I think you should take it. Having said that, I’ll try to provide some responses to your statements that might help. ? “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 ?” ? What do you mean when you say you can connect to TWSLib.dll? Do you mean you can reference the library, instantiate a TWS object and successfully call the eConnect() method? If so, that’s actually quite encouraging. If PureBasic can properly deal with ActiveX technology, then you should be able to use it quite successfully. ? What do you mean by ‘the windows socket solution’? I presume you mean the CSharpAPI.dll library? ?Every API implementation, no matter what language, use a socket connection to communicate with TWS/Gateway: there is no other way to communicate. Every API sends the same message in response to an API call. The TWSLib.dll (also known as the ActiveX API) is basically just a wrapper around CSharpAPI.dll. This wrapper uses Microsoft’s ActiveX technology to enable it to be used in environments that ‘speak’ ActiveX, such as Excel and other Office apps, Delphi, even Visual Basic 6. This version of the API is in some respects easier to program with than the CSharpAPI.dll, but it also has quite a few ‘gotchas’ that make it rather confusing. ? So assuming that PureBasic is a fully fledged ActiveX host, you should be able to use the full power of the API fairly straightforwardly, once you’ve got over the initial confusion. ? The other day I sent you a link to two sample programs written in Visual Basic .Net and C#, both using the CSharpAPI. As it happens, I also have a version that uses the ActiveX API. It does exactly the same things as the other two, but uses TWSLib.dll rather than CSharpAPI.dll. I’ve now uploaded that sample to the same location (see the SimpleTWSActiveXDemo folder, or SimpleTWSActiveXDemo.zip which is easier to download): ? (). ? Hopefully you’ll be able to use that code as a basis for something in PureBasic. I imagine the main differences will be in how you reference the project, the syntax for handling events, and of course the UI creation which would no doubt be quite an effort to port. I’d suggest starting with a much reduced version that has a button to connect/disconnect and handles the connectAck and errMsg events. Once that’s working you can build up with the reqMktData call and the tickPrice and tickSize events, etc. ? I should add that this version is also not using the latest API, but it works perfectly well. It doesn’t include the change from Double type to Decimal type for sizes, but that won’t matter for the moment (unless you want to trade crypto-currency contracts). I’ll try to get round to bringing these samples up-to-date? at some point. ? If you get stuck, email me privately (you can do this using the Reply to Sender facility). I’ll do what I can to help, but only if it looks like there’s a chance it can be made to work. ? ? “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 ?” ? No, not really. The ActiveX API provides pretty much exactly the same capabilities as the CSharpAPI.dll (I think there are a couple of things that IBKR haven’t been able to provide for technical reasons, but I can’t remember what they are without going and looking – certainly not things that you’re likely to need any time soon). Changes to TWSLib always affect the client program one way or another. ? “Please help me, I really would like to start making TWSAPI calls and start working on the interesting part of the program.” ? ??????????????? I’ll try!... ? ? Richard ? |
@Gordon Eldest ?
> ...I feel the need to share the feeling that you are "swimming against the flow".
? ? About 20 years ago I played a bit with the IB interface with VB6. It was as simple as dropping an .OCX control on a form and then calling its procedures. As simple as that! ? ? I had been able to request data and place orders in a very short time. What has happened since then that interfacing to IB has become as difficult as climbing the Everest mountain!
For now, I just expected things to be as simple, or just slightly more complicated. ? > 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. > 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) ? ? I am just beginning with Purebasic but I have read that it could use "strongly typed variables". As I am a C programmer I prefer those type of variables of course. ? > 3- I don't understand your terminology, what IBKR codes are you calling the "windows socket solution" ? > hints: the IBKR Client code sample ? ? ? Portions of code that I have found here and there, not supplied by IB. > 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 ? ? 2 precompiled files were supplied with the TWS API version that I recently downloaded (one for CPP I think). ? > Last: > Why don't you consider a mix with another REST data oriented vendor and using IBKR Web API (a REST API) ? ? ? This is the first time I hear about REST. Unfortunately for my current project, I am an electronician and not a computer scientist. There have been so many evolutions for the past 20-30 years. |
@Richard L King
?
> So if no-one objects, I’ll post that response here in due course (in case it might be useful for someone else in future), and I can then follow up privately with @Tradiator if he wants to try my suggestion.
? ? ? ? Richard, I would be very happy to try your solution. ? ? ? ? Thanking you in advance. ? |
开云体育Tradiator writes: ? ‘What has happened since then that interfacing to IB has become as difficult as climbing the Everest mountain!’ ? I think that’s a little unfair. If you use the ActiveX API, it’s still just as easy, except that you don’t host it on a form – thankfully, because that was never sensible. You just have to instantiate it like any other object with the New operator. Wiring up events in Visual Basic.Net is just as easy as it was in VB6, and more flexible. You can see all this in the ActiveX sample I linked to earlier. ? The ActiveX API also has the benefit that events are fired on the main program thread, so that there is no need to be concerned with threading issues, and the UI can be accessed and modified directly. Though of course you can easily offload processing onto another thread where needed, and the async facilities in .Net make asynchronous working a delight. ? It’s certainly true that it’s more complex when using the CSharp API. I think there were some bad design decisions back in 2015, and I was fairly vocal about them on the GitHub repository, but my arguments fell on deaf ears. But it’s all water under the bridge, and it’s really not that hard to get it going. Certainly not in the Everest league (I went trekking there in 1984, up to 18000 feet, just mindblowing…). ? Richard |