开云体育

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

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
---------------------------------------------------------------------------------------------------------

Join [email protected] to automatically receive all group messages.