Promote production portfolio strategy

This commit is contained in:
2026-07-04 07:48:38 +02:00
parent 66ef0564c1
commit 5f2d108227
22 changed files with 1677 additions and 132 deletions
+64 -4
View File
@@ -204,13 +204,48 @@ class TestTrailingClose:
assert svc._trailing_close("long", 100.0, 95.0, 0.12, bars) is None
class TestAtrTrailingClose:
def test_long_uses_ratchet_on_next_bar(self, monkeypatch):
monkeypatch.setattr(svc, "compute_atr", lambda *_args, **_kwargs: {"atr": 5.0})
rows = [
_r(date(2026, 1, 1), 100, 100, 100, 100),
_r(date(2026, 1, 2), 115, 121, 114, 120),
_r(date(2026, 1, 3), 106, 107, 103, 104),
]
hit = svc._atr_trailing_close(
"long", 100.0, 95.0, 3.0, 30, rows, date(2026, 1, 1)
)
assert hit is not None
price, when, reason = hit
assert price == pytest.approx(105.0)
assert when == date(2026, 1, 3)
assert reason == "trailing"
def test_max_hold_still_closes(self, monkeypatch):
monkeypatch.setattr(svc, "compute_atr", lambda *_args, **_kwargs: {"atr": 50.0})
rows = [
_r(date(2026, 1, 1), 100, 100, 100, 100),
_r(date(2026, 1, 2), 101, 102, 100, 101),
_r(date(2026, 1, 3), 102, 103, 101, 102),
]
assert svc._atr_trailing_close(
"long", 100.0, 95.0, 3.0, 2, rows, date(2026, 1, 1)
) == (102.0, date(2026, 1, 3), "time")
async def test_exit_policy_defaults_and_round_trip(session):
# Default: the backtest-validated hold-to-horizon exit.
# Default: the promoted production exit.
assert await svc.get_exit_policy(session) == {
"mode": "time", "trailing_pct": 12.0, "hold_days": 30,
"mode": "atr_trailing", "trailing_pct": 12.0,
"atr_multiplier": 3.0, "hold_days": 30,
}
updated = await svc.set_exit_policy(
session, mode="target", trailing_pct=15.0, atr_multiplier=2.5, hold_days=21
)
assert updated == {
"mode": "target", "trailing_pct": 15.0,
"atr_multiplier": 2.5, "hold_days": 21,
}
updated = await svc.set_exit_policy(session, mode="target", trailing_pct=15.0, hold_days=21)
assert updated == {"mode": "target", "trailing_pct": 15.0, "hold_days": 21}
assert (await svc.get_exit_policy(session))["mode"] == "target"
@@ -219,6 +254,8 @@ async def test_exit_policy_rejects_bad_input(session):
await svc.set_exit_policy(session, mode="bogus")
with pytest.raises(ValidationError):
await svc.set_exit_policy(session, trailing_pct=200.0)
with pytest.raises(ValidationError):
await svc.set_exit_policy(session, atr_multiplier=20.0)
with pytest.raises(ValidationError):
await svc.set_exit_policy(session, hold_days=1)
@@ -284,6 +321,18 @@ async def test_resolve_trailing_closes_with_reason(session):
assert closed[0]["close_reason"] == "trailing"
async def test_resolve_atr_trailing_closes_with_reason(session, monkeypatch):
monkeypatch.setattr(svc, "compute_atr", lambda *_args, **_kwargs: {"atr": 5.0})
await svc.set_exit_policy(session, mode="atr_trailing", atr_multiplier=3.0)
tid = await _seed(session, "AAA", close=100.0)
await _add_open_trade(session, tid, "long", entry=100.0, shares=10, days_ago=10)
await _add_bars(session, tid, [(121, 114), (107, 101)], start=date.today())
assert await svc.resolve_open_trades(session) == 1
closed = await svc.list_trades(session, 1, status="closed")
assert closed[0]["close_reason"] == "trailing"
assert closed[0]["current_price"] == pytest.approx(102.5)
async def test_manual_close_sets_reason(session):
await _seed(session, "AAA", close=112.0)
trade = await svc.create_trade(session, 1, symbol="AAA", direction="long",
@@ -300,3 +349,14 @@ async def test_list_open_exposes_trailing_stop(session):
row = (await svc.list_trades(session, 1, status="open"))[0]
assert row["trailing_stop"] == pytest.approx(110.0) # 125 * (1 - 0.12)
assert row["trailing_distance_pct"] is not None
async def test_list_open_exposes_atr_trailing_stop(session, monkeypatch):
monkeypatch.setattr(svc, "compute_atr", lambda *_args, **_kwargs: {"atr": 5.0})
await svc.set_exit_policy(session, mode="atr_trailing", atr_multiplier=3.0)
tid = await _seed(session, "AAA", close=120.0)
await _add_open_trade(session, tid, "long", entry=100.0, shares=10, days_ago=10)
await _add_bars(session, tid, [(125, 118)], start=date.today())
row = (await svc.list_trades(session, 1, status="open"))[0]
assert row["trailing_stop"] == pytest.approx(106.5) # latest close 121.5 - 3 * 5
assert row["trailing_distance_pct"] is not None