To access the API from your application you need to add a reference to the T4API.40.dll and T4Definitions.40.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. Login responses are raised as events from this host object.
' Reference to the main api host object.
Private WithEvents moAPI As Host
Easy Login with User Interaction
If you are creating an application that the user will interact with then the easiest way of logging in is to use the API's
Login method which displays a dialog to the user and gracefully handles all the login responses that can occur.
' 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 = Host.Login(APIServerType.Simulator, _
"T4Example", _
"112A04B0-5AAF-42F4-994E-FA7CB959C60B")
' Check for success.
If moHost Is Nothing Then
' Host object not returned which means the user cancelled the login dialog.
Me.Close()
Else
' Login was successfull.
Trace.WriteLine("Login Success")
End If
End Sub
If login succeeds then the method returns the Host object ready for you to subscribe to markets and accounts etc. If login failed (the user can try multiple times), and the user consequently cancelled the dialog then Nothing is returned. No LoginSuccess or LoginFailure events are raised during the initial login when using this method, but they are still raised if the API gets disconnected from the server while your application is running.
' 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
Unattended Login
If you want to handle login yourself, or are creating an application that will login automatically without user interaction then you will need to use the following approach.
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", _
"CTS", _
"T4Example", _
"password")
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 login. These events are for notification only, following a LoginFailure event with penResult=UnexpectedDisconnect the API will automatically keep trying to reconnect. You do not need to recreate the API to have it connect again, nor do you need to resubscribe to Accounts or Markets as they will automatically be resubscribed on reconnection.. You can also check the IsConnected property of the Host object to check the connection state.
The LoginSuccess and LoginFailure messages are raised following login or disconnection:
' 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. This should only occur when API compatibility is broken, such as when moving from .Net 2.0 to .Net 4.0.
If you receive a LoginFailure event with a password or application failure and want to try logging in 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