OrderTicket
OrderTicket
At a glance — An opaque correlation handle returned by OrderApi methods such as marketBuy, marketSell, limitBuy/limitSell, stopBuy/stopSell, closePosition and modifyPosition. It deliberately carries no price or fill information — the real state lives on ctx.position, and the outcome arrives via the asynchronous events onOrderFilled, onOrderRejected and onPositionClosed. Pass it to ctx.order.cancel() to cancel a pending order.
Signature
export interface OrderTicket { readonly id: string;}Example
import { defineStrategy, type OrderTicket } from '@nexpips/sdk-trading';
/** * Ordres en attente : poser un `limitBuy`, l'annuler (`cancel`) s'il n'est * pas rempli en quelques barres, et resserrer le stop d'une position ouverte * (`modifyPosition`). Le `limitBuy` renvoie un `OrderTicket` opaque à conserver. */export default defineStrategy({ symbol: 'EURUSD', timeframe: 'M15', risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 }, setup: () => { let pending: OrderTicket | null = null; let barsWaiting = 0;
return { onBar(ctx) { if (ctx.position.hasPendingOrder && pending) { barsWaiting += 1; if (barsWaiting > 3) { ctx.order.cancel(pending); pending = null; barsWaiting = 0; } return; }
if (ctx.position.isLong) { ctx.order.modifyPosition({ stopLoss: { type: 'pips', value: 10 } }); return; }
if (ctx.position.isFlat) { const limitPrice = ctx.bar.close - 0.001; pending = ctx.order.limitBuy(limitPrice, { riskPercent: 1, stopLoss: { type: 'pips', value: 20 }, takeProfit: { type: 'rr', value: 2 }, }); barsWaiting = 0; } }, }; },});Keep an OrderTicket to cancel the pending order later.