To access the API from your application you need to add a reference to the T4API.20.dll and T4Definitions.20.dll files located in the c:\Program Files\CTS\T4ClientSim\bin\ folder to your project.
The namespace that contains all the API objects is T4.API. Most enumerations are defined in the T4 namespace:
' Import the T4 definitions namespace.
Imports T4
' Import the API namespace.
Imports T4.API
In order for your application to connect and login to the system you must create and hold a reference to a T4.API.Host object. Connection status and login responses are raised as events from this host object.
' Reference to the main api host object.
Private WithEvents moAPI As Host
To connect to the system you simply create the
Host object:
' Initialise the api when the application starts.
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create the api host object.
moAPI = New Host("", _
APIServerType.Simulator, _
"T4Example", _
"112A04B0-5AAF-42F4-994E-FA7CB959C60B", _
"cfc", _
"T4Example", _
"t4example")
End Sub
The parameters to the Host constructor are listed
here.
The API will not connect immediately, instead it will create and return the Host object and then raise events following connection and login. The Connected and Disconnected events are raised to signal changes in the connection status:
' Event raised when the api has connected to a server.
Private Sub moAPI_Connected() Handles moAPI.Connected
Trace.WriteLine("Connected")
End Sub
' Event raised then the api has disconnected from a server.
Private Sub moAPI_Disconnected(ByVal psReason As String) Handles moAPI.Disconnected
Trace.WriteLine("Disconnected")
End Sub
These events are for notification only, following a Disconnected event the API will automatically keep trying to reconnect. You do not need to recreate the API to have it connect again. You can also check the IsConnected property of the Host object to check the connection state.
Note: Do not attempt to send messages (e.g. request market data, submit orders etc) between receiving the Connected event and before you receive the LoginSuccess event. Messages sent will either be ignored or cause the connection to be terminated. This applies also following a disconnect and reconnect.
Once the API has connected it will try to login automatically, the LoginSuccess and LoginFailure messages are raised with the result:
' Event raised if login is successful.
Private Sub moAPI_LoginSuccess() Handles moAPI.LoginSuccess
Trace.WriteLine("Login Success")
End Sub
' Event raised if login failed.
Private Sub moAPI_LoginFailure(ByVal penReason As LoginResult) Handles moAPI.LoginFailure
Trace.WriteLine("Login Failed due to " & penReason.ToString)
End Sub
Once the login success event is raised then you can start using the rest of the API (subscribing to market depth, submitting orders etc). If the LoginFailure event is raised then the penReason
LoginResult parameter will tell you why login failed.
If the failure reason is IncorrectVersion then you must upgrade your machine and application to the latest version of the API using the install on the website. The url to the latest install is given by the NewVersionURL property of the Host object.
NOTE: At the time of writing this document system upgrades that require a version upgrade were occurring about once a year. However, this does not necessarily mean that you will need to make any code changes to support the new version, and may not even need to recompile your application.
If you receive a LoginFailure event and want to try connecting again then you will need to create a new Host object.
When your application shuts down you should dispose of the host object so that the API shuts down correctly:
' Shutdown the api when the application exits.
Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
' Check to see that we have an api object.
If Not moAPI Is Nothing Then
' Dispose of the api.
moAPI.Dispose()
moAPI = Nothing
End If
End Sub