Backtest a 200-day reclaim on every bar the chart holds
This indicator tests one rule over every bar the chart holds: buy the close on the day it crosses back above its 200-day average, sell 20 bars later, and show the tally (trades, wins, win rate, average return, bars tested) in a card over the pane.
You need a running Taji with a daily chart and a way to add an indicator to it: ask your connected agent to write and bind this source, or make the write and bind calls yourself as Your first indicator does.
import { card, indicator, input, plot, ta } from "@taji/indicators";
export default indicator({
name: "200-day reclaim",
pane: "own",
inputs: { length: input.int(200, { min: 1 }), hold: input.int(20, { min: 1 }) },
plots: {
ret: plot.columns({ title: "Trade return %", unit: "percent", colors: ["#f23645", "#089981"] }),
avg_return: plot.line({ title: "Average return %", unit: "percent", color: "#ff9800", width: 2 }),
zero: plot.line({ title: "Zero", unit: "percent", color: "#787b86" }),
},
setup: ({ inputs }) => ({
average: ta.sma(inputs.length),
reclaim: ta.crossUp(),
open: [] as { index: number; entry: number }[],
trades: 0,
wins: 0,
total: 0,
}),
next(bar, s, { inputs, ctx }) {
const average = s.average.next(bar.close);
if (s.reclaim.next(bar.close, average)) s.open.push({ index: ctx.index, entry: bar.close });
let ret = NaN;
if (s.open.length > 0 && ctx.index - s.open[0].index === inputs.hold) {
ret = (bar.close / s.open.shift()!.entry - 1) * 100;
s.trades += 1;
if (ret > 0) s.wins += 1;
s.total += ret;
}
const avg_return = s.trades > 0 ? s.total / s.trades : NaN;
if (ctx.last) {
card("top_left", {
caption: `buy the close above its ${inputs.length}-bar average, sell ${inputs.hold} bars later`,
values: [
{ label: "Trades", value: s.trades, unit: "count" },
{ label: "Wins", value: s.wins, unit: "count" },
{ label: "Win rate", value: (100 * s.wins) / s.trades, unit: "percent" },
{ label: "Avg return", value: avg_return, unit: "percent", signed: true },
{ label: "Bars tested", value: ctx.index + 1, unit: "count" },
],
});
}
return { ret, avg_return, zero: 0, colors: { ret: ret > 0 ? 1 : 0 } };
},
});

