Manage open and closed trades: list, get, close, set dependent orders.
List trades with optional filtering.
list(
accountID: string,
queryParams: {
ids?: string[];
state?: 'OPEN' | 'CLOSED' | 'ALL';
instrument?: string;
count?: number;
beforeID?: string;
},
responseHandler: (response: Response) => void
): void;Response: { trades: Trade[], lastTransactionID: string }
List all open trades.
listOpen(accountID: string, responseHandler: (response: Response) => void): void;Response: { trades: Trade[], lastTransactionID: string }
Get specific trade details.
get(accountID: string, tradeSpecifier: string, responseHandler: (response: Response) => void): void;Response: { trade: Trade, lastTransactionID: string }
Close a trade fully or partially.
close(
accountID: string,
tradeSpecifier: string,
bodyParams: { units?: string },
responseHandler: (response: Response) => void
): void;Response: { orderCreateTransaction: Transaction, orderFillTransaction: Transaction, lastTransactionID: string }
Example:
// Close entire trade
ctx.trade.close(accountID, tradeID, {}, callback);
// Close partial
ctx.trade.close(accountID, tradeID, { units: '5000' }, callback);Update client extensions on a trade.
setClientExtensions(
accountID: string,
tradeSpecifier: string,
bodyParams: {
clientExtensions: {
id?: string;
tag?: string;
comment?: string;
}
},
responseHandler: (response: Response) => void
): void;Create, replace, or cancel dependent orders (TP/SL/TSL).
setDependentOrders(
accountID: string,
tradeSpecifier: string,
bodyParams: {
takeProfit?: {
price: string;
timeInForce?: string;
gtdTime?: string;
clientExtensions?: ClientExtensions;
} | null;
stopLoss?: {
price?: string;
distance?: string;
timeInForce?: string;
gtdTime?: string;
guaranteed?: boolean;
clientExtensions?: ClientExtensions;
} | null;
trailingStopLoss?: {
distance: string;
timeInForce?: string;
gtdTime?: string;
clientExtensions?: ClientExtensions;
} | null;
},
responseHandler: (response: Response) => void
): void;Example:
// Add TP and SL
ctx.trade.setDependentOrders(accountID, tradeID, {
takeProfit: { price: '1.1500', timeInForce: 'GTC' },
stopLoss: { price: '1.1400', timeInForce: 'GTC' }
}, callback);
// Cancel all dependent orders
ctx.trade.setDependentOrders(accountID, tradeID, {
takeProfit: null,
stopLoss: null,
trailingStopLoss: null
}, callback);Complete trade representation.
class Trade {
id: string;
instrument: string;
price: string;
openTime: string;
state: 'OPEN' | 'CLOSED';
initialUnits: string;
initialMarginRequired: string;
currentUnits: string;
realizedPL: string;
unrealizedPL: string;
marginUsed: string;
averageClosePrice: string;
closingTransactionIDs: string[];
financing: string;
closeTime?: string;
clientExtensions: ClientExtensions;
takeProfitOrder?: TakeProfitOrder;
stopLossOrder?: StopLossOrder;
trailingStopLossOrder?: TrailingStopLossOrder;
summary(): string;
name(): string;
}Summarized trade with order IDs instead of full objects.
class TradeSummary {
// Same as Trade except:
takeProfitOrderID?: string;
stopLossOrderID?: string;
trailingStopLossOrderID?: string;
}Real-time calculated trade metrics.
class CalculatedTradeState {
id: string;
unrealizedPL: string;
marginUsed: string;
}ctx.trade.close(accountID, tradeID, {}, (response) => {
if (response.isSuccess()) {
console.log('Trade closed');
} else if (response.statusCode === '400') {
console.error('Invalid request:', response.body.errorMessage);
} else if (response.statusCode === '404') {
console.error('Trade not found');
}
});