fix: correct short-side tie-break semantics in triple barrier
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user