crossedUnder
crossedUnder
At a glance — true when series a crossed below b on the current
bar: a was ≥ b one bar ago and a < b now. b can be another Series or
a constant number. Requires a.length ≥ 2 (and b.length ≥ 2 when b is a
series); returns false during warm-up instead of throwing.
Signature
function crossedUnder(a: Series, b: Series | number): boolean;Example
import { crossedOver, crossedUnder, defineStrategy, EMA } from '@nexpips/sdk-trading';
/** * Croisement de moyennes exponentielles. Golden cross (EMA rapide passe * au-dessus de la lente) → entrée ; death cross → sortie. Montre `EMA`, * `crossedOver` et `crossedUnder`. */export default defineStrategy({ symbol: 'EURUSD', timeframe: 'H1', risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 }, setup: (api) => { const fast = api.use(EMA, { source: 'close', period: 12 }); const slow = api.use(EMA, { source: 'close', period: 26 });
return { onBar(ctx) { if (ctx.position.isFlat && !ctx.position.hasPendingOrder && crossedOver(fast, slow)) { ctx.order.marketBuy({ riskPercent: 1, stopLoss: { type: 'atr', period: 14, multiple: 2 }, takeProfit: { type: 'rr', value: 2 }, }); return; }
if (ctx.position.isLong && crossedUnder(fast, slow)) { ctx.order.closePosition('ema death cross'); } }, }; },});Death cross closes the position.
See also
crossedOver— the upward counterpartSeries·EMA