The article describes how to log into and out of the system.
How to log into and out of the system
In order to access any of the functionality of the API, you will first need to authenticate and to create a session.
In order to end your session, you should perform the logout operation.
First, you should add the library and connect your application to it as described in How to add the API to your project.
Now that this is done, we can create a session using:
Create a session Hide
typescriptjavascriptswiftjavalet session = sessionFactory.create("LoginSample"); //LoginSample - any name
See also: FXConnectLiteSessionFactory, create().
The next step is to create a listener to monitor the connection status and subscribe to it.
This can be done using:
Listener Hide
typescriptjavascriptswiftjavaclass ConnectionStatusChangeListener implements FXConnectLite.IConnectionStatusChangeListener { onConnectionStatusChange(status: FXConnectLite.IConnectionStatus) { Printer.print("Connection status changed. " + this.getStatusString(status)) } getStatusString(status: FXConnectLite.IConnectionStatus): string { return (status.isConnected() ? "Connected" : "") + (status.isConnecting() ? "Connecting" : "") + (status.isReconnecting() ? "Reconnecting" : "") + (status.isDisconnected() ? "Disconnected" : ""); } } session.subscribeConnectionStatusChange(new ConnectionStatusChangeListener());
See also IConnectionStatusChangeListener,
subscribeConnectionStatusChange.
There are 2 ways to login:
1. For the first method, you will need several parameters: user and password, tradingSystemUrl and connectionName.
You can obtain your API access credentials by logging into your FCLite account, selecting "API" from the menu at the top of the screen, and then selecting "Create Token". This will generate an API access token that you can use to authenticate your requests to the API.
The tradingSystemUrl parameter should be set to the appropriate URL for your region, and the connectionName parameter can be set to any name you choose to identify your connection.
The first way to login Hide
typescriptjavascriptswiftjavasession.login(session, user, password, tradingSystemUrl, connectionName, new LoginCallback());
The first four parameters are the same as the ones that the user enters in the Trading Station Application connection settings dialog, however.
The loginCallback is the final parameter. As you can see from the code above, loginCallback is declared as an instance of the class ILoginCallback that is part of the installed library.
You, as the developer, must extend this class, as in the examples, and declare your own functions: onLoginError, onTradingTerminalRequest, so that your application can handle these as you need.
With this code executed, you should now have both a session, and your user logged in.
2. The second method for login is login by JSON Web token, which could be obtained from an OAuth service.
To authenticate with the FCLite API using OAuth, you will need to follow a series of steps. Here is an overview of the steps you will need to take:
1. Register an application with FCLite
2. Create a new OAuth client by providing some basic details about your client.
3. Generate an access token for your client by authenticating with the FXCM OAuth service.
4. Use the access token to authenticate your client with the FXCM API.
The second way to login Hide
typescriptjavascriptsession.attach(jwt, tradingSystemUrl, connectionName);
See also: login(), ILoginCallback, attach
To Logout, simply execute:
Logout Hide
typescriptjavascriptswiftjavasession.logout();
Download the sample TypeScript, JavaScript.
When the session is created, you can:
Getting available instruments Get available instruments.
Subscribe to an instrument Subscribe and Unsubscribe to instruments.
Receive price updates Get price updates for an instrument.
Receive historical prices Get historical prices for an instrument.
Create a market order Create a market order.
Create an entry order Create an entry order.
Get open positions Get open positions.