Trading Charts
The SDK allows the ability to consume chart data from the TradrAPI. The following example demonstrates how to retrieve a chart for a given symbol (instrument).
The charting API is meant to be used to obtain historic chart data (candles) for a given symbol. It is not meant to be used for real-time charting. For real-time charting, please use the TradrAPI Socket.
Fetching Historic Chart Data
In order to fetch historic chart data, you will need to use the charts.get()
method as follows:
import { GetCandlesDto, Bar } from '@tradrapi/trading-sdk';
const bars: Bar[] = tradrApi.charts.getBars({
timeframe: '1m',
symbol: 'ETHUSD',
startDateUTC: '2023-01-01T00:00:00.000Z',
endDateUTC: '2023-01-23:59:59.000Z',
});
The charts.getBars()
method accepts a GetCandlesDto
as a parameter, this has the following signature:
interface GetCandlesDto {
/** The timeframe to fetch the chart data for */
timeframe: '1m' | '5m' | '15m' | '30m' | '1h' | '4h' | '1d' | '1w' | '1mn';
/** The symbol to fetch the chart data for */
symbol: string;
/** The ID of the spread group to use for spreads */
spreadGroupId?: number;
/** The start date from which to get data (included) (ISO 8601) */
startDateUTC: string;
/** The start date until which to get data (included) (ISO 8601) */
endDateUTC?: string;
}
The response format for this method is a Bar[]
interface which is defined as follows:
interface Bar {
/** The symbol of the bar */
s: string;
/** The open price of the bar */
op: number;
/** The open time of the bar */
ot: number;
/** The close price of the bar */
cp: number;
/** The close time of the bar */
ct: number;
/** The high price of the bar */
h: number;
/** The low price of the bar */
l: number;
/** The volume of the bar */
v: number;
/** The timeframe of the bar */
t: string;
}
Current Active Bar
In order to fetch the active bar for a given symbol, you will need to use the charts.getActiveBar()
method as follows:
import { Bar, GetActiveCandleDto } from '@tradrapi/trading-sdk';
const bar: Bar = tradrApi.charts.getActiveBar({ symbol: 'ETHUSD' });
The charts.getActiveBar()
method accepts a GetActiveCandleDto
object as a parameter and returns a
single Bar
interface.