alphax/tests/test_trade_math.py
2026-05-31 19:00:46 +08:00

31 lines
1.0 KiB
Python

import pytest
from app.core.trade_math import (
close_price,
open_price,
pnl_pct,
should_hit_trailing_stop,
should_stop_loss,
should_take_profit,
tighter_stop,
)
def test_trade_math_is_side_aware_for_short():
assert open_price("short", 100, 0.1) == pytest.approx(99.9)
assert close_price("short", 90, 0.1) == pytest.approx(90.09)
assert pnl_pct("short", 100, 90) == pytest.approx(11.1111)
assert should_stop_loss("short", 106, 105) is True
assert should_take_profit("short", 94, 95) is True
assert should_hit_trailing_stop("short", 98, 97.5) is True
assert tighter_stop("short", 99, 97.5) == pytest.approx(97.5)
def test_trade_math_keeps_long_behavior():
assert open_price("long", 100, 0.1) == pytest.approx(100.1)
assert close_price("long", 110, 0.1) == pytest.approx(109.89)
assert pnl_pct("long", 100, 110) == pytest.approx(10)
assert should_stop_loss("long", 94, 95) is True
assert should_take_profit("long", 106, 105) is True
assert tighter_stop("long", 99, 101) == pytest.approx(101)