In this tutorial we will explain how to connect to Open API using a TCP client socket and a SSL stream over socket stream.
cTrader Open API has separate endpoints / proxies for live and demo trading accounts.
If your application needs to work with both types of accounts, you need to maintain a separate connection with each live / demo endpoint.
You can't use a live endpoint connection to interact with demo trading accounts or vice versa, if you try you will receive a routing error from API.
The client socket connection must use SSL, otherwise you will not be able to connect or interact with API.
If you are building a trading platform or integrating cTrader on your platform you can follow these guidelines:
- Create only two connections, one for the live and one for the demo endpoint.
- Use the live connection for live trading accounts and demo connection for demo trading accounts. You can handle many accounts with just one connection, there is no need to create more connections.
- After your application connects to the API server, you can authorize your API application for each connection by sending a ProtoOAApplicationAuthReq message.
- The API will send you back a ProtoOAApplicationAuthRes message. This means that your API application is authorized and you are ready to go.
- Your application needs to be authorized before you send any other message to the API end point. If you send any message before authorizing your application, you will receive an error.
- To keep your connections alive, keep sending a ProtoHeartbeatEvent in 10 seconds interval.
- Use a message queue for sending / receiving data to avoid concurrent send / receive, or any other solution that is available for you based on your environment, for example we use Channels in .NET.
- Please check the endpoint / proxy limitations to avoid connection issues.
You can also use Websocket for connecting to Open API, the host and port are same as using direct TCP client socket.
We have a .NET Blazor sample web app that uses Websocket for connection instead of a TCP socket, you can find it here.
When using Websocket you use the same Protocol Buffer messages that you use on TCP socket, nothing changes.