fix(suggestion): load full 50-symbol price snapshot + exclude falling-price names from profit buckets

- load_price_snapshot picked the last snapshot by filename (lexicographic),
  selecting a stale 9-symbol collection over the full 50-symbol universe. Now
  picks the snapshot with the latest source.retrieved_at.
- allocate_capital profit buckets now also require momentum > 0 (a falling-price
  name is not 'ทำกำไร'), while momentum/theme_signal stay Optional so the PIT
  backtest path (which doesn't provide them) still allocates.
- Suggestion now allocates across all 50 SET50 names (B1: BGRIM,TTB; B2: BANPU;
  B3: ADVANC,SCB,LH).
- Regression tests for both. Full suite 374 green.
This commit is contained in:
Kunthawat Greethong committed 2026-08-31 10:56:22 +07:00
1 parent 576d9e31ec
commit 4c32e2b737
3 files changed
+89 -14

No files matched your search

+44
View File
@@ -130,6 +130,50 @@ class MomentumSelectionTest(unittest.TestCase):
b2 = [o.symbol for o in res.orders if o.bucket == 2]
self.assertEqual(b2, ["ND_UP"]) # rising price chosen over falling
def test_negative_momentum_excluded_from_profit_buckets(self):
# A rising-price name wins the profit bucket; a FALLING-price name —
# even with a positive theme — must NOT be picked as "ทำกำไร".
cands = [
self._cand("UP", 10.0, 5.0, False, 0.0, +1.5, 0.6), # rising, non-div
self._cand("DOWN", 10.0, 5.0, False, 0.0, -1.5, 0.6), # falling, same theme
]
res = allocate_capital(1_000_000, cands)
b2 = [o.symbol for o in res.orders if o.bucket == 2]
self.assertEqual(b2, ["UP"]) # DOWN (falling price) excluded
self.assertNotIn("DOWN", b2)
class PriceSnapshotTest(unittest.TestCase):
"""Regression: load_price_snapshot must pick the snapshot retrieved MOST
RECENTLY, not the last filename lexicographically (a partial 9-symbol file
can sort after a full 50-symbol one and silently drop the universe)."""
def test_load_picks_most_recently_retrieved(self):
import json, tempfile
from pathlib import Path
from unittest import mock
from app import simulation as sim
def _snap(symbols, retrieved_at):
return {
"schema_version": 1,
"source": {"retrieved_at": retrieved_at, "period_start": "2024-01-01", "period_end": "2026-08-29"},
"series": {s: {"bars": [{"date": "2026-08-29", "adjusted_close": 10.0}]} for s in symbols},
}
with tempfile.TemporaryDirectory() as td:
snap_dir = Path(td) / "snapshots"
snap_dir.mkdir()
# name that sorts LAST lexicographically, but is OLD (partial 9)
old_partial = snap_dir / "prices-yahoo-chart-2024-01-01-2026-08-24-zzz.json"
old_partial.write_text(json.dumps(_snap(["A", "B"], "2026-08-23T00:00:00+00:00")))
# full 50-symbol, retrieved MORE recently — should win
full = snap_dir / "prices-yahoo-chart-2023-08-28-2026-08-29-aaa.json"
full.write_text(json.dumps(_snap([f"S{i}" for i in range(50)], "2026-08-30T00:00:00+00:00")))
with mock.patch.object(sim, "_PRICES_DIR", Path(td)):
series = sim.load_price_snapshot()
self.assertEqual(len(series), 50) # the full universe, not the 9
if __name__ == "__main__":
unittest.main()