Skip to main content

Mark where two EMAs cross

This indicator draws a fast and a slow exponential moving average over the price, with a triangle under each bar where the fast one crosses above the slow one and over each bar where it crosses below.

You need a running Taji and a way to add an indicator to a chart: ask your connected agent to write and bind this source, or make the write and bind calls yourself as Your first indicator does.

ema_cross.ts
import { indicator, input, plot, ta } from "@taji/indicators";

export default indicator({
name: "EMA cross",
pane: "price",
inputs: { fast_length: input.int(9, { min: 1 }), slow_length: input.int(21, { min: 1 }) },
plots: {
fast: plot.line({ title: "Fast EMA", color: "#2962ff" }),
slow: plot.line({ title: "Slow EMA", color: "#ff9800" }),
up: plot.marker({ shape: "triangle-up", at: "below" }),
down: plot.marker({ shape: "triangle-down", at: "above" }),
},
setup: ({ inputs }) => ({
fast: ta.ema(inputs.fast_length),
slow: ta.ema(inputs.slow_length),
up: ta.crossUp(),
down: ta.crossDown(),
}),
next(bar, s) {
const fast = s.fast.next(bar.close);
const slow = s.slow.next(bar.close);
const up = s.up.next(fast, slow);
const down = s.down.next(fast, slow);
return { fast, slow, up: up ? bar.low : NaN, down: down ? bar.high : NaN };
},
});

A daily SPY chart with a blue 9-bar EMA and an orange 21-bar EMA over the candles, a triangle under each bar where the blue crosses above the orange and over each bar where it crosses below

  1. import { indicator, input, plot, ta } takes everything from @taji/indicators, the only module an indicator may import.
  2. pane: "price" draws the plots over the bars, on the price scale, instead of in a pane of their own.
  3. inputs declares the two lengths, 9 and 21 by default, each a whole number from 1 up. They are named fast_length and slow_length because inputs and plots share one namespace, and the plots already use fast and slow.
  4. plots declares two lines and two markers. A marker draws its shape on each bar where next returns a number for it, and nothing where it returns NaN.
  5. setup creates the state once, before the first bar: an EMA (ta.ema) per line and a cross detector (ta.crossUp, ta.crossDown) per direction. A ta handle is created only here, because the kernel keeps its state and rolls it back on each tick of the forming bar.
  6. next runs on every bar. It feeds the close to both EMAs, asks each detector whether fast crossed slow on this bar, and returns the bar's low for an up cross and its high for a down cross, so the triangle sits just outside the candle.

The kernel serves the indicator's rows, one per bar. These are ten of them, the last one the latest cross on SPY's daily bars (the example's rows.jq picks them):

[
{
"start_dt": "2026-07-20T00:00:00+00:00",
"fast": 116.41174967460888,
"slow": 117.402366719245,
"up": null,
"down": null
},
{
"start_dt": "2026-07-21T00:00:00+00:00",
"fast": 116.7373997396871,
"slow": 117.46033338113182,
"up": null,
"down": null
},
{
"start_dt": "2026-07-22T00:00:00+00:00",
"fast": 117.19791979174968,
"slow": 117.60393943739257,
"up": null,
"down": null
},
{
"start_dt": "2026-07-23T00:00:00+00:00",
"fast": 117.17633583339975,
"slow": 117.55721767035688,
"up": null,
"down": null
},
{
"start_dt": "2026-07-24T00:00:00+00:00",
"fast": 117.0510686667198,
"slow": 117.46565242759716,
"up": null,
"down": null
},
{
"start_dt": "2026-07-27T00:00:00+00:00",
"fast": 116.99285493337584,
"slow": 117.40150220690651,
"up": null,
"down": null
},
{
"start_dt": "2026-07-28T00:00:00+00:00",
"fast": 117.08828394670067,
"slow": 117.40772927900592,
"up": null,
"down": null
},
{
"start_dt": "2026-07-29T00:00:00+00:00",
"fast": 117.18062715736053,
"slow": 117.42066298091447,
"up": null,
"down": null
},
{
"start_dt": "2026-07-30T00:00:00+00:00",
"fast": 117.46250172588843,
"slow": 117.52696634628589,
"up": null,
"down": null
},
{
"start_dt": "2026-07-31T00:00:00+00:00",
"fast": 117.71200138071075,
"slow": 117.6345148602599,
"up": 118.17,
"down": null
}
]

Captured 2026-12-04 on a headless kernel reading a made-up daily series for SPY.

  • fast and slow are each EMA's value at the bar's close. In the first nine rows fast stays under slow, the gap narrowing.
  • up and down are null on a bar with no cross. On a crossing bar, one of them holds the price where its triangle is drawn.
  • The last row, 2026-07-31, is the cross: fast (117.71) is now above slow (117.63), having been under on the bar before, so up holds that bar's low, 118.17, and the triangle is drawn under the candle.

Change it​

Use simple moving averages instead of exponential ones:

setup: ({ inputs }) => ({
- fast: ta.ema(inputs.fast_length),
- slow: ta.ema(inputs.slow_length),
+ fast: ta.sma(inputs.fast_length),
+ slow: ta.sma(inputs.slow_length),

Mark the 50- and 200-bar crosses, which many traders call the golden cross and the death cross:

- inputs: { fast_length: input.int(9, { min: 1 }), slow_length: input.int(21, { min: 1 }) },
+ inputs: { fast_length: input.int(50, { min: 1 }), slow_length: input.int(200, { min: 1 }) },

Mark where the close crosses the slow EMA, rather than the fast EMA:

- const up = s.up.next(fast, slow);
- const down = s.down.next(fast, slow);
+ const up = s.up.next(bar.close, slow);
+ const down = s.down.next(bar.close, slow);

Next​