[BACKTEST-MODEL] TKT-P1-003 Triple Barrier 라벨러 구현 #286

Merged
agentson merged 2 commits from feature/issue-tkt-p1-003-triple-barrier-labeler into feature/v3-session-policy-stream 2026-02-27 00:47:37 +09:00
2 changed files with 44 additions and 4 deletions
Showing only changes of commit 9f64c9944a - Show all commits

View File

@@ -80,11 +80,11 @@ def label_with_triple_barrier(
if up_touch and down_touch:
if spec.tie_break == "stop_first":
touched = "stop_loss" if side == 1 else "take_profit"
label = -1 if side == 1 else 1
touched = "stop_loss"
label = -1
else:
touched = "take_profit" if side == 1 else "stop_loss"
label = 1 if side == 1 else -1
touched = "take_profit"
label = 1
elif up_touch:
touched = "take_profit" if side == 1 else "stop_loss"
label = 1 if side == 1 else -1

View File

@@ -89,3 +89,43 @@ def test_short_side_inverts_barrier_semantics() -> None:
)
assert out.label == 1
assert out.touched == "take_profit"
def test_short_tie_break_modes() -> None:
highs = [100, 101.1]
lows = [100, 97.9]
closes = [100, 100]
stop_first = TripleBarrierSpec(
take_profit_pct=0.02,
stop_loss_pct=0.01,
max_holding_bars=1,
tie_break="stop_first",
)
out_stop = label_with_triple_barrier(
highs=highs,
lows=lows,
closes=closes,
entry_index=0,
side=-1,
spec=stop_first,
)
assert out_stop.label == -1
assert out_stop.touched == "stop_loss"
take_first = TripleBarrierSpec(
take_profit_pct=0.02,
stop_loss_pct=0.01,
max_holding_bars=1,
tie_break="take_first",
)
out_take = label_with_triple_barrier(
highs=highs,
lows=lows,
closes=closes,
entry_index=0,
side=-1,
spec=take_first,
)
assert out_take.label == 1
assert out_take.touched == "take_profit"