The WebSocket protocol allows 2 types of requests: Streaming commands (receive live updates) and Retrieve Trading Data (send a request to server retrieving some information).
You can configure the following properties in the XTB property.
When the client successfully connects to XTB servers, the event OnXTBConnect is fired. If there is any error while trying to connect, the event OnXTBError will be fired with the error details.
After the event OnXTBConnect is fired, then you can start to send and receive messages from XTB servers.
oClient := TsgcWebSocketClient.Create(nil);
oXTB := TsgcWSAPI_XTB.Create(nil);
oXTB.Client := oClient;
oXTB.XTB.User := 'user_0001';
oXTB.XTB.Password := 'secret_0001';
oClient.Active := True;
procedure OnXTBConnect(Sender: TObject; const aStreamSessionId: string);
begin
DoLog('#XTB Connected');
end;
procedure OnXTBError(Sender: TObject; aCode, aDescription, aRawMessage: string);
begin
DoLog('#error: ' + aDescription);
end;
Method | Description |
Login | In order to perform any action client application have to perform login process. No functionality is available before proper login process. The login method is called automatically after the client connects to the websocket server and the User/Password values are set. |
Logout |
You can subscribe to the following channels:
Method | Description |
SubscribeBalance | Allows to get actual account indicators values in real-time, as soon as they are available in the system. |
SubscribeCandles | Subscribes for and unsubscribes from API chart candles. The interval of every candle is 1 minute. A new candle arrives every minute. |
SubscribeKeepAlive | Subscribes for and unsubscribes from 'keep alive' messages. A new 'keep alive' message is sent by the API every 3 seconds. |
SubscribeNews | Subscribes for and unsubscribes from news. |
SubscribeProfits | Subscribes for and unsubscribes from profits. |
SubscribeTickPrices |
Establishes subscription for quotations and allows to obtain the relevant information in real-time, as soon as it is available in the system. The getTickPrices command can be invoked many times for the same symbol, but only one subscription for a given symbol will be created. Please beware that when multiple records are available, the order in which they are received is not guaranteed. |
SubscribeTrades | Establishes subscription for user trade status data and allows to obtain the relevant information in real-time, as soon as it is available in the system. Please beware that when multiple records are available, the order in which they are received is not guaranteed. |
SubscribeTradeStatus | Allows to get status for sent trade requests in real-time, as soon as it is available in the system. Please beware that when multiple records are available, the order in which they are received is not guaranteed |
SubscribePing | Regularly calling this function is enough to refresh the internal state of all the components in the system. Streaming connection, when any command is not sent by client in the session, generates only one way network traffic. It is recommended that any application that does not execute other commands, should call this command at least once every 10 minutes. |
You can send the following Requests:
Method | Description |
GetAllSymbols | Returns array of all symbols available for the user. |
GetCalendar | Returns calendar with market events. |
GetChartLastRequest | Please note that this function can be usually replaced by its streaming equivalent getCandles which is the preferred way of retrieving current candle data. Returns chart info, from start date to the current time. If the chosen period of CHART_LAST_INFO_RECORD is greater than 1 minute, the last candle returned by the API can change until the end of the period (the candle is being automatically updated every minute). |
GetChartRangeRequest | Please note that this function can be usually replaced by its streaming equivalent getCandles which is the preferred way of retrieving current candle data. Returns chart info with data between given start and end dates. |
GetCommissionDef | Returns calculation of commission and rate of exchange. The value is calculated as expected value, and therefore might not be perfectly accurate. |
GetCurrentUserData |
Returns information about account currency, and account leverage. |
GetIbsHistory |
Returns IBs data from the given time range. |
GetMarginLevel | Please note that this function can be usually replaced by its streaming equivalent getBalance which is the preferred way of retrieving account indicators. Returns various account indicators |
GetMarginTrade | Returns expected margin for given instrument and volume. The value is calculated as expected margin value, and therefore might not be perfectly accurate. |
GetNews | Please note that this function can be usually replaced by its streaming equivalent getNews which is the preferred way of retrieving news data. Returns news from trading server which were sent within specified period of time. |
GetProfitCalculation | Calculates estimated profit for given deal data Should be used for calculator-like apps only. Profit for opened transactions should be taken from server, due to higher precision of server calculation |
GetServerTime | Returns current time on trading server |
GetStepRules | Returns a list of step rules for DMAs |
GetSymbol | Returns information about symbol available for the user |
GetTickPrices | Please note that this function can be usually replaced by its streaming equivalent getTickPrices which is the preferred way of retrieving ticks data. Returns array of current quotations for given symbols, only quotations that changed from given timestamp are returned. New timestamp obtained from output will be used as an argument of the next call of this command. |
GetTradeRecords | Returns array of trades listed in orders argument |
GetTrades | Please note that this function can be usually replaced by its streaming equivalent getTrades which is the preferred way of retrieving trades data. Returns array of user's trades. |
GetTradesHistory | Please note that this function can be usually replaced by its streaming equivalent getTrades which is the preferred way of retrieving trades data. Returns array of user's trades which were closed within specified period of time. |
GetTradingHours | Returns quotes and trading times. |
GetVersion | Returns the current API version. |
Ping | Regularly calling this function is enough to refresh the internal state of all the components in the system. It is recommended that any application that does not execute other commands, should call this command at least once every 10 minutes. Please note that the streaming counterpart of this function is combination of ping and getKeepAlive |
TradeTransaction | Starts trade transaction. tradeTransaction sends main transaction information to the server. |
TradeTransactionStatus | Please note that this function can be usually replaced by its streaming equivalent getTradeStatus which is the preferred way of retrieving transaction status data. Returns current transaction status. At any time of transaction processing client might check the status of transaction on server side. In order to do that client must provide unique order taken from tradeTransaction invocation. |