Skip to content

Instantly share code, notes, and snippets.

@alshdavid
Created June 11, 2026 07:19
Show Gist options
  • Select an option

  • Save alshdavid/3535031e81a82ad359d568bcd64baadb to your computer and use it in GitHub Desktop.

Select an option

Save alshdavid/3535031e81a82ad359d568bcd64baadb to your computer and use it in GitHub Desktop.
// Runs on every candle close
export default async function handler(ctx) {
// Calculate EMAs
const ema20Builder = new ExponentialMovingAverage(20);
const ema9Builder = new ExponentialMovingAverage(9);
for (const trendbar of ctx.series) {
ema20Builder.next(trendbar.close);
ema9Builder.next(trendbar.close);
}
const ema20 = ema20Builder.calculate()
const ema9 = ema9Builder.calculate()
// Get EMA for current and last candles
const current_ema20 = ema20[ema20.length - 1];
const current_ema9 = ema9[ema9.length - 1];
const prev_ema20 = ema20[ema20.length - 2];
const prev_ema9 = ema9[ema9.length - 2];
console.info(`EMA9: ${current_ema9} > EMA20: ${current_ema20}`);
// Calculate cross
const crossed_above = prev_ema9 <= prev_ema20 && current_ema9 > current_ema20;
const crossed_below = prev_ema9 >= prev_ema20 && current_ema9 < current_ema20;
// Execute trade
if (crossed_above)
{
console.log(`๐Ÿš€ BULLISH CROSSOVER!`);
ctx.close_all_positions();
ctx.new_order({
ctid_trader_account_id: ctx.account_id,
symbol_id: ctx.symbol.symbol_id,
order_type: ORDER_TYPE.Market,
trade_side: TRADE_SIDE.Buy,
volume: 100,
});
}
else if (crossed_below)
{
console.log(`๐Ÿ“‰ BEARISH CROSSOVER!`);
ctx.close_all_positions();
ctx.new_order({
ctid_trader_account_id: ctx.account_id,
symbol_id: ctx.symbol.symbol_id,
order_type: ORDER_TYPE.Market,
trade_side: TRADE_SIDE.Sell,
volume: 100,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment