Skip to content

BarContext

BarContext

At a glance — Passed to onBar() on each closed bar. It bundles everything a strategy needs to react to the latest candle: the closed bar, the historical series, the current position and account state, the order API to place trades, plus clock and log. TickContext extends it for intrabar onTick() handling.

Signature

export interface BarContext {
readonly bar: Bar;
readonly series: MarketSeries;
readonly position: PositionReader;
readonly account: AccountReader;
readonly order: OrderApi;
readonly clock: ClockReader;
readonly log: LogApi;
}

Example

order/market-buy-simple
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 },
});
},
}),
});

onBar() reads a BarContext and places an order.

See also