SDKs
JavaScript/Typescript
Real Time

Realtime Socket

The SDK allows you to receive realtime updates without having to worry about reconnections, sessions and clients.

It is possible to find the following types of events: Ticks, Trades, Charting Data (Candles), Balances.

The realtime events use a Socket IO (opens in a new tab) connection under the hood to receive updates.

Please Note: Ticks may sometimes be referred to as Market Prices in this documentation or in general trading terms. Each tradeable asset (symbol/instrument), including forex rates, have a fluctuating bid and ask price which can be retrieved in real-time via this ticks subscription.

Connection

The SDK handles the connection to the server as soon as the first subscription happens. When there are no more active subscriptions, it disconnects the socket.

Re-connection in case of unexpected disconnections is handled automatically by the SDK.

⚠️

Remember to provide a valid authentication method before subscribing, or the promise will be rejected.

Ticks

Access to the ticks stream is provided by the subscribeTicks method. This method allows you to subscribe to a list of instruments and receive updates for all of them.

import { DecoratedTickItem } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.subscribeTicks(['ETHUSD', 'NFLX'], 'default_spreads', (tick: DecoratedTickItem) => {
  console.log(tick);
});

Multiple subscriptions can be made at the same time, and the SDK will handle the connection and disconnection of the socket. Each subscription must have a unique identifier, which is used to identify the subscription when unsubscribing.

import { DecoratedTickItem } from '@tradrapi/trading-sdk';
 
const ethUsdCallback = (tick: DecoratedTickItem) => {
  console.log(tick);
};
 
const netflixCallback = (tick: DecoratedTickItem) => {
  console.log(tick);
};
 
// Subscribe
tradrAPI.socket.subscribeTicks(['ETHUSD'], 'default_spreads', ethUsdCallback, 'ethusd-subscription-id');
sdk.socket.subscribeTicks(['NFLX'], 'default_spreads', netflixCallback, 'netflix-subscription-id');
 
// Unsubscribe
tradrAPI.socket.unSubscribeTicks('ethusd-subscription-id');
tradrAPI.socket.unSubscribeTicks('netflix-subscription-id');

Bars/Charts

Access to the charts stream is provided by the subscribeCharts method. This method allows you to subscribe to a single instrument and receive charting updates for it.

import { DecoratedCandleItem, BarTimeframe } from '@tradrapi/trading-sdk';
 
const latestCandle = {
  time: 1692258704,
  open: 120.21,
  close: 120.56,
  low: 120.21,
  high: 120.60,
  volume: 232202
}
 
tradrAPI.socket.subscribeCharts(
  'NFLX',
  'default_spreads',
  latestCandle,
  BarTimeframe.Value1M,
  (candle: DecoratedCandleItem) => {
    console.log(candle);
  },
  'netflix-chart-subscription-id'
);

The subscription model for candles works in a similar manner to the ticks subscription. Multiple subscriptions can be made at the same time, and the SDK will handle the connection and disconnection of the socket. Each subscription must have a callback function assigned to it.

⚠️

Please note a current limitation of the API is the requirement of the latest candle to be provided when subscribing. This requirement is cumbersome, is already depricated and will be removed in future releases.

Unsubscribing from a candle subscription is done by providing the subscription ID.

import { DecoratedCandleItem, BarTimeframe } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.unSubscribeCharts('netflix-chart-subscription-id');

Trades

Trade updates are received through the subscribeTrades method. This method allows you to fetch trade information for a particular account. The information provided will be relevant to all accounts which are held by the user which is authenticated.

import { DecoratedTradeItem } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.subscribeTrades((trade: DecoratedTradeItem) => {
  console.log(trade);
});

It is possible to restrict updates to a single account belonging to the user by providing the account ID as a parameter.

import { DecoratedTradeItem } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.subscribeTrades((trade: DecoratedTradeItem) => {
  console.log(trade);
}, 'my-callback-id', 1012202021);

Balances

Balance updates are received through the subscribeBalances method. This method allows you to fetch balance information for a particular user. The information provided will be relevant to all accounts which are held by the user which is authenticated.

import { DecoratedBalanceItem } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.subscribeBalances((balance: DecoratedBalanceItem) => {
  console.log(balance);
});
⚠️

Please note a current limitation of Balance related subscriptions is that they are not yet sent when a balance change occurs but rather on a fixed polling interval. This will be changed in future releases.

It is possible to restrict updates to a single account belonging to the user by providing the account ID as a parameter.

import { DecoratedBalanceItem } from '@tradrapi/trading-sdk';
 
tradrAPI.socket.subscribeBalances((balance: DecoratedTradeItem) => {
  console.log(balance);
}, 'my-callback-id', 1012202021);