PSContent
1. Introduction
2. Terms of Use
4. TWSLINK Setup and on principle usage
5. Description of exported functions
5.1. TWSLINK general functions
5.1.1. Function INITDLL
5.1.2. Function CONNECT
5.1.3. Function DISCONNECT
5.1.4. Function SET_DLLPARAM
5.1.5. Function GET_DLLSTATUS
5.1.6. Function SET_STOP
5.1.7. Function SET_BREAK
5.1.8. Function WAITDLL
5.1.9. Function WAIT_FOR_REQ_PROCESSED
5.1.10.Function GET_CONNECTED
5.1.11.Function WAIT_FOR_EVENT
5.1.12.Function DISPOSE
5.1.13.Function CONNECT_XML
5.1.14.Function INITDLL_XML
5.1.15.Function GET_EVENT_VAL
5.1.16.Function REQ_CURRENT_TIME
5.1.17.Function TIME_COMPONENT
5.1.18.Function TIME_STRING
5.1.19.Function SECONDS_1970
5.1.20.Function MILLISECONDS_1970
5.1.21.Function SET_TIMER
5.1.22.Function KILL_TIMER
5.1.23.Function PLAY_SOUND
5.1.24.Function GET_EVENT_VAL_L
5.1.25.Function GET_EVENT_VAL_D
5.2. Contract related functions
5.2.1. Function REGISTER_CONTRACT
5.2.2. Function WAIT_FOR_ACCEPTED
5.2.3. Function GET_CONTRACT_STATUS
5.2.4. Function REQ_MARKET_DATA
5.2.5. Function GET_CONTRACT_VAL
5.2.6. Function CLOSE_CONTRACT
5.2.7. Function GET_POSITIONS
5.2.8. Function GET_STOPPROTECTION
5.2.9. Function GET_MARKET_DATA
5.2.10.Function GET_TICK
5.2.11.Function QUERY_MARKET_DATA
5.2.12.Function ADD_COMBO_LEG
5.2.13.Function EXTRACT_MARKET_DATA
5.2.14.Function GET_CONTRACTUID_LIST
5.2.15.Function EXTRACT_ARRAY_VALUE
5.2.16.Function WAIT_FOR_TICK
5.2.17.Function CLOSE_CASH_POSITION
5.2.18.Function FORCE_CONTRACT
5.2.19.Function SET_INDICATOR_SIZE
5.2.20.Function VALIDATE_PRICE
5.2.21.Function REM_COMBO_LEG
5.2.22.Function SET_CONTRACT_VAL
5.2.23.Function REQ_BAR_DATA
5.2.24.Function GET_BAR_DATA
5.2.25.Function REGISTER_CONTRACT_XML
5.2.26.Function REGISTER_CONTRACT_XML_STYLE
5.2.27.Function REQ_MARKET_DEPTH
5.2.28.Function GET_MARKET_DEPTH
5.2.29.Function GET_MARKET_DEPTH2
5.2.30.Function REQ_HIST_DATA
5.2.31.Function REQ_SCANNER_PARAMETER
5.2.32.Function REQ_SCANNER_SUBSCRIPTION
5.2.33.Function CANCEL_SCANNER_SUBSCRIPTION
5.2.34.Function PIPS
5.3.1. Function PLACE_ORDER
5.3.2. Function SET_ORDERVAL
5.3.3. Function WAIT_FOR_ORDER_STATUS
5.3.4. Function WAIT_FOR_ORDER_STATUS_RNG
5.3.5. Function GET_ORDER_STATUS
5.3.6. Function GET_ORDER_ERROR
5.3.7. Function ATTACH
5.3.8. Function ORDER_MODIFIABLE
5.3.9. Function TRANSMIT
5.3.10.Function CANCEL_ORDER
5.3.11.Function SET_FA_MEMBERS
5.3.12.Function WAIT_FOR_FILLED
5.3.13.Function BUY_LMT
5.3.14.Function SELL_LMT
5.3.15.Function BUY_STPLMT
5.3.16.Function SELL_STPLMT
5.3.17.Function BUY_MKT
5.3.18.Function SELL_MKT
5.3.19.Function TOGGLE_ORDER
5.3.20.Function BUY_STP
5.3.21.Function SELL_STP
5.3.22.Function BUY_TRAIL
5.3.23.Function SELL_TRAIL
5.3.24.Function CONVERT_ID
5.3.25.Function CONVERT_ID_STRING
5.3.26.Function GET_ORDER_VAL
5.3.27.Function GET_EXECUTION_PRICE
5.3.28.Function GET_ORDERUID_LIST
5.3.29.Function TOGGLE_ORDER_PCT
5.3.30.Function WAIT_FOR_SUBMITTED
5.3.31.Function GET_OPEN_ORDERUID_LIST
5.4. Account related functions
5.4.1. Function GET_ACC_VAL
5.4.2. Function GET_BASECURRENCY
5.4.3. Function GET_ACC_NAME
5.4.4. Function REQ_ACC_UPDATE
5.4.5. Function GET_CASH_BALANCE
5.4.6. Function GET_EXCHANGE_RATE
5.4.7. Function GET_MARKET_VAL
5.4.8. Function GET_PORTFOLIO_VAL
5.5.1. General Consideration of Callback Usage
5.5.2. Function SET_EVENT_HANDLER
5.5.3. Function SET_EVENT_HANDLER_OLE
5.6.1. Function SEND_MAIL
5.6.2. Function SET_LNG_VAR
5.6.3. Function SET_DBL_VAR
5.6.4. Function SET_STR_VAR
5.6.5. Function GET_LNG_VAR
5.6.6. Function GET_DBL_VAR
5.6.7. Function GET_STR_VAR
5.6.8. Function REM_LNG_VAR
5.6.9. Function CUSTOMCOMMENT
5.6.10.Function START_APPLICATION
5.6.11.Function FROM_FILE
5.6.12.Function TO_FILE
5.6.13.Function REM_DBL_VAR
5.6.14.Function REM_STR_VAR
5.6.15.Function NEWS_BULLETIN
5.6.16.Function CONNECT_DB
5.6.17.Function DISCONNECT_DB
5.6.18.Function SQL
5.6.19.Function SQL2
6.1. Table Initialisation Flag Values
6.2. Table TWSLINK Parameter Names
6.3. Table TWSLINK Status Identifiers and their possible String Values
6.4. Table Contract Status Identifier
6.5. Table Contract Value Identifiers
6.6. Table Order Property Identifiers
6.7. Table Order Status Identifier
6.8. Table Order Error Identifier
6.9. Table Account Value Identifier
6.10. Table TWSLINK Loglevel Values
6.11. Table Callback Function Pointer Definition
6.11.1.Table of Primary Event Mask
6.12. Table Market Data Field Identifiers
6.12.1.Table Market Data Sub Field Identifiers
6.12.2.Table Market Data ID Values
6.13. Table TWSLINK Sub-Event Identifiers
6.14. Table ID Conversion Types
6.15. Table Market Value IDs
6.16. Table Portfolio Value IDs
6.17. Table Indicator Identifiers
6.18. Table Date/Time Format Specifiers
6.19. Table Realtime Bar Identifiers
6.20. Table Contract Property Attribute Names
7.1. Determination
7.2. Plausibility
7.3. Technical Notes/Restrictions´
7.4. API Reference-Help
10. General programming techniques
10.1. Register Contracts
10.1.1.Register multiple contracts
10.2. Accessing market data
10.3. Placing Orders for FA Accounts
10.4. Attaching Stop/Target Orders
10.5. The Order ID Types and their Meaning
10.6. Callback Usage
11. Database Support
The Interface DLL (TWSLINK) is thought to link any Application, that can utilize a DLL or COM, with Interactive Brokers TWS©, for the Purpose of Transmitting and Controlling Orders. All necessary Actions around orderhandling, connection and necessary requests, are done by the TWSLINK. This allows the user to concentrate on the strategy behind. Various functions take control over orders, contracts and account.
An extensive and scaleable logging allows users to track and fix problems and gives him the chance to integrate TWSLINK with his Strategy Builder / Signal Server to his preferred way.
Various samples e.g. for TradeStateson 2000i© given in .ELS – Format, Excel etc, demonstrate the use of TWSLINK. However, it should be easy to call all exported DLL-Functions from any other application that allows DLL Function Call or COM.
Several TWSLINK-Parameters take effect on how to connect to TWS, sizing buffers, timing delays for automatic reconnection etc.. But don’t feel cluttered by to many setup parameters. Most of these parameters can be used with their default-value.
The TWSLINK is free for Personal Use. However, any Donation is welcome and supports further Development. Please use:
PayPal for ce_@gmx.de.
Customized Versions are also possible. For this, please contact:
The Use of the TWSLINK is at your own Risk. The Author is not responsible for any monetary loss or physical or mental damage that could be related to the use of TWSLINK. It is strongly recommend to test the TWSLINK with Demo- or Paper Account before trading on real accounts.
It is not allowed to distribute TWSLink as it is or in conjunction with other applications/services without the permission of Trade Commander.
Simply run TWSLink.msi. Pay attention to the Readme/Install Text-Files in each sample directory. Depending of the language to which the Samples refer, additionally components may have to be installed (e.g. Perl, Python).
4. TWSLINK Setup and on principle usage.
A high-beep indicates that the DLL has been loaded. In contrariwise, a low-beep signals DLL has been unloaded. In Addition a Tray Icon shows DLL Status and Account Information. The Final Information String of the Tray has Format:
Account Name (or Short List FA Accounts)
[clientid][process id]
current net liquidation value in base currency
runtime net liquidation delta value in base currency
Tray Icons and their Meaning:
|
Icon |
Meaning |
|
|
TWSLink is not connected. |
|
|
TWSLink is connecting. |
|
|
TWS has accepted connection. |
|
|
TWS is not connected to IB or no Market Data Farm is available. |
|
|
TWS is connected to IB but currently at least one Market Data Farm is not available. |
|
|
TWS is connected to IB and all Market Data Farms are available. |
Before calling any other Functions, TWSLINK can be initialised unless you want to use the Default Settings provided by <twslink.xml>.
Call:
Once the Initialisation has been done, all Contracts have to be registered. For each Contract you want to trade,
Call:
Contractuid= REGISTER_CONTRACT(…).
Now connect to TWS. This Function normally has to be called once, if the automatic reconnect timer is enabled.
Call:
Check if your contract(s) has been accepted by TWS.
If WAIT_FOR_ACCEPTED(Contractuid, 000) >= -1 then
Begin
Do Your Trading Actions…
End
Else Begin
CUSTOMCOMMENT(“Something is wrong with my Contract Specification”,1,1)
End
5. Description of Exported Functions
5.1 TWSLINK Initialisation and Setup
5.1.1 Function INITDLL back
Description: Initialise TWSLINK. Call this function once before calling any other function if you want to change Default Setup.
Return Type: long
Return Value Table
|
Value |
Meaning |
|
1 |
Ok |
|
0 |
No Ok |
Parameter Table
|
# |
Name |
Type |
Comment |
Default |
Value Ref. |
|
timeoutminutes |
long |
Timeout in Minutes when placing an Order. Right before Placing, the Order gets checked against this Timeout Value. If the Timespan between Creation and Placement exceeds this Value, the Order won’t be placed. |
0 (disabled) |
|
|
|
2 |
logfile |
LPSTR |
Path and Name of Logfile. |
TWSLinkdll.log |
|
|
3 |
long |
Combination (Sum) of TWSLINK Setup Flags. |
0 |
||
|
4 |
twsloglevel |
long |
Loglevel on TWS Side (Server Loglevel). |
0 (unchanged) |
|
|
5 |
dllloglevel |
long |
The DLL internal Loglevel. |
2 (medium) |
|
|
6 |
reconnection_ms |
long |
Time Interval in Milliseconds for validating Connection. |
8000 |
|
Comments:
Sample: Init the DLL. Set a general Order Timeout of 2 Minutes, trace into File “mylog.log”, request for Account Updates, don’t change TWS Loglevel, set TWSLINK Loglevel to ‘Normal’ and set Reconnection Timer to 4 Seconds.
retcode = INITDLL(2,”mylog.log”,1,2,2,4000)
5.1.2 Function CONNECT back
Description: Connect to TWS.
Return Type: long
Return Value Table
|
Value |
Meaning |
|
1 |
Connection established. |
|
0 |
Connection failed. |
Parameter Table
|
# |
Name |
Type |
Comment |
Default |
|
1 |
Host |
LPSTR |
Host for TWS Connection. |
127.0.0.1 |
|
2 |
Port |
long |
Port for TWS Connection. |
|
|
3 |
startingClientId |
long |
Preferred TWS Client ID for TWSLINK Connection. Set < 0 to allow TWSLink try Connection on next free ClientId. |
1 |
|
4 |
Timeout |
long |
Timeout in Minutes. Pass -1 to wait Infinite. |
5000 |
Comments:
Sample: Connect to TWS at 192.166.0.2:7496 and use ClientID=1.
retcode = CONNECT (”192.166.0.2”,7496,1 ,-1)
Connect to TWS at 192.166.0.2:7496 and start with ClientID=1 and allow TWSLink to try next ClientId if Id=1 should be already used by another Client.
retcode = CONNECT (”192.166.0.2”,7496,-1 ,-1)
5.1.3 Function DISCONNECT back
Description: Disconnect from TWS. Once called, it stop the automatic reconnect-cycle. Normally this function has not to be called as TWSLink calls it before being unloaded.
Return Type: long
Return Value Table
|
Value |
Meaning |
|
1 |
Ok. |
|
0 |
No Ok. |
Sample: retcode = DISCONNECT ()
5.1.4 Function SET_DLLPARAM back
Description: Set a specific DLL parameter. This is usually called to set a parameter which Initialisation is not supported by INITDLL.
Return Type: long
Return Value Table
|
Value |
Meaning |
|
1 |
Ok. |
|
0 |
Parameter couldn’t be set. |
|
-2 |
Parameter unknown. |
Parameter Table
|
# |
Name |
Type |
Comment |
Value Ref. |
|
1 |
sCommand |
LPSTR |
Name of Parameter. |
|
|
2 |
sParam |
LPSTR |
Value of Parameter as string. |
|
Sample: Set the minimum delay between 2 commands to 300 milliseconds.
retcode = SET_DLLPARAM ("setcmddelay",“300“)
5.1.5 Function GET_DLLSTATUS back
Description: Get the status of a specific DLL parameter (property).
Return Type: string (Constant String)
Return Value Table
|
Value |
Meaning |
|
String describing the Status. |
|
|
“nok” |
#1 unknown. |
Parameter Table
|
# |
Name |
Type |
Comment |
Value Ref. |
|
1 |
sParam |
string |
Name of Status. |
|
|
2 |
lParam |
long |
Additional Integer Parameter. Its use depends on #1. |
|
|
2 |
sParam2 |
string |
Additional String Parameter. Its use depends on #1. |
|
Sample: Get the Connection Status of TWSLINK.
StringStatus = GET_DLLSTATUS("connected",0,””)
Check if Exchange ISLAND is in the List of unavailable Exchanges
ISLAND_UNAV = GET_DLLSTATUS("exunav",0,”ISLAND”)
5.1.6 Function SET_STOP (not threadsafe) back
Description: Set Stop Flag to enable/disable Function Call. The same Effect can be obtained by calling SET_DLLPARAM with #1 = “setfunctioncall” and Value = “1” or “0”. Unlike SET_STOP, a Call of SET_DLLPARAM is threadsafe. On the other Hand, SET_DLLPARAM could be locked due to a Prior Call of another threadsafe Function. So call SET_STOP in Emergency Situations only and take care to call this Function from one Thread only. Normally there should be no Need to call this Function. It is thought as Escape from unormal Situations during Tests.
Return Type: long
Return Value Table
|
Value |
Meaning |
|
New Stop Flag Value |
|
Parameter Table
|
# |
Name |
Type |
Comment |
Value Ref. |
|
1 |
Flag |
long |
If 1, Function gets disabled, otherwhise enabled |
|
Sample: Stop Function Call.
long_FunctionCallDisabled = SET_STOP(