> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xarchive.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Trend bars

> Candles coloured by trend, with a label where the close flips across the EMA.

<Frame caption="BTC on 1-hour bars in the 0xArchive Terminal, 2026-10-15.">
  <img src="https://mintcdn.com/0xarchive-e895b8e7/_p6Z1TtSEb5DBnPX/images/terminal/examples/trend-bars.webp?fit=max&auto=format&n=_p6Z1TtSEb5DBnPX&q=85&s=8f851d9c4c19206718ffb0981923bdd4" alt="Trend bars on a BTC chart" width="1038" height="664" data-path="images/terminal/examples/trend-bars.webp" />
</Frame>

<Card title="Open on a chart" icon="chart-candlestick" href="https://terminal.0xarchive.io/?symbol=BTC&interval=1h&scripts=example%3Atrend-bars" horizontal>
  Load it on BTC 1h in the terminal, then change its settings or open it in the script editor.
</Card>

Candles in the up colour while the close holds above a rising EMA, the down colour below a falling one, and grey in between. A label marks a flip of the close across the EMA that holds for two bars, and a dot marks a bar twice as wide as the recent average.

## How it works

`plot.barColor` recolours the chart's own candles. The flip labels are set on the second bar of a flip and drawn one bar back with `offset: -1`, so they sit on the bar where the flip began without looking ahead.

## The script

```ts trend-bars.ts theme={"theme":"github-dark"}
// Candles coloured by trend, as Pine's barcolor() colours them: the up colour while the close
// holds above a rising EMA, the down colour below a falling one, grey in between. A label marks
// a flip of the close across the EMA that holds for two bars, drawn on the first of them, and
// no sooner than a set number of bars after the last label, so chop does not stack them. A dot
// marks a bar twice as wide as the recent average.
indicator('Trend bars', { overlay: true });

const length = input.int('EMA length', 34, { min: 2 });
// How labels are spaced is not what the script computes, so it stays out of the legend name.
const gap = input.int('Bars between labels', 10, { min: 1, legend: false });

const chart = bars();
const ema = ta.ema(length);
const rising = ta.rising(1);
const falling = ta.falling(1);
const range = ta.sma(20);
// Which side of the EMA the close has held, the side it was on one bar ago, and bars since a label.
const held = state.bar<number>(0);
const before = state.bar<number>(0);
const quiet = state.bar<number>(1e9);

const line = plot.line('EMA', { width: 2 });
const trend = plot.barColor('Trend');
// Set on the second bar of a flip, drawn one bar back on its first.
const up = plot.shape('Flips up', { shape: 'labelUp', location: 'below', color: color.up, text: 'Up', offset: -1 });
const down = plot.shape('Flips down', { shape: 'labelDown', location: 'above', color: color.down, text: 'Down', offset: -1 });
const wide = plot.shape('Wide bar', { char: '•', location: 'above', color: color.neutral, size: 'normal' });

chart.onBar(() => {
  const e = ema.update(chart.close);
  const isRising = rising.update(e);
  const isFalling = falling.update(e);
  const average = range.update(chart.high - chart.low);
  line.set(e);
  if (isNa(e)) trend.set(null);
  else if (chart.close > e && isRising) trend.set(color.up);
  else if (chart.close < e && isFalling) trend.set(color.down);
  else trend.set(color.withAlpha(color.neutral, 0.55));

  const side = isNa(e) ? 0 : chart.close > e ? 1 : chart.close < e ? -1 : 0;
  const flipped = side !== 0 && side === before.value && side !== held.value;
  const labelled = flipped && held.value !== 0 && quiet.value >= gap;
  up.set(labelled && side > 0);
  down.set(labelled && side < 0);
  if (flipped) held.value = side;
  quiet.value = labelled ? 1 : quiet.value + 1;
  before.value = side;
  wide.set(chart.high - chart.low > 2 * average);
});
```

## Next step

See the next example, [Session table](/terminal/examples/session-table).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.