Triple-barrier labels
Implement triple_barrier(closes, i, up, down, horizon) — the triple-barrier label for the bar at index i. Walk forward through bars i+1 .. i+horizon (never past the end of the series): return 1 the first time a close is at or above closes[i]·(1 + up), -1 the first time a close is at or below closes[i]·(1 - down), and 0 if neither barrier is touched before the horizon runs out.
This is how de Prado labels a bar for a classifier: the profit-taking barrier, the stop-loss barrier and the vertical time barrier. The workbench's triple-barrier model is trained on exactly these labels. If both barriers are touched on the same bar, the stop wins.
closes=[100.0, 101.0, 103.0, 106.0, 104.0], i=0, up=0.05, down=0.03, horizon=5
1
closes=[100.0, 101.0, 102.0, 101.5, 103.0, 106.0], i=0, up=0.05, down=0.03, horizon=3
0
The profit barrier is only touched on bar 5 — after the 3-bar horizon.
- 0 ≤ i < len(closes) ≤ 10_000
- 0 < up, down ≤ 1
- 1 ≤ horizon ≤ 60
closes=[100.0, 101.0, 103.0, 106.0, 104.0], i=0, up=0.05, down=0.03, horizon=5
1
the judge calls triple_barrier() with exactly these inputs · outputs are compared to 6 decimal places