import { card, indicator, input, plot, ta }takes everything from@taji/indicators, the only module an indicator may import.pane: "own"gives the results a pane of their own: they are percentages, not prices.inputsdeclareslength, the average's length, 200 by default, andhold, how many bars a trade is held, 20 by default. The legend prints them after the name,200-day reclaim 200 20.plotsdeclares three, all in percent (unit: "percent", so the axis and legend print-1.11%):ret, a column on the bar a trade closes, at that trade's return, green for a win and red for a loss;avg_return, the running average return of every trade closed so far; andzero, a flat line at 0 so you see at a glance which side of it the average sits.setupcreates the state once, before the first bar: the 200-bar simple moving average (ta.sma), a detector (ta.crossUp) for the close crossing above it, a list of open trades, and the running totals. The list and the totals are plain data in the state, so the kernel commits them on each close and rolls them back on each tick of the forming bar.nextruns on every bar, oldest first. On a bar where the close crosses above the average, it opens a trade at that close, remembering the bar's index. On the barholdbars after the oldest open trade, it closes that trade at this bar's close and adds its return to the totals. Until the average has 200 bars it isNaN, no close crosses it, and no trade opens.carddraws the tally in the pane's top-left corner. It is filled onctx.last, the chart's latest bar, so it shows the test over every bar the chart holds. Its figures are numbers the window formats byunit:countfor the trades, wins and bars,percentfor the win rate and the average.
No look ahead. A trade's return is known only on the bar it closes, so it is returned as ret, and added to the totals, on that bar, never on the bar it opened. Each row holds only what was known at its own close: read the rows up to any date and you have the test as it stood on that date. A cross within 20 bars of another opens a second trade beside the first, and each closes on its own bar.
The kernel serves the indicator's rows, one per bar. The example's rows.jq keeps the rows on which the latest ten trades closed, then the latest bar, and numbers them from all the rows: bar is the row's place in the chart, trade counts the rows with a ret so far and wins those whose ret is above 0. ret and avg_return are the kernel's:
[
{
"start_dt": "2024-07-24T00:00:00+00:00",
"bar": 1723,
"trade": 26,
"wins": 16,
"ret": 5.41046363249682,
"avg_return": 0.11301857474214978
},
{
"start_dt": "2024-07-30T00:00:00+00:00",
"bar": 1727,
"trade": 27,
"wins": 17,
"ret": 4.521276595744683,
"avg_return": 0.2762873903348362
},
{
"start_dt": "2026-03-05T00:00:00+00:00",
"bar": 2127,
"trade": 28,
"wins": 18,
"ret": 3.3637400228050174,
"avg_return": 0.3865535557801998
},
{
"start_dt": "2026-04-17T00:00:00+00:00",
"bar": 2157,
"trade": 29,
"wins": 18,
"ret": -0.21155410903174188,
"avg_return": 0.36592915354530525
},
{
"start_dt": "2026-05-21T00:00:00+00:00",
"bar": 2181,
"trade": 30,
"wins": 18,
"ret": -1.487321196358915,
"avg_return": 0.30415414188183126
},
{
"start_dt": "2026-06-26T00:00:00+00:00",
"bar": 2205,
"trade": 31,
"wins": 18,
"ret": -2.641846854170049,
"avg_return": 0.2091218516866093
},
{
"start_dt": "2026-07-01T00:00:00+00:00",
"bar": 2208,
"trade": 32,
"wins": 18,
"ret": -5.966912945711522,
"avg_return": 0.016120764267917698
},
{
"start_dt": "2026-09-14T00:00:00+00:00",
"bar": 2259,
"trade": 33,
"wins": 19,
"ret": 1.3827524136802438,
"avg_return": 0.0575338445531397
},
{
"start_dt": "2026-09-16T00:00:00+00:00",
"bar": 2261,
"trade": 34,
"wins": 20,
"ret": 1.1967760319140375,
"avg_return": 0.09104096771081316
},
{
"start_dt": "2026-10-08T00:00:00+00:00",
"bar": 2277,
"trade": 35,
"wins": 21,
"ret": 3.2940219601464094,
"avg_return": 0.1825547103518302
},
{
"start_dt": "2026-12-03T00:00:00+00:00",
"bar": 2316,
"trade": 35,
"wins": 21,
"ret": null,
"avg_return": 0.1825547103518302
}
]
Captured 2026-12-04 on a headless kernel reading a made-up daily series for SPY.
- The latest row, 2026-12-03, is bar 2,316: the test ran over 2,316 daily bars, every bar the chart holds. It closed 35 trades, 21 of them wins, for an average 20-bar return of +0.18%: the card's Trades, Wins and Avg return. Its win rate, 21 of 35, is 60.00%.
- Each other row is a bar on which one trade closed. On 2026-10-08 the 35th trade made +3.29% and
winswent from 20 to 21; on 2026-07-01 the 32nd lost 5.97% andwinsstayed at 18. avg_returnmoves only on those rows: each trade'sretpulls it toward itself, from 0.09% to 0.18% with the 35th.- No trade closed between 2026-10-08 and 2026-12-03, so the picture has no column after October, and the last two rows hold the same counts.
Change it
Test a 50-day average instead of the 200-day:
- inputs: { length: input.int(200, { min: 1 }), hold: input.int(20, { min: 1 }) },
+ inputs: { length: input.int(50, { min: 1 }), hold: input.int(20, { min: 1 }) },
Hold each trade 60 bars, about three months:
- inputs: { length: input.int(200, { min: 1 }), hold: input.int(20, { min: 1 }) },
+ inputs: { length: input.int(200, { min: 1 }), hold: input.int(60, { min: 1 }) },
Add a 5% stop: a trade whose low reaches 5% below its entry closes on that bar, counted as a 5% loss:
- if (s.open.length > 0 && ctx.index - s.open[0].index === inputs.hold) {
- ret = (bar.close / s.open.shift()!.entry - 1) * 100;
+ for (const trade of s.open.filter((t) => t.index < ctx.index)) {
+ const stopped = bar.low <= trade.entry * 0.95;
+ if (!stopped && ctx.index - trade.index !== inputs.hold) continue;
+ ret = stopped ? -5 : (bar.close / trade.entry - 1) * 100;
+ s.open.splice(s.open.indexOf(trade), 1);
Next
- Your first indicator: write, check, bind, read and test an indicator step by step.
- How an indicator executes: why the open trades and totals live in
setup's state. - Have your agent write an indicator: ask for a rule like this one in a sentence.
Your agent can also run a test like this one over the bars Taji stores, without a chart, with reads that see only what was known at each instant: What can a read see at an instant?.