Balances
Each account (irrelevant of which trading platform it exists on) has a balance. Balances may be retrieved and updated via the REST API. Balance updates may also be listened for via the WebSocket API.
Get Balance
To fetch a balance via the SDK you must provide the account ID for the account you wish to retrieve the balance for.
For example:
import { Balance } from '@tradrapi/trading-sdk';
const balance: Balance = await tradrApi.balances.get(11012232);
Response
The response will be a Balance object as follows:
interface Balance {
/** The currency in which these values are denominated */
currency: SupportedCurrency;
/** The tradrAPI account id to which this balance belongs */
accountId: number;
/** The total balance for the account */
balance: number;
/** The total equity for the account */
equity: number;
/** The maximum amount of funds which can be withdrawn from the account */
withdrawable: number;
/** The total credit (bonus money) available for the account */
credit: number;
/** The total money borrowed from the broker to purchase all investments */
margin: number;
/** The total equity not reserved for margin or open positions, and which is available to be used to open new trades. */
freeMargin: number;
/** The net profit or loss on open positions. */
netPnl?: number;
/** The total lifetime profit or loss of the account from the user perspective */
lifetimePnl?: number;
}
ℹ️️
For MT4 & 5 the withdrawable
property of the Balance is defined as the freeMargin
minus credit
.
Fetching multiple balances at once
It is possible to fetch multiple balances at once by providing an array of account IDs to the getMany
method. The
accounts do not need to belong to the same user or exist on the same trading platform.
import { Balance } from '@tradrapi/trading-sdk';
const balance: Balance[] = await tradrApi.balances.getMany([1010001, 1010002, 1010003]);