Skip to content

crossedOver

crossedOver

At a glancetrue when series a crossed above 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 crossedOver(a: Series, b: Series | number): boolean;

Example

indicators/ema-crossover
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');
}
},
};
},
});

Golden cross of a fast and slow EMA.

See also