PositionReader
PositionReader
The single source of truth for your current position, exposed as ctx.position. Read flags like isFlat, isLong, isShort, and hasPendingOrder to branch your logic, and fields such as size, entryPrice, stopLoss, takeProfit, unrealizedPnl, and openTime to inspect an open trade. Everything is readonly — you change state through ctx.order, never by mutating this view.
Signature
export interface PositionReader { readonly isFlat: boolean; readonly isLong: boolean; readonly isShort: boolean; readonly hasPendingOrder: boolean; readonly size: number; readonly entryPrice: number | null; readonly stopLoss: number | null; readonly takeProfit: number | null; readonly unrealizedPnl: number; readonly openTime: number | null;}Example
import { defineStrategy } from '@nexpips/sdk-trading';
/** * Acheter au marché si on est flat, avec un stop-loss à 20 pips. * * Pattern : entrée propre — vérifier `hasPendingOrder` ET `isFlat` * pour éviter d'envoyer un ordre alors qu'un précédent est encore * en attente de confirmation broker. */export default defineStrategy({ symbol: 'EURUSD', timeframe: 'H1', risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5, }, setup: () => ({ onBar(ctx) { if (!ctx.position.isFlat) return; if (ctx.position.hasPendingOrder) return;
ctx.order.marketBuy({ riskPercent: 1, stopLoss: { type: 'pips', value: 20 }, takeProfit: { type: 'rr', value: 2 }, }); }, }),});Check ctx.position before opening a trade.