LIVE
connecting to the live quote stream…
--:--:--UTC
mediumML○
#21

Triple-barrier labels

mediumMLacceptance —~30 min

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.

example 1
input
closes=[100.0, 101.0, 103.0, 106.0, 104.0], i=0, up=0.05, down=0.03, horizon=5
output
1
example 2
input
closes=[100.0, 101.0, 102.0, 101.5, 103.0, 106.0], i=0, up=0.05, down=0.03, horizon=3
output
0
note

The profit barrier is only touched on bar 5 — after the 3-bar horizon.

constraints
  • 0 ≤ i < len(closes) ≤ 10_000
  • 0 < up, down ≤ 1
  • 1 ≤ horizon ≤ 60
teacher · 3 hints · the first is free
↗ read the theory
the chapter this problem is cut from
▶ open in the workbench
run the same math on a real tape
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to triple_barrier()
closes=[100.0, 101.0, 103.0, 106.0, 104.0], i=0, up=0.05, down=0.03, horizon=5
expected
1

the judge calls triple_barrier() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice