commit 3d0f572e5318f0aad108efed907515e8d14ca3fd Author: PrimeThai Automation Date: Tue Sep 29 14:08:43 2026 +0700 Initial PrimeThai trading system app diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..99d301a --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +# Copy this file to .env and replace both placeholders before starting the app. +PRIMETHAI_USERNAME=admin +PRIMETHAI_PASSWORD=CHANGE_ME +PRIMETHAI_SESSION_SECRET=CHANGE_ME +PRIMETHAI_SESSION_TTL_HOURS=168 +# auto adds Secure when the HTTPS proxy sends X-Forwarded-Proto: https. +PRIMETHAI_COOKIE_SECURE=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a5cc09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.venv/ +.venv-temp/ +.deps/ +.env +.env.* +!.env.example +__pycache__/ +*.py[cod] +data/cache/ +data/portfolio/ +data/raw/ +data/backtest_*/ +reports/ +tests/tmp*/ +tmp*/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c5bfd9 --- /dev/null +++ b/README.md @@ -0,0 +1,265 @@ +# PrimeThai + +PrimeThai is a **research, daily scanner and historical backtest toolkit** for Thai listed equities. It does not connect to a broker and cannot place live orders. Stock price downloads use Yahoo Finance through `yfinance`; symbols use Yahoo's `.BK` suffix. The SET benchmark can be imported from SiamChart's daily EOD ZIP files as `^SET.csv`. + +## Architecture + +```text +Yahoo Finance stocks / SiamChart SET archive + ↓ +Local CSV cache → dated SET universe → indicators / RS / breadth + ↓ + SET + SET100EW + MCO / MCSI + ↓ + deterministic market regime + ↓ ↓ + leader scan event backtest + ↓ + metrics / experiments / JSON +``` + +- `MarketDataProvider` isolates downloads from the rest of the engine. `YFinanceProvider` stores Yahoo OHLC, `Adj Close`, derived adjusted OHLC, dividends and splits locally. Yahoo is called with `auto_adjust=False`, `actions=True`, and `repair=True`. The simulator default assumes repaired Yahoo OHLC is on a split-adjusted share basis, and credits dividends as cash. Yahoo says `repair=True` attempts price and split repairs; inspect split dates and unusual corporate actions before trusting a run. Other providers can set the backtest's `yahoo_split_adjusted=False` option to apply split ratios explicitly. +- Indicators, regimes, candidate scores, risk sizing and backtest transitions are deterministic Python code. LLM output is a JSON handoff for later explanation only. +- `data/cache/` holds provider cache; `data/raw/` contains the working input files; `reports/` receives generated results. + +## Windows setup + +Use Python 3.11 or later. From PowerShell in this folder: + +```powershell +py -3.14 -m venv .venv +.\.venv\Scripts\Activate.ps1 +python -m pip install --upgrade pip +python -m pip install -r requirements.txt +``` + +If PowerShell blocks activation, call the environment's interpreter directly: + +```powershell +.\.venv\Scripts\python.exe -m pip install -r requirements.txt +``` + +The core dependencies are pinned in `requirements.txt` and `pyproject.toml`. `scipy` and `scikit-learn` are included because yfinance's optional price-repair path uses them when `repair=True`. + +## Download data + +The default command downloads a ten-symbol smoke universe and attempts to download `^SET.BK`: + +```powershell +python -m primethai download +``` + +### Import the SET index from SiamChart + +SiamChart's Stock page publishes two ZIP archives under “SET INDEX Historical (*.csv)”: one through the previous year and one for the current year. Each archive contains one daily CSV per trading date. Import the SET index rows into the standard local input file with: + +```powershell +python -m primethai import-siamchart --archives data/raw/siamchart/set-archive_EOD_1970-LAST.zip data/raw/siamchart/set-archive_EOD.zip --symbols ^SET +``` + +The importer accepts additional SiamChart tickers, for example `--symbols ^SET ADVANC AOT`, and writes one normalized CSV per selected ticker. The EOD archives contain raw OHLCV without action fields. Import the separate stock-info action history with: + +```powershell +python -m primethai import-siamchart-actions --symbols ADVANC AOT CPALL DELTA PTT KBANK KTB TRUE GULF SCB +``` + +This writes `data/raw/actions/SYMBOL_dividends.csv` and `SYMBOL_par_changes.csv`. Dividend dates are treated as XD dates. Backtests load those dividend sidecars automatically, credit cash only for shares held before the XD date, and include an action date even when that symbol has no OHLC row. Par-value history is saved for review and is **not** converted into a stock split automatically. Missing or unavailable stock-info pages are listed per ticker; a partial import exits with a nonzero status. + +To compare imported events against a user-reviewed SET CSV, use columns `symbol,action_type,date,value,source_url`, where `action_type` is `dividend` or `par_value`: + +```powershell +python -m primethai verify-siamchart-actions --symbols AP INTUCH --set-reference data/reference/siamchart_actions_set_reference_sample.csv +``` + +The command writes `reports/siamchart_actions_audit.csv`, including matches, missing events and value mismatches. The included reference is a two-row example, not a complete action history. A reliable audit needs a complete SET reference for the same symbols and date range. The SiamChart archives are not bundled in Git; download them from [SiamChart Stock](https://siamchart.com/stock/) and keep them under `data/raw/siamchart/`. + +### SET cash-dividend reference and action-adjusted backtests + +The official SET Rights & Benefits history is the reference for cash XD events within its available date range. Import the same dated PIT price symbols, plus any source ticker aliases, and keep the generated coverage manifest beside the CSV: + +```powershell +$symbols = Import-Csv data/universes/set100_pit_2024_2026_h1.csv | Select-Object -ExpandProperty symbol -Unique +python -m primethai import-set-actions --symbols $symbols --start 2025-01-01 --end 2026-07-01 --symbol-aliases data/reference/set_symbol_aliases.csv +python -m primethai backtest --universe SET100 --mode accurate --membership-file data/universes/set100_pit_2024_2026_h1.csv --start 2025-01-01 --end 2026-07-01 --data-dir data/backtest_2025_2026_accurate --set-reference data/reference/set_actions_2025_2026_h1.csv --output-dir reports/backtest_2025_2026_accurate_set_actions +``` + +The importer writes a `.manifest.json` file with requested price symbols, the inclusive/exclusive coverage dates, source ticker aliases, and any failed ticker requests. A backtest refuses a missing, incomplete, or symbol-incomplete manifest. Inside that verified date range it credits the SET cash-action list and suppresses other dividend fields; SiamChart-only events remain flagged in the audit and are not credited there. Outside that date range, imported SiamChart dividend sidecars remain available. Alias rows map the SET action ticker to the historical price-series ticker for cash-action reconciliation only; they do not relabel OHLCV data. Par-value rows remain review-only. + +Choose symbols and a date range explicitly: + +```powershell +python -m primethai download --symbols ADVANC AOT CPALL DELTA PTT KBANK KTB TRUE GULF SCB --start 2020-01-01 --end 2026-09-29 +``` + +The end date is exclusive, as in yfinance. Repeated requests use the local cache; use `--refresh` to fetch again. Symbols that fail are reported individually, and successful symbols are kept. The Yahoo data terms, adjustments and historical availability can change; inspect downloaded data before relying on a result. A failed or delisted symbol is never silently filled with a surviving symbol. A real-data download can be run with one command after installing dependencies: + +```powershell +python -m primethai download +``` + +## SET100, SET50, SET and mai universes + +Membership is supplied as CSV with `symbol,start_date,end_date,sector`. Dated membership intervals are used for breadth and the equal-weight series. The consolidated `data/universes/set100_pit_2024_2026_h1.csv` contains official SET snapshots from **2024-01-02 through 2026-06-30**, including dated SET100 changes during 1H 2025. Its 2024-2025 sector cells are blank; sector labels are included only for H1 2026. The original `data/universes/set100_2026_h1.csv` remains as the official H1 2026 fixture. + +The SET constituent archive lists H2 2026, but its full constituent file is not available in the public materials used here. The PIT CSV stops at June 30, 2026 and does not carry the H1 roster forward. SET50/SET/mai and custom lists use the same interface and need their own membership files for accurate historical breadth. + +### Fast versus accurate mode + +- **Fast** takes the latest member snapshot available in the file and holds it fixed through the requested price history. Every command prints a `SURVIVORSHIP BIAS` warning. Use it for development and smoke runs only, never to claim historical index performance. +- **Accurate** resolves members by effective date, clips a longer index file to the covered PIT interval and requested end, and retains covered pre-start history for indicator warm-up. It errors when the requested evaluation dates or any market-context session lack PIT membership coverage. For SET100EW / breadth / MCO history to be accurate, its PIT file must cover the warm-up window too. The consolidated SET100 file covers 2024-01-02 through 2026-06-30. + +For an accurate run from 2025 through H1 2026 (including a year of price warm-up and PIT breadth coverage): + +```powershell +python -m primethai download --universe SET100 --mode accurate --membership-file data/universes/set100_pit_2024_2026_h1.csv --start 2024-01-02 --end 2026-07-01 +``` + +Accurate mode correctly stops for dates after June 30, 2026 until a complete H2 2026 constituent history is supplied. See `data/universes/README.md` for the official sources, dated 2025 interim updates, sector coverage, and known boundaries. + +## Indicators + +Per-symbol and SET-index features include EMA21 High / Close / Low, UP/DOWN/NEUTRAL EMA structure, ABOVE/INSIDE/BELOW price state, ATR14 (14-session simple mean of true range), ADR20 (mean daily range as percent of previous close), SMA50/SMA200, fully warmed rolling 252-session high/low, 20-session average volume/value turnover, and weekly 10-period EMA High/Close/Low with weekly trend state. The ATR extension is `(close - EMA21 high) / ATR14`; the default filter blocks new entries beyond +1 ATR. + +RS is a project-owned cross-sectional percentile from 0–100. It combines 1-, 3- and 12-month excess return versus SET100EW and close proximity to the rolling 252-session high. The starting weights are 20/35/30/15 percent. Period excess-return comparisons versus both SET and SET100EW, and the raw 52-week proximity percentage, are retained for inspection. This is a **PrimeThai research assumption**, not TradersLab's proprietary RS rank or formula. + +Breadth uses advancing, declining, unchanged and net advances from effective constituents. MCO is defined here as EMA19(net advances) minus EMA39(net advances), with a rolling mean, standard deviation and z-score; MCSI is the cumulative MCO with a 10-session average and crossing/curl states. These are transparent implementation choices; breadth counts are not ratio-adjusted for universe size. MCO is timing context, not a standalone buy rule; MCSI is confirmation context. + +SET100EW is a daily-rebalanced equal-weight **price-return proxy** with base 100, computed from available member returns. It is intended as a breadth/participation companion to cap-weighted `^SET.BK`, not an official SET index and not a total-return series. + +## Market regime and scanner + +The regime state machine emits `CORRECTION`, `REPAIR`, `EARLY_UPTREND`, `CONFIRMED_UPTREND`, `UPTREND_PULLBACK`, `OVERBOUGHT` or `BREAKDOWN`, plus deterministic reasons. It combines SET structure/50DMA, SET100EW structure, MCO/MCSI and breadth. Its cutoffs and MCO repair threshold can be changed in `config/default.json`. No LLM participates in the decision. + +The leader scanner filters by price and 20-session average turnover (default THB 20m), then ranks RS, market alignment, EMA structure, confirmed higher low, ATR distance, volume behavior, tightness, available sector strength and weekly confluence. In dated mode it uses only members with a bar on the latest market session; blocked market regimes show candidates as Watch with zero proposed risk. Weights start at 25/20/15/10/10/5/5/5/5 and are configurable under `focus_weights` in `config/default.json`. Indicator warm-ups, pivot confirmation width, pullback volume window, tightness window and the 20-session base breakout window are configurable. Missing sector data is omitted and remaining weights are renormalized; no sector is guessed. Candidate entry references are research levels from the latest close; Watch-only rows show zero shares; backtest fills are at the next open. + +```powershell +python -m primethai market --universe SET100 --mode fast +python -m primethai scan --universe SET100 --mode fast --top 10 --json-out reports\qwen_candidates.json +``` + +## Backtester + +Signals from a daily close are submitted for the next session's open. The engine charges configurable commission, fees, sell tax and slippage on every fill; defaults are research assumptions, not broker quotes. Entry risk starts at 0.25% equity for weakness and 0.50% for confirmation, with a 20% notional cap per position. Quantity is rounded down by configured board lot. The default Thai lot is 100; a 50-share lot is available for eligible high-priced securities, but eligibility needs to be supplied/verified for the symbol and period. + +The engine records initial R once, takes one third at +2R (odd-lot remainder is permitted for the trim), and follows the runner with a daily close below EMA21 Low → next-open exit by default. Position sizing uses the initial EMA21 Low distance, while the actual exit follows the moving EMA21 Low at the next open; therefore initial R is a sizing reference, not a guaranteed maximum loss, and overnight gaps can exceed it. The 8% emergency max-loss check is a separate research trigger and cannot cap a gap through its price. The alternative exit mode records a structure break, then a close back into structure as a reclaim attempt, and exits next open if price breaks structure again. Portfolio heat is the remaining shares' initial risk divided by equity; the regime heat limits and portfolio cushion are saved daily. The default heat map and position cap are PrimeThai research assumptions. + +Backtest output includes total return, CAGR, drawdown, win rate, average win/loss, expectancy in cash and R, profit factor, average/median R, MAE/MFE (including actual fill prices), holding time, average invested exposure, trades/month, 2R hit rate, separate 2R trim/runner contribution, a fill ledger, and skipped-entry reasons. See `reports/backtest_2025_2026_accurate_set_actions/strategy_diagnosis.md` for the current loss analysis. + +The opt-in `strategy-overlays` research command compares the baseline with no-progress exits for weak 21DMA retests and a break-even runner floor after a 2R trim. It requires explicit dates and saves each run separately; none of these settings change the production defaults. + +```powershell +python -m primethai strategy-overlays --universe SET100 --mode accurate --membership-file data/universes/set100_pit_2024_2026_h1.csv --start 2025-01-01 --end 2026-07-01 --data-dir data/backtest_2025_2026_accurate --set-reference data/reference/set_actions_2025_2026_h1.csv --output-dir reports/strategy_overlays_2025_2026_accurate_set_actions +``` + +The result summary and research interpretation are in `reports/strategy_overlays_2025_2026_accurate_set_actions/strategy_overlays/README.md`. + +```powershell +python -m primethai backtest --universe SET100 --mode fast --start 2021-01-01 --end 2026-01-01 +``` + +Outputs are saved under `reports/`. Fast-mode results are survivorship biased. Accurate mode also requires continuous PIT membership across all market-context warm-up dates. + +### Plain EMA crossover benchmark + +Compare a no-frills trend-following rule against the research strategy. A fast EMA crossing above a slow EMA at the daily close schedules a buy for the next open; a cross below schedules a full exit for the next open. The benchmark has no market-regime, RS, stop-loss, or profit-taking rule. By default it compares EMA 10/30, 20/50 and 50/200, caps each stock at 20% of equity, spreads available cash equally across simultaneous signals, reallocates budgets that cannot buy a minimum board lot, applies only the configured minimum-price and 20-session average-turnover filters, and uses the same Thai board lots, costs, PIT membership and cash-dividend handling as the main backtest. + +```powershell +python -m primethai ema-crossover --universe SET100 --mode accurate --membership-file data/universes/set100_pit_2024_2026_h1.csv --start 2025-01-01 --end 2026-07-01 --data-dir data/backtest_2025_2026_accurate --set-reference data/reference/set_actions_2025_2026_h1.csv --split-by-year --output-dir reports/ema_crossover_2025_2026_accurate_set_actions +``` + +The command saves a summary and separate fills, trades, equity curves and skipped-entry reasons for each EMA pair and period. Year slices reset capital and liquidate independently; they are diagnostics and should not be added together to reproduce the full-period run. EMA spans and allocation can be changed with `--ema-pairs FAST:SLOW ...` and `--max-position-pct`. + +To test overlays, pass one or more `--market-filters` (`none`, `ew_trend_up`, `ew_above_ema200`), `--exit-modes` (`cross_under`, `ema21_low`) and `--max-loss-pcts` (0 disables the hard stop; values such as 0.08 mean 8%). The command tests the Cartesian combination of the selected options and saves each combination separately. + +### Long-history model lab + +The separate `model-lab` command screens five fixed rule families on the SiamChart market-wide archive: 12–1 month momentum rotation, the same momentum ranked by realized volatility, EMA 50/200 crossovers, RSI(2) pullbacks in an uptrend, and 55/20 Donchian breakouts. Each uses a daily as-of-date top-100 liquidity universe, next-open execution, dividend/split adjustments where the local action history supports them, and both base and stressed transaction costs. Reports include annual and early/middle/recent-period returns. The low-volatility momentum, EMA 50/200 and Donchian candidates also get separate 100-share board-lot simulations with raw prices and cash dividends, plus a sensitivity that writes off a position after 60 consecutive sessions without a bar. Remaining limitations include incomplete action history and no market-impact model. It does not change PrimeThai defaults or count the previously identified EMA 20/50 + SET100EW candidate. + +```powershell +python -m primethai model-lab --start 2005-01-01 --end 2026-07-01 --output-dir reports/model_lab_2005_2026_h1 +``` + +The defaults expect both `data/raw/siamchart/set-archive_EOD_1970-LAST.zip` and `data/raw/siamchart/set-archive_EOD.zip`. Review `summary.csv`, `period_metrics.csv`, `annual_returns.csv`, `symbol_attribution.csv`, `board_lot_validation.csv`, `board_lot_period_metrics.csv`, `board_lot_stale60_validation.csv`, `board_lot_stale60_period_metrics.csv`, and `data_notes.json` together; a positive full-period return alone is not enough to promote a strategy. + +## Experiments and walk-forward + +The cumulative A–H ablation table adds RS, regime, MCO, MCSI, higher lows, weekly structure and the ATR extension filter in order. The experiment command also reports chronological train/validation/out-of-sample slices and rolling fixed-parameter windows; it does not optimize on the full sample. The slices are only defensible OOS evidence when the historical membership and price/action data are valid. + +```powershell +python -m primethai experiment --universe SET100 --mode accurate --start 2015-01-01 --end 2026-01-01 +``` + +## Tests + +The suite uses Python's standard `unittest` test cases and synthetic/fixture data, so it runs without Yahoo access or pytest: + +```powershell +python -m unittest discover -s tests -v +``` + +Run the end-to-end offline smoke pipeline on generated data: + +```powershell +python -m primethai smoke +``` + +This is only a wiring check; generated returns are not market evidence. For a real-data pipeline smoke run, download the ten default symbols and the SET index, then run: + +```powershell +python -m primethai market --universe custom --symbols ADVANC AOT CPALL DELTA PTT KBANK KTB TRUE GULF SCB +python -m primethai scan --universe custom --symbols ADVANC AOT CPALL DELTA PTT KBANK KTB TRUE GULF SCB --top 5 +python -m primethai backtest --universe custom --symbols ADVANC AOT CPALL DELTA PTT KBANK KTB TRUE GULF SCB +``` + +Custom fast runs also show `SURVIVORSHIP BIAS`; these ten tickers are only a pipeline smoke set, not a historical SET100 universe. + +## Original PrimeTrading concepts + +The reference framework describes market-first positioning, the EMA21 high/close/low structure, buying pullbacks near rising structure, relative strength, liquid leaders, breadth timing with MCO and confirmation with MCSI, weekly 10-period confluence, avoiding entries more than about 1 ATR above the structure, trimming near 2R, and managing the remainder around EMA21 low. The author explicitly describes the system as discretionary and tailored to their own risk tolerance. PrimeThai models a subset as deterministic research rules; it does not reproduce discretionary judgment or claim the same performance. + +## PrimeThai research assumptions + +The precise EMA slope lookback, ATR/ADR formulas, RS periods/weights and percentile method, MCO normalization/Z-score window, regime transition conditions, scanner score, pivot confirmation window, volume contraction metric, entry triggers, initial risk, one-third trim, max-loss stop, portfolio heat, position cap, transaction cost defaults and daily-rebalanced SET100EW calculation are implementation assumptions. They are configurable where practical and must be assessed with out-of-sample research. They are not asserted to be original TradersLab formulas. + +## Known limitations + +- Yahoo Finance is a convenience source, not an exchange-grade historical database. Check gaps, ticker changes, corporate-action behavior, suspensions, delistings and licensing/availability for each symbol. The package reports failures per symbol, but cannot recover missing delisted securities automatically. +- In a live probe on 2026-09-28, Yahoo returned only one current quote for `^SET.BK`, so the provider refuses incomplete benchmark history. The SiamChart archives now provide a direct path to populate `data/raw/^SET.csv`; the imported series must still be checked for missing/corrupt daily records. The current archive has one unreadable entry dated 2018-09-14, and some older daily files omit the SET row. The importer reports unreadable entries and the CLI requires at least 260 valid index bars before market context or scanning. SET's official historical index data is also available as a paid data request. +- The included SET100 PIT file covers 2024-01-02 through 2026-06-30 only. Accurate research before or after that range needs dated constituent files for every tested period and warm-up; there is no fabricated backfill. +- yfinance prices can be revised and Yahoo corporate-action semantics may change. SiamChart's EOD archive omits its separately listed actions; import the stock-info sidecars and check them against a complete SET reference before relying on dividend-adjusted results. Par-value history is audit-only pending manual confirmation of actual share splits. Rights offerings, tender offers, spin-offs, mergers and symbol migrations are not fully modeled. +- The simulator uses daily OHLC. If both emergency risk and target levels occur in one bar, it assumes the adverse stop occurs first. Gaps, limit-up/down, halts, queue priority, partial liquidity fills, lot-size history, odd-lot execution, broker-specific costs, settlement and earnings exposure are not fully modeled. +- The stock 50-share board lot requires six months at/above THB 500 and an exchange notice; historical eligibility is not inferred. Index constituents may include products with different trading units. +- SET100EW is a reconstructed proxy, not a licensed exchange-calculated index. Breadth uses price changes among observed bars; suspended/no-print shares are excluded on a date. +- Earnings/calendar risk, sector mapping outside the supplied SET membership file, downtrend-line breakouts, discretionary character-change assessments and actual paper-trading order lifecycle are not implemented. + +## Phase 3 direction + +Keep the scanner and deterministic JSON payload as the interface. Add a local Qwen copilot that explains setups, points out conflicting fields, lists missing confirmations, and states invalidation conditions. Validate every explanation against the payload; do not allow it to compute indicators, alter a regime, size a position or submit a trade. + +## Primary model focus (2026-09-29) + +The selected primary research models are `donchian_55_20` and `ema50_200_cross`. Their combined “either model signals” portfolio uses the latest daily archive, currently through 2026-09-28. The current fresh-start run is in `reports/primary_union_current/primary_union_donchian_ema/`; the earlier separate-model comparison through 2026-09-25 remains in `reports/model_lab_2026_fresh_start_through_2026_09_25/ytd_summary.md`. + +The combined primary signal is the union of both model target lists: a symbol is held when either model selects it, overlapping selections count once, and distinct active symbols are equal-weighted up to 20 positions. For 2026-01-05 through 2026-09-28 (179 sessions), its 100-share lot simulation returned 37.9% at base costs and 35.1% under stressed costs, with maximum drawdowns of 11.0% and 11.2%; turnover was about 10.2 times starting capital. This is a short historical simulation, not a live track record or a promise of future returns. The daily table is a **target basket**, not a buy/sell delta against a brokerage account; reconcile it with actual holdings and current quotes before placing any orders. The current `scan` and `backtest` commands still use the PrimeThai setup described above; this project has no broker connection or live order execution. + +### Local Web UI and daily signals + +Run the strategy dashboard from the project folder: + +```powershell +python -m primethai webui +``` + +The app requires a local `.env` file for its login. The project has a generated `.env` for this workspace; for another installation, copy `.env.example` to `.env` and set a username, a unique password of at least 12 characters, and a random session secret of at least 32 bytes. The `.env` file is excluded from Git. `PRIMETHAI_SESSION_TTL_HOURS` controls how long a login lasts (default 168 hours); `PRIMETHAI_COOKIE_SECURE=auto` enables secure cookies when an HTTPS reverse proxy sends `X-Forwarded-Proto: https`. Restart the server after changing these settings. + +Then open `http://127.0.0.1:18900` and sign in. The same login protects the portfolio page, backtest page, and API; there is also an **ออกจากระบบ** button. Sessions use signed, HTTP-only, same-site cookies and expire after the configured lifetime. The home page is the portfolio book; the historical chart and simulation are at `/backtest`. At startup, the service checks SiamChart and downloads the latest EOD ZIP unless a successful after-close refresh has already happened that day, then recalculates signals. It merges each dated CSV into a cumulative local archive instead of replacing earlier sessions; `data/raw/siamchart/set-archive_EOD_LAST.sources.json` records the provider and coverage for each stored session. It repeats automatically on Thai weekdays after 20:30 Asia/Bangkok and retries network failures every 30 minutes. If SiamChart fails after the close, Yahoo Finance through `yfinance` is the secondary feed. The fallback must include a SET index bar and at least 95% of the previous session's top-100 liquid model universe, plus current signal/held symbols, before its data is accepted. If it cannot meet that check, the app keeps the prior complete price history and shows its date; there is no upload step. The **อัปเดตสัญญาณ** button remains as an optional immediate refresh. The service process must remain running for scheduled refreshes. The calculation runs on the server and can take a few minutes. + +Yahoo Finance is an independent fallback, not an exchange-licensed feed or a guarantee of complete coverage. `yfinance` describes itself as an open-source interface to Yahoo's public APIs, intended for research/personal use; use the downloaded data only for the user's private app and review [Yahoo's terms](https://legal.yahoo.com/us/en/yahoo/terms/otos/index.html). For an official SET market-data API source, SET's specification requires an assigned account and API key and documents latest stock/index quotations; it is not configured here. See the [SET Market Data API specification](https://media.set.or.th/set/Documents/2023/May/Market_Data_API_Service_Specification.pdf). + +For a server deployment, bind to the network interface with `--host 0.0.0.0` and place the app behind an HTTPS reverse proxy that forwards the original `Host` and `X-Forwarded-Proto` headers. The app's login protects its pages and APIs, but the app itself uses HTTP; do not expose its port directly to the public internet. Example: `python -m primethai webui --host 0.0.0.0 --port 18900`. + +The cumulative daily ZIP is saved automatically as `data/raw/siamchart/set-archive_EOD_LAST.zip`; the two historical ZIPs remain the warm-up and backtest sources. The dashboard displays the stored session range and the source used for its latest date. **ลงทุนครั้งแรก** records the initial cash contribution; later contributions increase cash and recalculate target holdings from current portfolio equity and the model weights. The app shows current shares, target shares and proposed BUY/SELL differences. Click **บันทึกซื้อ** or **บันทึกขาย** beside a daily signal to add its proposed share quantity to the local portfolio ledger at the latest EOD close. This only updates the app; it does not send a broker order. Use **แก้** in the recent history after your order fills to update the quantity, actual price and fees, or **ยกเลิก** to remove an estimated signal entry from portfolio calculations while retaining its history. Record dividends and withdrawals in the cash ledger too; share splits and rights entitlements still need manual reconciliation. Account history is persisted locally in `data/portfolio/prime_thai_portfolio.sqlite3` and that folder is ignored by Git. + +The proposed limit reference is the latest EOD close, not a live quote or a guaranteed fill. Signal entries use that close as an estimate until you edit the ledger with the actual fill. The app does not connect to a broker, transmit orders, or automatically reconcile broker holdings. The backtest page is a research simulation, not a validated live record, and does not model market impact, bid/ask spreads, every corporate action, or broker-specific buying power. Verify current quotes, liquidity, order size and cash with the broker before each order. + +The default dashboard report is written to `reports/primary_union_current`. You can change the local port, date range, starting cash, report folder, or input archives with `--port`, `--start`, `--initial-equity`, `--report-dir`, and `--archives`. diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..62624c1 --- /dev/null +++ b/config/default.json @@ -0,0 +1,81 @@ +{ + "strategy": { + "initial_equity": 1000000, + "weakness_risk_pct": 0.0025, + "confirmation_risk_pct": 0.005, + "max_position_pct": 0.2, + "max_extension_atr": 1.0, + "min_mco_z_for_entry": -2.0, + "min_rs_percentile": 70, + "min_value_turnover_thb": 20000000, + "min_price": 2, + "partial_r_multiple": 2.0, + "partial_fraction": 0.3333333333, + "max_loss_pct": 0.08, + "exit_mode": "close_below_ema21_low", + "enable_rs": true, + "enable_market_regime": true, + "enable_mco": true, + "enable_mcsi": true, + "enable_higher_low": true, + "enable_weekly": true, + "enable_atr_filter": true, + "require_market_support": true, + "volume_pullback_ratio": 1.0, + "base_breakout_window": 20, + "heat": { + "correction": 0.0, + "repair": 0.005, + "early_uptrend": 0.01, + "confirmed_uptrend": 0.02, + "uptrend_pullback": 0.02, + "overbought": 0.0, + "breakdown": 0.0 + } + }, + "market_rules": { + "default_board_lot": 100, + "high_price_board_lot": 50, + "high_price_threshold": 500, + "high_price_months": 6, + "minimum_quantity": 1, + "etf_tick": 0.01 + }, + "costs": { + "commission_rate": 0.0015, + "fees_rate": 0.0, + "sell_tax_rate": 0.0, + "slippage_bps": 5.0 + }, + "rs_weights": { + "one_month": 0.20, + "three_month": 0.35, + "twelve_month": 0.30, + "high_52w": 0.15 + }, + "research": { + "slope_lookback": 5, + "atr_period": 14, + "adr_period": 20, + "mco_short_span": 19, + "mco_long_span": 39, + "mco_z_window": 252, + "pivot_left_bars": 3, + "pivot_right_bars": 3, + "pullback_volume_window": 5, + "tightness_window": 10, + "overbought_extension_atr": 1.5, + "mco_repair_sigma": 1.0 + }, + "focus_weights": { + "rs": 25, + "market_alignment": 20, + "structure": 15, + "higher_low": 10, + "atr_distance": 10, + "volume_behavior": 5, + "tightness": 5, + "sector_strength": 5, + "weekly": 5 + } +} diff --git a/data/reference/set_actions_2025_2026_h1.csv b/data/reference/set_actions_2025_2026_h1.csv new file mode 100644 index 0000000..78c428e --- /dev/null +++ b/data/reference/set_actions_2025_2026_h1.csv @@ -0,0 +1,293 @@ +symbol,action_type,date,source_url,value,source_symbol,coverage_start,coverage_end,coverage_complete +ADVANC,dividend,2025-02-20,https://www.set.or.th/en/market/product/stock/quote/ADVANC/rights-benefits,5.74,ADVANC,2025-01-01,2026-07-01,True +ADVANC,dividend,2025-08-19,https://www.set.or.th/en/market/product/stock/quote/ADVANC/rights-benefits,6.89,ADVANC,2025-01-01,2026-07-01,True +ADVANC,dividend,2026-02-27,https://www.set.or.th/en/market/product/stock/quote/ADVANC/rights-benefits,27.41,ADVANC,2025-01-01,2026-07-01,True +AEONTS,dividend,2025-04-28,https://www.set.or.th/en/market/product/stock/quote/AEONTS/rights-benefits,2.95,AEONTS,2025-01-01,2026-07-01,True +AEONTS,dividend,2025-10-21,https://www.set.or.th/en/market/product/stock/quote/AEONTS/rights-benefits,2.55,AEONTS,2025-01-01,2026-07-01,True +AEONTS,dividend,2026-04-27,https://www.set.or.th/en/market/product/stock/quote/AEONTS/rights-benefits,2.95,AEONTS,2025-01-01,2026-07-01,True +AMATA,dividend,2025-05-13,https://www.set.or.th/en/market/product/stock/quote/AMATA/rights-benefits,0.55,AMATA,2025-01-01,2026-07-01,True +AMATA,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/AMATA/rights-benefits,0.35,AMATA,2025-01-01,2026-07-01,True +AMATA,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/AMATA/rights-benefits,0.75,AMATA,2025-01-01,2026-07-01,True +AOT,dividend,2025-12-11,https://www.set.or.th/en/market/product/stock/quote/AOT/rights-benefits,0.81,AOT,2025-01-01,2026-07-01,True +AP,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/AP/rights-benefits,0.6,AP,2025-01-01,2026-07-01,True +AP,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/AP/rights-benefits,0.52,AP,2025-01-01,2026-07-01,True +AURA,dividend,2025-04-30,https://www.set.or.th/en/market/product/stock/quote/AURA/rights-benefits,0.42,AURA,2025-01-01,2026-07-01,True +AURA,dividend,2026-04-30,https://www.set.or.th/en/market/product/stock/quote/AURA/rights-benefits,0.56,AURA,2025-01-01,2026-07-01,True +AWC,dividend,2025-05-08,https://www.set.or.th/en/market/product/stock/quote/AWC/rights-benefits,0.075,AWC,2025-01-01,2026-07-01,True +AWC,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/AWC/rights-benefits,0.08,AWC,2025-01-01,2026-07-01,True +BA,dividend,2025-03-12,https://www.set.or.th/en/market/product/stock/quote/BA/rights-benefits,0.7,BA,2025-01-01,2026-07-01,True +BA,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/BA/rights-benefits,0.5,BA,2025-01-01,2026-07-01,True +BA,dividend,2026-03-11,https://www.set.or.th/en/market/product/stock/quote/BA/rights-benefits,0.7,BA,2025-01-01,2026-07-01,True +BAM,dividend,2025-04-25,https://www.set.or.th/en/market/product/stock/quote/BAM/rights-benefits,0.35,BAM,2025-01-01,2026-07-01,True +BAM,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/BAM/rights-benefits,0.5,BAM,2025-01-01,2026-07-01,True +BANPU,dividend,2025-04-11,https://www.set.or.th/en/market/product/stock/quote/BANPUU/rights-benefits,0.12,BANPUU,2025-01-01,2026-07-01,True +BANPU,dividend,2025-09-09,https://www.set.or.th/en/market/product/stock/quote/BANPUU/rights-benefits,0.12,BANPUU,2025-01-01,2026-07-01,True +BANPU,dividend,2026-04-16,https://www.set.or.th/en/market/product/stock/quote/BANPUU/rights-benefits,0.18,BANPUU,2025-01-01,2026-07-01,True +BBL,dividend,2025-04-23,https://www.set.or.th/en/market/product/stock/quote/BBL/rights-benefits,6.5,BBL,2025-01-01,2026-07-01,True +BBL,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/BBL/rights-benefits,2.0,BBL,2025-01-01,2026-07-01,True +BBL,dividend,2026-04-22,https://www.set.or.th/en/market/product/stock/quote/BBL/rights-benefits,8.0,BBL,2025-01-01,2026-07-01,True +BCH,dividend,2025-05-02,https://www.set.or.th/en/market/product/stock/quote/BCH/rights-benefits,0.28,BCH,2025-01-01,2026-07-01,True +BCH,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/BCH/rights-benefits,0.15,BCH,2025-01-01,2026-07-01,True +BCH,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/BCH/rights-benefits,0.3,BCH,2025-01-01,2026-07-01,True +BCP,dividend,2025-03-13,https://www.set.or.th/en/market/product/stock/quote/BCP/rights-benefits,0.45,BCP,2025-01-01,2026-07-01,True +BCP,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/BCP/rights-benefits,1.05,BCP,2025-01-01,2026-07-01,True +BCPG,dividend,2025-03-05,https://www.set.or.th/en/market/product/stock/quote/BCPG/rights-benefits,0.18,BCPG,2025-01-01,2026-07-01,True +BCPG,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/BCPG/rights-benefits,0.1,BCPG,2025-01-01,2026-07-01,True +BCPG,dividend,2026-03-04,https://www.set.or.th/en/market/product/stock/quote/BCPG/rights-benefits,0.25,BCPG,2025-01-01,2026-07-01,True +BDMS,dividend,2025-03-12,https://www.set.or.th/en/market/product/stock/quote/BDMS/rights-benefits,0.4,BDMS,2025-01-01,2026-07-01,True +BDMS,dividend,2025-09-09,https://www.set.or.th/en/market/product/stock/quote/BDMS/rights-benefits,0.35,BDMS,2025-01-01,2026-07-01,True +BDMS,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/BDMS/rights-benefits,0.65,BDMS,2025-01-01,2026-07-01,True +BEM,dividend,2025-03-11,https://www.set.or.th/en/market/product/stock/quote/BEM/rights-benefits,0.15,BEM,2025-01-01,2026-07-01,True +BEM,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/BEM/rights-benefits,0.15,BEM,2025-01-01,2026-07-01,True +BGRIM,dividend,2025-03-13,https://www.set.or.th/en/market/product/stock/quote/BGRIM/rights-benefits,0.25,BGRIM,2025-01-01,2026-07-01,True +BGRIM,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/BGRIM/rights-benefits,0.18,BGRIM,2025-01-01,2026-07-01,True +BGRIM,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/BGRIM/rights-benefits,0.232,BGRIM,2025-01-01,2026-07-01,True +BH,dividend,2025-03-19,https://www.set.or.th/en/market/product/stock/quote/BH/rights-benefits,3.0,BH,2025-01-01,2026-07-01,True +BH,dividend,2025-08-14,https://www.set.or.th/en/market/product/stock/quote/BH/rights-benefits,2.0,BH,2025-01-01,2026-07-01,True +BH,dividend,2026-03-17,https://www.set.or.th/en/market/product/stock/quote/BH/rights-benefits,9.0,BH,2025-01-01,2026-07-01,True +BJC,dividend,2025-04-29,https://www.set.or.th/en/market/product/stock/quote/BJC/rights-benefits,0.56,BJC,2025-01-01,2026-07-01,True +BJC,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/BJC/rights-benefits,0.15,BJC,2025-01-01,2026-07-01,True +BJC,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/BJC/rights-benefits,0.36,BJC,2025-01-01,2026-07-01,True +BLA,dividend,2025-04-22,https://www.set.or.th/en/market/product/stock/quote/BLA/rights-benefits,0.48,BLA,2025-01-01,2026-07-01,True +BLA,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/BLA/rights-benefits,0.38,BLA,2025-01-01,2026-07-01,True +BLA,dividend,2026-04-21,https://www.set.or.th/en/market/product/stock/quote/BLA/rights-benefits,0.48,BLA,2025-01-01,2026-07-01,True +BTG,dividend,2025-03-11,https://www.set.or.th/en/market/product/stock/quote/BTG/rights-benefits,0.5,BTG,2025-01-01,2026-07-01,True +BTG,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/BTG/rights-benefits,1.5,BTG,2025-01-01,2026-07-01,True +CBG,dividend,2025-03-06,https://www.set.or.th/en/market/product/stock/quote/CBG/rights-benefits,0.7,CBG,2025-01-01,2026-07-01,True +CBG,dividend,2025-08-21,https://www.set.or.th/en/market/product/stock/quote/CBG/rights-benefits,0.7,CBG,2025-01-01,2026-07-01,True +CBG,dividend,2026-03-05,https://www.set.or.th/en/market/product/stock/quote/CBG/rights-benefits,0.6,CBG,2025-01-01,2026-07-01,True +CCET,dividend,2025-03-19,https://www.set.or.th/en/market/product/stock/quote/CCET/rights-benefits,0.13,CCET,2025-01-01,2026-07-01,True +CCET,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/CCET/rights-benefits,0.07,CCET,2025-01-01,2026-07-01,True +CCET,dividend,2026-03-19,https://www.set.or.th/en/market/product/stock/quote/CCET/rights-benefits,0.09,CCET,2025-01-01,2026-07-01,True +CENTEL,dividend,2025-05-02,https://www.set.or.th/en/market/product/stock/quote/CENTEL/rights-benefits,0.59,CENTEL,2025-01-01,2026-07-01,True +CENTEL,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/CENTEL/rights-benefits,0.67,CENTEL,2025-01-01,2026-07-01,True +CHG,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/CHG/rights-benefits,0.05,CHG,2025-01-01,2026-07-01,True +CHG,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/CHG/rights-benefits,0.02,CHG,2025-01-01,2026-07-01,True +CHG,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/CHG/rights-benefits,0.05,CHG,2025-01-01,2026-07-01,True +CK,dividend,2025-03-12,https://www.set.or.th/en/market/product/stock/quote/CK/rights-benefits,0.15,CK,2025-01-01,2026-07-01,True +CK,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/CK/rights-benefits,0.2,CK,2025-01-01,2026-07-01,True +CK,dividend,2026-03-11,https://www.set.or.th/en/market/product/stock/quote/CK/rights-benefits,0.25,CK,2025-01-01,2026-07-01,True +CKP,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/CKP/rights-benefits,0.085,CKP,2025-01-01,2026-07-01,True +CKP,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/CKP/rights-benefits,0.088,CKP,2025-01-01,2026-07-01,True +COCOCO,dividend,2025-04-29,https://www.set.or.th/en/market/product/stock/quote/COCOCO/rights-benefits,0.3,COCOCO,2025-01-01,2026-07-01,True +COCOCO,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/COCOCO/rights-benefits,0.12,COCOCO,2025-01-01,2026-07-01,True +COM7,dividend,2025-03-10,https://www.set.or.th/en/market/product/stock/quote/COM7/rights-benefits,0.86,COM7,2025-01-01,2026-07-01,True +COM7,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/COM7/rights-benefits,1.1,COM7,2025-01-01,2026-07-01,True +CPALL,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/CPALL/rights-benefits,1.35,CPALL,2025-01-01,2026-07-01,True +CPALL,dividend,2026-04-30,https://www.set.or.th/en/market/product/stock/quote/CPALL/rights-benefits,1.65,CPALL,2025-01-01,2026-07-01,True +CPF,dividend,2025-05-08,https://www.set.or.th/en/market/product/stock/quote/CPF/rights-benefits,0.55,CPF,2025-01-01,2026-07-01,True +CPF,dividend,2025-08-29,https://www.set.or.th/en/market/product/stock/quote/CPF/rights-benefits,1.0,CPF,2025-01-01,2026-07-01,True +CPF,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/CPF/rights-benefits,0.25,CPF,2025-01-01,2026-07-01,True +CPN,dividend,2025-03-17,https://www.set.or.th/en/market/product/stock/quote/CPN/rights-benefits,2.1,CPN,2025-01-01,2026-07-01,True +CPN,dividend,2026-03-09,https://www.set.or.th/en/market/product/stock/quote/CPN/rights-benefits,2.4,CPN,2025-01-01,2026-07-01,True +CRC,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/CRC/rights-benefits,0.6,CRC,2025-01-01,2026-07-01,True +CRC,dividend,2025-12-04,https://www.set.or.th/en/market/product/stock/quote/CRC/rights-benefits,0.7,CRC,2025-01-01,2026-07-01,True +CRC,dividend,2026-04-21,https://www.set.or.th/en/market/product/stock/quote/CRC/rights-benefits,1.11,CRC,2025-01-01,2026-07-01,True +DELTA,dividend,2025-02-27,https://www.set.or.th/en/market/product/stock/quote/DELTA/rights-benefits,0.46,DELTA,2025-01-01,2026-07-01,True +DELTA,dividend,2026-02-26,https://www.set.or.th/en/market/product/stock/quote/DELTA/rights-benefits,0.6,DELTA,2025-01-01,2026-07-01,True +DOHOME,dividend,2025-03-04,https://www.set.or.th/en/market/product/stock/quote/DOHOME/rights-benefits,0.0052910053,DOHOME,2025-01-01,2026-07-01,True +DOHOME,dividend,2026-03-05,https://www.set.or.th/en/market/product/stock/quote/DOHOME/rights-benefits,0.0044444444,DOHOME,2025-01-01,2026-07-01,True +EGCO,dividend,2025-03-13,https://www.set.or.th/en/market/product/stock/quote/EGCO/rights-benefits,3.25,EGCO,2025-01-01,2026-07-01,True +EGCO,dividend,2025-09-05,https://www.set.or.th/en/market/product/stock/quote/EGCO/rights-benefits,3.25,EGCO,2025-01-01,2026-07-01,True +EGCO,dividend,2026-03-13,https://www.set.or.th/en/market/product/stock/quote/EGCO/rights-benefits,3.25,EGCO,2025-01-01,2026-07-01,True +ERW,dividend,2025-03-10,https://www.set.or.th/en/market/product/stock/quote/ERW/rights-benefits,0.09,ERW,2025-01-01,2026-07-01,True +ERW,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/ERW/rights-benefits,0.07,ERW,2025-01-01,2026-07-01,True +FORTH,dividend,2026-03-13,https://www.set.or.th/en/market/product/stock/quote/FORTH/rights-benefits,0.21,FORTH,2025-01-01,2026-07-01,True +GFPT,dividend,2025-03-05,https://www.set.or.th/en/market/product/stock/quote/GFPT/rights-benefits,0.2,GFPT,2025-01-01,2026-07-01,True +GFPT,dividend,2026-03-04,https://www.set.or.th/en/market/product/stock/quote/GFPT/rights-benefits,0.2,GFPT,2025-01-01,2026-07-01,True +GLOBAL,dividend,2025-02-27,https://www.set.or.th/en/market/product/stock/quote/GLOBAL/rights-benefits,0.1842735043,GLOBAL,2025-01-01,2026-07-01,True +GLOBAL,dividend,2026-02-24,https://www.set.or.th/en/market/product/stock/quote/GLOBAL/rights-benefits,0.1841152263,GLOBAL,2025-01-01,2026-07-01,True +GPSC,dividend,2025-03-03,https://www.set.or.th/en/market/product/stock/quote/GPSC/rights-benefits,0.45,GPSC,2025-01-01,2026-07-01,True +GPSC,dividend,2025-09-08,https://www.set.or.th/en/market/product/stock/quote/GPSC/rights-benefits,0.5,GPSC,2025-01-01,2026-07-01,True +GPSC,dividend,2026-02-25,https://www.set.or.th/en/market/product/stock/quote/GPSC/rights-benefits,0.95,GPSC,2025-01-01,2026-07-01,True +GULF,dividend,2025-02-19,https://www.set.or.th/en/market/product/stock/quote/GULFI/rights-benefits,1.01,GULFI,2025-01-01,2026-07-01,True +GULF,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/GULF/rights-benefits,3.25,GULF,2025-01-01,2026-07-01,True +GUNKUL,dividend,2025-05-28,https://www.set.or.th/en/market/product/stock/quote/GUNKUL/rights-benefits,0.08,GUNKUL,2025-01-01,2026-07-01,True +GUNKUL,dividend,2025-11-27,https://www.set.or.th/en/market/product/stock/quote/GUNKUL/rights-benefits,0.04,GUNKUL,2025-01-01,2026-07-01,True +GUNKUL,dividend,2026-05-22,https://www.set.or.th/en/market/product/stock/quote/GUNKUL/rights-benefits,0.1,GUNKUL,2025-01-01,2026-07-01,True +HANA,dividend,2025-03-13,https://www.set.or.th/en/market/product/stock/quote/HANA/rights-benefits,0.5,HANA,2025-01-01,2026-07-01,True +HANA,dividend,2025-11-27,https://www.set.or.th/en/market/product/stock/quote/HANA/rights-benefits,0.25,HANA,2025-01-01,2026-07-01,True +HANA,dividend,2026-03-16,https://www.set.or.th/en/market/product/stock/quote/HANA/rights-benefits,0.75,HANA,2025-01-01,2026-07-01,True +HMPRO,dividend,2025-04-22,https://www.set.or.th/en/market/product/stock/quote/HMPRO/rights-benefits,0.25,HMPRO,2025-01-01,2026-07-01,True +HMPRO,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/HMPRO/rights-benefits,0.16,HMPRO,2025-01-01,2026-07-01,True +HMPRO,dividend,2026-04-22,https://www.set.or.th/en/market/product/stock/quote/HMPRO/rights-benefits,0.22,HMPRO,2025-01-01,2026-07-01,True +ICHI,dividend,2025-03-07,https://www.set.or.th/en/market/product/stock/quote/ICHI/rights-benefits,0.5,ICHI,2025-01-01,2026-07-01,True +ICHI,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/ICHI/rights-benefits,0.55,ICHI,2025-01-01,2026-07-01,True +ICHI,dividend,2026-03-13,https://www.set.or.th/en/market/product/stock/quote/ICHI/rights-benefits,0.55,ICHI,2025-01-01,2026-07-01,True +INTUCH,dividend,2025-02-19,https://www.set.or.th/en/market/product/stock/quote/INTUCH/rights-benefits,6.54,INTUCH,2025-01-01,2026-07-01,True +IRPC,dividend,2025-02-25,https://www.set.or.th/en/market/product/stock/quote/IRPC/rights-benefits,0.01,IRPC,2025-01-01,2026-07-01,True +IRPC,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/IRPC/rights-benefits,0.01,IRPC,2025-01-01,2026-07-01,True +ITC,dividend,2025-02-26,https://www.set.or.th/en/market/product/stock/quote/ITC/rights-benefits,0.75,ITC,2025-01-01,2026-07-01,True +ITC,dividend,2025-08-13,https://www.set.or.th/en/market/product/stock/quote/ITC/rights-benefits,0.4,ITC,2025-01-01,2026-07-01,True +ITC,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/ITC/rights-benefits,0.45,ITC,2025-01-01,2026-07-01,True +IVL,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +IVL,dividend,2025-05-27,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +IVL,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +IVL,dividend,2025-11-25,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +IVL,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +IVL,dividend,2026-05-28,https://www.set.or.th/en/market/product/stock/quote/IVL/rights-benefits,0.175,IVL,2025-01-01,2026-07-01,True +JMART,dividend,2025-05-09,https://www.set.or.th/en/market/product/stock/quote/JMART/rights-benefits,0.23,JMART,2025-01-01,2026-07-01,True +JMART,dividend,2025-11-24,https://www.set.or.th/en/market/product/stock/quote/JMART/rights-benefits,0.13,JMART,2025-01-01,2026-07-01,True +JMART,dividend,2026-05-27,https://www.set.or.th/en/market/product/stock/quote/JMART/rights-benefits,0.135,JMART,2025-01-01,2026-07-01,True +JMT,dividend,2025-02-25,https://www.set.or.th/en/market/product/stock/quote/JMT/rights-benefits,0.28,JMT,2025-01-01,2026-07-01,True +JMT,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/JMT/rights-benefits,0.24,JMT,2025-01-01,2026-07-01,True +JMT,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/JMT/rights-benefits,0.43,JMT,2025-01-01,2026-07-01,True +KBANK,dividend,2025-04-17,https://www.set.or.th/en/market/product/stock/quote/KBANK/rights-benefits,8.0,KBANK,2025-01-01,2026-07-01,True +KBANK,dividend,2025-05-15,https://www.set.or.th/en/market/product/stock/quote/KBANK/rights-benefits,2.5,KBANK,2025-01-01,2026-07-01,True +KBANK,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/KBANK/rights-benefits,2.0,KBANK,2025-01-01,2026-07-01,True +KBANK,dividend,2026-04-21,https://www.set.or.th/en/market/product/stock/quote/KBANK/rights-benefits,12.0,KBANK,2025-01-01,2026-07-01,True +KCE,dividend,2025-03-24,https://www.set.or.th/en/market/product/stock/quote/KCE/rights-benefits,0.6,KCE,2025-01-01,2026-07-01,True +KCE,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/KCE/rights-benefits,0.6,KCE,2025-01-01,2026-07-01,True +KCE,dividend,2026-03-23,https://www.set.or.th/en/market/product/stock/quote/KCE/rights-benefits,0.6,KCE,2025-01-01,2026-07-01,True +KKP,dividend,2025-05-02,https://www.set.or.th/en/market/product/stock/quote/KKP/rights-benefits,2.75,KKP,2025-01-01,2026-07-01,True +KKP,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/KKP/rights-benefits,1.5,KKP,2025-01-01,2026-07-01,True +KKP,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/KKP/rights-benefits,4.2,KKP,2025-01-01,2026-07-01,True +KTB,dividend,2025-04-16,https://www.set.or.th/en/market/product/stock/quote/KTB/rights-benefits,1.545,KTB,2025-01-01,2026-07-01,True +KTB,dividend,2025-11-11,https://www.set.or.th/en/market/product/stock/quote/KTB/rights-benefits,0.43,KTB,2025-01-01,2026-07-01,True +KTB,dividend,2026-04-10,https://www.set.or.th/en/market/product/stock/quote/KTB/rights-benefits,2.24,KTB,2025-01-01,2026-07-01,True +KTC,dividend,2025-04-17,https://www.set.or.th/en/market/product/stock/quote/KTC/rights-benefits,1.32,KTC,2025-01-01,2026-07-01,True +KTC,dividend,2026-04-10,https://www.set.or.th/en/market/product/stock/quote/KTC/rights-benefits,1.77,KTC,2025-01-01,2026-07-01,True +LH,dividend,2025-04-30,https://www.set.or.th/en/market/product/stock/quote/LH/rights-benefits,0.17,LH,2025-01-01,2026-07-01,True +LH,dividend,2025-10-15,https://www.set.or.th/en/market/product/stock/quote/LH/rights-benefits,0.13,LH,2025-01-01,2026-07-01,True +LH,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/LH/rights-benefits,0.12,LH,2025-01-01,2026-07-01,True +M,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/M/rights-benefits,1.0,M,2025-01-01,2026-07-01,True +M,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/M/rights-benefits,0.5,M,2025-01-01,2026-07-01,True +M,dividend,2026-05-11,https://www.set.or.th/en/market/product/stock/quote/M/rights-benefits,0.5,M,2025-01-01,2026-07-01,True +MBK,dividend,2025-03-19,https://www.set.or.th/en/market/product/stock/quote/MBK/rights-benefits,0.5,MBK,2025-01-01,2026-07-01,True +MBK,dividend,2025-09-17,https://www.set.or.th/en/market/product/stock/quote/MBK/rights-benefits,0.5,MBK,2025-01-01,2026-07-01,True +MBK,dividend,2026-03-17,https://www.set.or.th/en/market/product/stock/quote/MBK/rights-benefits,0.6,MBK,2025-01-01,2026-07-01,True +MEGA,dividend,2025-03-07,https://www.set.or.th/en/market/product/stock/quote/MEGA/rights-benefits,0.8,MEGA,2025-01-01,2026-07-01,True +MEGA,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/MEGA/rights-benefits,0.8,MEGA,2025-01-01,2026-07-01,True +MEGA,dividend,2026-03-09,https://www.set.or.th/en/market/product/stock/quote/MEGA/rights-benefits,0.8,MEGA,2025-01-01,2026-07-01,True +MINT,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/MINT/rights-benefits,0.35,MINT,2025-01-01,2026-07-01,True +MINT,dividend,2025-09-01,https://www.set.or.th/en/market/product/stock/quote/MINT/rights-benefits,0.3,MINT,2025-01-01,2026-07-01,True +MINT,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/MINT/rights-benefits,0.4,MINT,2025-01-01,2026-07-01,True +MOSHI,dividend,2025-04-30,https://www.set.or.th/en/market/product/stock/quote/MOSHI/rights-benefits,0.8,MOSHI,2025-01-01,2026-07-01,True +MOSHI,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/MOSHI/rights-benefits,1.22,MOSHI,2025-01-01,2026-07-01,True +MTC,dividend,2025-04-28,https://www.set.or.th/en/market/product/stock/quote/MTC/rights-benefits,0.25,MTC,2025-01-01,2026-07-01,True +MTC,dividend,2026-04-28,https://www.set.or.th/en/market/product/stock/quote/MTC/rights-benefits,0.29,MTC,2025-01-01,2026-07-01,True +OR,dividend,2025-02-24,https://www.set.or.th/en/market/product/stock/quote/OR/rights-benefits,0.13,OR,2025-01-01,2026-07-01,True +OR,dividend,2025-09-04,https://www.set.or.th/en/market/product/stock/quote/OR/rights-benefits,0.3,OR,2025-01-01,2026-07-01,True +OR,dividend,2026-02-23,https://www.set.or.th/en/market/product/stock/quote/OR/rights-benefits,0.3,OR,2025-01-01,2026-07-01,True +ORI,dividend,2025-11-27,https://www.set.or.th/en/market/product/stock/quote/ORI/rights-benefits,0.021,ORI,2025-01-01,2026-07-01,True +ORI,dividend,2026-05-11,https://www.set.or.th/en/market/product/stock/quote/ORI/rights-benefits,0.049,ORI,2025-01-01,2026-07-01,True +OSP,dividend,2025-05-08,https://www.set.or.th/en/market/product/stock/quote/OSP/rights-benefits,0.3,OSP,2025-01-01,2026-07-01,True +OSP,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/OSP/rights-benefits,0.4,OSP,2025-01-01,2026-07-01,True +OSP,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/OSP/rights-benefits,0.4,OSP,2025-01-01,2026-07-01,True +PLANB,dividend,2025-11-24,https://www.set.or.th/en/market/product/stock/quote/PLANB/rights-benefits,0.1674,PLANB,2025-01-01,2026-07-01,True +PLANB,dividend,2026-04-20,https://www.set.or.th/en/market/product/stock/quote/PLANB/rights-benefits,0.0435,PLANB,2025-01-01,2026-07-01,True +PR9,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/PR9/rights-benefits,0.25,PR9,2025-01-01,2026-07-01,True +PR9,dividend,2025-11-20,https://www.set.or.th/en/market/product/stock/quote/PR9/rights-benefits,0.2,PR9,2025-01-01,2026-07-01,True +PR9,dividend,2026-04-30,https://www.set.or.th/en/market/product/stock/quote/PR9/rights-benefits,0.3,PR9,2025-01-01,2026-07-01,True +PRM,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/PRM/rights-benefits,0.24,PRM,2025-01-01,2026-07-01,True +PRM,dividend,2025-10-08,https://www.set.or.th/en/market/product/stock/quote/PRM/rights-benefits,0.25,PRM,2025-01-01,2026-07-01,True +PRM,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/PRM/rights-benefits,0.25,PRM,2025-01-01,2026-07-01,True +PTG,dividend,2025-03-10,https://www.set.or.th/en/market/product/stock/quote/PTG/rights-benefits,0.25,PTG,2025-01-01,2026-07-01,True +PTG,dividend,2026-03-10,https://www.set.or.th/en/market/product/stock/quote/PTG/rights-benefits,0.35,PTG,2025-01-01,2026-07-01,True +PTT,dividend,2025-03-06,https://www.set.or.th/en/market/product/stock/quote/PTT/rights-benefits,1.3,PTT,2025-01-01,2026-07-01,True +PTT,dividend,2025-10-01,https://www.set.or.th/en/market/product/stock/quote/PTT/rights-benefits,0.9,PTT,2025-01-01,2026-07-01,True +PTT,dividend,2026-03-05,https://www.set.or.th/en/market/product/stock/quote/PTT/rights-benefits,1.4,PTT,2025-01-01,2026-07-01,True +PTTEP,dividend,2025-02-25,https://www.set.or.th/en/market/product/stock/quote/PTTEP/rights-benefits,5.125,PTTEP,2025-01-01,2026-07-01,True +PTTEP,dividend,2025-08-08,https://www.set.or.th/en/market/product/stock/quote/PTTEP/rights-benefits,4.1,PTTEP,2025-01-01,2026-07-01,True +PTTEP,dividend,2026-02-23,https://www.set.or.th/en/market/product/stock/quote/PTTEP/rights-benefits,4.65,PTTEP,2025-01-01,2026-07-01,True +PTTGC,dividend,2025-03-03,https://www.set.or.th/en/market/product/stock/quote/PTTGC/rights-benefits,0.5,PTTGC,2025-01-01,2026-07-01,True +PTTGC,dividend,2026-02-24,https://www.set.or.th/en/market/product/stock/quote/PTTGC/rights-benefits,0.5,PTTGC,2025-01-01,2026-07-01,True +QH,dividend,2025-04-28,https://www.set.or.th/en/market/product/stock/quote/QH/rights-benefits,0.08,QH,2025-01-01,2026-07-01,True +QH,dividend,2025-08-29,https://www.set.or.th/en/market/product/stock/quote/QH/rights-benefits,0.02,QH,2025-01-01,2026-07-01,True +QH,dividend,2026-04-27,https://www.set.or.th/en/market/product/stock/quote/QH/rights-benefits,0.07,QH,2025-01-01,2026-07-01,True +RATCH,dividend,2025-03-17,https://www.set.or.th/en/market/product/stock/quote/RATCH/rights-benefits,0.8,RATCH,2025-01-01,2026-07-01,True +RATCH,dividend,2025-09-02,https://www.set.or.th/en/market/product/stock/quote/RATCH/rights-benefits,0.8,RATCH,2025-01-01,2026-07-01,True +RATCH,dividend,2026-03-12,https://www.set.or.th/en/market/product/stock/quote/RATCH/rights-benefits,0.8,RATCH,2025-01-01,2026-07-01,True +RBF,dividend,2025-05-06,https://www.set.or.th/en/market/product/stock/quote/RBF/rights-benefits,0.175,RBF,2025-01-01,2026-07-01,True +RBF,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/RBF/rights-benefits,0.21,RBF,2025-01-01,2026-07-01,True +RCL,dividend,2025-03-12,https://www.set.or.th/en/market/product/stock/quote/RCL/rights-benefits,1.5,RCL,2025-01-01,2026-07-01,True +RCL,dividend,2025-08-22,https://www.set.or.th/en/market/product/stock/quote/RCL/rights-benefits,0.5,RCL,2025-01-01,2026-07-01,True +RCL,dividend,2025-11-20,https://www.set.or.th/en/market/product/stock/quote/RCL/rights-benefits,0.5,RCL,2025-01-01,2026-07-01,True +RCL,dividend,2026-03-11,https://www.set.or.th/en/market/product/stock/quote/RCL/rights-benefits,1.5,RCL,2025-01-01,2026-07-01,True +ROJNA,dividend,2025-05-02,https://www.set.or.th/en/market/product/stock/quote/ROJNA/rights-benefits,0.5,ROJNA,2025-01-01,2026-07-01,True +ROJNA,dividend,2026-04-30,https://www.set.or.th/en/market/product/stock/quote/ROJNA/rights-benefits,0.5,ROJNA,2025-01-01,2026-07-01,True +SAPPE,dividend,2025-04-22,https://www.set.or.th/en/market/product/stock/quote/SAPPE/rights-benefits,2.25,SAPPE,2025-01-01,2026-07-01,True +SAPPE,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/SAPPE/rights-benefits,1.75,SAPPE,2025-01-01,2026-07-01,True +SAWAD,dividend,2025-05-08,https://www.set.or.th/en/market/product/stock/quote/SAWAD/rights-benefits,0.04,SAWAD,2025-01-01,2026-07-01,True +SAWAD,dividend,2025-10-06,https://www.set.or.th/en/market/product/stock/quote/SAWAD/rights-benefits,0.35,SAWAD,2025-01-01,2026-07-01,True +SAWAD,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/SAWAD/rights-benefits,0.35,SAWAD,2025-01-01,2026-07-01,True +SCB,dividend,2025-04-16,https://www.set.or.th/en/market/product/stock/quote/SCB/rights-benefits,8.44,SCB,2025-01-01,2026-07-01,True +SCB,dividend,2025-09-02,https://www.set.or.th/en/market/product/stock/quote/SCB/rights-benefits,2.0,SCB,2025-01-01,2026-07-01,True +SCB,dividend,2026-04-20,https://www.set.or.th/en/market/product/stock/quote/SCB/rights-benefits,9.28,SCB,2025-01-01,2026-07-01,True +SCC,dividend,2025-04-02,https://www.set.or.th/en/market/product/stock/quote/SCC/rights-benefits,2.5,SCC,2025-01-01,2026-07-01,True +SCC,dividend,2025-08-13,https://www.set.or.th/en/market/product/stock/quote/SCC/rights-benefits,2.5,SCC,2025-01-01,2026-07-01,True +SCC,dividend,2026-04-01,https://www.set.or.th/en/market/product/stock/quote/SCC/rights-benefits,2.5,SCC,2025-01-01,2026-07-01,True +SCGP,dividend,2025-04-01,https://www.set.or.th/en/market/product/stock/quote/SCGP/rights-benefits,0.3,SCGP,2025-01-01,2026-07-01,True +SCGP,dividend,2025-08-08,https://www.set.or.th/en/market/product/stock/quote/SCGP/rights-benefits,0.25,SCGP,2025-01-01,2026-07-01,True +SCGP,dividend,2026-03-31,https://www.set.or.th/en/market/product/stock/quote/SCGP/rights-benefits,0.35,SCGP,2025-01-01,2026-07-01,True +SIRI,dividend,2025-03-17,https://www.set.or.th/en/market/product/stock/quote/SIRI/rights-benefits,0.08,SIRI,2025-01-01,2026-07-01,True +SIRI,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/SIRI/rights-benefits,0.05,SIRI,2025-01-01,2026-07-01,True +SIRI,dividend,2026-03-17,https://www.set.or.th/en/market/product/stock/quote/SIRI/rights-benefits,0.08,SIRI,2025-01-01,2026-07-01,True +SISB,dividend,2025-03-07,https://www.set.or.th/en/market/product/stock/quote/SISB/rights-benefits,0.42,SISB,2025-01-01,2026-07-01,True +SISB,dividend,2026-03-06,https://www.set.or.th/en/market/product/stock/quote/SISB/rights-benefits,0.51,SISB,2025-01-01,2026-07-01,True +SJWD,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/SJWD/rights-benefits,0.28,SJWD,2025-01-01,2026-07-01,True +SJWD,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/SJWD/rights-benefits,0.1,SJWD,2025-01-01,2026-07-01,True +SJWD,dividend,2026-04-30,https://www.set.or.th/en/market/product/stock/quote/SJWD/rights-benefits,0.3,SJWD,2025-01-01,2026-07-01,True +SKY,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/SKY/rights-benefits,0.3,SKY,2025-01-01,2026-07-01,True +SNNP,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/SNNP/rights-benefits,0.35,SNNP,2025-01-01,2026-07-01,True +SNNP,dividend,2025-08-21,https://www.set.or.th/en/market/product/stock/quote/SNNP/rights-benefits,0.25,SNNP,2025-01-01,2026-07-01,True +SNNP,dividend,2026-05-05,https://www.set.or.th/en/market/product/stock/quote/SNNP/rights-benefits,0.345,SNNP,2025-01-01,2026-07-01,True +SPALI,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/SPALI/rights-benefits,0.85,SPALI,2025-01-01,2026-07-01,True +SPALI,dividend,2025-08-27,https://www.set.or.th/en/market/product/stock/quote/SPALI/rights-benefits,0.55,SPALI,2025-01-01,2026-07-01,True +SPALI,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/SPALI/rights-benefits,0.7,SPALI,2025-01-01,2026-07-01,True +SPRC,dividend,2025-03-06,https://www.set.or.th/en/market/product/stock/quote/SPRC/rights-benefits,0.15,SPRC,2025-01-01,2026-07-01,True +SPRC,dividend,2025-08-21,https://www.set.or.th/en/market/product/stock/quote/SPRC/rights-benefits,0.15,SPRC,2025-01-01,2026-07-01,True +SPRC,dividend,2026-03-05,https://www.set.or.th/en/market/product/stock/quote/SPRC/rights-benefits,0.3,SPRC,2025-01-01,2026-07-01,True +STA,dividend,2025-04-18,https://www.set.or.th/en/market/product/stock/quote/STA/rights-benefits,1.0,STA,2025-01-01,2026-07-01,True +STA,dividend,2026-04-21,https://www.set.or.th/en/market/product/stock/quote/STA/rights-benefits,0.5,STA,2025-01-01,2026-07-01,True +STECON,dividend,2026-03-09,https://www.set.or.th/en/market/product/stock/quote/STECON/rights-benefits,0.55,STECON,2025-01-01,2026-07-01,True +STGT,dividend,2025-04-18,https://www.set.or.th/en/market/product/stock/quote/STGT/rights-benefits,0.5,STGT,2025-01-01,2026-07-01,True +STGT,dividend,2026-04-21,https://www.set.or.th/en/market/product/stock/quote/STGT/rights-benefits,0.5,STGT,2025-01-01,2026-07-01,True +TASCO,dividend,2025-03-03,https://www.set.or.th/en/market/product/stock/quote/TASCO/rights-benefits,0.9,TASCO,2025-01-01,2026-07-01,True +TASCO,dividend,2025-11-21,https://www.set.or.th/en/market/product/stock/quote/TASCO/rights-benefits,0.8,TASCO,2025-01-01,2026-07-01,True +TASCO,dividend,2026-03-04,https://www.set.or.th/en/market/product/stock/quote/TASCO/rights-benefits,0.2,TASCO,2025-01-01,2026-07-01,True +TCAP,dividend,2025-04-11,https://www.set.or.th/en/market/product/stock/quote/TCAP/rights-benefits,2.05,TCAP,2025-01-01,2026-07-01,True +TCAP,dividend,2025-09-12,https://www.set.or.th/en/market/product/stock/quote/TCAP/rights-benefits,1.3,TCAP,2025-01-01,2026-07-01,True +TCAP,dividend,2026-04-16,https://www.set.or.th/en/market/product/stock/quote/TCAP/rights-benefits,2.2,TCAP,2025-01-01,2026-07-01,True +TFG,dividend,2025-03-07,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.225,TFG,2025-01-01,2026-07-01,True +TFG,dividend,2025-05-27,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.075,TFG,2025-01-01,2026-07-01,True +TFG,dividend,2025-08-22,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.225,TFG,2025-01-01,2026-07-01,True +TFG,dividend,2025-11-26,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.1,TFG,2025-01-01,2026-07-01,True +TFG,dividend,2026-03-04,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.3,TFG,2025-01-01,2026-07-01,True +TFG,dividend,2026-05-25,https://www.set.or.th/en/market/product/stock/quote/TFG/rights-benefits,0.085,TFG,2025-01-01,2026-07-01,True +THG,dividend,2026-03-12,https://www.set.or.th/en/market/product/stock/quote/THG/rights-benefits,0.14,THG,2025-01-01,2026-07-01,True +TIDLOR,dividend,2025-12-08,https://www.set.or.th/en/market/product/stock/quote/TIDLOR/rights-benefits,0.34,TIDLOR,2025-01-01,2026-07-01,True +TIDLOR,dividend,2026-06-02,https://www.set.or.th/en/market/product/stock/quote/TIDLOR/rights-benefits,0.69,TIDLOR,2025-01-01,2026-07-01,True +TIPH,dividend,2025-03-10,https://www.set.or.th/en/market/product/stock/quote/TIPH/rights-benefits,1.0,TIPH,2025-01-01,2026-07-01,True +TIPH,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/TIPH/rights-benefits,0.5,TIPH,2025-01-01,2026-07-01,True +TIPH,dividend,2026-03-13,https://www.set.or.th/en/market/product/stock/quote/TIPH/rights-benefits,1.0,TIPH,2025-01-01,2026-07-01,True +TISCO,dividend,2025-04-25,https://www.set.or.th/en/market/product/stock/quote/TISCO/rights-benefits,5.75,TISCO,2025-01-01,2026-07-01,True +TISCO,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/TISCO/rights-benefits,2.0,TISCO,2025-01-01,2026-07-01,True +TISCO,dividend,2026-04-29,https://www.set.or.th/en/market/product/stock/quote/TISCO/rights-benefits,5.75,TISCO,2025-01-01,2026-07-01,True +TKN,dividend,2025-03-19,https://www.set.or.th/en/market/product/stock/quote/TKN/rights-benefits,0.207,TKN,2025-01-01,2026-07-01,True +TKN,dividend,2025-08-26,https://www.set.or.th/en/market/product/stock/quote/TKN/rights-benefits,0.12,TKN,2025-01-01,2026-07-01,True +TKN,dividend,2026-03-09,https://www.set.or.th/en/market/product/stock/quote/TKN/rights-benefits,0.13,TKN,2025-01-01,2026-07-01,True +TLI,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/TLI/rights-benefits,0.5,TLI,2025-01-01,2026-07-01,True +TLI,dividend,2026-05-07,https://www.set.or.th/en/market/product/stock/quote/TLI/rights-benefits,0.6,TLI,2025-01-01,2026-07-01,True +TOA,dividend,2025-05-09,https://www.set.or.th/en/market/product/stock/quote/TOA/rights-benefits,0.27,TOA,2025-01-01,2026-07-01,True +TOA,dividend,2025-08-28,https://www.set.or.th/en/market/product/stock/quote/TOA/rights-benefits,0.36,TOA,2025-01-01,2026-07-01,True +TOA,dividend,2026-05-06,https://www.set.or.th/en/market/product/stock/quote/TOA/rights-benefits,0.39,TOA,2025-01-01,2026-07-01,True +TOP,dividend,2025-02-27,https://www.set.or.th/en/market/product/stock/quote/TOP/rights-benefits,0.7,TOP,2025-01-01,2026-07-01,True +TOP,dividend,2025-09-10,https://www.set.or.th/en/market/product/stock/quote/TOP/rights-benefits,0.8,TOP,2025-01-01,2026-07-01,True +TOP,dividend,2026-02-25,https://www.set.or.th/en/market/product/stock/quote/TOP/rights-benefits,1.0,TOP,2025-01-01,2026-07-01,True +TRUE,dividend,2025-11-17,https://www.set.or.th/en/market/product/stock/quote/TRUE/rights-benefits,0.19,TRUE,2025-01-01,2026-07-01,True +TRUE,dividend,2026-05-08,https://www.set.or.th/en/market/product/stock/quote/TRUE/rights-benefits,0.12,TRUE,2025-01-01,2026-07-01,True +TRUE,dividend,2026-05-20,https://www.set.or.th/en/market/product/stock/quote/TRUE/rights-benefits,0.14,TRUE,2025-01-01,2026-07-01,True +TTB,dividend,2025-04-25,https://www.set.or.th/en/market/product/stock/quote/TTB/rights-benefits,0.065,TTB,2025-01-01,2026-07-01,True +TTB,dividend,2025-10-06,https://www.set.or.th/en/market/product/stock/quote/TTB/rights-benefits,0.066,TTB,2025-01-01,2026-07-01,True +TTB,dividend,2026-04-27,https://www.set.or.th/en/market/product/stock/quote/TTB/rights-benefits,0.068,TTB,2025-01-01,2026-07-01,True +TU,dividend,2025-02-28,https://www.set.or.th/en/market/product/stock/quote/TU/rights-benefits,0.35,TU,2025-01-01,2026-07-01,True +TU,dividend,2025-08-15,https://www.set.or.th/en/market/product/stock/quote/TU/rights-benefits,0.35,TU,2025-01-01,2026-07-01,True +TU,dividend,2026-03-02,https://www.set.or.th/en/market/product/stock/quote/TU/rights-benefits,0.35,TU,2025-01-01,2026-07-01,True +VGI,dividend,2025-07-29,https://www.set.or.th/en/market/product/stock/quote/VGI/rights-benefits,0.013,VGI,2025-01-01,2026-07-01,True +WHA,dividend,2025-05-07,https://www.set.or.th/en/market/product/stock/quote/WHA/rights-benefits,0.1237,WHA,2025-01-01,2026-07-01,True +WHA,dividend,2025-11-27,https://www.set.or.th/en/market/product/stock/quote/WHA/rights-benefits,0.0669,WHA,2025-01-01,2026-07-01,True +WHA,dividend,2026-05-11,https://www.set.or.th/en/market/product/stock/quote/WHA/rights-benefits,0.1438,WHA,2025-01-01,2026-07-01,True +WHAUP,dividend,2025-04-28,https://www.set.or.th/en/market/product/stock/quote/WHAUP/rights-benefits,0.1925,WHAUP,2025-01-01,2026-07-01,True +WHAUP,dividend,2025-11-27,https://www.set.or.th/en/market/product/stock/quote/WHAUP/rights-benefits,0.06,WHAUP,2025-01-01,2026-07-01,True +WHAUP,dividend,2026-04-28,https://www.set.or.th/en/market/product/stock/quote/WHAUP/rights-benefits,0.1925,WHAUP,2025-01-01,2026-07-01,True diff --git a/data/reference/set_actions_2025_2026_h1.csv.manifest.json b/data/reference/set_actions_2025_2026_h1.csv.manifest.json new file mode 100644 index 0000000..7a94869 --- /dev/null +++ b/data/reference/set_actions_2025_2026_h1.csv.manifest.json @@ -0,0 +1,271 @@ +{ + "schema_version": 1, + "provider": "SET public Rights & Benefits history", + "source_page": "https://www.set.or.th/en/market/product/stock/quote/{symbol}/rights-benefits", + "source_endpoint": "https://www.set.or.th/api/set/stock/{symbol}/corporate-action?lang=en", + "start_inclusive": "2025-01-01", + "end_exclusive": "2026-07-01", + "requested_price_symbols": [ + "AAV", + "ADVANC", + "AEONTS", + "AMATA", + "AOT", + "AP", + "AURA", + "AWC", + "BA", + "BAM", + "BANPU", + "BBL", + "BCH", + "BCP", + "BCPG", + "BDMS", + "BEM", + "BGRIM", + "BH", + "BJC", + "BLA", + "BSRC", + "BTG", + "BTS", + "BYD", + "CBG", + "CCET", + "CENTEL", + "CHG", + "CK", + "CKP", + "COCOCO", + "COM7", + "CPALL", + "CPF", + "CPN", + "CRC", + "DELTA", + "DOHOME", + "EA", + "EGCO", + "ERW", + "FORTH", + "GFPT", + "GLOBAL", + "GPSC", + "GULF", + "GULFI", + "GUNKUL", + "HANA", + "HMPRO", + "ICHI", + "INTUCH", + "IRPC", + "ITC", + "IVL", + "JAS", + "JMART", + "JMT", + "JTS", + "KBANK", + "KCE", + "KKP", + "KTB", + "KTC", + "LH", + "M", + "MBK", + "MEGA", + "MINT", + "MOSHI", + "MTC", + "NEX", + "OR", + "ORI", + "OSP", + "PLANB", + "PR9", + "PRM", + "PTG", + "PTT", + "PTTEP", + "PTTGC", + "QH", + "RATCH", + "RBF", + "RCL", + "ROJNA", + "SAPPE", + "SAWAD", + "SCB", + "SCC", + "SCGP", + "SIRI", + "SISB", + "SJWD", + "SKY", + "SNNP", + "SPALI", + "SPRC", + "STA", + "STECON", + "STGT", + "TASCO", + "TCAP", + "TFG", + "THG", + "TIDLOR", + "TIPH", + "TISCO", + "TKN", + "TLI", + "TOA", + "TOP", + "TRUE", + "TTB", + "TU", + "VGI", + "WHA", + "WHAUP" + ], + "fetched_set_symbols": [ + "AAV", + "ADVANC", + "AEONTS", + "AMATA", + "AOT", + "AP", + "AURA", + "AWC", + "BA", + "BAM", + "BANPU", + "BANPUU", + "BBL", + "BCH", + "BCP", + "BCPG", + "BDMS", + "BEM", + "BGRIM", + "BH", + "BJC", + "BLA", + "BSRC", + "BTG", + "BTS", + "BYD", + "CBG", + "CCET", + "CENTEL", + "CHG", + "CK", + "CKP", + "COCOCO", + "COM7", + "CPALL", + "CPF", + "CPN", + "CRC", + "DELTA", + "DOHOME", + "EA", + "EGCO", + "ERW", + "FORTH", + "GFPT", + "GLOBAL", + "GPSC", + "GULF", + "GULFI", + "GUNKUL", + "HANA", + "HMPRO", + "ICHI", + "INTUCH", + "IRPC", + "ITC", + "IVL", + "JAS", + "JMART", + "JMT", + "JTS", + "KBANK", + "KCE", + "KKP", + "KTB", + "KTC", + "LH", + "M", + "MBK", + "MEGA", + "MINT", + "MOSHI", + "MTC", + "NEX", + "OR", + "ORI", + "OSP", + "PLANB", + "PR9", + "PRM", + "PTG", + "PTT", + "PTTEP", + "PTTGC", + "QH", + "RATCH", + "RBF", + "RCL", + "ROJNA", + "SAPPE", + "SAWAD", + "SCB", + "SCC", + "SCGP", + "SIRI", + "SISB", + "SJWD", + "SKY", + "SNNP", + "SPALI", + "SPRC", + "STA", + "STECON", + "STGT", + "TASCO", + "TCAP", + "TFG", + "THG", + "TIDLOR", + "TIPH", + "TISCO", + "TKN", + "TLI", + "TOA", + "TOP", + "TRUE", + "TTB", + "TU", + "VGI", + "WHA", + "WHAUP" + ], + "symbol_aliases": [ + { + "source_symbol": "BANPUU", + "price_symbol": "BANPU", + "start_date": "2025-01-01", + "end_date": "2026-07-01", + "reason": "SET cash-action endpoint reports these historical distributions under BANPUU; dates and values match SiamChart BANPU history. Used only to reconcile actions to the existing BANPU price series." + }, + { + "source_symbol": "GULFI", + "price_symbol": "GULF", + "start_date": "2025-01-01", + "end_date": "2026-07-01", + "reason": "SET historical ticker for the GULF price series; the 2025-02-19 XD date and 1.01 THB amount match SiamChart GULF. Used only to reconcile actions, not to relabel prices." + } + ], + "coverage_complete": true, + "issues": [] +} \ No newline at end of file diff --git a/data/reference/set_symbol_aliases.csv b/data/reference/set_symbol_aliases.csv new file mode 100644 index 0000000..6dc3d54 --- /dev/null +++ b/data/reference/set_symbol_aliases.csv @@ -0,0 +1,3 @@ +source_symbol,price_symbol,start_date,end_date,reason +BANPUU,BANPU,2025-01-01,2026-07-01,"SET cash-action endpoint reports these historical distributions under BANPUU; dates and values match SiamChart BANPU history. Used only to reconcile actions to the existing BANPU price series." +GULFI,GULF,2025-01-01,2026-07-01,"SET historical ticker for the GULF price series; the 2025-02-19 XD date and 1.01 THB amount match SiamChart GULF. Used only to reconcile actions, not to relabel prices." diff --git a/data/reference/siamchart_actions_set_reference_sample.csv b/data/reference/siamchart_actions_set_reference_sample.csv new file mode 100644 index 0000000..0aadba7 --- /dev/null +++ b/data/reference/siamchart_actions_set_reference_sample.csv @@ -0,0 +1,3 @@ +symbol,action_type,date,value,source_url +AP,dividend,2024-05-08,0.70,https://www.set.or.th/en/market/news-and-alert/newsdetails?id=86190600&symbol=AP +INTUCH,dividend,2025-02-19,6.54,https://www.set.or.th/en/market/news-and-alert/newsdetails?id=93418200&symbol=INTUCH diff --git a/data/universes/README.md b/data/universes/README.md new file mode 100644 index 0000000..c670ecb --- /dev/null +++ b/data/universes/README.md @@ -0,0 +1,20 @@ +# SET100 constituent data + +`set100_pit_2024_2026_h1.csv` contains dated SET100 membership from **January 2, 2024 through June 30, 2026**. Each row is an inclusive interval for a symbol. `build_set100_pit.py` recreates it from the reviewed snapshots and dated SET notices. The original `set100_2026_h1.csv` remains as the official H1 2026 fixture. + +The H1 2025 interval is split for SET's between-review changes: MOSHI replaced BSRC on February 27; GULF's ticker changed to GULFI before the March 21 suspension; the merger changes took effect in the index on April 2, with GULF and ROJNA replacing GULFI and INTUCH; SNNP replaced JTS on April 16. SET's May 15 TIDLOR restructuring replaces NTL with TIDLOR; the membership file tracks the SET100 symbol `TIDLOR` continuously across that replacement because this CSV does not encode issuer identity or share-swap price treatment. + +Sector labels are blank for 2024 and 2025. They are only populated for H1 2026, where the official SET constituent table supplies them. The loader treats blank sectors as unavailable; no historical sector is inferred from a later classification. + +## Official sources + +- [SET100 H1 2024 constituents](https://media.set.or.th/set/Documents/2023/Dec/SET50_100_H1_2024.pdf) +- [SET100 H2 2024 constituents and inclusion/exclusion table](https://media.set.or.th/set/Documents/2024/Jun/SET50_100_H2_2024.pdf) +- [SET100 H1 2025 constituents](https://media.set.or.th/set/Documents/2024/Dec/SET50_100_H1_2025.pdf) and [revised list](https://media.set.or.th/set/Documents/2025/Feb/SET50_100_H1_2025_revise.pdf) +- [SET100 H2 2025 constituents](https://media.set.or.th/set/Documents/2025/Jun/SET50_100_H2_2025.pdf) +- [SET100 H1 2026 constituents](https://media.set.or.th/set/Documents/2025/Dec/SET50_100_H1_2026.pdf) +- Interim notices: [MOSHI added / BSRC removed, effective February 27, 2025](https://www.set.or.th/en/market/news-and-alert/newsdetails?id=93883200&symbol=SET), [GULF/INTUCH merger and ROJNA replacement, effective April 2, 2025](https://www.set.or.th/en/market/news-and-alert/newsdetails?id=95454500&symbol=SET), [JTS removed, effective April 16, 2025](https://www.set.or.th/en/market/news-and-alert/newsdetails?id=95606500&symbol=JTS), [SNNP added, effective April 16, 2025](https://www.set.or.th/th/market/news-and-alert/newsdetails?id=95606801&symbol=SNNP), and [TIDLOR replaces NTL, effective May 15, 2025](https://www.set.or.th/en/market/news-and-alert/newsdetails?id=96768700&symbol=TIDLOR). + +The SET archive lists a second H2 2026 file marked “between the periodic review update,” but its full constituent file is not available in the public materials used here. The H2 2026 membership is therefore **not included**; accurate mode should stop at June 30, 2026 rather than carry the H1 list forward. The archive page is [SET50 and SET100 constituents](https://www.set.or.th/en/market/information/securities-list/constituents-list-set50-set100). + +CSV columns are `symbol,start_date,end_date,sector`. The point-in-time loader rejects overlapping intervals and accurate backtests reject dates without membership data. Sector history and security identity through mergers/share swaps are separate data requirements; this file supplies membership symbols and dates only. diff --git a/data/universes/build_set100_pit.py b/data/universes/build_set100_pit.py new file mode 100644 index 0000000..cd77b25 --- /dev/null +++ b/data/universes/build_set100_pit.py @@ -0,0 +1,159 @@ +"""Build the dated SET100 universe from public SET constituent publications. + +The symbol snapshots below are transcribed from the official SET PDFs listed in +data/universes/README.md. The 2025 H1 events are dated from SET's index notices. +""" + +from __future__ import annotations + +import csv +from collections import defaultdict +from dataclasses import dataclass +from datetime import date, timedelta +from pathlib import Path + + +HERE = Path(__file__).resolve().parent + + +def symbols(value: str) -> set[str]: + return set(value.split()) + + +SET100_H2_2024 = symbols(""" +AAV ADVANC AEONTS AMATA AOT AP AWC BA BAM BANPU BBL BCH BCP BCPG BDMS BEM +BGRIM BH BJC BLA BSRC BTG BTS CBG CENTEL CHG CK CKP COM7 CPALL CPF CPN CRC +DELTA DOHOME EA EGCO ERW GLOBAL GPSC GULF GUNKUL HANA HMPRO ICHI INTUCH IRPC +ITC IVL JAS JMART JMT KBANK KCE KKP KTB KTC LH M MBK MEGA MINT MTC OR OSP +PLANB PRM PTT PTTEP PTTGC QH RATCH RBF RCL SAPPE SAWAD SCB SCC SCGP SIRI +SISB SJWD SKY SPALI SPRC STA STGT TASCO TCAP TIDLOR TIPH TISCO TLI TOA TOP +TRUE TTB TU VGI WHA +""") + +# SET's H2 2024 PDF lists the nine inclusions and nine exclusions. Reversing +# those dated changes reconstructs the January-June 2024 review snapshot. +H2_2024_INCLUSIONS = symbols("BA BJC CKP JAS MBK PRM QH SKY TIPH") +H2_2024_EXCLUSIONS = symbols("AURA BYD FORTH MOSHI NEX ORI SNNP THG TKN") +SET100_H1_2024 = (SET100_H2_2024 - H2_2024_INCLUSIONS) | H2_2024_EXCLUSIONS + +SET100_H1_2025 = symbols(""" +AAV ADVANC AEONTS AMATA AOT AP AWC BA BAM BANPU BBL BCH BCP BCPG BDMS BEM +BGRIM BH BJC BLA BSRC BTG BTS CBG CCET CENTEL CHG CK CKP COCOCO COM7 CPALL +CPF CPN CRC DELTA DOHOME EA EGCO ERW GLOBAL GPSC GULF GUNKUL HANA HMPRO ICHI +INTUCH IRPC ITC IVL JAS JMART JMT JTS KBANK KCE KKP KTB KTC LH M MEGA MINT +MTC OR OSP PLANB PR9 PRM PTT PTTEP PTTGC QH RATCH RCL SAPPE SAWAD SCB SCC +SCGP SIRI SISB SJWD SKY SPALI SPRC STA STGT TASCO TCAP TIDLOR TISCO TLI TOP +TRUE TTB TU VGI WHA +""") + +SET100_H2_2025 = symbols(""" +AAV ADVANC AEONTS AMATA AOT AP AURA AWC BA BAM BANPU BBL BCH BCP BCPG BDMS +BEM BGRIM BH BJC BLA BTG BTS CBG CCET CENTEL CHG CK COM7 CPALL CPF CPN CRC +DELTA DOHOME EA EGCO ERW GLOBAL GPSC GULF GUNKUL HANA HMPRO ICHI IRPC ITC +IVL JAS JMART JMT JTS KBANK KCE KKP KTB KTC LH M MBK MEGA MINT MOSHI MTC OR +OSP PLANB PR9 PRM PTT PTTEP PTTGC QH RATCH RCL SAWAD SCB SCC SCGP SIRI SISB +SJWD SPALI SPRC STA STGT TASCO TCAP TFG TIDLOR TISCO TLI TOA TOP TRUE TTB TU +VGI WHA WHAUP +""") + + +@dataclass +class Snapshot: + start: date + end: date + members: set[str] + sectors: dict[str, str] + + +def change(members: set[str], *, add: str = "", remove: str = "") -> set[str]: + updated = set(members) + for symbol in symbols(remove): + if symbol not in updated: + raise ValueError(f"cannot remove missing SET100 member: {symbol}") + updated.remove(symbol) + for symbol in symbols(add): + if symbol in updated: + raise ValueError(f"cannot add existing SET100 member: {symbol}") + updated.add(symbol) + if len(updated) != 100: + raise ValueError(f"dated SET100 snapshot has {len(updated)} members") + return updated + + +def snapshots() -> list[Snapshot]: + current = set(SET100_H1_2025) + result = [ + Snapshot(date(2024, 1, 2), date(2024, 6, 30), SET100_H1_2024, {}), + Snapshot(date(2024, 7, 1), date(2024, 12, 31), SET100_H2_2024, {}), + Snapshot(date(2025, 1, 1), date(2025, 2, 26), current, {}), + ] + + # 27 Feb 2025: MOSHI replaced BSRC. + current = change(current, add="MOSHI", remove="BSRC") + result.append(Snapshot(date(2025, 2, 27), date(2025, 3, 20), current, {})) + + # GULF's symbol changed to GULFI when the suspension began on 21 Mar 2025. + current = change(current, add="GULFI", remove="GULF") + result.append(Snapshot(date(2025, 3, 21), date(2025, 4, 1), current, {})) + + # 2 Apr 2025: the merger delisted GULFI and INTUCH; new GULF and reserve + # stock ROJNA entered SET100. + current = change(current, add="GULF ROJNA", remove="GULFI INTUCH") + result.append(Snapshot(date(2025, 4, 2), date(2025, 4, 15), current, {})) + + # 16 Apr 2025: SNNP replaced JTS after JTS defaulted on bond payments. + current = change(current, add="SNNP", remove="JTS") + result.append(Snapshot(date(2025, 4, 16), date(2025, 6, 30), current, {})) + + result.extend([ + Snapshot(date(2025, 7, 1), date(2025, 12, 31), SET100_H2_2025, {}), + ]) + + # Preserve SET's H1 2026 sector labels only for their effective dates. + h1_2026 = HERE / "set100_2026_h1.csv" + sectors: dict[str, str] = {} + members: set[str] = set() + with h1_2026.open(encoding="utf-8-sig", newline="") as source: + for row in csv.DictReader(source): + symbol = row["symbol"].strip().upper() + members.add(symbol) + sectors[symbol] = row.get("sector", "").strip() + result.append(Snapshot(date(2026, 1, 1), date(2026, 6, 30), members, sectors)) + return result + + +def build() -> Path: + by_symbol: dict[str, list[dict[str, str]]] = defaultdict(list) + for snapshot in snapshots(): + if len(snapshot.members) != 100: + raise ValueError( + f"{snapshot.start} snapshot has {len(snapshot.members)} members, expected 100" + ) + for symbol in sorted(snapshot.members): + sector = snapshot.sectors.get(symbol, "") + rows = by_symbol[symbol] + if (rows and rows[-1]["sector"] == sector + and date.fromisoformat(rows[-1]["end_date"]) + timedelta(days=1) + == snapshot.start): + rows[-1]["end_date"] = snapshot.end.isoformat() + else: + rows.append({ + "symbol": symbol, + "start_date": snapshot.start.isoformat(), + "end_date": snapshot.end.isoformat(), + "sector": sector, + }) + + output = HERE / "set100_pit_2024_2026_h1.csv" + with output.open("w", encoding="utf-8", newline="") as destination: + writer = csv.DictWriter( + destination, fieldnames=["symbol", "start_date", "end_date", "sector"] + ) + writer.writeheader() + for symbol in sorted(by_symbol): + writer.writerows(by_symbol[symbol]) + return output + + +if __name__ == "__main__": + print(build()) diff --git a/data/universes/set100_2026_h1.csv b/data/universes/set100_2026_h1.csv new file mode 100644 index 0000000..8b6b55e --- /dev/null +++ b/data/universes/set100_2026_h1.csv @@ -0,0 +1,101 @@ +symbol,start_date,end_date,sector +AAV,2026-01-01,2026-06-30,Transportation & Logistics +ADVANC,2026-01-01,2026-06-30,Information & Communication Technology +AEONTS,2026-01-01,2026-06-30,Finance & Securities +AMATA,2026-01-01,2026-06-30,Property Development +AOT,2026-01-01,2026-06-30,Transportation & Logistics +AP,2026-01-01,2026-06-30,Property Development +AURA,2026-01-01,2026-06-30,Fashion +AWC,2026-01-01,2026-06-30,Property Development +BA,2026-01-01,2026-06-30,Transportation & Logistics +BAM,2026-01-01,2026-06-30,Finance & Securities +BANPU,2026-01-01,2026-06-30,Energy & Utilities +BBL,2026-01-01,2026-06-30,Banking +BCH,2026-01-01,2026-06-30,Health Care Services +BCP,2026-01-01,2026-06-30,Energy & Utilities +BCPG,2026-01-01,2026-06-30,Energy & Utilities +BDMS,2026-01-01,2026-06-30,Health Care Services +BEM,2026-01-01,2026-06-30,Transportation & Logistics +BGRIM,2026-01-01,2026-06-30,Energy & Utilities +BH,2026-01-01,2026-06-30,Health Care Services +BJC,2026-01-01,2026-06-30,Commerce +BLA,2026-01-01,2026-06-30,Insurance +BTG,2026-01-01,2026-06-30,Food & Beverage +BTS,2026-01-01,2026-06-30,Transportation & Logistics +CBG,2026-01-01,2026-06-30,Food & Beverage +CCET,2026-01-01,2026-06-30,Electronic Components +CENTEL,2026-01-01,2026-06-30,Tourism & Leisure +CHG,2026-01-01,2026-06-30,Health Care Services +CK,2026-01-01,2026-06-30,Construction Services +COM7,2026-01-01,2026-06-30,Commerce +CPALL,2026-01-01,2026-06-30,Commerce +CPF,2026-01-01,2026-06-30,Food & Beverage +CPN,2026-01-01,2026-06-30,Property Development +CRC,2026-01-01,2026-06-30,Commerce +DELTA,2026-01-01,2026-06-30,Electronic Components +DOHOME,2026-01-01,2026-06-30,Commerce +EA,2026-01-01,2026-06-30,Energy & Utilities +EGCO,2026-01-01,2026-06-30,Energy & Utilities +ERW,2026-01-01,2026-06-30,Tourism & Leisure +GFPT,2026-01-01,2026-06-30,Agribusiness +GLOBAL,2026-01-01,2026-06-30,Commerce +GPSC,2026-01-01,2026-06-30,Energy & Utilities +GULF,2026-01-01,2026-06-30,Energy & Utilities +GUNKUL,2026-01-01,2026-06-30,Energy & Utilities +HANA,2026-01-01,2026-06-30,Electronic Components +HMPRO,2026-01-01,2026-06-30,Commerce +ICHI,2026-01-01,2026-06-30,Food & Beverage +IRPC,2026-01-01,2026-06-30,Energy & Utilities +IVL,2026-01-01,2026-06-30,Petrochemicals & Chemicals +JAS,2026-01-01,2026-06-30,Information & Communication Technology +JMART,2026-01-01,2026-06-30,Information & Communication Technology +JMT,2026-01-01,2026-06-30,Finance & Securities +JTS,2026-01-01,2026-06-30,Information & Communication Technology +KBANK,2026-01-01,2026-06-30,Banking +KCE,2026-01-01,2026-06-30,Electronic Components +KKP,2026-01-01,2026-06-30,Banking +KTB,2026-01-01,2026-06-30,Banking +KTC,2026-01-01,2026-06-30,Finance & Securities +LH,2026-01-01,2026-06-30,Property Development +M,2026-01-01,2026-06-30,Food & Beverage +MEGA,2026-01-01,2026-06-30,Commerce +MINT,2026-01-01,2026-06-30,Tourism & Leisure +MOSHI,2026-01-01,2026-06-30,Commerce +MTC,2026-01-01,2026-06-30,Finance & Securities +OR,2026-01-01,2026-06-30,Energy & Utilities +OSP,2026-01-01,2026-06-30,Food & Beverage +PLANB,2026-01-01,2026-06-30,Media & Publishing +PR9,2026-01-01,2026-06-30,Health Care Services +PRM,2026-01-01,2026-06-30,Transportation & Logistics +PTG,2026-01-01,2026-06-30,Energy & Utilities +PTT,2026-01-01,2026-06-30,Energy & Utilities +PTTEP,2026-01-01,2026-06-30,Energy & Utilities +PTTGC,2026-01-01,2026-06-30,Petrochemicals & Chemicals +QH,2026-01-01,2026-06-30,Property Development +RATCH,2026-01-01,2026-06-30,Energy & Utilities +RCL,2026-01-01,2026-06-30,Transportation & Logistics +SAWAD,2026-01-01,2026-06-30,Finance & Securities +SCB,2026-01-01,2026-06-30,Banking +SCC,2026-01-01,2026-06-30,Construction Materials +SCGP,2026-01-01,2026-06-30,Packaging +SIRI,2026-01-01,2026-06-30,Property Development +SISB,2026-01-01,2026-06-30,Professional Services +SJWD,2026-01-01,2026-06-30,Transportation & Logistics +SPALI,2026-01-01,2026-06-30,Property Development +SPRC,2026-01-01,2026-06-30,Energy & Utilities +STA,2026-01-01,2026-06-30,Agribusiness +STECON,2026-01-01,2026-06-30,Construction Services +STGT,2026-01-01,2026-06-30,Personal Products & Pharmaceuticals +TASCO,2026-01-01,2026-06-30,Construction Materials +TCAP,2026-01-01,2026-06-30,Banking +TFG,2026-01-01,2026-06-30,Food & Beverage +TIDLOR,2026-01-01,2026-06-30,Finance & Securities +TISCO,2026-01-01,2026-06-30,Banking +TLI,2026-01-01,2026-06-30,Insurance +TOA,2026-01-01,2026-06-30,Construction Materials +TOP,2026-01-01,2026-06-30,Energy & Utilities +TRUE,2026-01-01,2026-06-30,Information & Communication Technology +TTB,2026-01-01,2026-06-30,Banking +TU,2026-01-01,2026-06-30,Food & Beverage +VGI,2026-01-01,2026-06-30,Media & Publishing +WHA,2026-01-01,2026-06-30,Property Development diff --git a/data/universes/set100_pit_2024_2026_h1.csv b/data/universes/set100_pit_2024_2026_h1.csv new file mode 100644 index 0000000..83287a7 --- /dev/null +++ b/data/universes/set100_pit_2024_2026_h1.csv @@ -0,0 +1,225 @@ +symbol,start_date,end_date,sector +AAV,2024-01-02,2025-12-31, +AAV,2026-01-01,2026-06-30,Transportation & Logistics +ADVANC,2024-01-02,2025-12-31, +ADVANC,2026-01-01,2026-06-30,Information & Communication Technology +AEONTS,2024-01-02,2025-12-31, +AEONTS,2026-01-01,2026-06-30,Finance & Securities +AMATA,2024-01-02,2025-12-31, +AMATA,2026-01-01,2026-06-30,Property Development +AOT,2024-01-02,2025-12-31, +AOT,2026-01-01,2026-06-30,Transportation & Logistics +AP,2024-01-02,2025-12-31, +AP,2026-01-01,2026-06-30,Property Development +AURA,2024-01-02,2024-06-30, +AURA,2025-07-01,2025-12-31, +AURA,2026-01-01,2026-06-30,Fashion +AWC,2024-01-02,2025-12-31, +AWC,2026-01-01,2026-06-30,Property Development +BA,2024-07-01,2025-12-31, +BA,2026-01-01,2026-06-30,Transportation & Logistics +BAM,2024-01-02,2025-12-31, +BAM,2026-01-01,2026-06-30,Finance & Securities +BANPU,2024-01-02,2025-12-31, +BANPU,2026-01-01,2026-06-30,Energy & Utilities +BBL,2024-01-02,2025-12-31, +BBL,2026-01-01,2026-06-30,Banking +BCH,2024-01-02,2025-12-31, +BCH,2026-01-01,2026-06-30,Health Care Services +BCP,2024-01-02,2025-12-31, +BCP,2026-01-01,2026-06-30,Energy & Utilities +BCPG,2024-01-02,2025-12-31, +BCPG,2026-01-01,2026-06-30,Energy & Utilities +BDMS,2024-01-02,2025-12-31, +BDMS,2026-01-01,2026-06-30,Health Care Services +BEM,2024-01-02,2025-12-31, +BEM,2026-01-01,2026-06-30,Transportation & Logistics +BGRIM,2024-01-02,2025-12-31, +BGRIM,2026-01-01,2026-06-30,Energy & Utilities +BH,2024-01-02,2025-12-31, +BH,2026-01-01,2026-06-30,Health Care Services +BJC,2024-07-01,2025-12-31, +BJC,2026-01-01,2026-06-30,Commerce +BLA,2024-01-02,2025-12-31, +BLA,2026-01-01,2026-06-30,Insurance +BSRC,2024-01-02,2025-02-26, +BTG,2024-01-02,2025-12-31, +BTG,2026-01-01,2026-06-30,Food & Beverage +BTS,2024-01-02,2025-12-31, +BTS,2026-01-01,2026-06-30,Transportation & Logistics +BYD,2024-01-02,2024-06-30, +CBG,2024-01-02,2025-12-31, +CBG,2026-01-01,2026-06-30,Food & Beverage +CCET,2025-01-01,2025-12-31, +CCET,2026-01-01,2026-06-30,Electronic Components +CENTEL,2024-01-02,2025-12-31, +CENTEL,2026-01-01,2026-06-30,Tourism & Leisure +CHG,2024-01-02,2025-12-31, +CHG,2026-01-01,2026-06-30,Health Care Services +CK,2024-01-02,2025-12-31, +CK,2026-01-01,2026-06-30,Construction Services +CKP,2024-07-01,2025-06-30, +COCOCO,2025-01-01,2025-06-30, +COM7,2024-01-02,2025-12-31, +COM7,2026-01-01,2026-06-30,Commerce +CPALL,2024-01-02,2025-12-31, +CPALL,2026-01-01,2026-06-30,Commerce +CPF,2024-01-02,2025-12-31, +CPF,2026-01-01,2026-06-30,Food & Beverage +CPN,2024-01-02,2025-12-31, +CPN,2026-01-01,2026-06-30,Property Development +CRC,2024-01-02,2025-12-31, +CRC,2026-01-01,2026-06-30,Commerce +DELTA,2024-01-02,2025-12-31, +DELTA,2026-01-01,2026-06-30,Electronic Components +DOHOME,2024-01-02,2025-12-31, +DOHOME,2026-01-01,2026-06-30,Commerce +EA,2024-01-02,2025-12-31, +EA,2026-01-01,2026-06-30,Energy & Utilities +EGCO,2024-01-02,2025-12-31, +EGCO,2026-01-01,2026-06-30,Energy & Utilities +ERW,2024-01-02,2025-12-31, +ERW,2026-01-01,2026-06-30,Tourism & Leisure +FORTH,2024-01-02,2024-06-30, +GFPT,2026-01-01,2026-06-30,Agribusiness +GLOBAL,2024-01-02,2025-12-31, +GLOBAL,2026-01-01,2026-06-30,Commerce +GPSC,2024-01-02,2025-12-31, +GPSC,2026-01-01,2026-06-30,Energy & Utilities +GULF,2024-01-02,2025-03-20, +GULF,2025-04-02,2025-12-31, +GULF,2026-01-01,2026-06-30,Energy & Utilities +GULFI,2025-03-21,2025-04-01, +GUNKUL,2024-01-02,2025-12-31, +GUNKUL,2026-01-01,2026-06-30,Energy & Utilities +HANA,2024-01-02,2025-12-31, +HANA,2026-01-01,2026-06-30,Electronic Components +HMPRO,2024-01-02,2025-12-31, +HMPRO,2026-01-01,2026-06-30,Commerce +ICHI,2024-01-02,2025-12-31, +ICHI,2026-01-01,2026-06-30,Food & Beverage +INTUCH,2024-01-02,2025-04-01, +IRPC,2024-01-02,2025-12-31, +IRPC,2026-01-01,2026-06-30,Energy & Utilities +ITC,2024-01-02,2025-12-31, +IVL,2024-01-02,2025-12-31, +IVL,2026-01-01,2026-06-30,Petrochemicals & Chemicals +JAS,2024-07-01,2025-12-31, +JAS,2026-01-01,2026-06-30,Information & Communication Technology +JMART,2024-01-02,2025-12-31, +JMART,2026-01-01,2026-06-30,Information & Communication Technology +JMT,2024-01-02,2025-12-31, +JMT,2026-01-01,2026-06-30,Finance & Securities +JTS,2025-01-01,2025-04-15, +JTS,2025-07-01,2025-12-31, +JTS,2026-01-01,2026-06-30,Information & Communication Technology +KBANK,2024-01-02,2025-12-31, +KBANK,2026-01-01,2026-06-30,Banking +KCE,2024-01-02,2025-12-31, +KCE,2026-01-01,2026-06-30,Electronic Components +KKP,2024-01-02,2025-12-31, +KKP,2026-01-01,2026-06-30,Banking +KTB,2024-01-02,2025-12-31, +KTB,2026-01-01,2026-06-30,Banking +KTC,2024-01-02,2025-12-31, +KTC,2026-01-01,2026-06-30,Finance & Securities +LH,2024-01-02,2025-12-31, +LH,2026-01-01,2026-06-30,Property Development +M,2024-01-02,2025-12-31, +M,2026-01-01,2026-06-30,Food & Beverage +MBK,2024-07-01,2024-12-31, +MBK,2025-07-01,2025-12-31, +MEGA,2024-01-02,2025-12-31, +MEGA,2026-01-01,2026-06-30,Commerce +MINT,2024-01-02,2025-12-31, +MINT,2026-01-01,2026-06-30,Tourism & Leisure +MOSHI,2024-01-02,2024-06-30, +MOSHI,2025-02-27,2025-12-31, +MOSHI,2026-01-01,2026-06-30,Commerce +MTC,2024-01-02,2025-12-31, +MTC,2026-01-01,2026-06-30,Finance & Securities +NEX,2024-01-02,2024-06-30, +OR,2024-01-02,2025-12-31, +OR,2026-01-01,2026-06-30,Energy & Utilities +ORI,2024-01-02,2024-06-30, +OSP,2024-01-02,2025-12-31, +OSP,2026-01-01,2026-06-30,Food & Beverage +PLANB,2024-01-02,2025-12-31, +PLANB,2026-01-01,2026-06-30,Media & Publishing +PR9,2025-01-01,2025-12-31, +PR9,2026-01-01,2026-06-30,Health Care Services +PRM,2024-07-01,2025-12-31, +PRM,2026-01-01,2026-06-30,Transportation & Logistics +PTG,2026-01-01,2026-06-30,Energy & Utilities +PTT,2024-01-02,2025-12-31, +PTT,2026-01-01,2026-06-30,Energy & Utilities +PTTEP,2024-01-02,2025-12-31, +PTTEP,2026-01-01,2026-06-30,Energy & Utilities +PTTGC,2024-01-02,2025-12-31, +PTTGC,2026-01-01,2026-06-30,Petrochemicals & Chemicals +QH,2024-07-01,2025-12-31, +QH,2026-01-01,2026-06-30,Property Development +RATCH,2024-01-02,2025-12-31, +RATCH,2026-01-01,2026-06-30,Energy & Utilities +RBF,2024-01-02,2024-12-31, +RCL,2024-01-02,2025-12-31, +RCL,2026-01-01,2026-06-30,Transportation & Logistics +ROJNA,2025-04-02,2025-06-30, +SAPPE,2024-01-02,2025-06-30, +SAWAD,2024-01-02,2025-12-31, +SAWAD,2026-01-01,2026-06-30,Finance & Securities +SCB,2024-01-02,2025-12-31, +SCB,2026-01-01,2026-06-30,Banking +SCC,2024-01-02,2025-12-31, +SCC,2026-01-01,2026-06-30,Construction Materials +SCGP,2024-01-02,2025-12-31, +SCGP,2026-01-01,2026-06-30,Packaging +SIRI,2024-01-02,2025-12-31, +SIRI,2026-01-01,2026-06-30,Property Development +SISB,2024-01-02,2025-12-31, +SISB,2026-01-01,2026-06-30,Professional Services +SJWD,2024-01-02,2025-12-31, +SJWD,2026-01-01,2026-06-30,Transportation & Logistics +SKY,2024-07-01,2025-06-30, +SNNP,2024-01-02,2024-06-30, +SNNP,2025-04-16,2025-06-30, +SPALI,2024-01-02,2025-12-31, +SPALI,2026-01-01,2026-06-30,Property Development +SPRC,2024-01-02,2025-12-31, +SPRC,2026-01-01,2026-06-30,Energy & Utilities +STA,2024-01-02,2025-12-31, +STA,2026-01-01,2026-06-30,Agribusiness +STECON,2026-01-01,2026-06-30,Construction Services +STGT,2024-01-02,2025-12-31, +STGT,2026-01-01,2026-06-30,Personal Products & Pharmaceuticals +TASCO,2024-01-02,2025-12-31, +TASCO,2026-01-01,2026-06-30,Construction Materials +TCAP,2024-01-02,2025-12-31, +TCAP,2026-01-01,2026-06-30,Banking +TFG,2025-07-01,2025-12-31, +TFG,2026-01-01,2026-06-30,Food & Beverage +THG,2024-01-02,2024-06-30, +TIDLOR,2024-01-02,2025-12-31, +TIDLOR,2026-01-01,2026-06-30,Finance & Securities +TIPH,2024-07-01,2024-12-31, +TISCO,2024-01-02,2025-12-31, +TISCO,2026-01-01,2026-06-30,Banking +TKN,2024-01-02,2024-06-30, +TLI,2024-01-02,2025-12-31, +TLI,2026-01-01,2026-06-30,Insurance +TOA,2024-01-02,2024-12-31, +TOA,2025-07-01,2025-12-31, +TOA,2026-01-01,2026-06-30,Construction Materials +TOP,2024-01-02,2025-12-31, +TOP,2026-01-01,2026-06-30,Energy & Utilities +TRUE,2024-01-02,2025-12-31, +TRUE,2026-01-01,2026-06-30,Information & Communication Technology +TTB,2024-01-02,2025-12-31, +TTB,2026-01-01,2026-06-30,Banking +TU,2024-01-02,2025-12-31, +TU,2026-01-01,2026-06-30,Food & Beverage +VGI,2024-01-02,2025-12-31, +VGI,2026-01-01,2026-06-30,Media & Publishing +WHA,2024-01-02,2025-12-31, +WHA,2026-01-01,2026-06-30,Property Development +WHAUP,2025-07-01,2025-12-31, diff --git a/primethai/__init__.py b/primethai/__init__.py new file mode 100644 index 0000000..4c6ec68 --- /dev/null +++ b/primethai/__init__.py @@ -0,0 +1,3 @@ +"""PrimeThai research-only market analysis and backtesting toolkit.""" + +__version__ = "0.1.0" diff --git a/primethai/__main__.py b/primethai/__main__.py new file mode 100644 index 0000000..eb53e2f --- /dev/null +++ b/primethai/__main__.py @@ -0,0 +1,3 @@ +from .cli import main + +raise SystemExit(main()) diff --git a/primethai/auth.py b/primethai/auth.py new file mode 100644 index 0000000..ddb4658 --- /dev/null +++ b/primethai/auth.py @@ -0,0 +1,186 @@ +"""Single-user, signed-cookie authentication for the PrimeThai web app.""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from http.cookies import SimpleCookie +import base64 +import hashlib +import hmac +import json +import os +from pathlib import Path +import re +import time + + +ENV_PATH = Path(__file__).resolve().parent.parent / ".env" +COOKIE_NAME = "primethai_session" +_ENV_KEY = re.compile(r"[A-Za-z_][A-Za-z0-9_]*$") + + +@dataclass(frozen=True) +class AuthConfig: + username: str + password: str = field(repr=False) + session_secret: bytes = field(repr=False) + session_ttl_seconds: int = 7 * 24 * 60 * 60 + cookie_secure: str = "auto" + + +def _dotenv_value(raw: str) -> str: + value = raw.strip() + if not value: + return "" + if value[0] in {"'", '"'}: + quote = value[0] + if quote == '"': + try: + parsed, end = json.JSONDecoder().raw_decode(value) + except json.JSONDecodeError as exc: + raise ValueError("ค่าใน .env มีเครื่องหมายคำพูดปิดไม่ครบ") from exc + trailing = value[end:].strip() + else: + end = value.rfind(quote) + if end < 1: + raise ValueError("ค่าใน .env มีเครื่องหมายคำพูดปิดไม่ครบ") + parsed = value[1:end].replace("\\'", "'").replace("\\\\", "\\") + trailing = value[end + 1:].strip() + if trailing and not trailing.startswith("#"): + raise ValueError("ค่าใน .env มีข้อความต่อท้ายหลังเครื่องหมายคำพูด") + return parsed + return re.split(r"\s+#", value, maxsplit=1)[0].rstrip() + + +def load_dotenv(path: str | Path = ENV_PATH) -> None: + """Load simple KEY=VALUE entries without overriding process environment.""" + env_path = Path(path) + try: + lines = env_path.read_text(encoding="utf-8").splitlines() + except FileNotFoundError: + return + for line_number, raw in enumerate(lines, start=1): + stripped = raw.strip() + if not stripped or stripped.startswith("#"): + continue + if stripped.startswith("export "): + stripped = stripped[7:].lstrip() + key, separator, value = stripped.partition("=") + key = key.strip() + if not separator or not _ENV_KEY.fullmatch(key): + raise ValueError(f"รูปแบบ .env ไม่ถูกต้องที่บรรทัด {line_number}") + os.environ.setdefault(key, _dotenv_value(value)) + + +def load_auth_config(path: str | Path = ENV_PATH) -> AuthConfig: + load_dotenv(path) + username = os.environ.get("PRIMETHAI_USERNAME", "").strip() + password = os.environ.get("PRIMETHAI_PASSWORD", "") + secret = os.environ.get("PRIMETHAI_SESSION_SECRET", "") + missing = [name for name, value in ( + ("PRIMETHAI_USERNAME", username), + ("PRIMETHAI_PASSWORD", password), + ("PRIMETHAI_SESSION_SECRET", secret), + ) if not value] + if missing: + raise RuntimeError( + "ต้องกำหนด " + ", ".join(missing) + + " ในไฟล์ .env ก่อนเริ่มระบบ (ดู .env.example)" + ) + if len(password) < 12: + raise RuntimeError("PRIMETHAI_PASSWORD ต้องมีอย่างน้อย 12 ตัวอักษร") + if len(secret.encode("utf-8")) < 32: + raise RuntimeError("PRIMETHAI_SESSION_SECRET ต้องมีอย่างน้อย 32 ไบต์") + try: + ttl_hours = int(os.environ.get("PRIMETHAI_SESSION_TTL_HOURS", "168")) + except ValueError as exc: + raise RuntimeError("PRIMETHAI_SESSION_TTL_HOURS ต้องเป็นจำนวนเต็ม") from exc + if not 1 <= ttl_hours <= 720: + raise RuntimeError("PRIMETHAI_SESSION_TTL_HOURS ต้องอยู่ระหว่าง 1 ถึง 720") + cookie_secure = os.environ.get("PRIMETHAI_COOKIE_SECURE", "auto").strip().casefold() + if cookie_secure not in {"auto", "true", "false"}: + raise RuntimeError("PRIMETHAI_COOKIE_SECURE ต้องเป็น auto, true หรือ false") + return AuthConfig( + username=username, + password=password, + session_secret=secret.encode("utf-8"), + session_ttl_seconds=ttl_hours * 60 * 60, + cookie_secure=cookie_secure, + ) + + +def _b64encode(data: bytes) -> str: + return base64.urlsafe_b64encode(data).rstrip(b"=").decode("ascii") + + +def _b64decode(value: str) -> bytes: + return base64.urlsafe_b64decode(value + "=" * (-len(value) % 4)) + + +def _credential_version(config: AuthConfig) -> str: + material = config.username.encode("utf-8") + b"\0" + config.password.encode("utf-8") + return _b64encode(hmac.new(config.session_secret, material, hashlib.sha256).digest()) + + +def create_session(config: AuthConfig, *, now: int | None = None) -> str: + issued_at = int(time.time() if now is None else now) + payload = json.dumps( + {"sub": config.username, "cv": _credential_version(config), + "iat": issued_at, "exp": issued_at + config.session_ttl_seconds}, + separators=(",", ":"), + ).encode("utf-8") + encoded = _b64encode(payload) + signature = hmac.new(config.session_secret, encoded.encode("ascii"), hashlib.sha256).digest() + return encoded + "." + _b64encode(signature) + + +def verify_session(config: AuthConfig, token: str | None, *, now: int | None = None) -> bool: + if not token or len(token) > 2048 or token.count(".") != 1: + return False + encoded, supplied_signature = token.split(".", 1) + expected_signature = _b64encode( + hmac.new(config.session_secret, encoded.encode("ascii", "ignore"), hashlib.sha256).digest() + ) + if not hmac.compare_digest(supplied_signature, expected_signature): + return False + try: + payload = json.loads(_b64decode(encoded).decode("utf-8")) + current_time = int(time.time() if now is None else now) + return ( + isinstance(payload, dict) + and hmac.compare_digest(str(payload.get("sub", "")).encode("utf-8"), + config.username.encode("utf-8")) + and hmac.compare_digest(str(payload.get("cv", "")), _credential_version(config)) + and isinstance(payload.get("exp"), int) + and payload["exp"] > current_time + and isinstance(payload.get("iat"), int) + and payload["iat"] <= current_time + 60 + ) + except (ValueError, TypeError, UnicodeDecodeError, json.JSONDecodeError): + return False + + +def session_cookie(config: AuthConfig, token: str, *, secure: bool) -> str: + cookie = SimpleCookie() + cookie[COOKIE_NAME] = token + morsel = cookie[COOKIE_NAME] + morsel["path"] = "/" + morsel["max-age"] = str(config.session_ttl_seconds) + morsel["httponly"] = True + morsel["samesite"] = "Strict" + if secure: + morsel["secure"] = True + return cookie.output(header="").strip() + + +def expired_session_cookie(*, secure: bool) -> str: + cookie = SimpleCookie() + cookie[COOKIE_NAME] = "" + morsel = cookie[COOKIE_NAME] + morsel["path"] = "/" + morsel["max-age"] = "0" + morsel["httponly"] = True + morsel["samesite"] = "Strict" + if secure: + morsel["secure"] = True + return cookie.output(header="").strip() diff --git a/primethai/backtest.html b/primethai/backtest.html new file mode 100644 index 0000000..3a09924 --- /dev/null +++ b/primethai/backtest.html @@ -0,0 +1,176 @@ + + + + + + PrimeThai | Strategy desk + + + +
+
+
+
PrimeThai · Research dashboard
+

Donchian 55/20 + EMA 50/200

+

← กลับหน้าพอร์ตจริง · หน้าผล backtest และสัญญาณย้อนหลัง

+
+ +
+ +
Paper / manual review เท่านั้น · ไม่มีการส่งคำสั่งซื้อขายผ่านโบรกเกอร์
+ +
+
ข้อมูลราคาปิดล่าสุด
—
สัญญาณสำหรับวันซื้อขายถัดไป
+
ผลตอบแทน YTD · ต้นทุนฐาน
—
—
+
ขาดทุนสูงสุดจากจุดสูงสุด
—
คำนวณจากมูลค่าพอร์ตใน backtest
+
ผลตอบแทนเมื่อต้นทุนสูงขึ้น
—
ค่าคอมมิชชัน 0.30% + slippage 15 bps/ข้าง
+
+ +
+
+

เส้นมูลค่าพอร์ต

เริ่มเงินสด 1,000,000 บาท
+ +
——
+
+
+

รายละเอียดรอบทดสอบ

หุ้นละ 100 หุ้น
+
+
จำนวนวันซื้อขาย—
+
มูลค่าพอร์ตปลายงวด—
+
ต้นทุนซื้อขายที่จำลอง—
+
จำนวนรายการซื้อ/ขาย—
+
ซื้อ+ขาย เทียบเงินตั้งต้น—
+
จำนวนหุ้นเฉลี่ยในพอร์ต—
+
สัดส่วนเงินลงทุนเฉลี่ย—
+
+
กำลังอ่านรายงานในเครื่อง…
+
+
+ +
+

เป้าหมายถือหุ้นรอบถัดไป

จำนวนคำนวณจากราคาปิดและพอร์ตตัวอย่าง 1 ล้านบาท · เป็น target basket ไม่ใช่คำสั่งซื้อ
ยังไม่เทียบกับพอร์ตที่ถือจริง
+
+ + +
หุ้นโมเดลที่ส่งสัญญาณน้ำหนักราคาปิดอ้างอิงหุ้นโดยประมาณข้อมูลถึงวันที่วันซื้อขายที่ข้อมูลค้าง
ยังไม่มีรายงาน · กด “คำนวณใหม่” เพื่อเริ่ม
+
+ +
+

กติกาและข้อจำกัดที่ควรรู้

+
    +
  • ซื้อเมื่อ Donchian 55/20 หรือ EMA 50/200 เลือกหุ้น; หุ้นที่ติดทั้งสองโมเดลนับครั้งเดียว และแบ่งน้ำหนักเท่ากัน สูงสุด 20 หุ้น
  • +
  • ตารางด้านบนแสดงเป้าหมายถือหุ้น ไม่ได้แสดงจำนวนที่ควรซื้อหรือขายสุทธิ; ถ้ามีหุ้นอยู่แล้วต้องเทียบกับพอร์ตจริงก่อนส่งคำสั่ง
  • +
  • backtest เริ่มพอร์ตเงินสดต้นปี 2026 และใช้ข้อมูลก่อนหน้าเพื่อ warm-up ตัวชี้วัด ผลช่วง ม.ค.–มิ.ย. 2026 ไม่ใช่ holdout อิสระ
  • +
  • ผลตอบแทน YTD ยังสั้น ไม่รับประกันผลในอนาคต; turnover สูง และยังไม่มีแบบจำลอง market impact, สภาพคล่อง/คิวซื้อขายจริง หรือขั้นตอนจัดการคำสั่ง
  • +
  • ข้อมูล corporate actions ครอบคลุมไม่ครบทุกหุ้น; ราคาปิดอ้างอิงอาจต่างจากราคาเปิดจริง จึงต้องตรวจราคาและขนาดสถานะใหม่ก่อนตัดสินใจ
  • +
+
แหล่งข้อมูล EOD: SiamChart พร้อม Yahoo Finance สำรอง · เซิร์ฟเวอร์ดึงอัตโนมัติทุกวันจันทร์–ศุกร์หลัง 20:30 น. เวลาไทย
+
+
+
บริการดึง SiamChart EOD พร้อม Yahoo Finance สำรองทุกวันจันทร์–ศุกร์ · ไม่มีการเชื่อมต่อหรือส่งคำสั่งไปยังโบรกเกอร์
+
+ + + diff --git a/primethai/backtest.py b/primethai/backtest.py new file mode 100644 index 0000000..3dc281e --- /dev/null +++ b/primethai/backtest.py @@ -0,0 +1,525 @@ +"""Daily close-signal / next-open execution backtester; research only.""" + +from __future__ import annotations + +from dataclasses import asdict, dataclass, field +import math + +import numpy as np +import pandas as pd + +from .config import StrategyConfig, ThaiMarketRules, TradingCosts +from .portfolio import round_quantity, round_to_tick, size_position + + +@dataclass +class PendingEntry: + symbol: str + signal_date: pd.Timestamp + setup: str + entry_type: str + stop_reference: float + signal_close: float + ema21_high: float + atr14: float + risk_pct: float + regime: str + lot_size: int + + +@dataclass +class Position: + symbol: str + entry_date: pd.Timestamp + entry_price: float + initial_qty: int + remaining_qty: int + initial_stop: float + initial_r: float + target_2r: float + setup: str + entry_type: str + risk_pct: float + entry_cost: float + entry_price_at_fill: float + stop_at_fill: float + locked_initial_r: float + entry_shares: int + trim_pnl: float = 0.0 + runner_pnl: float = 0.0 + dividend_cash: float = 0.0 + min_low: float = math.inf + max_high: float = -math.inf + holding_sessions: int = 0 + trimmed: bool = False + structure_break_seen: bool = False + failed_reclaim_seen: bool = False + initial_risk: float = 0.0 + + +@dataclass +class BacktestResult: + metrics: dict[str, float] + trades: pd.DataFrame + equity_curve: pd.DataFrame + fills: pd.DataFrame + skipped: list[dict[str, str]] = field(default_factory=list) + + def to_dict(self) -> dict[str, object]: + return {"metrics": self.metrics, "trades": self.trades.to_dict("records"), + "equity_curve": self.equity_curve.reset_index().to_dict("records"), + "fills": self.fills.to_dict("records"), "skipped": self.skipped} + + +def _cost(notional: float, costs: TradingCosts, *, sell: bool) -> float: + return notional * (costs.commission_rate + costs.fees_rate + (costs.sell_tax_rate if sell else 0.0)) + + +def _number(value: object, default: float = 0.0) -> float: + if value is None or pd.isna(value): + return default + return float(value) + + +def _fill_price(price: float, side: str, costs: TradingCosts, rules: ThaiMarketRules) -> float: + slip = costs.slippage_bps / 10_000 + raw = price * (1 + slip if side == "buy" else 1 - slip) + return round_to_tick(raw, rules, direction="up" if side == "buy" else "down") + + +def _signal_for_day(features: pd.DataFrame, date: pd.Timestamp, strategy: StrategyConfig, + rules: ThaiMarketRules, sector_lot_size: int | None = None, + symbol: str = "") -> PendingEntry | None: + if date not in features.index: + return None + i = features.index.get_loc(date) + if isinstance(i, slice) or i < 1: + return None + row, prev = features.iloc[i], features.iloc[i - 1] + need = ["close", "open", "high", "low", "ema21_high", "ema21_low", "ema21_close", "atr14"] + if any(pd.isna(row.get(key, np.nan)) for key in need): + return None + regime = str(row.get("regime", "CORRECTION")) + if strategy.require_market_support and strategy.enable_market_regime: + if regime in {"CORRECTION", "BREAKDOWN", "OVERBOUGHT"} or not bool(row.get("new_risk_allowed", False)): + return None + rs = row.get("rs_percentile", 0.0) + if strategy.enable_rs and (pd.isna(rs) or float(rs) < strategy.min_rs_percentile): + return None + if strategy.enable_mco: + mco_z = row.get("mco_z", np.nan) + # MCO is a timing context; no standalone MCO buy rule. Extreme downside disables fresh risk. + if pd.notna(mco_z) and float(mco_z) < strategy.min_mco_z_for_entry: + return None + if strategy.enable_mcsi and str(row.get("mcsi_state", "DOWN")) in {"DOWN", "LOSE_10DMA"}: + if strategy.require_market_support: + return None + if strategy.enable_weekly and regime in {"REPAIR", "EARLY_UPTREND"}: + if row.get("weekly_trend_state", "NEUTRAL") == "DOWN": + return None + extension = _number(row.get("extension_atr", 0.0)) + if strategy.enable_atr_filter and extension > strategy.max_extension_atr: + return None + if float(row["close"]) < strategy.min_price or _number(row.get("avg_value_turnover20", 0.0)) < strategy.min_value_turnover_thb: + return None + stop = float(row["ema21_low"]) + if not (0 < stop < float(row["close"])): + return None + weakness = (row["trend_state"] == "UP" and row["low"] <= row["ema21_high"] + and row["close"] >= row["ema21_low"]) + prior_high_reclaim = row["close"] > prev["high"] + red_to_green = row["open"] < prev["close"] and row["close"] > prev["close"] + reversal = row["close"] > row["open"] and prev["close"] < prev["open"] + ema_reclaim = row["close"] > row["ema21_high"] and prev["close"] <= prev["ema21_high"] + base_breakout = row["close"] > features["high"].iloc[max(0, i - strategy.base_breakout_window):i].max() + higher_low = bool(row.get("higher_low", False)) + if strategy.enable_higher_low and not higher_low and not weakness: + # Confirmation setups remain available; only the specific higher-low pattern is disabled. + higher_low = False + if weakness: + setup, entry_type, risk_pct = "21DMA Retest", "WEAKNESS", strategy.weakness_risk_pct + elif prior_high_reclaim or ema_reclaim or red_to_green or reversal or base_breakout or (higher_low and row["close"] > prev["close"]): + setup = ("Base Breakout" if base_breakout else "Prior High Reclaim" if prior_high_reclaim + else "21DMA High Reclaim" if ema_reclaim else "Red-to-Green" if red_to_green + else "Daily Reversal" if reversal else "Higher Low") + entry_type, risk_pct = "CONFIRMATION", strategy.confirmation_risk_pct + else: + return None + if strategy.enable_higher_low and setup == "Higher Low" and not higher_low: + return None + return PendingEntry( + symbol=str(row.get("symbol", symbol)), signal_date=pd.Timestamp(date), setup=setup, + entry_type=entry_type, stop_reference=stop, signal_close=float(row["close"]), + ema21_high=float(row["ema21_high"]), atr14=float(row["atr14"]), risk_pct=risk_pct, + regime=regime, lot_size=sector_lot_size or rules.default_board_lot, + ) + + +def _finish_trade(position: Position, date: pd.Timestamp) -> dict[str, object]: + total = position.trim_pnl + position.runner_pnl + position.dividend_cash + initial_risk = position.locked_initial_r * position.entry_shares + return { + "symbol": position.symbol, "entry_date": position.entry_date.date().isoformat(), + "exit_date": date.date().isoformat(), "setup": position.setup, + "entry_type": position.entry_type, "entry_price": position.entry_price_at_fill, + "initial_stop": position.stop_at_fill, "initial_R": position.locked_initial_r, + "initial_shares": position.entry_shares, "holding_sessions": position.holding_sessions, + "trim_2r_pnl": position.trim_pnl, "runner_pnl": position.runner_pnl, + "dividend_cash": position.dividend_cash, "total_pnl": total, + "r_multiple": total / initial_risk if initial_risk else 0.0, + "mae_r": (position.min_low - position.entry_price) / position.initial_r if position.initial_r else 0.0, + "mfe_r": (position.max_high - position.entry_price) / position.initial_r if position.initial_r else 0.0, + "two_r_hit": position.trimmed, + } + + +def calculate_metrics(equity_curve: pd.DataFrame, trades: pd.DataFrame) -> dict[str, float]: + if equity_curve.empty: + return {key: 0.0 for key in ["total_return", "cagr", "max_drawdown", "win_rate", "average_win", + "average_loss", "expectancy_per_trade", "expectancy_r", "profit_factor", + "average_r", "median_r", "mae_r", "mfe_r", "average_holding_period", + "exposure_pct", "trades_per_month", "two_r_hit_rate", "trim_2r_contribution", + "runner_contribution"]} + equity = equity_curve["equity"].astype(float) + total_return = equity.iloc[-1] / equity.iloc[0] - 1 + years = max((equity.index[-1] - equity.index[0]).days / 365.25, 1 / 252) + cagr = (max(equity.iloc[-1], 1) / max(equity.iloc[0], 1)) ** (1 / years) - 1 + dd = equity / equity.cummax() - 1 + pnl = trades["total_pnl"].astype(float) if not trades.empty else pd.Series(dtype=float) + wins, losses = pnl[pnl > 0], pnl[pnl < 0] + gross_loss = abs(losses.sum()) + average_r = float(trades["r_multiple"].mean()) if not trades.empty else 0.0 + months = max(years * 12, 1 / 12) + exposure_pct = float(equity_curve["exposure"].mean()) if "exposure" in equity_curve else 0.0 + return { + "total_return": float(total_return), "cagr": float(cagr), "max_drawdown": float(dd.min()), + "win_rate": float((pnl > 0).mean()) if len(pnl) else 0.0, + "average_win": float(wins.mean()) if len(wins) else 0.0, + "average_loss": float(losses.mean()) if len(losses) else 0.0, + "expectancy_per_trade": float(pnl.mean()) if len(pnl) else 0.0, + "expectancy_r": average_r, + "profit_factor": float(wins.sum() / gross_loss) if gross_loss else (float("inf") if len(wins) else 0.0), + "average_r": average_r, + "median_r": float(trades["r_multiple"].median()) if not trades.empty else 0.0, + "mae_r": float(trades["mae_r"].mean()) if not trades.empty else 0.0, + "mfe_r": float(trades["mfe_r"].mean()) if not trades.empty else 0.0, + "average_holding_period": float(trades["holding_sessions"].mean()) if not trades.empty else 0.0, + "exposure_pct": exposure_pct, + "trades_per_month": len(trades) / months, + "two_r_hit_rate": float(trades["two_r_hit"].mean()) if not trades.empty else 0.0, + "trim_2r_contribution": float(trades["trim_2r_pnl"].sum()) if not trades.empty else 0.0, + "runner_contribution": float(trades["runner_pnl"].sum()) if not trades.empty else 0.0, + } + + +def run_backtest(features_by_symbol: dict[str, pd.DataFrame], market: pd.DataFrame, *, + strategy: StrategyConfig | None = None, rules: ThaiMarketRules | None = None, + costs: TradingCosts | None = None, start: str | None = None, end: str | None = None, + lot_sizes: dict[str, int] | None = None, yahoo_split_adjusted: bool = True, + stalled_retest_sessions: int | None = None, + stalled_retest_progress_r: float = 1.0, + runner_protection_r: float | None = None) -> BacktestResult: + """Backtest known close signals at next open; it never submits real orders.""" + strategy, rules, costs = strategy or StrategyConfig(), rules or ThaiMarketRules(), costs or TradingCosts() + if stalled_retest_sessions is not None and stalled_retest_sessions < 1: + raise ValueError("stalled_retest_sessions must be positive") + if not math.isfinite(stalled_retest_progress_r) or stalled_retest_progress_r <= 0: + raise ValueError("stalled_retest_progress_r must be finite and positive") + if runner_protection_r is not None and (not math.isfinite(runner_protection_r) or runner_protection_r < 0): + raise ValueError("runner_protection_r must be finite and non-negative") + if not features_by_symbol: + raise ValueError("features_by_symbol is empty") + members_by_date = market.attrs.get("members_by_date") + external_dividends = { + str(symbol).upper(): { + pd.Timestamp(date).normalize(): float(amount) for date, amount in events.items() + } + for symbol, events in market.attrs.get("external_dividends", {}).items() + } + market_fields = [column for column in ("regime", "regime_reasons", "new_risk_allowed", "net_advances", + "mco_z", "mcsi_state", "set100ew_close", "set100ew_ema21", + "ew_trend_state") if column in market.columns] + if market_fields: + market_context = market[market_fields] + prepared: dict[str, pd.DataFrame] = {} + for symbol, source in features_by_symbol.items(): + base = source.drop(columns=[column for column in market_fields if column in source.columns]) + prepared[symbol] = base.join(market_context.reindex(base.index)) + features_by_symbol = prepared + feature_dates = set().union(*(frame.index for frame in features_by_symbol.values())) + dates_set = set(feature_dates) + if feature_dates: + first_date, last_date = min(feature_dates), max(feature_dates) + dates_set.update(pd.Timestamp(date).normalize() for date in market.index + if first_date <= pd.Timestamp(date).normalize() <= last_date) + dates_set.update(date for events in external_dividends.values() for date in events + if first_date <= date <= last_date) + dates = sorted(dates_set) + if start: + dates = [date for date in dates if date >= pd.Timestamp(start)] + if end: + dates = [date for date in dates if date < pd.Timestamp(end)] + if len(dates) < 2: + raise ValueError("backtest requires at least two daily bars") + cash, positions = float(strategy.initial_equity), {} + pending_entries: list[PendingEntry] = [] + pending_exits: dict[str, str] = {} + closed_trades: list[dict[str, object]] = [] + fills: list[dict[str, object]] = [] + skipped: list[dict[str, str]] = [] + equity_rows = [] + previous_close: dict[str, float] = {} + + def execute_sell(position: Position, qty: int, reference_price: float, date: pd.Timestamp, reason: str, + *, runner: bool) -> int: + nonlocal cash + qty = min(qty, position.remaining_qty) + if qty <= 0: + return 0 + fill = _fill_price(reference_price, "sell", costs, rules) + # Include the actual execution point in trade excursions. A next-open exit + # can gap beyond every low seen before the position was removed from the book. + position.min_low = min(position.min_low, fill) + position.max_high = max(position.max_high, fill) + notional = fill * qty + sale_cost = _cost(notional, costs, sell=True) + entry_basis = position.entry_cost * qty / position.initial_qty + pnl = notional - sale_cost - entry_basis + cash += notional - sale_cost + if runner: + position.runner_pnl += pnl + else: + position.trim_pnl += pnl + position.remaining_qty -= qty + fills.append({"date": date, "symbol": position.symbol, "side": "SELL", "reason": reason, + "shares": qty, "price": fill, "cost": sale_cost}) + return qty + + for date in dates: + # Yahoo OHLC is split-adjusted; retain the event for audit but avoid double-adjusting shares. + # Alternative providers may supply unadjusted OHLC and opt in to explicit split handling. + for symbol, position in list(positions.items()): + frame = features_by_symbol[symbol] + bar = frame.loc[date] if date in frame.index else None + if bar is not None: + split = _number(bar.get("stock_splits", 0.0)) + if not yahoo_split_adjusted and split > 0 and split != 1: + position.remaining_qty = int(round(position.remaining_qty * split)) + position.initial_qty = int(round(position.initial_qty * split)) + position.entry_price /= split + position.initial_stop /= split + position.initial_r /= split + position.target_2r = position.entry_price + 2 * position.initial_r + if math.isfinite(position.min_low): + position.min_low /= split + if math.isfinite(position.max_high): + position.max_high /= split + event_amount = external_dividends.get(symbol, {}).get(pd.Timestamp(date).normalize()) + dividend = (float(event_amount) if event_amount is not None else + _number(bar.get("dividends", 0.0)) if bar is not None else 0.0) + if dividend and date > position.entry_date: + dividend_amount = position.remaining_qty * dividend + position.dividend_cash += dividend_amount + cash += dividend_amount + # If an ex-date has no stock bar, reduce the carried mark by the + # distribution so cash receipt alone does not manufacture return. + if bar is None and symbol in previous_close: + previous_close[symbol] = max(0.0, previous_close[symbol] - dividend) + for symbol, reason in list(pending_exits.items()): + if symbol not in positions: + pending_exits.pop(symbol, None) + continue + frame = features_by_symbol[symbol] + if date not in frame.index or pd.isna(frame.loc[date].get("open", np.nan)): + continue + position = positions[symbol] + execute_sell(position, position.remaining_qty, float(frame.loc[date, "open"]), date, reason, runner=True) + if position.remaining_qty == 0: + closed_trades.append(_finish_trade(position, date)) + positions.pop(symbol) + pending_exits.pop(symbol, None) + + for entry in pending_entries: + frame = features_by_symbol[entry.symbol] + if date not in frame.index or entry.symbol in positions: + continue + bar = frame.loc[date] + if pd.isna(bar.get("open", np.nan)): + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "missing next open"}) + continue + open_price = float(bar["open"]) + if open_price > entry.ema21_high + strategy.max_extension_atr * entry.atr14: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "next-open gap exceeds ATR extension limit"}) + continue + stop = entry.stop_reference + if open_price <= stop: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "open at/below structural stop"}) + continue + fill_price = _fill_price(open_price, "buy", costs, rules) + live_market_value = 0.0 + for held_symbol, held_position in positions.items(): + held_frame = features_by_symbol[held_symbol] + if date in held_frame.index and pd.notna(held_frame.loc[date].get("open", np.nan)): + mark = float(held_frame.loc[date, "open"]) + else: + mark = previous_close.get(held_symbol, held_position.entry_price) + live_market_value += mark * held_position.remaining_qty + open_equity = cash + live_market_value + current_heat = sum(p.initial_r * p.remaining_qty for p in positions.values()) / max(open_equity, 1.0) + heat_limit = strategy.heat.for_regime(entry.regime) + if heat_limit <= 0 or current_heat >= heat_limit: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": f"portfolio heat limit for {entry.regime}"}) + continue + sizing = size_position(open_equity, entry.risk_pct, fill_price, stop, + lot_size=entry.lot_size, max_position_pct=strategy.max_position_pct, rules=rules) + quantity = int(sizing["shares"]) + if quantity <= 0: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "risk/position cap below one board lot"}) + continue + projected_heat = (sum(p.initial_r * p.remaining_qty for p in positions.values()) + + (fill_price - stop) * quantity) / max(open_equity, 1.0) + if projected_heat > heat_limit + 1e-12: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "new position exceeds portfolio heat"}) + continue + buy_notional = fill_price * quantity + buy_cost = _cost(buy_notional, costs, sell=False) + if buy_notional + buy_cost > cash: + quantity = round_quantity(cash / (fill_price * (1 + costs.commission_rate + costs.fees_rate)), entry.lot_size) + buy_notional = fill_price * quantity + buy_cost = _cost(buy_notional, costs, sell=False) + if quantity <= 0: + skipped.append({"symbol": entry.symbol, "date": date.date().isoformat(), "reason": "insufficient cash"}) + continue + initial_r = fill_price - stop + position = Position(entry.symbol, date, fill_price, quantity, quantity, stop, initial_r, + fill_price + strategy.partial_r_multiple * initial_r, entry.setup, + entry.entry_type, entry.risk_pct, buy_notional + buy_cost, + entry_price_at_fill=fill_price, stop_at_fill=stop, + locked_initial_r=initial_r, entry_shares=quantity, + initial_risk=initial_r * quantity) + positions[entry.symbol] = position + cash -= buy_notional + buy_cost + fills.append({"date": date, "symbol": entry.symbol, "side": "BUY", "reason": entry.setup, + "shares": quantity, "price": fill_price, "cost": buy_cost}) + pending_entries.clear() + + # Daily high/low checks happen after next-open orders. Ambiguous stop/target bars assume stop first. + for symbol, position in list(positions.items()): + frame = features_by_symbol[symbol] + if date not in frame.index: + continue + bar = frame.loc[date] + op, hi, lo, cl = (float(bar[k]) for k in ("open", "high", "low", "close")) + runner_was_trimmed = position.trimmed + position.holding_sessions += 1 + position.min_low = min(position.min_low, lo) + position.max_high = max(position.max_high, hi) + if runner_was_trimmed and runner_protection_r is not None: + runner_floor = position.entry_price + runner_protection_r * position.initial_r + if lo <= runner_floor: + fill_ref = min(op, runner_floor) if op < runner_floor else runner_floor + execute_sell(position, position.remaining_qty, fill_ref, date, + "runner_protection_after_2r", runner=True) + if position.remaining_qty == 0: + closed_trades.append(_finish_trade(position, date)) + positions.pop(symbol) + continue + emergency = position.entry_price * (1 - strategy.max_loss_pct) + if lo <= emergency: + fill_ref = min(op, emergency) if op < emergency else emergency + execute_sell(position, position.remaining_qty, fill_ref, date, "emergency_max_loss", runner=True) + if position.remaining_qty == 0: + closed_trades.append(_finish_trade(position, date)) + positions.pop(symbol) + continue + if not position.trimmed and hi >= position.target_2r: + if lo <= position.initial_stop: + execute_sell(position, position.remaining_qty, lo, date, "structural_stop_ambiguous_bar", runner=True) + if position.remaining_qty == 0: + closed_trades.append(_finish_trade(position, date)) + positions.pop(symbol) + continue + requested = int(position.initial_qty * strategy.partial_fraction) + trim_qty = max(rules.minimum_quantity, requested) + trim_qty = min(trim_qty, position.remaining_qty) + execute_sell(position, trim_qty, max(op, position.target_2r), date, "2R_trim", runner=False) + position.trimmed = True + ema_low = bar.get("ema21_low", np.nan) + below = pd.notna(ema_low) and cl < float(ema_low) + if strategy.exit_mode == "close_below_ema21_low" and below: + pending_exits[symbol] = "close_below_ema21_low_next_open" + elif strategy.exit_mode == "break_failed_reclaim" and pd.notna(ema_low): + if cl > float(bar.get("ema21_high", math.inf)): + position.structure_break_seen = False + position.failed_reclaim_seen = False + elif below: + if position.failed_reclaim_seen: + pending_exits[symbol] = "break_failed_reclaim_next_open" + else: + position.structure_break_seen = True + elif position.structure_break_seen: + # A close back into/above the structure is the reclaim attempt. + position.failed_reclaim_seen = True + if (stalled_retest_sessions is not None + and position.entry_type == "WEAKNESS" + and not position.trimmed + and position.holding_sessions >= stalled_retest_sessions + and position.max_high < position.entry_price + stalled_retest_progress_r * position.initial_r): + pending_exits.setdefault( + symbol, f"stalled_retest_without_{stalled_retest_progress_r:g}r_" + f"after_{stalled_retest_sessions}_sessions_next_open" + ) + if hi < math.inf: + previous_close[symbol] = cl + + # Signal generation uses only this day's close and features; every fill is scheduled for the next open. + for symbol, frame in features_by_symbol.items(): + if symbol in positions or any(item.symbol == symbol for item in pending_entries): + continue + if date not in frame.index: + continue + if members_by_date is not None and symbol not in members_by_date.get(pd.Timestamp(date), []): + continue + signal = _signal_for_day(frame, date, strategy, rules, (lot_sizes or {}).get(symbol), symbol=symbol) + if signal is not None: + pending_entries.append(signal) + + close_market_value = 0.0 + heat_risk = 0.0 + unrealized = 0.0 + for symbol, position in positions.items(): + if date in features_by_symbol[symbol].index: + close = float(features_by_symbol[symbol].loc[date, "close"]) + previous_close[symbol] = close + else: + close = previous_close.get(symbol, position.entry_price) + close_market_value += close * position.remaining_qty + unrealized += (close - position.entry_price) * position.remaining_qty + heat_risk += position.initial_r * position.remaining_qty + equity = cash + close_market_value + equity_rows.append({"date": date, "equity": equity, "cash": cash, + "market_value": close_market_value, "portfolio_heat": heat_risk / equity if equity else 0, + "portfolio_cushion": unrealized / equity if equity else 0, + "exposure": close_market_value / equity if equity else 0.0, + "open_positions": len(positions)}) + + # Liquidate at last available close to report realized runner attribution. + final_date = dates[-1] + for symbol, position in list(positions.items()): + frame = features_by_symbol[symbol] + available = frame.loc[frame.index <= final_date] + if available.empty: + continue + bar = available.iloc[-1] + execute_sell(position, position.remaining_qty, float(bar["close"]), final_date, "end_of_test", runner=True) + closed_trades.append(_finish_trade(position, final_date)) + positions.pop(symbol) + # Reflect final liquidation costs in the terminal equity observation. + if equity_rows: + equity_rows[-1].update({"equity": cash, "cash": cash, "market_value": 0.0, + "portfolio_heat": 0.0, "portfolio_cushion": 0.0, + "exposure": 0.0, "open_positions": 0}) + trade_frame = pd.DataFrame(closed_trades) + curve = pd.DataFrame(equity_rows).set_index("date") + fill_frame = pd.DataFrame(fills) + return BacktestResult(calculate_metrics(curve, trade_frame), trade_frame, curve, fill_frame, skipped) diff --git a/primethai/cli.py b/primethai/cli.py new file mode 100644 index 0000000..39b70ed --- /dev/null +++ b/primethai/cli.py @@ -0,0 +1,864 @@ +"""Command-line entry points for download, market, scan, backtest and research.""" + +from __future__ import annotations + +import argparse +import json +import math +from pathlib import Path +import sys +import time +import warnings + +import pandas as pd + +from .backtest import run_backtest +from .config import AppConfig +from .corporate_actions import ( + SiamChartActionError, + audit_siamchart_actions, + fetch_set_cash_dividend_reference, + fetch_siamchart_stock_info, + merge_siamchart_dividends, + read_siamchart_dividend_sidecar, +) +from .data import SymbolDataError, YFinanceProvider, normalize_bars, read_siamchart_archives, safe_filename +from .ema_crossover import run_ema_crossover_backtest +from .experiments import run_ablations, run_chronological_holdouts, rolling_walk_forward +from .model_lab import run_model_lab +from .research import build_feature_panel, build_market_context +from .scanner import scan_candidates +from .synthetic import SMOKE_SYMBOLS, synthetic_bars +from .universe import MembershipError, PointInTimeUniverse, resolve_universe + + +DEFAULT_MEMBERSHIP = Path("data/universes/set100_2026_h1.csv") + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(prog="python -m primethai", description="Thai stock research and backtesting; no broker execution.") + sub = parser.add_subparsers(dest="command", required=True) + download = sub.add_parser("download", help="download/cache daily Yahoo Finance bars") + download.add_argument("--symbols", nargs="*", help="symbols without .BK; defaults to the 10-symbol smoke list") + download.add_argument("--universe", default="custom", choices=["SET100", "SET50", "SET", "mai", "custom"]) + download.add_argument("--membership-file", default=str(DEFAULT_MEMBERSHIP)) + download.add_argument("--mode", choices=["fast", "accurate"], default="fast") + download.add_argument("--start") + download.add_argument("--end") + download.add_argument("--period", default="5y") + download.add_argument("--refresh", action="store_true") + download.add_argument("--data-dir", default="data/raw") + + siamchart = sub.add_parser("import-siamchart", help="import selected tickers from SiamChart EOD ZIP archives") + siamchart.add_argument("--archives", nargs="+", required=True, help="SiamChart set-archive_EOD ZIP files") + siamchart.add_argument("--symbols", nargs="+", default=["^SET"], help="tickers to extract; defaults to the SET index") + siamchart.add_argument("--data-dir", default="data/raw") + + actions = sub.add_parser("import-siamchart-actions", help="import dividend and par-value history from SiamChart stock-info pages") + actions.add_argument("--symbols", nargs="+", required=True, help="tickers without .BK") + actions.add_argument("--data-dir", default="data/raw") + actions.add_argument("--delay-seconds", type=float, default=0.5, + help="pause between stock-info requests to avoid excessive traffic") + + verify_actions = sub.add_parser("verify-siamchart-actions", help="compare action sidecars with a SET reference CSV") + verify_actions.add_argument("--symbols", nargs="+", required=True, help="tickers without .BK") + verify_actions.add_argument("--set-reference", required=True, + help="CSV with symbol,action_type,date,value and optional source_url columns") + verify_actions.add_argument("--data-dir", default="data/raw") + verify_actions.add_argument("--output", default="reports/siamchart_actions_audit.csv") + verify_actions.add_argument("--start", help="inclusive action date") + verify_actions.add_argument("--end", help="exclusive action date") + verify_actions.add_argument("--action-types", nargs="+", choices=["dividend", "par_value"], + default=["dividend"], help="action classes to compare; defaults to dividends") + + set_reference = sub.add_parser("import-set-actions", help="import cash-dividend records from SET's public rights-and-benefits history") + set_reference.add_argument("--symbols", nargs="+", required=True, help="tickers without .BK") + set_reference.add_argument("--start", default="2025-01-01", help="inclusive action date") + set_reference.add_argument("--end", default="2026-07-01", help="exclusive action date") + set_reference.add_argument("--output", default="data/reference/set_actions_2025_2026_h1.csv") + set_reference.add_argument("--delay-seconds", type=float, default=0.35) + set_reference.add_argument("--symbol-aliases", default="data/reference/set_symbol_aliases.csv", + help="optional CSV mapping SET action symbols to the historical price-series symbol") + + model_lab = sub.add_parser("model-lab", help="screen long-history Thai stock strategy candidates") + model_lab.add_argument("--archives", nargs="+", default=[ + "data/raw/siamchart/set-archive_EOD_1970-LAST.zip", + "data/raw/siamchart/set-archive_EOD.zip", + ], help="SiamChart market-wide EOD ZIP archives") + model_lab.add_argument("--start", default="2005-01-01", help="inclusive evaluation start") + model_lab.add_argument("--end", default="2026-07-01", help="exclusive evaluation end") + model_lab.add_argument("--output-dir", default="reports/model_lab") + model_lab.add_argument("--action-dir", default="data/raw/actions") + model_lab.add_argument("--set-index-csv", default="data/raw/^SET.csv") + model_lab.add_argument("--set-reference", default="data/reference/set_actions_2025_2026_h1.csv") + model_lab.add_argument("--initial-equity", type=float, default=1_000_000.0) + + webui = sub.add_parser("webui", help="run the portfolio app with automatic SiamChart updates") + webui.add_argument("--host", default="127.0.0.1", help="bind address; remote use requires an HTTPS reverse proxy") + webui.add_argument("--port", type=int, default=18900) + webui.add_argument("--archives", nargs="+", default=[ + "data/raw/siamchart/set-archive_EOD_1970-LAST.zip", + "data/raw/siamchart/set-archive_EOD.zip", + "data/raw/siamchart/set-archive_EOD_LAST.zip", + ], help="SiamChart market-wide EOD ZIP archives") + webui.add_argument("--start", default="2026-01-01", help="fresh-start portfolio evaluation start") + webui.add_argument("--initial-equity", type=float, default=1_000_000.0) + webui.add_argument("--report-dir", default="reports/primary_union_current") + + for command in ("market", "scan", "backtest", "experiment", "ema-crossover", "strategy-overlays"): + item = sub.add_parser(command) + item.add_argument("--universe", default="SET100", choices=["SET100", "SET50", "SET", "mai", "custom"]) + item.add_argument("--membership-file", default=str(DEFAULT_MEMBERSHIP)) + item.add_argument("--mode", choices=["fast", "accurate"], default="fast") + item.add_argument("--symbols", nargs="*", help="symbols for custom universe") + item.add_argument("--start") + item.add_argument("--end") + item.add_argument("--data-dir", default="data/raw") + item.add_argument("--config", default="config/default.json") + item.add_argument("--output-dir", default="reports") + item.add_argument("--set-reference", help="complete SET cash-dividend reference CSV from import-set-actions") + if command == "scan": + item.add_argument("--top", type=int, default=10, choices=[5, 10, 20]) + item.add_argument("--json-out") + if command == "ema-crossover": + item.add_argument("--ema-pairs", nargs="+", default=["10:30", "20:50", "50:200"], + help="fast:slow EMA spans to compare; default: 10:30 20:50 50:200") + item.add_argument("--max-position-pct", type=float, + help="target portfolio fraction per position; defaults to config") + item.add_argument("--min-price", type=float, + help="minimum signal close; defaults to config") + item.add_argument("--min-value-turnover-thb", type=float, + help="minimum 20-day average value turnover; defaults to config") + item.add_argument("--market-filters", nargs="+", + choices=["none", "ew_trend_up", "ew_above_ema200"], default=["none"], + help="entry gates to compare") + item.add_argument("--exit-modes", nargs="+", choices=["cross_under", "ema21_low"], + default=["cross_under"], help="exit rules to compare") + item.add_argument("--max-loss-pcts", nargs="+", type=float, default=[0.0], + help="hard intraday loss stops to compare; 0 disables, e.g. 0.08 0.12") + item.add_argument("--split-by-year", action="store_true", + help="also run independent simulations for each calendar-year slice") + sub.add_parser("smoke", help="run the full research pipeline on synthetic offline data") + return parser + + +def _membership(path: str | None, universe: str) -> PointInTimeUniverse | None: + if universe.lower() == "custom": + return None + if not path or not Path(path).exists(): + raise MembershipError(f"membership CSV not found: {path}. Use --membership-file with dated constituents.") + return PointInTimeUniverse.from_csv(path, name=universe.upper()) + + +def _symbols_for_download(args: argparse.Namespace) -> tuple[list[str], PointInTimeUniverse | None]: + membership = _membership(args.membership_file, args.universe) if args.universe.lower() != "custom" else None + if args.symbols: + symbols = [symbol.upper().removesuffix(".BK") for symbol in args.symbols] + elif membership: + symbols = sorted({row.symbol for row in membership.periods}) + else: + symbols = SMOKE_SYMBOLS.copy() + return sorted(set(symbols + ["^SET"])), membership + + +def _read_bars(directory: str | Path, symbol: str) -> pd.DataFrame | None: + directory = Path(directory) + options = [directory / f"{safe_filename(symbol)}.csv"] + if symbol == "^SET": + options += [directory / "^SET.csv", directory / "^SET.BK.csv", directory / "SET.csv"] + else: + options += [directory / f"{safe_filename(symbol + '.BK')}.csv"] + for path in options: + if path.exists(): + frame = pd.read_csv(path) + date_column = next((column for column in frame.columns + if str(column).strip().lower() == "date"), None) + if date_column is None: + raise SymbolDataError(f"CSV has no date column: {path}") + frame = frame.set_index(date_column) + if symbol == "^SET" and not any(str(column).strip().lower() == "volume" + for column in frame.columns): + frame["volume"] = 0.0 + bars = normalize_bars(frame) + action_path = directory / "actions" / f"{safe_filename(symbol)}_dividends.csv" + if action_path.exists(): + dividends = read_siamchart_dividend_sidecar(action_path, symbol) + bars = merge_siamchart_dividends(bars, dividends, symbol) + return bars + return None + + +def _accurate_context_bars(set_bars: pd.DataFrame, membership: PointInTimeUniverse, + start: str | None, end: str | None) -> pd.DataFrame: + """Keep PIT-covered market history through the evaluation end, including warm-up.""" + coverage_start = min(row.start for row in membership.periods) + coverage_end = max(row.end for row in membership.periods) + evaluation_start = pd.Timestamp(start) if start else coverage_start + evaluation_end = pd.Timestamp(end) if end else coverage_end + pd.Timedelta(days=1) + if evaluation_start < coverage_start or evaluation_start > coverage_end: + raise MembershipError( + f"requested backtest start {evaluation_start.date()} is outside {membership.name} PIT coverage " + f"({coverage_start.date()} to {coverage_end.date()})" + ) + if evaluation_end <= evaluation_start: + raise ValueError("--end must be later than --start") + if evaluation_end > coverage_end + pd.Timedelta(days=1): + raise MembershipError( + f"requested backtest end {evaluation_end.date()} exceeds {membership.name} PIT coverage " + f"through {coverage_end.date()}" + ) + clipped = set_bars.loc[(set_bars.index >= coverage_start) & (set_bars.index < evaluation_end)] + if clipped.empty: + raise SymbolDataError("no SET sessions fall inside the requested PIT coverage and date range") + if len(clipped) < 260: + raise SymbolDataError( + f"only {len(clipped)} SET sessions remain inside PIT coverage and the requested end; " + "at least 260 are needed for the 52-week market warm-up" + ) + membership.require_coverage(clipped.index) + return clipped + + +def _prepare(args: argparse.Namespace, config: AppConfig) -> tuple[dict[str, pd.DataFrame], pd.DataFrame, pd.DataFrame, PointInTimeUniverse | None, list[str]]: + membership = _membership(args.membership_file, args.universe) if args.universe.lower() != "custom" else None + set_bars = _read_bars(args.data_dir, "^SET") + if set_bars is None: + raise SymbolDataError( + "^SET.BK daily history is missing or incomplete. A provider may have returned only a current quote; " + "supply a full daily OHLCV CSV as data/raw/^SET.csv or data/raw/^SET.BK.csv, or use a provider " + "with historical SET index data" + ) + if len(set_bars) < 260: + raise SymbolDataError( + f"^SET.BK has only {len(set_bars)} daily bars; at least 260 are needed for the 52-week " + "market warm-up. Supply a longer index history before scanning or backtesting" + ) + if membership: + symbols = sorted({row.symbol for row in membership.periods}) + else: + if not args.symbols: + raise ValueError("--universe custom requires --symbols") + symbols = sorted({symbol.upper().removesuffix(".BK") for symbol in args.symbols}) + stock_bars = {symbol: bars for symbol in symbols if (bars := _read_bars(args.data_dir, symbol)) is not None} + external_dividends: dict[str, dict[pd.Timestamp, float]] = {} + for symbol, bars in stock_bars.items(): + events = bars.attrs.get("external_dividends", {}) + if events: + external_dividends[symbol] = { + pd.Timestamp(date).normalize(): float(amount) for date, amount in events.items() + } + if args.set_reference: + reference_path = Path(args.set_reference) + manifest_path = Path(str(reference_path) + ".manifest.json") + if not reference_path.exists() or not manifest_path.exists(): + raise SiamChartActionError( + f"SET backtest reference requires both {reference_path} and its coverage manifest {manifest_path}; " + "create them with import-set-actions" + ) + manifest = json.loads(manifest_path.read_text(encoding="utf-8")) + if not manifest.get("coverage_complete"): + raise SiamChartActionError("SET reference coverage is incomplete; refusing an action-adjusted backtest") + covered_symbols = {str(symbol).upper() for symbol in manifest.get("requested_price_symbols", [])} + uncovered_symbols = sorted(set(symbols) - covered_symbols) + if uncovered_symbols: + raise SiamChartActionError( + "SET reference does not cover selected price symbols: " + ", ".join(uncovered_symbols[:20]) + ) + coverage_start = pd.Timestamp(manifest["start_inclusive"]).normalize() + coverage_end = pd.Timestamp(manifest["end_exclusive"]).normalize() + reference = pd.read_csv(reference_path, dtype={"symbol": str, "action_type": str}) + required = {"symbol", "action_type", "date", "value"} + if not required.issubset(reference.columns): + raise SiamChartActionError("SET reference CSV must contain symbol, action_type, date and value") + reference["symbol"] = reference["symbol"].str.upper().str.strip() + reference["action_type"] = reference["action_type"].str.lower().str.strip() + reference["date"] = pd.to_datetime(reference["date"], errors="coerce").dt.normalize() + reference["value"] = pd.to_numeric(reference["value"], errors="coerce") + if reference[["date", "value"]].isna().any().any(): + raise SiamChartActionError("SET reference CSV contains invalid dates or dividend values") + reference = reference.loc[ + reference["symbol"].isin(symbols) + & (reference["action_type"] == "dividend") + & (reference["date"] >= coverage_start) + & (reference["date"] < coverage_end) + ] + if reference.duplicated(["symbol", "date"]).any(): + raise SiamChartActionError("SET reference has duplicate cash-dividend rows for a symbol and date") + excluded: list[tuple[str, pd.Timestamp, float]] = [] + value_differences: list[tuple[str, pd.Timestamp, float, float]] = [] + for symbol, bars in stock_bars.items(): + events = external_dividends.setdefault(symbol, {}) + set_events = { + pd.Timestamp(row.date).normalize(): float(row.value) + for row in reference.loc[reference["symbol"] == symbol].itertuples(index=False) + } + for date, amount in list(events.items()): + if coverage_start <= date < coverage_end: + if date not in set_events: + excluded.append((symbol, date, amount)) + elif abs(amount - set_events[date]) > 5.1e-7: + value_differences.append((symbol, date, amount, set_events[date])) + del events[date] + events.update(set_events) + # Suppress provider-native dividend values inside the covered period; + # the complete SET event list is the only cash-action source there. + in_coverage = (bars.index >= coverage_start) & (bars.index < coverage_end) + if "dividends" in bars.columns and in_coverage.any(): + bars.loc[in_coverage, "dividends"] = 0.0 + if excluded: + detail = ", ".join(f"{symbol} {date.date()} {amount:g}" for symbol, date, amount in excluded[:12]) + warnings.warn( + f"SET is authoritative for cash dividends from {coverage_start.date()} through " + f"{(coverage_end - pd.Timedelta(days=1)).date()}; excluded SiamChart-only events pending review: {detail}", + RuntimeWarning, + ) + if value_differences: + detail = ", ".join(f"{symbol} {date.date()} SiamChart={old:g} SET={new:g}" + for symbol, date, old, new in value_differences[:12]) + warnings.warn("SET amounts replaced differing SiamChart amounts: " + detail, RuntimeWarning) + print(f"SET cash actions applied: {len(reference)} event(s), coverage {coverage_start.date()} to " + f"{(coverage_end - pd.Timedelta(days=1)).date()} inclusive.") + missing = sorted(set(symbols) - set(stock_bars)) + if missing: + print("No cached data for: " + ", ".join(missing), file=sys.stderr) + if args.mode == "accurate": + raise SymbolDataError(f"accurate membership mode is missing historical OHLCV for {len(missing)} constituents; refusing incomplete breadth/backtest") + print("SURVIVORSHIP / COVERAGE WARNING: breadth and SET100EW will use only downloaded members.", file=sys.stderr) + if not stock_bars: + raise SymbolDataError("no constituent price data available") + if args.mode == "accurate": + if membership is None: + raise MembershipError("accurate mode requires a dated membership file") + set_bars = _accurate_context_bars(set_bars, membership, args.start, args.end) + session_dates = set_bars.index + stock_bars = {symbol: bars.loc[bars.index.isin(session_dates)] for symbol, bars in stock_bars.items()} + market, _, _ = build_market_context(stock_bars, set_bars, membership, accurate=True, + research=config.research) + warnings.warn("ACCURATE membership mode uses the supplied PIT file. Price history and corporate actions depend on the selected source and must be validated before performance claims.", RuntimeWarning) + else: + dates = set_bars.index + members, _ = resolve_universe(args.universe, membership_file=args.membership_file if membership else None, + symbols=args.symbols, mode="fast", dates=dates) + market, _, _ = build_market_context(stock_bars, set_bars, membership, + current_members=members, accurate=False, + research=config.research) + market.attrs["external_dividends"] = external_dividends + panel = build_feature_panel(stock_bars, set_bars, market, rs_weights=config.rs_weights, + volume_ratio=config.strategy.volume_pullback_ratio, + research=config.research) + return panel, market, set_bars, membership, symbols + + +def _load_config(path: str | None) -> AppConfig: + return AppConfig.load(path) if path and Path(path).exists() else AppConfig() + + +def _parse_ema_pair(value: str) -> tuple[int, int]: + try: + fast_text, slow_text = value.split(":", 1) + fast, slow = int(fast_text), int(slow_text) + except (ValueError, TypeError) as exc: + raise ValueError(f"invalid EMA pair {value!r}; use FAST:SLOW such as 20:50") from exc + if fast < 1 or slow < 2 or fast >= slow: + raise ValueError(f"invalid EMA pair {value!r}; require positive spans with fast < slow") + return fast, slow + + +def _ema_periods(args: argparse.Namespace, market: pd.DataFrame) -> list[tuple[str, str, str]]: + start = pd.Timestamp(args.start).normalize() if args.start else pd.Timestamp(market.index.min()).normalize() + end = pd.Timestamp(args.end).normalize() if args.end else pd.Timestamp(market.index.max()).normalize() + pd.Timedelta(days=1) + if end <= start: + raise ValueError("--end must be later than --start") + periods = [("full", start.date().isoformat(), end.date().isoformat())] + if args.split_by_year: + cursor = start + while cursor < end: + next_year = pd.Timestamp(year=cursor.year + 1, month=1, day=1) + period_end = min(next_year, end) + if cursor.month == 1 and cursor.day == 1 and period_end == next_year: + label = str(cursor.year) + else: + label = f"{cursor.date().isoformat()}_to_{(period_end - pd.Timedelta(days=1)).date().isoformat()}" + periods.append((label, cursor.date().isoformat(), period_end.date().isoformat())) + cursor = period_end + return periods + + +def _download(args: argparse.Namespace) -> int: + symbols, membership = _symbols_for_download(args) + if args.mode == "accurate": + if membership is None or not args.start: + raise MembershipError("accurate downloads require a dated membership file and explicit --start/--end") + end = pd.Timestamp(args.end) - pd.Timedelta(days=1) if args.end else pd.Timestamp.now().normalize() + membership.require_coverage(pd.bdate_range(pd.Timestamp(args.start), end)) + print(f"PIT membership validation passed for {args.universe} from {args.start} through {end.date()}.") + elif membership is None: + print("SURVIVORSHIP BIAS: current/custom sample symbols are for pipeline development only.") + else: + latest_end = max(row.end for row in membership.periods) + print(f"SURVIVORSHIP BIAS: fast download uses the supplied constituent snapshot (latest effective date {latest_end.date()}) for research only.") + provider = YFinanceProvider() + data, errors = provider.download_many(symbols, start=args.start, end=args.end, + period=args.period if not args.start else None, + refresh=args.refresh) + output = Path(args.data_dir) + output.mkdir(parents=True, exist_ok=True) + for symbol, frame in data.items(): + destination = output / f"{safe_filename(symbol)}.csv" + frame.to_csv(destination, index_label="date") + print(f"{symbol}: {len(frame)} daily bars -> {destination}") + if errors: + print("\nDownload issues (other symbols were retained):", file=sys.stderr) + for symbol, message in errors.items(): + print(f"- {symbol}: {message}", file=sys.stderr) + print(f"\nDownloaded {len(data)}/{len(symbols)} symbols. auto_adjust=False; Adj Close, dividends and splits retained.") + return 1 if not data else 0 + + +def _import_siamchart(args: argparse.Namespace) -> int: + data, issues = read_siamchart_archives(args.archives, args.symbols) + output = Path(args.data_dir) + output.mkdir(parents=True, exist_ok=True) + for symbol, frame in sorted(data.items()): + filename = "^SET.csv" if symbol == "^SET" else f"{safe_filename(symbol)}.csv" + destination = output / filename + frame.to_csv(destination, index_label="date") + print(f"{symbol}: {len(frame)} daily bars ({frame.index.min().date()} to {frame.index.max().date()}) -> {destination}") + if issues: + print(f"\nSiamChart entries skipped: {len(issues)}", file=sys.stderr) + for issue in issues[:20]: + print(f"- {issue}", file=sys.stderr) + if len(issues) > 20: + print(f"- ... and {len(issues) - 20} more", file=sys.stderr) + print("SiamChart EOD archives contain raw OHLCV. Separate stock-info pages list dividend and par-value history, but this importer does not ingest those tables.") + return 0 + + +def _import_siamchart_actions(args: argparse.Namespace) -> int: + if args.delay_seconds < 0: + raise ValueError("--delay-seconds must be zero or greater") + symbols = sorted({symbol.strip().upper().removesuffix(".BK") for symbol in args.symbols if symbol.strip()}) + if not symbols: + raise ValueError("select at least one ticker to import") + action_dir = Path(args.data_dir) / "actions" + action_dir.mkdir(parents=True, exist_ok=True) + failures: list[str] = [] + succeeded = 0 + for index, symbol in enumerate(symbols): + if index and args.delay_seconds: + time.sleep(args.delay_seconds) + try: + actions = fetch_siamchart_stock_info(symbol) + except SiamChartActionError as exc: + failures.append(str(exc)) + continue + dividend_path = action_dir / f"{safe_filename(symbol)}_dividends.csv" + par_path = action_dir / f"{safe_filename(symbol)}_par_changes.csv" + actions.dividends.to_csv(dividend_path, index=False) + actions.par_changes.to_csv(par_path, index=False) + print(f"{symbol}: {len(actions.dividends)} dividend event(s), {len(actions.par_changes)} par-value row(s)") + print(f" -> {dividend_path}") + print(f" -> {par_path}") + succeeded += 1 + if failures: + print("\nSiamChart stock-info pages not imported:", file=sys.stderr) + for failure in failures: + print(f"- {failure}", file=sys.stderr) + print(f"\nImported {succeeded}/{len(symbols)} tickers. Dividend dates are treated as XD dates; par-value changes remain review-only.") + return 1 if failures or not succeeded else 0 + + +def _verify_siamchart_actions(args: argparse.Namespace) -> int: + symbols = sorted({symbol.strip().upper().removesuffix(".BK") for symbol in args.symbols if symbol.strip()}) + if not symbols: + raise ValueError("select at least one ticker to verify") + result = audit_siamchart_actions(Path(args.data_dir) / "actions", args.set_reference, symbols, + start=args.start, end=args.end, + action_types=args.action_types) + output = Path(args.output) + output.parent.mkdir(parents=True, exist_ok=True) + result.to_csv(output, index=False) + counts = result["status"].value_counts().to_dict() if not result.empty else {} + print("SET versus SiamChart action audit") + for status in ("MATCH", "VALUE_MISMATCH", "ONLY_SET", "ONLY_SIAMCHART"): + if status in counts: + print(f"{status:18s} {counts[status]}") + print(f"\nSaved audit to {output.resolve()}") + return 1 if any(status != "MATCH" for status in result["status"]) else 0 + + +def _import_set_actions(args: argparse.Namespace) -> int: + symbols = sorted({symbol.strip().upper().removesuffix(".BK") for symbol in args.symbols if symbol.strip()}) + if not symbols: + raise ValueError("select at least one ticker to import") + aliases_path = Path(args.symbol_aliases) if args.symbol_aliases else None + aliases = pd.DataFrame(columns=["source_symbol", "price_symbol", "start_date", "end_date", "reason"]) + if aliases_path and aliases_path.exists(): + aliases = pd.read_csv(aliases_path, dtype=str).fillna("") + required = {"source_symbol", "price_symbol", "start_date", "end_date", "reason"} + if not required.issubset(aliases.columns): + raise ValueError(f"symbol alias CSV must contain {', '.join(sorted(required))}") + for column in ("source_symbol", "price_symbol"): + aliases[column] = aliases[column].str.upper().str.strip() + aliases["start_date"] = pd.to_datetime(aliases["start_date"], errors="coerce").dt.normalize() + aliases["end_date"] = pd.to_datetime(aliases["end_date"], errors="coerce").dt.normalize() + if aliases[["start_date", "end_date"]].isna().any().any() or (aliases["end_date"] <= aliases["start_date"]).any(): + raise ValueError("symbol alias CSV contains invalid effective date intervals") + fetch_symbols = sorted(set(symbols) | set(aliases["source_symbol"].tolist())) + reference, issues = fetch_set_cash_dividend_reference( + fetch_symbols, start=args.start, end=args.end, delay_seconds=args.delay_seconds, + ) + start_date, end_date = pd.Timestamp(args.start).normalize(), pd.Timestamp(args.end).normalize() + if not reference.empty: + reference["source_symbol"] = reference["symbol"].astype(str).str.upper() + reference["date"] = pd.to_datetime(reference["date"]).dt.normalize() + for alias in aliases.itertuples(index=False): + mask = ((reference["source_symbol"] == alias.source_symbol) + & (reference["date"] >= alias.start_date) + & (reference["date"] < alias.end_date)) + reference.loc[mask, "symbol"] = alias.price_symbol + key = ["symbol", "action_type", "date"] + collapsed: list[dict[str, object]] = [] + for _, group in reference.groupby(key, sort=False): + if group["value"].max() - group["value"].min() > 1e-9: + names = ", ".join(sorted(group["source_symbol"].astype(str).unique())) + raise SiamChartActionError( + f"aliased SET symbols disagree for {group.iloc[0]['symbol']} " + f"{pd.Timestamp(group.iloc[0]['date']).date()}: {names}" + ) + row = group.iloc[0].to_dict() + row["source_symbol"] = ",".join(sorted(group["source_symbol"].astype(str).unique())) + row["source_url"] = " | ".join(sorted(group["source_url"].astype(str).unique())) + collapsed.append(row) + reference = pd.DataFrame(collapsed).sort_values(["symbol", "date"]).reset_index(drop=True) + else: + reference["source_symbol"] = pd.Series(dtype=str) + reference["coverage_start"] = start_date.date().isoformat() + reference["coverage_end"] = end_date.date().isoformat() + reference["coverage_complete"] = not issues + output = Path(args.output) + output.parent.mkdir(parents=True, exist_ok=True) + reference.to_csv(output, index=False) + manifest = { + "schema_version": 1, + "provider": "SET public Rights & Benefits history", + "source_page": "https://www.set.or.th/en/market/product/stock/quote/{symbol}/rights-benefits", + "source_endpoint": "https://www.set.or.th/api/set/stock/{symbol}/corporate-action?lang=en", + "start_inclusive": start_date.date().isoformat(), + "end_exclusive": end_date.date().isoformat(), + "requested_price_symbols": symbols, + "fetched_set_symbols": fetch_symbols, + "symbol_aliases": [ + {"source_symbol": row.source_symbol, "price_symbol": row.price_symbol, + "start_date": row.start_date.date().isoformat(), "end_date": row.end_date.date().isoformat(), + "reason": row.reason} + for row in aliases.itertuples(index=False) + ], + "coverage_complete": not issues, + "issues": issues, + } + manifest_path = Path(str(output) + ".manifest.json") + manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8") + print(f"Imported {len(reference)} SET cash-dividend event(s) across {len(symbols)} price symbols " + f"({len(fetch_symbols)} SET ticker pages fetched).") + print(f"Saved SET reference to {output.resolve()}") + print(f"Saved coverage manifest to {manifest_path.resolve()}") + if issues: + print(f"\nSET symbols without reference data: {len(issues)}", file=sys.stderr) + for issue in issues: + print(f"- {issue}", file=sys.stderr) + return 1 if issues else 0 + + +def _market_report(market: pd.DataFrame) -> None: + row = market.iloc[-1] + mco_z = row.get("mco_z", float("nan")) + sigma = f"{mco_z:.2f} sigma" if pd.notna(mco_z) else "warming up" + print("THAI MARKET") + print(f"Date {market.index[-1].date()}") + print(f"SET 21DMA {row.get('trend_state', 'UNKNOWN')}") + print(f"SET100EW 21DMA {row.get('ew_trend_state', 'UNKNOWN')}") + print(f"MCO {row.get('mco', float('nan')):.2f} ({sigma})" if pd.notna(row.get("mco", float("nan"))) else f"MCO {sigma}") + print(f"MCSI {row.get('mcsi_state', 'UNKNOWN')}") + print(f"Breadth A {int(row.get('advancing', 0))} / D {int(row.get('declining', 0))} / U {int(row.get('unchanged', 0))}") + print(f"\nREGIME {row.get('regime', 'UNKNOWN')}") + print(f"NEW RISK {'ALLOWED' if bool(row.get('new_risk_allowed', False)) else 'BLOCKED'}") + for reason in row.get("regime_reasons", []): + print(f" - {reason}") + + +def main(argv: list[str] | None = None) -> int: + args = _parser().parse_args(argv) + try: + if args.command == "download": + return _download(args) + if args.command == "import-siamchart": + return _import_siamchart(args) + if args.command == "import-siamchart-actions": + return _import_siamchart_actions(args) + if args.command == "verify-siamchart-actions": + return _verify_siamchart_actions(args) + if args.command == "import-set-actions": + return _import_set_actions(args) + if args.command == "webui": + from .webui import run_server + run_server(host=args.host, port=args.port, archives=args.archives, report_dir=args.report_dir, + start=args.start, initial_equity=args.initial_equity) + return 0 + if args.command == "smoke": + config = AppConfig() + stock_bars, set_bars = synthetic_bars() + market, _, _ = build_market_context(stock_bars, set_bars, current_members=SMOKE_SYMBOLS, + research=config.research) + panel = build_feature_panel(stock_bars, set_bars, market, rs_weights=config.rs_weights, + volume_ratio=config.strategy.volume_pullback_ratio, + research=config.research) + scan = scan_candidates(panel, market, config=config, top_n=5) + result = run_backtest(panel, market, strategy=config.strategy, + rules=config.market_rules, costs=config.costs) + print("SYNTHETIC OFFLINE SMOKE - NOT MARKET PERFORMANCE") + print(f"bars={len(set_bars)} | price series={len(panel)} | SET index=yes | market context=yes") + print(f"indicators=ready | breadth/MCO/MCSI=ready | regime={market.iloc[-1]['regime']}") + print(f"scanner candidates={len(scan.candidates)} | backtest trades={len(result.trades)}") + print(f"total return={result.metrics['total_return']:.2%} | trim@2R={result.metrics['trim_2r_contribution']:,.0f} THB | runner={result.metrics['runner_contribution']:,.0f} THB") + return 0 + if args.command == "model-lab": + if pd.Timestamp(args.end) <= pd.Timestamp(args.start): + raise ValueError("--end must be later than --start") + summary = run_model_lab( + args.archives, start=args.start, end=args.end, output_dir=args.output_dir, + action_dir=args.action_dir, set_index_csv=args.set_index_csv, + set_reference=args.set_reference, initial_equity=args.initial_equity, + ) + print("MODEL SCREEN (fractional-share pass)") + for row in summary.itertuples(index=False): + print(f"{row.model:34s} {row.scenario:12s} " + f"return={row.total_return:7.1%} CAGR={row.cagr:6.1%} " + f"maxDD={row.max_drawdown:7.1%} Sharpe={row.sharpe_daily:5.2f}") + lot_report = Path(args.output_dir) / "board_lot_validation.csv" + if lot_report.exists(): + lots = pd.read_csv(lot_report) + print("\nTOP CANDIDATES (100-share buy lots)") + for row in lots.itertuples(index=False): + print(f"{row.model:34s} {row.scenario:12s} " + f"return={row.total_return:7.1%} CAGR={row.cagr:6.1%} " + f"maxDD={row.max_drawdown:7.1%}") + primary_union = lots[lots["model"] == "primary_union_donchian_ema"] + if not primary_union.empty: + print("\nPRIMARY MODEL UNION (either Donchian or EMA signal)") + for row in primary_union.itertuples(index=False): + print(f"{row.scenario:12s} return={row.total_return:7.1%} " + f"CAGR={row.cagr:6.1%} maxDD={row.max_drawdown:7.1%}") + print(f"\nSaved model-lab reports to {Path(args.output_dir).resolve()}") + return 0 + config = _load_config(args.config) + panel, market, set_bars, membership, _ = _prepare(args, config) + if args.command == "market": + _market_report(market) + return 0 + out_dir = Path(args.output_dir) + out_dir.mkdir(parents=True, exist_ok=True) + if args.command == "scan": + when = pd.Timestamp(args.end) - pd.Timedelta(days=1) if args.end else None + if when is not None: + panel = {symbol: frame.loc[frame.index <= when] for symbol, frame in panel.items()} + market = market.loc[market.index <= when] + result = scan_candidates(panel, market, config=config, top_n=args.top, + sectors={symbol: membership.sector(symbol, market.index[-1]) + for symbol in panel if membership} if membership else None) + _market_report(market) + print("\nFOCUS LIST") + if result.candidates.empty: + print("No liquid candidates meet the configured research filters.") + else: + view = result.candidates[["ticker", "score", "rs", "atr_distance", "setup", "entry", "stop", "shares"]] + print(view.to_string(index=False, float_format=lambda value: f"{value:.2f}")) + print("\nDETAILS") + for row in result.candidates.head(3).to_dict("records"): + print(f"\n{row['ticker']} Score {row['score']:.1f} RS {row['rs']:.1f}") + print(f"Setup {row['setup']} | Entry ref {row['entry']:.2f} | Stop {row['stop']:.2f} | R {row['initial_R']}") + print(f"Risk {row['risk_pct']:.2%} | Shares {row['shares']} | Position {row['position_value_thb']:,.0f} THB") + print("Signals: " + (", ".join(row["reasons"]) or "Watch only")) + if args.json_out: + result.write_json(args.json_out) + print(f"\nQwen-ready JSON: {args.json_out}") + return 0 + if args.command == "strategy-overlays": + if not args.start or not args.end: + raise ValueError("strategy-overlays requires explicit --start and --end dates") + window_start, window_end = pd.Timestamp(args.start), pd.Timestamp(args.end) + if window_end <= window_start: + raise ValueError("--end must be later than --start") + periods: list[tuple[str, str, str]] = [("full", args.start, args.end)] + for year in range(window_start.year, window_end.year + 1): + year_start = max(window_start, pd.Timestamp(year=year, month=1, day=1)) + year_end = min(window_end, pd.Timestamp(year=year + 1, month=1, day=1)) + if year_start < year_end: + periods.append((str(year), year_start.date().isoformat(), year_end.date().isoformat())) + variants: list[tuple[str, dict[str, object]]] = [ + ("baseline", {}), + ("weak_retest_no_1r_after_5_sessions", {"stalled_retest_sessions": 5}), + ("weak_retest_no_1r_after_8_sessions", {"stalled_retest_sessions": 8}), + ("weak_retest_no_1r_after_10_sessions", {"stalled_retest_sessions": 10}), + ("runner_floor_0r_after_2r_trim", {"runner_protection_r": 0.0}), + ("weak_retest_5_sessions_plus_runner_floor_0r", + {"stalled_retest_sessions": 5, "runner_protection_r": 0.0}), + ] + summary_rows: list[dict[str, object]] = [] + for variant, overlay in variants: + for period, period_start, period_end in periods: + result = run_backtest( + panel, market, strategy=config.strategy, rules=config.market_rules, + costs=config.costs, start=period_start, end=period_end, **overlay, + ) + run_dir = out_dir / "strategy_overlays" / variant / period + run_dir.mkdir(parents=True, exist_ok=True) + metrics_json = { + key: (value if math.isfinite(value) else None) + for key, value in result.metrics.items() + } + (run_dir / "backtest_metrics.json").write_text( + json.dumps(metrics_json, indent=2, allow_nan=False), encoding="utf-8" + ) + result.trades.to_csv(run_dir / "trades.csv", index=False) + result.equity_curve.to_csv(run_dir / "equity_curve.csv", index_label="date") + result.fills.to_csv(run_dir / "fills.csv", index=False) + pd.DataFrame(result.skipped, columns=["symbol", "date", "reason"]).to_csv( + run_dir / "skipped_entries.csv", index=False + ) + summary_rows.append({ + "variant": variant, "period": period, "start": period_start, + "end_exclusive": period_end, "trades": len(result.trades), + **result.metrics, + }) + summary = pd.DataFrame(summary_rows) + summary_path = out_dir / "strategy_overlays" / "summary.csv" + summary.to_csv(summary_path, index=False) + readme = ( + "# Strategy exit overlays\n\n" + f"Signal window: {window_start.date()} through {(window_end - pd.Timedelta(days=1)).date()} inclusive.\n\n" + "The baseline and overlays use the same PrimeThai entries, position sizing, execution costs, " + "and corporate-action inputs. The stalled-retest overlays exit a WEAKNESS/21DMA Retest at " + "the next open after 5, 8, or 10 sessions if its intraday maximum has not reached +1R. " + "The runner-floor overlay activates a stop at entry (0R) only from the session after a 2R trim. " + "The combined overlay applies both rules. Defaults remain unchanged.\n\n" + "Each calendar slice starts with fresh equity and liquidates at its own end; slices are not " + "additive. This short, repeatedly examined window is exploratory and is not an untouched " + "out-of-sample validation. Results do not establish a durable edge or predict live returns.\n" + ) + (out_dir / "strategy_overlays" / "README.md").write_text(readme, encoding="utf-8") + view = summary[["variant", "period", "trades", "total_return", "max_drawdown", + "profit_factor", "expectancy_per_trade", "average_holding_period"]] + print("PRIMETHAI EXIT OVERLAYS - SAME ENTRIES, NEXT-OPEN EXECUTION") + print(view.to_string(index=False, float_format=lambda value: f"{value:.4f}")) + print(f"\nSaved detailed runs to {(out_dir / 'strategy_overlays').resolve()}") + return 0 + if args.command == "ema-crossover": + pairs = [_parse_ema_pair(value) for value in args.ema_pairs] + if len(set(pairs)) != len(pairs): + raise ValueError("--ema-pairs contains a duplicate pair") + if not args.market_filters or len(set(args.market_filters)) != len(args.market_filters): + raise ValueError("--market-filters must be non-empty and contain no duplicates") + if not args.exit_modes or len(set(args.exit_modes)) != len(args.exit_modes): + raise ValueError("--exit-modes must be non-empty and contain no duplicates") + if (not args.max_loss_pcts + or any(not math.isfinite(value) or not 0 <= value < 1 for value in args.max_loss_pcts) + or len(set(args.max_loss_pcts)) != len(args.max_loss_pcts)): + raise ValueError("--max-loss-pcts must be unique values from 0 (off) to less than 1") + periods = _ema_periods(args, market) + position_pct = (config.strategy.max_position_pct if args.max_position_pct is None + else args.max_position_pct) + min_price = config.strategy.min_price if args.min_price is None else args.min_price + min_turnover = (config.strategy.min_value_turnover_thb + if args.min_value_turnover_thb is None else args.min_value_turnover_thb) + summary_rows: list[dict[str, object]] = [] + for fast, slow in pairs: + for market_filter in args.market_filters: + for exit_mode in args.exit_modes: + for stop_pct in args.max_loss_pcts: + stop_setting = None if stop_pct == 0 else stop_pct + for label, period_start, period_end in periods: + result = run_ema_crossover_backtest( + panel, market, fast=fast, slow=slow, strategy=config.strategy, + rules=config.market_rules, costs=config.costs, + start=period_start, end=period_end, + max_position_pct=position_pct, min_price=min_price, + min_value_turnover_thb=min_turnover, + market_filter=market_filter, exit_mode=exit_mode, + max_loss_pct=stop_setting, + ) + stop_label = "none" if stop_setting is None else f"{stop_setting:.0%}" + variant = f"filter_{market_filter}__exit_{exit_mode}__stop_{stop_label}" + run_dir = out_dir / f"ema_{fast}_{slow}" / variant / label + run_dir.mkdir(parents=True, exist_ok=True) + metrics_json = { + key: (value if math.isfinite(value) else None) + for key, value in result.metrics.items() + } + (run_dir / "backtest_metrics.json").write_text( + json.dumps(metrics_json, indent=2, allow_nan=False), encoding="utf-8" + ) + result.trades.to_csv(run_dir / "trades.csv", index=False) + result.equity_curve.to_csv(run_dir / "equity_curve.csv", index_label="date") + result.fills.to_csv(run_dir / "fills.csv", index=False) + pd.DataFrame(result.skipped, columns=["symbol", "date", "reason"]).to_csv( + run_dir / "skipped_entries.csv", index=False + ) + summary_rows.append({ + "ema_pair": f"{fast}:{slow}", "market_filter": market_filter, + "exit_mode": exit_mode, "max_loss_pct": stop_pct, + "period": label, "start": period_start, + "end_exclusive": period_end, "trades": len(result.trades), + **result.metrics, + }) + summary = pd.DataFrame(summary_rows) + summary.to_csv(out_dir / "summary.csv", index=False) + print("EMA CROSSOVER BACKTEST - CLOSE SIGNAL, NEXT-OPEN FILL") + print(summary[["ema_pair", "market_filter", "exit_mode", "max_loss_pct", "period", + "trades", "total_return", "max_drawdown", + "profit_factor", "win_rate", "exposure_pct"]].to_string( + index=False, float_format=lambda value: f"{value:.4f}" + )) + print(f"\nSaved detailed runs and summary to {out_dir.resolve()}") + print(f"Assumptions: at most {position_pct:.1%} of equity per stock; simultaneous entries share available cash equally; " + f"{min_price:g} THB minimum price; {min_turnover:,.0f} THB minimum 20-day average turnover; " + f"market filters={', '.join(args.market_filters)}; exits={', '.join(args.exit_modes)}; " + f"hard stops={', '.join('off' if value == 0 else f'{value:.0%}' for value in args.max_loss_pcts)}.") + return 0 + backtest = run_backtest(panel, market, strategy=config.strategy, rules=config.market_rules, + costs=config.costs, start=args.start, end=args.end) + if args.command == "backtest": + (out_dir / "backtest_metrics.json").write_text(json.dumps(backtest.metrics, indent=2), encoding="utf-8") + backtest.trades.to_csv(out_dir / "trades.csv", index=False) + backtest.equity_curve.to_csv(out_dir / "equity_curve.csv", index_label="date") + backtest.fills.to_csv(out_dir / "fills.csv", index=False) + pd.DataFrame(backtest.skipped, columns=["symbol", "date", "reason"]).to_csv( + out_dir / "skipped_entries.csv", index=False + ) + print(json.dumps(backtest.metrics, indent=2)) + print(f"\nTrades: {len(backtest.trades)} | trim @ 2R: {backtest.metrics['trim_2r_contribution']:,.2f} THB | runner: {backtest.metrics['runner_contribution']:,.2f} THB") + print(f"Saved reports to {out_dir.resolve()}") + return 0 + if args.command == "experiment": + comparison, _ = run_ablations(panel, market, base=config.strategy, rules=config.market_rules, costs=config.costs) + holdouts, _ = run_chronological_holdouts(panel, market, strategy=config.strategy, rules=config.market_rules, costs=config.costs) + walk = rolling_walk_forward(panel, market, strategy=config.strategy, rules=config.market_rules, costs=config.costs) + comparison.to_csv(out_dir / "ablation.csv", index=False) + holdouts.to_csv(out_dir / "holdouts.csv", index=False) + walk.to_csv(out_dir / "walk_forward.csv", index=False) + print("ABLATION") + print(comparison[["experiment", "total_return", "max_drawdown", "win_rate", "expectancy_r", "profit_factor", "trades"]].to_string(index=False)) + print("\nCHRONOLOGICAL HOLDOUTS") + print(holdouts[["period", "start", "end", "total_return", "max_drawdown", "trades"]].to_string(index=False)) + print(f"\nSaved tables to {out_dir.resolve()}") + return 0 + except (ValueError, MembershipError, SymbolDataError, SiamChartActionError, OSError, KeyError) as exc: + print(f"Error: {exc}", file=sys.stderr) + return 2 + return 0 + + +if __name__ == "__main__": + with warnings.catch_warnings(record=False): + warnings.simplefilter("default") + raise SystemExit(main()) diff --git a/primethai/config.py b/primethai/config.py new file mode 100644 index 0000000..99218bf --- /dev/null +++ b/primethai/config.py @@ -0,0 +1,191 @@ +"""Configuration objects for strategy and Thai market assumptions.""" + +from __future__ import annotations + +from dataclasses import asdict, dataclass, field +import json +import math +from pathlib import Path +from typing import Any + + +DEFAULT_FOCUS_WEIGHTS = { + "rs": 25, "market_alignment": 20, "structure": 15, "higher_low": 10, + "atr_distance": 10, "volume_behavior": 5, "tightness": 5, + "sector_strength": 5, "weekly": 5, +} + + +@dataclass(frozen=True) +class ThaiMarketRules: + default_board_lot: int = 100 + high_price_board_lot: int = 50 + high_price_threshold: float = 500.0 + high_price_months: int = 6 + minimum_quantity: int = 1 + etf_tick: float = 0.01 + + def tick_size(self, price: float, *, etf: bool = False) -> float: + if etf: + return self.etf_tick + if price < 2: + return 0.01 + if price < 5: + return 0.02 + if price < 10: + return 0.05 + if price < 25: + return 0.10 + if price < 100: + return 0.25 + if price < 200: + return 0.50 + if price < 400: + return 1.00 + return 2.00 + + def board_lot(self, price: float, *, high_price_eligible: bool = False) -> int: + return self.high_price_board_lot if high_price_eligible and price >= self.high_price_threshold else self.default_board_lot + + +@dataclass(frozen=True) +class TradingCosts: + """Research cost assumptions; rates are fractions of traded notional.""" + + commission_rate: float = 0.0015 + fees_rate: float = 0.0 + sell_tax_rate: float = 0.0 + slippage_bps: float = 5.0 + + def __post_init__(self) -> None: + for name in ("commission_rate", "fees_rate", "sell_tax_rate", "slippage_bps"): + if getattr(self, name) < 0: + raise ValueError(f"{name} must be non-negative") + + +@dataclass(frozen=True) +class RegimeHeat: + correction: float = 0.0 + repair: float = 0.005 + early_uptrend: float = 0.01 + confirmed_uptrend: float = 0.02 + uptrend_pullback: float = 0.02 + overbought: float = 0.0 + breakdown: float = 0.0 + + def for_regime(self, regime: str) -> float: + return { + "CORRECTION": self.correction, "REPAIR": self.repair, + "EARLY_UPTREND": self.early_uptrend, "CONFIRMED_UPTREND": self.confirmed_uptrend, + "UPTREND_PULLBACK": self.uptrend_pullback, "OVERBOUGHT": self.overbought, + "BREAKDOWN": self.breakdown, + }.get(regime.upper(), 0.0) + + +@dataclass(frozen=True) +class StrategyConfig: + initial_equity: float = 1_000_000.0 + weakness_risk_pct: float = 0.0025 + confirmation_risk_pct: float = 0.005 + max_position_pct: float = 0.20 + max_extension_atr: float = 1.0 + min_mco_z_for_entry: float = -2.0 + min_rs_percentile: float = 70.0 + min_value_turnover_thb: float = 20_000_000.0 + min_price: float = 2.0 + partial_r_multiple: float = 2.0 + partial_fraction: float = 1 / 3 + max_loss_pct: float = 0.08 + exit_mode: str = "close_below_ema21_low" + enable_rs: bool = True + enable_market_regime: bool = True + enable_mco: bool = True + enable_mcsi: bool = True + enable_higher_low: bool = True + enable_weekly: bool = True + enable_atr_filter: bool = True + require_market_support: bool = True + volume_pullback_ratio: float = 1.0 + tightness_lookback: int = 10 + base_breakout_window: int = 20 + heat: RegimeHeat = field(default_factory=RegimeHeat) + + def __post_init__(self) -> None: + if self.initial_equity <= 0 or self.max_position_pct <= 0: + raise ValueError("equity and position cap must be positive") + if not 0 < self.partial_fraction < 1: + raise ValueError("partial_fraction must be between 0 and 1") + if self.base_breakout_window < 1: + raise ValueError("base_breakout_window must be positive") + if self.exit_mode not in {"close_below_ema21_low", "break_failed_reclaim"}: + raise ValueError("unsupported exit_mode") + + +@dataclass(frozen=True) +class RelativeStrengthWeights: + one_month: float = 0.20 + three_month: float = 0.35 + twelve_month: float = 0.30 + high_52w: float = 0.15 + + +@dataclass(frozen=True) +class ResearchSettings: + slope_lookback: int = 5 + atr_period: int = 14 + adr_period: int = 20 + mco_short_span: int = 19 + mco_long_span: int = 39 + mco_z_window: int = 252 + pivot_left_bars: int = 3 + pivot_right_bars: int = 3 + pullback_volume_window: int = 5 + tightness_window: int = 10 + overbought_extension_atr: float = 1.5 + mco_repair_sigma: float = 1.0 + + def __post_init__(self) -> None: + for name in ("slope_lookback", "atr_period", "adr_period", "mco_short_span", + "mco_long_span", "mco_z_window", "pivot_left_bars", "pivot_right_bars", + "pullback_volume_window", "tightness_window"): + if getattr(self, name) < 1: + raise ValueError(f"{name} must be positive") + if self.overbought_extension_atr <= 0 or self.mco_repair_sigma < 0: + raise ValueError("regime thresholds must be non-negative and extension must be positive") + + +@dataclass(frozen=True) +class AppConfig: + strategy: StrategyConfig = field(default_factory=StrategyConfig) + market_rules: ThaiMarketRules = field(default_factory=ThaiMarketRules) + costs: TradingCosts = field(default_factory=TradingCosts) + rs_weights: RelativeStrengthWeights = field(default_factory=RelativeStrengthWeights) + research: ResearchSettings = field(default_factory=ResearchSettings) + focus_weights: dict[str, float] = field(default_factory=lambda: dict(DEFAULT_FOCUS_WEIGHTS)) + + def __post_init__(self) -> None: + unknown = set(self.focus_weights) - set(DEFAULT_FOCUS_WEIGHTS) + if unknown: + raise ValueError(f"unknown focus weight keys: {', '.join(sorted(unknown))}") + if not self.focus_weights or any(not math.isfinite(value) or value < 0 for value in self.focus_weights.values()): + raise ValueError("focus weights must be non-negative with at least one enabled component") + if sum(self.focus_weights.values()) <= 0: + raise ValueError("focus weights must sum to a positive value") + + @classmethod + def load(cls, path: str | Path | None = None) -> "AppConfig": + if path is None: + return cls() + raw: dict[str, Any] = json.loads(Path(path).read_text(encoding="utf-8")) + strategy_data = raw.get("strategy", {}) + if "heat" in strategy_data: + strategy_data["heat"] = RegimeHeat(**strategy_data["heat"]) + return cls(strategy=StrategyConfig(**strategy_data), + market_rules=ThaiMarketRules(**raw.get("market_rules", {})), + costs=TradingCosts(**raw.get("costs", {})), + rs_weights=RelativeStrengthWeights(**raw.get("rs_weights", {})), + research=ResearchSettings(**raw.get("research", {})), + focus_weights={**DEFAULT_FOCUS_WEIGHTS, **raw.get("focus_weights", {})}) + + def to_dict(self) -> dict[str, Any]: + return asdict(self) diff --git a/primethai/corporate_actions.py b/primethai/corporate_actions.py new file mode 100644 index 0000000..00c66f6 --- /dev/null +++ b/primethai/corporate_actions.py @@ -0,0 +1,463 @@ +"""SiamChart stock-info corporate-action import and SET cross-check helpers.""" + +from __future__ import annotations + +import ast +from dataclasses import dataclass +from html.parser import HTMLParser +import json +from math import isclose +from pathlib import Path +import re +import time +from http.cookiejar import CookieJar +from urllib.error import HTTPError, URLError +from urllib.parse import quote +from urllib.request import HTTPCookieProcessor, Request, build_opener, urlopen + +import pandas as pd + + +class SiamChartActionError(ValueError): + """Invalid or unavailable SiamChart action data.""" + + +DIVIDEND_COLUMNS = ["symbol", "ex_date", "dividend_per_share", "source_url"] +PAR_COLUMNS = ["symbol", "effective_date", "par_value", "previous_par_value", + "potential_share_ratio", "review_status", "source_url"] +SET_REFERENCE_COLUMNS = ["symbol", "action_type", "date", "value", "source_url"] +SET_ACTIONS_URL = "https://www.set.or.th/api/set/stock/{symbol}/corporate-action?lang=en" +SET_RIGHTS_BENEFITS_URL = "https://www.set.or.th/en/market/product/stock/quote/{symbol}/rights-benefits" +_BROWSER_USER_AGENT = ( + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" +) + + +@dataclass(frozen=True) +class SiamChartActions: + dividends: pd.DataFrame + par_changes: pd.DataFrame + + +class _HTMLTables(HTMLParser): + """Collect plain-text rows from HTML tables without optional parser packages.""" + + def __init__(self) -> None: + super().__init__(convert_charrefs=True) + self.tables: list[list[list[str]]] = [] + self._table_stack: list[list[list[str]]] = [] + self._row_stack: list[list[str] | None] = [] + self._cell_stack: list[list[str] | None] = [] + + def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: + tag = tag.lower() + if tag == "table": + table: list[list[str]] = [] + self.tables.append(table) + self._table_stack.append(table) + self._row_stack.append(None) + self._cell_stack.append(None) + elif self._table_stack and tag == "tr": + self._row_stack[-1] = [] + elif self._table_stack and tag in {"td", "th"} and self._row_stack[-1] is not None: + self._cell_stack[-1] = [] + + def handle_data(self, data: str) -> None: + if self._table_stack and self._cell_stack[-1] is not None: + self._cell_stack[-1].append(data) + + def handle_endtag(self, tag: str) -> None: + tag = tag.lower() + if not self._table_stack: + return + if tag in {"td", "th"} and self._cell_stack[-1] is not None: + row = self._row_stack[-1] + if row is not None: + row.append(" ".join("".join(self._cell_stack[-1]).split())) + self._cell_stack[-1] = None + elif tag == "tr" and self._row_stack[-1] is not None: + row = self._row_stack[-1] + if any(cell.strip() for cell in row): + self._table_stack[-1].append(row) + self._row_stack[-1] = None + elif tag == "table": + self._table_stack.pop() + self._row_stack.pop() + self._cell_stack.pop() + + +def _header_text(value: str) -> str: + return re.sub(r"[\s\u200b\ufeff]+", "", value).lower() + + +def _find_column(headers: list[str], *, kind: str) -> int | None: + normalized = [_header_text(value) for value in headers] + if kind == "date": + return next((i for i, value in enumerate(normalized) + if "วันที่" in value or "date" in value), None) + if kind == "dividend": + return next((i for i, value in enumerate(normalized) + if ("ปันผล" in value or "dividend" in value or value in {"dps", "dps/หุ้น"}) + and not any(skip in value for skip in ("ก่อน", "ราคา", "yield", "%"))), None) + if kind == "par": + return next((i for i, value in enumerate(normalized) + if "ราคาพาร์" in value or "parvalue" in value or value in {"par", "parvalue"}), None) + raise ValueError(f"unknown table column kind: {kind}") + + +def _parse_date(value: str) -> pd.Timestamp | None: + match = re.search(r"\b(\d{4}-\d{1,2}-\d{1,2})\b", value) + if not match: + return None + result = pd.to_datetime(match.group(1), errors="coerce") + return None if pd.isna(result) else pd.Timestamp(result).normalize() + + +def _parse_number(value: str) -> float | None: + normalized = value.strip().replace(",", "") + if not normalized or normalized in {"-", "–", "—", "n/a", "N/A"}: + return None + cleaned = re.sub(r"[^0-9.+-]", "", normalized) + try: + number = float(cleaned) + except ValueError: + return None + return number if pd.notna(number) else None + + +def _javascript_string_assignment(html: str, variable: str) -> str | None: + """Read a quoted JavaScript assignment without evaluating page code.""" + if variable == "data_table_html": + pattern = r"document\.getElementById\(\s*['\"]data_table['\"]\s*\)\.innerHTML\s*=\s*(['\"])" + else: + pattern = rf"\bvar\s+{re.escape(variable)}\s*=\s*(['\"])" + match = re.search(pattern, html) + if not match: + return None + quote_char = match.group(1) + start = match.end() + escaped = False + end = start + while end < len(html): + char = html[end] + if escaped: + escaped = False + elif char == "\\": + escaped = True + elif char == quote_char: + literal = quote_char + html[start:end] + quote_char + try: + value = ast.literal_eval(literal) + except (SyntaxError, ValueError): + return None + return value if isinstance(value, str) else None + end += 1 + return None + + +def _extract_history_tables(tables: list[list[list[str]]], *, kind: str, + symbol: str, source_url: str) -> tuple[pd.DataFrame, bool]: + rows_out: list[dict[str, object]] = [] + found_table = False + value_kind = "dividend" if kind == "dividend" else "par" + for rows in tables: + header_row: int | None = None + date_col = value_col = None + for row_idx, row in enumerate(rows): + date_candidate = _find_column(row, kind="date") + value_candidate = _find_column(row, kind=value_kind) + if date_candidate is not None and value_candidate is not None: + header_row, date_col, value_col = row_idx, date_candidate, value_candidate + found_table = True + break + if header_row is None or date_col is None or value_col is None: + continue + for row in rows[header_row + 1:]: + if max(date_col, value_col) >= len(row): + continue + date = _parse_date(row[date_col]) + value = _parse_number(row[value_col]) + if date is None or value is None or value <= 0: + continue + if kind == "dividend": + rows_out.append({"symbol": symbol, "ex_date": date, + "dividend_per_share": value, "source_url": source_url}) + else: + rows_out.append({"symbol": symbol, "effective_date": date, + "par_value": value, "source_url": source_url}) + if kind == "dividend": + frame = pd.DataFrame(rows_out, columns=DIVIDEND_COLUMNS) + if not frame.empty: + # Multiple cash distributions can share one XD date; backtest cash is additive. + frame = (frame.groupby(["symbol", "ex_date", "source_url"], as_index=False) + ["dividend_per_share"].sum().sort_values("ex_date").reset_index(drop=True)) + return frame, found_table + frame = pd.DataFrame(rows_out, columns=["symbol", "effective_date", "par_value", "source_url"]) + if not frame.empty: + frame = frame.sort_values("effective_date").drop_duplicates( + ["symbol", "effective_date"], keep="last").reset_index(drop=True) + previous: list[float | None] = [] + ratios: list[float | None] = [] + statuses: list[str] = [] + previous_value: float | None = None + for value in frame["par_value"].astype(float): + previous.append(previous_value) + ratios.append(previous_value / value if previous_value is not None and value > 0 else None) + statuses.append("review_required" if previous_value is not None else "baseline") + previous_value = value + frame["previous_par_value"] = previous + frame["potential_share_ratio"] = ratios + frame["review_status"] = statuses + frame = frame[PAR_COLUMNS] + else: + frame = pd.DataFrame(columns=PAR_COLUMNS) + return frame, found_table + + +def parse_siamchart_stock_info(html: str, symbol: str, *, source_url: str | None = None) -> SiamChartActions: + """Parse SiamChart's stock-info dividend (XD) and par-value history tables.""" + normalized_symbol = symbol.strip().upper() + if not re.fullmatch(r"[A-Z0-9._-]+", normalized_symbol): + raise SiamChartActionError(f"invalid SiamChart stock symbol: {symbol!r}") + url = source_url or f"https://siamchart.com/stock-info/{quote(normalized_symbol)}/" + parser = _HTMLTables() + parser.feed(html) + # SiamChart places its action tables inside a JavaScript innerHTML string. + # Extract that string as data rather than executing page scripts. + for variable in ("web_head", "data_table_html"): + embedded_content = _javascript_string_assignment(html, variable) + if embedded_content: + parser.feed(embedded_content) + dividends, has_dividend_table = _extract_history_tables( + parser.tables, kind="dividend", symbol=normalized_symbol, source_url=url) + par_changes, has_par_table = _extract_history_tables( + parser.tables, kind="par", symbol=normalized_symbol, source_url=url) + if not has_dividend_table and not has_par_table: + raise SiamChartActionError(f"{normalized_symbol}: no recognizable SiamChart action tables found") + return SiamChartActions(dividends, par_changes) + + +def fetch_siamchart_stock_info(symbol: str, *, timeout: float = 20.0) -> SiamChartActions: + normalized_symbol = symbol.strip().upper() + if not re.fullmatch(r"[A-Z0-9._-]+", normalized_symbol): + raise SiamChartActionError(f"invalid SiamChart stock symbol: {symbol!r}") + url = f"https://siamchart.com/stock-info/{quote(normalized_symbol)}/" + request = Request(url, headers={"User-Agent": _BROWSER_USER_AGENT}) + try: + with urlopen(request, timeout=timeout) as response: + charset = response.headers.get_content_charset() or "utf-8" + html = response.read().decode(charset, errors="replace") + except (HTTPError, URLError, TimeoutError, OSError) as exc: + raise SiamChartActionError(f"{normalized_symbol}: could not fetch {url}: {exc}") from exc + return parse_siamchart_stock_info(html, normalized_symbol, source_url=url) + + +def fetch_set_cash_dividend_reference(symbols: list[str], *, start: str, end: str, + delay_seconds: float = 0.35, + timeout: float = 20.0) -> tuple[pd.DataFrame, list[str]]: + """Fetch cash-XD rows exposed by SET's public five-year Rights & Benefits pages. + + ``end`` is exclusive. The SET public page is session-backed, so one official + rights-benefits page is opened first to acquire its normal site cookies before + reading the corresponding JSON route. This is the page's public data route, + not the separately sold SMART Marketplace CA feed. + """ + if delay_seconds < 0: + raise SiamChartActionError("delay_seconds must be zero or greater") + normalized_symbols = sorted({item.strip().upper() for item in symbols if item.strip()}) + if not normalized_symbols: + raise SiamChartActionError("select at least one symbol for SET reference import") + if any(not re.fullmatch(r"[A-Z0-9._-]+", symbol) for symbol in normalized_symbols): + raise SiamChartActionError("SET symbols may only contain letters, digits, dot, underscore or hyphen") + start_date, end_date = pd.Timestamp(start).normalize(), pd.Timestamp(end).normalize() + if end_date <= start_date: + raise SiamChartActionError("SET reference end date must be later than start date") + + opener = build_opener(HTTPCookieProcessor(CookieJar())) + seed_url = SET_RIGHTS_BENEFITS_URL.format(symbol="AP") + try: + with opener.open(Request(seed_url, headers={ + "User-Agent": _BROWSER_USER_AGENT, + "Accept-Language": "en-US,en;q=0.9", + }), timeout=timeout) as response: + response.read() + except (HTTPError, URLError, TimeoutError, OSError) as exc: + raise SiamChartActionError(f"could not open SET rights-and-benefits page {seed_url}: {exc}") from exc + + rows_out: list[dict[str, object]] = [] + issues: list[str] = [] + for index, symbol in enumerate(normalized_symbols): + if index and delay_seconds: + time.sleep(delay_seconds) + source_url = SET_RIGHTS_BENEFITS_URL.format(symbol=quote(symbol, safe="")) + endpoint = SET_ACTIONS_URL.format(symbol=quote(symbol, safe="")) + request = Request(endpoint, headers={ + "User-Agent": _BROWSER_USER_AGENT, + "Accept": "application/json, text/plain, */*", + "Accept-Language": "en-US,en;q=0.9", + "Referer": source_url, + "Origin": "https://www.set.or.th", + "X-Requested-With": "XMLHttpRequest", + }) + try: + with opener.open(request, timeout=timeout) as response: + actions = json.loads(response.read().decode("utf-8")) + if not isinstance(actions, list): + raise ValueError("SET response was not a list") + except (HTTPError, URLError, TimeoutError, OSError, UnicodeDecodeError, json.JSONDecodeError, ValueError) as exc: + issues.append(f"{symbol}: SET action data unavailable: {exc}") + continue + for action in actions: + if not isinstance(action, dict) or str(action.get("caType", "")).upper() != "XD": + continue + if "cash dividend" not in str(action.get("dividendType", "")).lower(): + continue + event_date = pd.to_datetime(action.get("xdate"), errors="coerce", utc=True) + value = pd.to_numeric(action.get("dividend"), errors="coerce") + if pd.isna(event_date) or pd.isna(value) or float(value) <= 0: + continue + local_date = event_date.tz_convert("Asia/Bangkok").tz_localize(None).normalize() + if start_date <= local_date < end_date: + rows_out.append({"symbol": symbol, "action_type": "dividend", "date": local_date, + "value": float(value), "source_url": source_url}) + + reference = pd.DataFrame(rows_out, columns=SET_REFERENCE_COLUMNS) + if not reference.empty: + reference = (reference.groupby(["symbol", "action_type", "date", "source_url"], as_index=False) + ["value"].sum().sort_values(["symbol", "date"]).reset_index(drop=True)) + return reference, issues + + +def read_siamchart_dividend_sidecar(path: str | Path, symbol: str) -> pd.DataFrame: + """Read and validate the normalized dividend sidecar emitted by the importer.""" + path = Path(path) + frame = pd.read_csv(path, dtype={"symbol": str}) + required = {"symbol", "ex_date", "dividend_per_share"} + if not required.issubset(frame.columns): + raise SiamChartActionError(f"{path}: expected columns {', '.join(sorted(required))}") + frame["symbol"] = frame["symbol"].str.upper().str.strip() + frame = frame.loc[frame["symbol"] == symbol.upper()].copy() + frame["ex_date"] = pd.to_datetime(frame["ex_date"], errors="coerce").dt.normalize() + frame["dividend_per_share"] = pd.to_numeric(frame["dividend_per_share"], errors="coerce") + if frame["ex_date"].isna().any() or frame["dividend_per_share"].isna().any() or (frame["dividend_per_share"] < 0).any(): + raise SiamChartActionError(f"{path}: invalid dividend date or amount") + return frame.sort_values("ex_date").reset_index(drop=True) + + +def merge_siamchart_dividends(bars: pd.DataFrame, actions: pd.DataFrame, + symbol: str) -> pd.DataFrame: + """Apply imported XD cash amounts to bars and retain action-only dates for the simulator.""" + out = bars.copy() + external: dict[pd.Timestamp, float] = {} + if not actions.empty: + for row in actions.itertuples(index=False): + date = pd.Timestamp(row.ex_date).normalize() + amount = float(row.dividend_per_share) + external[date] = external.get(date, 0.0) + amount + for date, amount in external.items(): + if date not in out.index: + continue + existing = float(out.loc[date, "dividends"]) + if existing and not isclose(existing, amount, rel_tol=1e-8, abs_tol=1e-8): + raise SiamChartActionError( + f"{symbol} {date.date()}: existing dividend {existing:g} conflicts with " + f"SiamChart amount {amount:g}; reconcile sources before backtesting" + ) + out.loc[date, "dividends"] = amount + out.attrs.update(bars.attrs) + out.attrs["external_dividends"] = external + return out + + +def audit_siamchart_actions(action_dir: str | Path, set_reference: str | Path, + symbols: list[str], *, start: str | None = None, + end: str | None = None, + action_types: list[str] | None = None) -> pd.DataFrame: + """Compare imported SiamChart action sidecars with user-verified SET reference rows. + + SET reference CSV schema: symbol,action_type,date,value,source_url; action_type is + ``dividend`` or ``par_value``. The reference should cover the same symbols and dates. + """ + reference = pd.read_csv(set_reference, dtype={"symbol": str, "action_type": str}) + required = set(SET_REFERENCE_COLUMNS[:-1]) + if not required.issubset(reference.columns): + raise SiamChartActionError( + f"SET reference must contain columns {', '.join(sorted(required))}" + ) + reference["symbol"] = reference["symbol"].str.upper().str.strip() + reference["action_type"] = reference["action_type"].str.lower().str.strip() + reference["date"] = pd.to_datetime(reference["date"], errors="coerce", utc=True) + reference["date"] = reference["date"].dt.tz_convert("Asia/Bangkok").dt.tz_localize(None).dt.normalize() + reference["value"] = pd.to_numeric(reference["value"], errors="coerce") + if reference["date"].isna().any() or reference["value"].isna().any(): + raise SiamChartActionError("SET reference has invalid dates or values") + if not reference["action_type"].isin({"dividend", "par_value"}).all(): + raise SiamChartActionError("SET reference action_type must be dividend or par_value") + selected_types = {kind.lower() for kind in action_types} if action_types else {"dividend", "par_value"} + if not selected_types.issubset({"dividend", "par_value"}): + raise SiamChartActionError("action_types must contain dividend and/or par_value") + normalized_symbols = {item.upper() for item in symbols} + reference = reference.loc[ + reference["symbol"].isin(normalized_symbols) & reference["action_type"].isin(selected_types) + ] + if start: + reference = reference.loc[reference["date"] >= pd.Timestamp(start).normalize()] + if end: + reference = reference.loc[reference["date"] < pd.Timestamp(end).normalize()] + + siam_rows: list[dict[str, object]] = [] + action_path = Path(action_dir) + for symbol in sorted({item.upper() for item in symbols}): + dividend_path = action_path / f"{symbol}_dividends.csv" + if dividend_path.exists(): + dividends = read_siamchart_dividend_sidecar(dividend_path, symbol) + siam_rows.extend({"symbol": symbol, "action_type": "dividend", "date": row.ex_date, + "value": float(row.dividend_per_share), "source_url": getattr(row, "source_url", "")} + for row in dividends.itertuples(index=False)) + par_path = action_path / f"{symbol}_par_changes.csv" + if par_path.exists(): + par = pd.read_csv(par_path) + required_par = {"symbol", "effective_date", "par_value"} + if not required_par.issubset(par.columns): + raise SiamChartActionError(f"{par_path}: invalid par-change sidecar schema") + par["symbol"] = par["symbol"].astype(str).str.upper().str.strip() + par = par.loc[(par["symbol"] == symbol) & (par.get("review_status", "review_required") != "baseline")] + for row in par.itertuples(index=False): + siam_rows.append({"symbol": symbol, "action_type": "par_value", + "date": pd.Timestamp(row.effective_date).normalize(), + "value": float(row.par_value), "source_url": getattr(row, "source_url", "")}) + + siam = pd.DataFrame(siam_rows, columns=SET_REFERENCE_COLUMNS) + siam = siam.loc[siam["action_type"].isin(selected_types)] if not siam.empty else siam + if not siam.empty: + if start: + siam = siam.loc[siam["date"] >= pd.Timestamp(start).normalize()] + if end: + siam = siam.loc[siam["date"] < pd.Timestamp(end).normalize()] + key = ["symbol", "action_type", "date"] + if not siam.empty and siam.duplicated(key).any(): + raise SiamChartActionError("SiamChart sidecars contain duplicate action keys") + if reference.duplicated(key).any(): + raise SiamChartActionError("SET reference contains duplicate action keys") + merged = reference.merge(siam, on=key, how="outer", suffixes=("_set", "_siamchart"), indicator=True) + rows_out = [] + for _, row in merged.iterrows(): + set_value = row.get("value_set", float("nan")) + siam_value = row.get("value_siamchart", float("nan")) + merge_status = row["_merge"] + # SiamChart rounds some cash amounts to six decimals while SET keeps the + # underlying fractional value. Treat a sub-0.00000051 difference as display rounding. + match = merge_status == "both" and isclose( + float(set_value), float(siam_value), rel_tol=1e-8, abs_tol=5.1e-7 + ) + status = "MATCH" if match else ( + "VALUE_MISMATCH" if merge_status == "both" else + "ONLY_SET" if merge_status == "left_only" else "ONLY_SIAMCHART" + ) + rows_out.append({"symbol": row["symbol"], "action_type": row["action_type"], "date": row["date"], + "set_value": set_value, "siamchart_value": siam_value, "status": status, + "set_source_url": row.get("source_url_set", ""), + "siamchart_source_url": row.get("source_url_siamchart", row.get("source_url", ""))}) + return pd.DataFrame(rows_out, columns=["symbol", "action_type", "date", "set_value", "siamchart_value", + "status", "set_source_url", "siamchart_source_url"]) diff --git a/primethai/data.py b/primethai/data.py new file mode 100644 index 0000000..624fb7d --- /dev/null +++ b/primethai/data.py @@ -0,0 +1,272 @@ +"""Provider abstraction and defensive local CSV cache for daily market bars.""" + +from __future__ import annotations + +from abc import ABC, abstractmethod +import csv +from datetime import datetime +import io +from pathlib import Path +import re +import time +from datetime import timedelta +import zipfile +import zlib + +import pandas as pd + + +class SymbolDataError(RuntimeError): + pass + + +class _IncompleteHistoryError(SymbolDataError): + pass + + +def yahoo_symbol(symbol: str) -> str: + symbol = symbol.strip().upper() + if symbol in {"^SET", "^SET.BK"}: + return "^SET.BK" + if symbol.startswith("^") or "." in symbol: + return symbol + return f"{symbol}.BK" + + +def safe_filename(symbol: str) -> str: + return re.sub(r"[^A-Za-z0-9._-]", "_", symbol.upper()) + + +def normalize_bars(frame: pd.DataFrame) -> pd.DataFrame: + if frame is None or frame.empty: + raise SymbolDataError("provider returned no bars") + out = frame.copy() + if isinstance(out.columns, pd.MultiIndex): + out.columns = [column[0] for column in out.columns] + out.columns = [str(column).strip().lower().replace(" ", "_") for column in out.columns] + required = {"open", "high", "low", "close", "volume"} + if not required.issubset(out.columns): + missing = required - set(out.columns) + raise SymbolDataError(f"bar data missing columns: {', '.join(sorted(missing))}") + if "adj_close" not in out: + out["adj_close"] = out["close"] + if "dividends" not in out: + out["dividends"] = 0.0 + if "stock_splits" not in out: + out["stock_splits"] = 0.0 + out.index = pd.to_datetime(out.index).tz_localize(None).normalize() + out.index.name = "date" + for column in ["open", "high", "low", "close", "adj_close", "volume", "dividends", "stock_splits"]: + out[column] = pd.to_numeric(out[column], errors="coerce") + out = out[~out.index.duplicated(keep="last")].sort_index() + valid = out[["open", "high", "low", "close"]].notna().all(axis=1) + valid &= (out[["open", "high", "low", "close"]] > 0).all(axis=1) + out = out.loc[valid] + if out.empty: + raise SymbolDataError("provider returned no valid OHLC bars") + out["value_turnover"] = out["close"] * out["volume"] + # Yahoo Adj Close is its split/dividend-adjusted series. Keep a corresponding + # adjusted OHLC proxy alongside the unadjusted-by-dividend fields; the simulator + # deliberately uses the latter and credits cash dividends separately. + factor = out["adj_close"] / out["close"].where(out["close"] != 0) + for column in ("open", "high", "low"): + out[f"adj_{column}"] = out[column] * factor + return out + + +def read_siamchart_archives(archives: list[str | Path], symbols: list[str]) -> tuple[dict[str, pd.DataFrame], list[str]]: + """Read selected securities from SiamChart's daily EOD ZIP archives. + + SiamChart stores one CSV per trading date with columns such as ```` + and ````. This importer keeps only explicitly requested symbols, + maps the index ticker ``SET`` to ``^SET``, and skips unreadable daily files + while returning a warning for each skipped entry. + """ + tickers: dict[str, str] = {} + for requested in symbols: + symbol = requested.strip().upper() + if symbol in {"SET", "^SET", "^SET.BK"}: + symbol, ticker = "^SET", "SET" + else: + ticker = symbol + if not symbol: + continue + tickers[ticker] = symbol + if not tickers: + raise SymbolDataError("select at least one SiamChart ticker to import") + + records: dict[str, dict[str, dict[str, float | str]]] = { + symbol: {} for symbol in tickers.values() + } + issues: list[str] = [] + dated_files = 0 + filename_pattern = re.compile(r"set-history_EOD_\d{4}-\d{2}-\d{2}\.csv$", re.IGNORECASE) + required_columns = {"", "", "", "", "", "", ""} + + for archive_name in archives: + archive_path = Path(archive_name) + if not archive_path.is_file(): + raise SymbolDataError(f"SiamChart archive not found: {archive_path}") + try: + archive = zipfile.ZipFile(archive_path) + except (OSError, zipfile.BadZipFile) as exc: + raise SymbolDataError(f"cannot open SiamChart archive {archive_path}: {exc}") from exc + with archive: + for entry in archive.infolist(): + if not filename_pattern.search(Path(entry.filename).name): + continue + dated_files += 1 + daily_records: dict[tuple[str, str], dict[str, float | str]] = {} + try: + with archive.open(entry) as binary, io.TextIOWrapper(binary, encoding="utf-8-sig", newline="") as stream: + reader = csv.DictReader(stream) + columns = set(reader.fieldnames or []) + if not required_columns.issubset(columns): + raise ValueError("unexpected CSV columns") + for row in reader: + ticker = (row.get("") or "").strip().upper() + symbol = tickers.get(ticker) + if symbol is None: + continue + raw_date = (row.get("") or "").strip() + day = datetime.strptime(raw_date, "%Y%m%d").date().isoformat() + values = { + "open": float(row[""]), + "high": float(row[""]), + "low": float(row[""]), + "close": float(row[""]), + "volume": float(row[""]), + } + if not all(pd.notna(value) for value in values.values()): + raise ValueError(f"invalid OHLCV for {ticker} on {raw_date}") + daily_records[(symbol, day)] = {"date": day, **values} + except (OSError, EOFError, UnicodeDecodeError, csv.Error, ValueError, zipfile.BadZipFile, zlib.error) as exc: + issues.append(f"{archive_path.name}:{entry.filename}: {exc}") + continue + for (symbol, day), record in daily_records.items(): + records[symbol][day] = record + + if dated_files == 0: + raise SymbolDataError("no daily set-history_EOD_YYYY-MM-DD.csv files found in the supplied archives") + imported: dict[str, pd.DataFrame] = {} + for symbol, by_date in records.items(): + if not by_date: + raise SymbolDataError(f"no {symbol} rows found in the supplied SiamChart archives") + frame = pd.DataFrame(sorted(by_date.values(), key=lambda row: row["date"])) + imported[symbol] = normalize_bars(frame.set_index("date")) + return imported, issues + + +class MarketDataProvider(ABC): + @abstractmethod + def get_history(self, symbol: str, *, start: str | None = None, end: str | None = None, + period: str | None = None, refresh: bool = False) -> pd.DataFrame: + """Return normalized daily bars, or raise SymbolDataError.""" + + +class YFinanceProvider(MarketDataProvider): + """Yahoo daily data. auto_adjust=False and corporate-action columns are explicit.""" + + def __init__(self, cache_dir: str | Path = "data/cache", *, repair: bool = True, + retries: int = 2, retry_delay: float = 1.0) -> None: + self.cache_dir, self.repair = Path(cache_dir), repair + self.retries, self.retry_delay = retries, retry_delay + + def _cache_path(self, symbol: str) -> Path: + return self.cache_dir / f"{safe_filename(yahoo_symbol(symbol))}.csv" + + @staticmethod + def _period_start(period: str | None) -> pd.Timestamp | None: + if not period: + return None + now = pd.Timestamp.now().normalize() + if period == "ytd": + return pd.Timestamp(year=now.year, month=1, day=1) + if period == "max": + return pd.Timestamp("1980-01-01") + match = re.fullmatch(r"(\d+)(d|wk|mo|y)", period) + if not match: + return None + amount, unit = int(match.group(1)), match.group(2) + delta = {"d": timedelta(days=amount), "wk": timedelta(weeks=amount), + "mo": timedelta(days=30 * amount), "y": timedelta(days=365 * amount)}[unit] + return now - delta + + @classmethod + def _cache_covers(cls, frame: pd.DataFrame, *, start: str | None, end: str | None, + period: str | None) -> bool: + lower = pd.Timestamp(start) if start else cls._period_start(period or "5y") + upper = pd.Timestamp(end) if end else pd.Timestamp.now().normalize() + # Market holidays/weekends can leave a few calendar days between the last bar and end. + if lower is not None and frame.index.min() > lower + pd.Timedelta(days=10): + return False + if frame.index.max() < upper - pd.Timedelta(days=10): + return False + return True + + def get_history(self, symbol: str, *, start: str | None = None, end: str | None = None, + period: str | None = None, refresh: bool = False) -> pd.DataFrame: + yf_symbol, cache_path = yahoo_symbol(symbol), self._cache_path(symbol) + cached: pd.DataFrame | None = None + if cache_path.exists() and not refresh: + cached = normalize_bars(pd.read_csv(cache_path, index_col="date", parse_dates=True)) + if self._cache_covers(cached, start=start, end=end, period=period): + result = cached + if start: + result = result.loc[result.index >= pd.Timestamp(start)] + if end: + result = result.loc[result.index < pd.Timestamp(end)] + if not result.empty: + return result + try: + import yfinance as yf + except ImportError as exc: + raise SymbolDataError("yfinance is not installed; run python -m pip install -r requirements.txt") from exc + last_error: Exception | None = None + for attempt in range(self.retries + 1): + try: + downloaded = yf.download( + yf_symbol, start=start, end=end, + period=period or (None if start else "5y"), interval="1d", + auto_adjust=False, actions=True, repair=self.repair, + progress=False, threads=False, multi_level_index=False, + ) + normalized = normalize_bars(downloaded) + if cached is not None: + normalized = normalize_bars(pd.concat([cached, normalized]).sort_index()) + if yf_symbol == "^SET.BK" and period != "max" and not self._cache_covers( + normalized, start=start, end=end, period=period): + raise _IncompleteHistoryError( + f"{yf_symbol}: Yahoo returned {len(normalized)} bar(s) but did not cover " + "the requested history; refusing an incomplete SET benchmark" + ) + if yf_symbol == "^SET.BK" and period == "max" and len(normalized) < 1500: + raise _IncompleteHistoryError( + f"{yf_symbol}: Yahoo returned only {len(normalized)} bars for period=max; " + "refusing an incomplete SET benchmark" + ) + self.cache_dir.mkdir(parents=True, exist_ok=True) + normalized.to_csv(cache_path, index_label="date") + if start: + normalized = normalized.loc[normalized.index >= pd.Timestamp(start)] + if end: + normalized = normalized.loc[normalized.index < pd.Timestamp(end)] + return normalized + except _IncompleteHistoryError: + raise + except Exception as exc: + last_error = exc + if attempt < self.retries: + time.sleep(self.retry_delay * (attempt + 1)) + raise SymbolDataError(f"{yf_symbol}: Yahoo download failed: {last_error}") from last_error + + def download_many(self, symbols: list[str], *, start: str | None = None, end: str | None = None, + period: str | None = None, refresh: bool = False) -> tuple[dict[str, pd.DataFrame], dict[str, str]]: + data: dict[str, pd.DataFrame] = {} + errors: dict[str, str] = {} + for symbol in symbols: + try: + data[symbol] = self.get_history(symbol, start=start, end=end, period=period, refresh=refresh) + except SymbolDataError as exc: + errors[symbol] = str(exc) + return data, errors diff --git a/primethai/ema_crossover.py b/primethai/ema_crossover.py new file mode 100644 index 0000000..8d9daf2 --- /dev/null +++ b/primethai/ema_crossover.py @@ -0,0 +1,391 @@ +"""Plain EMA crossover portfolio backtest using next-open execution.""" + +from __future__ import annotations + +from dataclasses import dataclass +import math + +import numpy as np +import pandas as pd + +from .backtest import BacktestResult, _cost, _fill_price +from .config import StrategyConfig, ThaiMarketRules, TradingCosts +from .portfolio import round_quantity + + +@dataclass +class _Position: + symbol: str + entry_date: pd.Timestamp + entry_price: float + quantity: int + entry_cost: float + min_low: float + max_high: float + dividend_cash: float = 0.0 + holding_sessions: int = 0 + + +def _number(value: object, default: float = 0.0) -> float: + if value is None or pd.isna(value): + return default + return float(value) + + +def _metrics(curve: pd.DataFrame, trades: pd.DataFrame) -> dict[str, float]: + if curve.empty: + return {key: 0.0 for key in ( + "total_return", "cagr", "max_drawdown", "win_rate", "average_win", + "average_loss", "expectancy_per_trade", "profit_factor", + "average_trade_return", "average_holding_period", "exposure_pct", + "trades_per_month", "gross_dividend_cash", + )} + equity = curve["equity"].astype(float) + start_equity = float(equity.iloc[0]) + total_return = float(equity.iloc[-1] / start_equity - 1) if start_equity else 0.0 + years = max((equity.index[-1] - equity.index[0]).days / 365.25, 1 / 252) + drawdown = equity / equity.cummax() - 1 + pnl = trades["total_pnl"].astype(float) if not trades.empty else pd.Series(dtype=float) + winners, losers = pnl[pnl > 0], pnl[pnl < 0] + gross_loss = float(abs(losers.sum())) + return { + "total_return": total_return, + "cagr": float((max(float(equity.iloc[-1]), 1.0) / max(start_equity, 1.0)) ** (1 / years) - 1), + "max_drawdown": float(drawdown.min()), + "win_rate": float((pnl > 0).mean()) if len(pnl) else 0.0, + "average_win": float(winners.mean()) if len(winners) else 0.0, + "average_loss": float(losers.mean()) if len(losers) else 0.0, + "expectancy_per_trade": float(pnl.mean()) if len(pnl) else 0.0, + "profit_factor": float(winners.sum() / gross_loss) if gross_loss else (math.inf if len(winners) else 0.0), + "average_trade_return": float(trades["return_pct"].mean()) if not trades.empty else 0.0, + "average_holding_period": float(trades["holding_sessions"].mean()) if not trades.empty else 0.0, + "exposure_pct": float(curve["exposure"].mean()) if "exposure" in curve else 0.0, + "trades_per_month": float(len(trades) / max(years * 12, 1 / 12)), + "gross_dividend_cash": float(trades["dividend_cash"].sum()) if not trades.empty else 0.0, + } + + +def run_ema_crossover_backtest( + features_by_symbol: dict[str, pd.DataFrame], + market: pd.DataFrame, + *, + fast: int = 20, + slow: int = 50, + strategy: StrategyConfig | None = None, + rules: ThaiMarketRules | None = None, + costs: TradingCosts | None = None, + start: str | None = None, + end: str | None = None, + lot_sizes: dict[str, int] | None = None, + max_position_pct: float = 0.20, + min_price: float = 2.0, + min_value_turnover_thb: float = 20_000_000.0, + market_filter: str = "none", + exit_mode: str = "cross_under", + max_loss_pct: float | None = None, + yahoo_split_adjusted: bool = True, +) -> BacktestResult: + """Buy a fast-over-slow EMA cross and exit on the reverse cross. + + Signals are computed from closing prices and filled at the next available open. + Optional market filters, EMA21-low exits and hard loss stops can be used for + controlled overlay comparisons. + The portfolio caps each name at a fixed fraction of equity, spreads available + cash equally across simultaneous entry signals, and uses cash-only fills. + """ + if fast < 1 or slow < 2 or fast >= slow: + raise ValueError("EMA spans must be positive integers with fast < slow") + if not 0 < max_position_pct <= 1: + raise ValueError("max_position_pct must be greater than 0 and at most 1") + if min_price < 0 or min_value_turnover_thb < 0: + raise ValueError("price and turnover filters must be non-negative") + if market_filter not in {"none", "ew_trend_up", "ew_above_ema200"}: + raise ValueError("market_filter must be none, ew_trend_up or ew_above_ema200") + if exit_mode not in {"cross_under", "ema21_low"}: + raise ValueError("exit_mode must be cross_under or ema21_low") + if max_loss_pct is not None and not 0 < max_loss_pct < 1: + raise ValueError("max_loss_pct must be greater than 0 and less than 1") + if not features_by_symbol: + raise ValueError("features_by_symbol is empty") + + strategy = strategy or StrategyConfig() + rules = rules or ThaiMarketRules() + costs = costs or TradingCosts() + members_by_date = market.attrs.get("members_by_date") + ew_close = pd.to_numeric(market.get("set100ew_close", pd.Series(dtype=float)), errors="coerce") + ew_ema200 = ew_close.ewm(span=200, adjust=False, min_periods=200).mean() + external_dividends = { + str(symbol).upper(): { + pd.Timestamp(date).normalize(): float(amount) for date, amount in events.items() + } + for symbol, events in market.attrs.get("external_dividends", {}).items() + } + + indicators: dict[str, pd.DataFrame] = {} + all_dates: set[pd.Timestamp] = set(market.index) + for symbol, source in features_by_symbol.items(): + frame = source.sort_index().copy() + if "close" not in frame: + raise ValueError(f"{symbol} has no close column") + close = pd.to_numeric(frame["close"], errors="coerce") + frame["_ema_fast"] = close.ewm(span=fast, adjust=False, min_periods=fast).mean() + frame["_ema_slow"] = close.ewm(span=slow, adjust=False, min_periods=slow).mean() + indicators[symbol] = frame + all_dates.update(pd.Timestamp(date).normalize() for date in frame.index) + all_dates.update(date for events in external_dividends.values() for date in events) + dates = sorted(all_dates) + if start: + dates = [date for date in dates if date >= pd.Timestamp(start).normalize()] + if end: + dates = [date for date in dates if date < pd.Timestamp(end).normalize()] + if len(dates) < 2: + raise ValueError("backtest requires at least two daily bars") + + cash = float(strategy.initial_equity) + positions: dict[str, _Position] = {} + pending_entries: dict[str, pd.Timestamp] = {} + pending_exits: dict[str, pd.Timestamp] = {} + previous_close: dict[str, float] = {} + trades: list[dict[str, object]] = [] + fills: list[dict[str, object]] = [] + skipped: list[dict[str, str]] = [] + equity_rows: list[dict[str, object]] = [] + + def execute_sell(position: _Position, reference_price: float, date: pd.Timestamp, + reason: str) -> None: + nonlocal cash + fill = _fill_price(reference_price, "sell", costs, rules) + position.min_low = min(position.min_low, fill) + position.max_high = max(position.max_high, fill) + notional = fill * position.quantity + sale_cost = _cost(notional, costs, sell=True) + cash += notional - sale_cost + total_pnl = notional - sale_cost - position.entry_cost + position.dividend_cash + trades.append({ + "symbol": position.symbol, + "entry_date": position.entry_date.date().isoformat(), + "exit_date": date.date().isoformat(), + "entry_price": position.entry_price, + "exit_price": fill, + "shares": position.quantity, + "entry_cost": position.entry_cost, + "dividend_cash": position.dividend_cash, + "total_pnl": total_pnl, + "return_pct": total_pnl / position.entry_cost if position.entry_cost else 0.0, + "mae_pct": (position.min_low / position.entry_price - 1) if position.entry_price else 0.0, + "mfe_pct": (position.max_high / position.entry_price - 1) if position.entry_price else 0.0, + "holding_sessions": position.holding_sessions, + "exit_reason": reason, + }) + fills.append({"date": date, "symbol": position.symbol, "side": "SELL", + "reason": reason, "shares": position.quantity, "price": fill, + "cost": sale_cost}) + positions.pop(position.symbol, None) + + for date in dates: + # Apply corporate actions to positions carried from the prior close. + for symbol, position in list(positions.items()): + frame = indicators[symbol] + bar = frame.loc[date] if date in frame.index else None + if bar is not None and not yahoo_split_adjusted: + split = _number(bar.get("stock_splits", 0.0)) + if split > 0 and split != 1: + position.quantity = int(round(position.quantity * split)) + position.entry_price /= split + position.min_low /= split + position.max_high /= split + if symbol in previous_close: + previous_close[symbol] /= split + dividend = external_dividends.get(symbol, {}).get(date) + if dividend is None and bar is not None: + dividend = _number(bar.get("dividends", 0.0)) + if dividend and date > position.entry_date: + amount = position.quantity * float(dividend) + position.dividend_cash += amount + cash += amount + if bar is None and symbol in previous_close: + previous_close[symbol] = max(0.0, previous_close[symbol] - float(dividend)) + + # Cross-under exits execute before new entries at the next open. + for symbol in list(pending_exits): + if symbol not in positions: + pending_exits.pop(symbol, None) + continue + frame = indicators[symbol] + if date not in frame.index or pd.isna(frame.loc[date].get("open", np.nan)): + continue + execute_sell(positions[symbol], float(frame.loc[date, "open"]), date, "ema_cross_down_next_open") + pending_exits.pop(symbol, None) + + # Spread cash equally across today's eligible cross-up signals. This avoids + # selecting simultaneous signals by arbitrary ticker ordering. + entry_orders: list[tuple[str, pd.Timestamp, float]] = [] + for symbol, signal_date in sorted(pending_entries.items()): + frame = indicators[symbol] + if date not in frame.index or pd.isna(frame.loc[date].get("open", np.nan)): + skipped.append({"symbol": symbol, "date": signal_date.date().isoformat(), + "reason": "missing next open"}) + continue + if symbol in positions: + continue + bar = frame.loc[date] + open_price = float(bar["open"]) + # Prefer the signal bar's liquidity observation to avoid using later data. + signal_bar = frame.loc[signal_date] if signal_date in frame.index else bar + signal_turnover = _number(signal_bar.get("avg_value_turnover20", 0.0)) + signal_close = _number(signal_bar.get("close", open_price)) + if signal_close < min_price or signal_turnover < min_value_turnover_thb: + skipped.append({"symbol": symbol, "date": signal_date.date().isoformat(), + "reason": "price or 20-day turnover filter"}) + continue + fill = _fill_price(open_price, "buy", costs, rules) + entry_orders.append((symbol, signal_date, fill)) + + if entry_orders: + open_market_value = 0.0 + for held_symbol, held in positions.items(): + held_frame = indicators[held_symbol] + if date in held_frame.index and pd.notna(held_frame.loc[date].get("open", np.nan)): + mark = float(held_frame.loc[date, "open"]) + else: + mark = previous_close.get(held_symbol, held.entry_price) + open_market_value += mark * held.quantity + open_equity = cash + open_market_value + allocatable_orders = entry_orders.copy() + allocation_plan: list[tuple[str, pd.Timestamp, float, int]] = [] + while allocatable_orders: + per_entry_budget = min(open_equity * max_position_pct, cash / len(allocatable_orders)) + allocation_plan = [] + unable_to_buy: list[tuple[str, pd.Timestamp, float]] = [] + for symbol, signal_date, fill in allocatable_orders: + lot_size = (lot_sizes or {}).get(symbol, rules.default_board_lot) + target_qty = round_quantity(per_entry_budget / fill, lot_size) + if target_qty <= 0: + unable_to_buy.append((symbol, signal_date, fill)) + else: + allocation_plan.append((symbol, signal_date, fill, target_qty)) + if not unable_to_buy: + break + for symbol, signal_date, _ in unable_to_buy: + skipped.append({"symbol": symbol, "date": signal_date.date().isoformat(), + "reason": "equal allocation below minimum board lot"}) + allocatable_orders = [ + (symbol, signal_date, fill) + for symbol, signal_date, fill, _ in allocation_plan + ] + + for symbol, signal_date, fill, target_qty in allocation_plan: + lot_size = (lot_sizes or {}).get(symbol, rules.default_board_lot) + affordable_qty = round_quantity( + cash / (fill * (1 + costs.commission_rate + costs.fees_rate)), lot_size + ) + quantity = min(target_qty, affordable_qty) + if quantity <= 0: + skipped.append({"symbol": symbol, "date": signal_date.date().isoformat(), + "reason": "cash or board lot below minimum"}) + continue + buy_notional = fill * quantity + buy_cost = _cost(buy_notional, costs, sell=False) + position = _Position(symbol=symbol, entry_date=date, entry_price=fill, + quantity=quantity, entry_cost=buy_notional + buy_cost, + min_low=fill, max_high=fill) + positions[symbol] = position + cash -= buy_notional + buy_cost + fills.append({"date": date, "symbol": symbol, "side": "BUY", + "reason": f"ema_{fast}_over_{slow}", "shares": quantity, + "price": fill, "cost": buy_cost}) + pending_entries.clear() + + # Observe the whole session after next-open fills and check closing signals. + for symbol, position in list(positions.items()): + frame = indicators[symbol] + if date not in frame.index: + continue + bar = frame.loc[date] + if pd.notna(bar.get("low", np.nan)): + position.min_low = min(position.min_low, float(bar["low"])) + if pd.notna(bar.get("high", np.nan)): + position.max_high = max(position.max_high, float(bar["high"])) + position.holding_sessions += 1 + if max_loss_pct is not None: + stop_price = position.entry_price * (1 - max_loss_pct) + low = float(bar["low"]) + if low <= stop_price: + opening = float(bar["open"]) + fill_reference = min(opening, stop_price) if opening < stop_price else stop_price + execute_sell(position, fill_reference, date, f"hard_stop_{max_loss_pct:.0%}") + continue + previous_close[symbol] = float(bar["close"]) + + for symbol, frame in indicators.items(): + if symbol in positions or symbol in pending_entries or date not in frame.index: + continue + if members_by_date is not None and symbol not in members_by_date.get(date, []): + continue + index = frame.index.get_loc(date) + if isinstance(index, slice) or index < 1: + continue + current = frame.iloc[index] + prior = frame.iloc[index - 1] + values = [current.get("_ema_fast"), current.get("_ema_slow"), + prior.get("_ema_fast"), prior.get("_ema_slow")] + if any(pd.isna(value) for value in values): + continue + crossed_up = float(prior["_ema_fast"]) <= float(prior["_ema_slow"]) and float(current["_ema_fast"]) > float(current["_ema_slow"]) + if crossed_up: + if market_filter == "ew_trend_up": + state = market.loc[date].get("ew_trend_state", "DOWN") if date in market.index else "DOWN" + if str(state).upper() != "UP": + continue + elif market_filter == "ew_above_ema200": + if date not in ew_close.index or pd.isna(ew_ema200.get(date)) or ew_close.get(date, np.nan) <= ew_ema200.get(date): + continue + pending_entries[symbol] = date + + for symbol, frame in indicators.items(): + if symbol not in positions or date not in frame.index: + continue + index = frame.index.get_loc(date) + if isinstance(index, slice) or index < 1: + continue + current = frame.iloc[index] + prior = frame.iloc[index - 1] + values = [current.get("_ema_fast"), current.get("_ema_slow"), + prior.get("_ema_fast"), prior.get("_ema_slow")] + if any(pd.isna(value) for value in values): + continue + if exit_mode == "ema21_low": + ema21_low = _number(current.get("ema21_low"), np.nan) + should_exit = pd.notna(ema21_low) and float(current["close"]) < ema21_low + else: + should_exit = float(prior["_ema_fast"]) >= float(prior["_ema_slow"]) and float(current["_ema_fast"]) < float(current["_ema_slow"]) + if should_exit: + pending_exits[symbol] = date + + market_value = 0.0 + for symbol, position in positions.items(): + frame = indicators[symbol] + close = float(frame.loc[date, "close"]) if date in frame.index else previous_close.get(symbol, position.entry_price) + market_value += close * position.quantity + equity = cash + market_value + equity_rows.append({"date": date, "equity": equity, "cash": cash, + "market_value": market_value, + "exposure": market_value / equity if equity else 0.0, + "open_positions": len(positions)}) + + final_date = dates[-1] + for symbol, position in list(positions.items()): + frame = indicators[symbol] + available = frame.loc[frame.index <= final_date] + if available.empty: + continue + bar = available.iloc[-1] + execute_sell(position, float(bar["close"]), pd.Timestamp(available.index[-1]), "end_of_test") + if equity_rows: + equity_rows[-1].update({"equity": cash, "cash": cash, "market_value": 0.0, + "exposure": 0.0, "open_positions": 0}) + + trade_frame = pd.DataFrame(trades) + curve = pd.DataFrame(equity_rows).set_index("date") + fill_frame = pd.DataFrame(fills) + return BacktestResult(_metrics(curve, trade_frame), trade_frame, curve, fill_frame, skipped) diff --git a/primethai/experiments.py b/primethai/experiments.py new file mode 100644 index 0000000..2b8512d --- /dev/null +++ b/primethai/experiments.py @@ -0,0 +1,99 @@ +"""Cumulative ablation, chronological holdouts and rolling walk-forward reports.""" + +from __future__ import annotations + +from dataclasses import replace + +import pandas as pd + +from .backtest import BacktestResult, run_backtest +from .config import StrategyConfig, ThaiMarketRules, TradingCosts + + +ABLATIONS = [ + ("A 21DMA only", {}), + ("B + RS", {"enable_rs": True}), + ("C + Market Regime", {"enable_market_regime": True, "require_market_support": True}), + ("D + MCO", {"enable_mco": True}), + ("E + MCSI", {"enable_mcsi": True}), + ("F + Higher Low", {"enable_higher_low": True}), + ("G + Weekly Structure", {"enable_weekly": True}), + ("H + ATR Extension Filter", {"enable_atr_filter": True}), +] + + +def run_ablations(features_by_symbol: dict[str, pd.DataFrame], market: pd.DataFrame, *, + base: StrategyConfig | None = None, rules: ThaiMarketRules | None = None, + costs: TradingCosts | None = None) -> tuple[pd.DataFrame, dict[str, BacktestResult]]: + base = base or StrategyConfig() + results: dict[str, BacktestResult] = {} + rows = [] + toggles = {"enable_rs": False, "enable_market_regime": False, "enable_mco": False, + "enable_mcsi": False, "enable_higher_low": False, "enable_weekly": False, + "enable_atr_filter": False, "require_market_support": False} + for label, changes in ABLATIONS: + toggles.update(changes) + strategy = replace(base, **toggles) + result = run_backtest(features_by_symbol, market, strategy=strategy, rules=rules, costs=costs) + results[label] = result + rows.append({"experiment": label, **result.metrics, "trades": len(result.trades)}) + return pd.DataFrame(rows), results + + +def chronological_splits(dates: pd.Index, *, train_fraction: float = 0.60, + validation_fraction: float = 0.20) -> dict[str, tuple[pd.Timestamp, pd.Timestamp]]: + if len(dates) < 10 or not 0 < train_fraction < 1 or not 0 < validation_fraction < 1 or train_fraction + validation_fraction >= 1: + raise ValueError("invalid date count or train/validation fractions") + ordered = pd.DatetimeIndex(sorted(pd.Timestamp(d) for d in dates)) + train_end = max(1, int(len(ordered) * train_fraction)) + validation_end = max(train_end + 1, int(len(ordered) * (train_fraction + validation_fraction))) + validation_end = min(validation_end, len(ordered) - 1) + return { + "train": (ordered[0], ordered[train_end - 1]), + "validation": (ordered[train_end], ordered[validation_end - 1]), + "out_of_sample": (ordered[validation_end], ordered[-1]), + } + + +def run_chronological_holdouts(features_by_symbol: dict[str, pd.DataFrame], market: pd.DataFrame, *, + strategy: StrategyConfig | None = None, + rules: ThaiMarketRules | None = None, + costs: TradingCosts | None = None) -> tuple[pd.DataFrame, dict[str, BacktestResult]]: + dates = pd.DatetimeIndex(sorted(set().union(*(frame.index for frame in features_by_symbol.values())))) + splits = chronological_splits(dates) + results = {} + rows = [] + for label, (start, end) in splits.items(): + result = run_backtest(features_by_symbol, market, strategy=strategy, rules=rules, costs=costs, + start=start.isoformat(), end=(end + pd.Timedelta(days=1)).isoformat()) + results[label] = result + rows.append({"period": label, "start": start.date().isoformat(), "end": end.date().isoformat(), + **result.metrics, "trades": len(result.trades)}) + return pd.DataFrame(rows), results + + +def rolling_walk_forward(features_by_symbol: dict[str, pd.DataFrame], market: pd.DataFrame, *, + train_sessions: int = 252, validation_sessions: int = 63, + test_sessions: int = 63, step_sessions: int = 63, + strategy: StrategyConfig | None = None, + rules: ThaiMarketRules | None = None, + costs: TradingCosts | None = None) -> pd.DataFrame: + dates = pd.DatetimeIndex(sorted(set().union(*(frame.index for frame in features_by_symbol.values())))) + if min(train_sessions, validation_sessions, test_sessions, step_sessions) < 1: + raise ValueError("walk-forward window lengths must be positive") + rows = [] + start = 0 + fold = 1 + while start + train_sessions + validation_sessions + test_sessions <= len(dates): + train_end = start + train_sessions + val_end = train_end + validation_sessions + test_end = val_end + test_sessions + # Parameters remain fixed: training and validation are reported separately, not optimized here. + for label, begin, finish in [("train", start, train_end), ("validation", train_end, val_end), ("out_of_sample", val_end, test_end)]: + result = run_backtest(features_by_symbol, market, strategy=strategy, rules=rules, costs=costs, + start=dates[begin].isoformat(), end=(dates[finish - 1] + pd.Timedelta(days=1)).isoformat()) + rows.append({"fold": fold, "period": label, "start": dates[begin].date().isoformat(), + "end": dates[finish - 1].date().isoformat(), **result.metrics, "trades": len(result.trades)}) + fold += 1 + start += step_sessions + return pd.DataFrame(rows) diff --git a/primethai/indicators.py b/primethai/indicators.py new file mode 100644 index 0000000..4c2c415 --- /dev/null +++ b/primethai/indicators.py @@ -0,0 +1,198 @@ +"""Deterministic price, breadth, relative-strength and market indicators.""" + +from __future__ import annotations + +import numpy as np +import pandas as pd + + +def price_features(bars: pd.DataFrame, *, slope_lookback: int = 5, + atr_period: int = 14, adr_period: int = 20) -> pd.DataFrame: + df = bars.copy().sort_index() + for column in ("open", "high", "low", "close", "volume"): + if column not in df: + raise ValueError(f"missing required column: {column}") + out = df.copy() + out["ema21_high"] = df["high"].ewm(span=21, adjust=False, min_periods=21).mean() + out["ema21_close"] = df["close"].ewm(span=21, adjust=False, min_periods=21).mean() + out["ema21_low"] = df["low"].ewm(span=21, adjust=False, min_periods=21).mean() + prior_close = df["close"].shift(1) + out["tr"] = pd.concat([df["high"] - df["low"], (df["high"] - prior_close).abs(), + (df["low"] - prior_close).abs()], axis=1).max(axis=1) + out["atr14"] = out["tr"].rolling(atr_period, min_periods=atr_period).mean() + day_range_pct = (df["high"] - df["low"]) / prior_close.replace(0, np.nan) * 100 + out["adr20_pct"] = day_range_pct.rolling(adr_period, min_periods=adr_period).mean() + out["sma50"] = df["close"].rolling(50, min_periods=50).mean() + out["sma200"] = df["close"].rolling(200, min_periods=200).mean() + out["high_52w"] = df["high"].rolling(252, min_periods=252).max() + out["low_52w"] = df["low"].rolling(252, min_periods=252).min() + out["avg_volume20"] = df["volume"].rolling(20, min_periods=20).mean() + out["avg_value_turnover20"] = (df["close"] * df["volume"]).rolling(20, min_periods=20).mean() + out["extension_atr"] = (df["close"] - out["ema21_high"]) / out["atr14"].replace(0, np.nan) + d_high = out["ema21_high"].diff(slope_lookback) + d_close = out["ema21_close"].diff(slope_lookback) + d_low = out["ema21_low"].diff(slope_lookback) + out["trend_state"] = np.select( + [(d_high > 0) & (d_close > 0) & (d_low > 0), (d_high < 0) & (d_close < 0) & (d_low < 0)], + ["UP", "DOWN"], default="NEUTRAL") + out["price_state"] = np.select( + [df["close"] > out["ema21_high"], df["close"] < out["ema21_low"]], + ["ABOVE_STRUCTURE", "BELOW_STRUCTURE"], default="INSIDE_STRUCTURE") + weekly = df[["high", "low", "close"]].resample("W-FRI").agg( + {"high": "max", "low": "min", "close": "last"}).dropna() + for column in ("high", "close", "low"): + weekly[f"weekly_ema10_{column}"] = weekly[column].ewm(span=10, adjust=False, min_periods=10).mean() + wh, wc, wl = (weekly[f"weekly_ema10_{c}"].diff() for c in ("high", "close", "low")) + weekly["weekly_trend_state"] = np.select( + [(wh > 0) & (wc > 0) & (wl > 0), (wh < 0) & (wc < 0) & (wl < 0)], + ["UP", "DOWN"], default="NEUTRAL") + weekly_fields = [f"weekly_ema10_{c}" for c in ("high", "close", "low")] + ["weekly_trend_state"] + out[weekly_fields] = weekly[weekly_fields].reindex(df.index, method="ffill") + out["volume_vs_avg20"] = df["volume"] / out["avg_volume20"].replace(0, np.nan) + return out + + +def relative_strength(prices: dict[str, pd.Series], benchmark: pd.Series, + weights: dict[str, float] | None = None, + set_benchmark: pd.Series | None = None, + high_prices: dict[str, pd.Series] | None = None, + members_by_date: dict[pd.Timestamp, list[str]] | None = None) -> pd.DataFrame: + """Cross-sectional percentile rank from price returns; not proprietary TradersLab RS.""" + weights = weights or {"1m": .20, "3m": .35, "12m": .30, "52w": .15} + if set(weights) != {"1m", "3m", "12m", "52w"} or not np.isclose(sum(weights.values()), 1.0): + raise ValueError("weights must include 1m/3m/12m/52w and sum to 1") + dates = benchmark.index + raw: dict[str, pd.DataFrame] = {} + for label, periods in {"1m": 21, "3m": 63, "12m": 252}.items(): + returns = pd.DataFrame({symbol: series.reindex(dates).pct_change(periods) for symbol, series in prices.items()}) + raw[label] = returns.sub(benchmark.reindex(dates).pct_change(periods), axis=0) + highs = high_prices or prices + raw["52w"] = pd.DataFrame({symbol: prices[symbol].reindex(dates) / + highs[symbol].reindex(dates).rolling(252, min_periods=252).max() + for symbol in prices}) + if members_by_date is None: + pct = {key: value.rank(axis=1, pct=True) * 100 for key, value in raw.items()} + else: + pct = {} + for key, values in raw.items(): + rankable = values.copy() + for date in rankable.index: + members = set(members_by_date.get(pd.Timestamp(date), [])) + rankable.loc[date, ~rankable.columns.isin(members)] = np.nan + pct[key] = rankable.rank(axis=1, pct=True) * 100 + score = sum(pct[key] * weight for key, weight in weights.items()) + score.columns = [f"{symbol}_rs_percentile" for symbol in score.columns] + for key, frame in pct.items(): + frame.columns = [f"{symbol}_rs_{key}" for symbol in frame.columns] + score = score.join(frame) + for label in ("1m", "3m", "12m"): + for symbol in prices: + score[f"{symbol}_rs_{label}_vs_set100ew"] = raw[label][symbol] + for symbol in prices: + score[f"{symbol}_52w_proximity_pct"] = raw["52w"][symbol] * 100 + if set_benchmark is not None: + for label, periods in {"1m": 21, "3m": 63, "12m": 252}.items(): + set_returns = pd.DataFrame({symbol: series.reindex(dates).pct_change(periods) + for symbol, series in prices.items()}) + relative = set_returns.sub(set_benchmark.reindex(dates).pct_change(periods), axis=0) + for symbol in relative.columns: + score[f"{symbol}_rs_{label}_vs_set"] = relative[symbol] + return score + + +def breadth_series(closes: pd.DataFrame, members_by_date: dict[pd.Timestamp, list[str]]) -> pd.DataFrame: + changes = closes.pct_change(fill_method=None) + rows = [] + for date in closes.index: + members = members_by_date.get(pd.Timestamp(date), []) + current = changes.loc[date, [symbol for symbol in members if symbol in changes.columns]].dropna() + advancing, declining = int((current > 0).sum()), int((current < 0).sum()) + rows.append({"advancing": advancing, "declining": declining, + "unchanged": int((current == 0).sum()), + "net_advances": advancing - declining, "issues": int(len(current))}) + return pd.DataFrame(rows, index=closes.index) + + +def mcclellan(breadth: pd.DataFrame, *, short_span: int = 19, long_span: int = 39, + z_window: int = 252) -> pd.DataFrame: + """MCO=EMA19(net advances)-EMA39(net advances); MCSI=cumulative MCO.""" + net = breadth["net_advances"].astype(float) + out = pd.DataFrame(index=breadth.index) + out["mco_ema_short"] = net.ewm(span=short_span, adjust=False, min_periods=short_span).mean() + out["mco_ema_long"] = net.ewm(span=long_span, adjust=False, min_periods=long_span).mean() + out["mco"] = out["mco_ema_short"] - out["mco_ema_long"] + minimum = min(z_window, 20) + out["mco_mean"] = out["mco"].rolling(z_window, min_periods=minimum).mean() + out["mco_std"] = out["mco"].rolling(z_window, min_periods=minimum).std(ddof=0) + out["mco_z"] = (out["mco"] - out["mco_mean"]) / out["mco_std"].replace(0, np.nan) + out["mcsi"] = out["mco"].fillna(0).cumsum() + out["mcsi_10dma"] = out["mcsi"].rolling(10, min_periods=10).mean() + prev, prev_ma = out["mcsi"].shift(1), out["mcsi_10dma"].shift(1) + rising, falling = out["mcsi"] > prev, out["mcsi"] < prev + reclaim = (out["mcsi"] > out["mcsi_10dma"]) & (prev <= prev_ma) + lose = (out["mcsi"] < out["mcsi_10dma"]) & (prev >= prev_ma) + out["mcsi_state"] = np.select( + [reclaim, lose, (out["mcsi"] > out["mcsi_10dma"]) & rising, + (out["mcsi"] < out["mcsi_10dma"]) & falling, rising, falling], + ["RECLAIM_10DMA", "LOSE_10DMA", "UP", "DOWN", "CURL_UP", "CURL_DOWN"], default="DOWN") + return out + + +def equal_weight_index(closes: pd.DataFrame, members_by_date: dict[pd.Timestamp, list[str]], + base_value: float = 100.0) -> pd.Series: + """Daily-rebalanced price-return proxy, equal-weighted over effective members.""" + returns = closes.pct_change(fill_method=None) + daily_returns = [] + counts = [] + for date in closes.index: + members = [symbol for symbol in members_by_date.get(pd.Timestamp(date), []) if symbol in returns.columns] + daily = returns.loc[date, members].dropna() if members else pd.Series(dtype=float) + daily_returns.append(float(daily.mean()) if len(daily) else 0.0) + counts.append(len(daily)) + result = pd.Series(base_value, index=closes.index, dtype=float) * (1 + pd.Series(daily_returns, index=closes.index)).cumprod() + result.name = "set100ew" + result.attrs["constituent_counts"] = counts + return result + + +def market_regimes(market: pd.DataFrame, *, overbought_extension_atr: float = 1.5, + repair_sigma: float = 1.0) -> pd.DataFrame: + """Deterministic daily market state machine with reasons, no language model.""" + out = market.copy() + regimes, reasons_all = [], [] + previous = "CORRECTION" + for _, row in out.iterrows(): + close, ema_high, ema_low = (row.get(key, np.nan) for key in ("close", "ema21_high", "ema21_low")) + sma50, ew_close, ew_ema = (row.get(key, np.nan) for key in ("sma50", "set100ew_close", "set100ew_ema21")) + trend, mcsi_state = row.get("trend_state", "NEUTRAL"), row.get("mcsi_state", "DOWN") + mco_z, net, extension = row.get("mco_z", np.nan), row.get("net_advances", 0), row.get("extension_atr", 0.0) + above_structure = pd.notna(close) and pd.notna(ema_high) and close > ema_high + below_structure = pd.notna(close) and pd.notna(ema_low) and close < ema_low + above_50 = pd.notna(close) and pd.notna(sma50) and close > sma50 + ew_support = pd.notna(ew_close) and pd.notna(ew_ema) and ew_close > ew_ema + repaired = bool(above_structure and ew_support) + breakdown = bool(below_structure and trend == "DOWN" and mcsi_state in {"DOWN", "LOSE_10DMA"}) + reasons: list[str] = [] + if breakdown: + regime, reasons = "BREAKDOWN", ["SET below falling 21DMA structure", "MCSI confirms downside"] + elif pd.notna(extension) and extension > overbought_extension_atr and net > 0: + regime, reasons = "OVERBOUGHT", [f"SET extension exceeds {overbought_extension_atr:g} ATR", "breadth net advances positive"] + elif above_structure and above_50 and ew_support and trend == "UP" and mcsi_state in {"UP", "RECLAIM_10DMA"}: + regime, reasons = "CONFIRMED_UPTREND", ["SET above rising 21DMA and 50DMA", "SET100EW above 21DMA", "MCSI supportive"] + elif trend == "UP" and above_50 and not above_structure and ew_support: + regime, reasons = "UPTREND_PULLBACK", ["SET pullback holds 21DMA structure", "SET100EW remains above 21DMA"] + elif repaired and (mcsi_state in {"CURL_UP", "RECLAIM_10DMA", "UP"} or (pd.notna(mco_z) and mco_z < -repair_sigma)): + regime = "EARLY_UPTREND" if previous in {"REPAIR", "CORRECTION", "BREAKDOWN"} else "REPAIR" + reasons = ["SET and SET100EW reclaimed 21DMA", f"MCSI {mcsi_state.lower()}"] + elif (pd.notna(mco_z) and mco_z < -repair_sigma) or mcsi_state in {"CURL_UP", "RECLAIM_10DMA"}: + regime, reasons = "REPAIR", [f"MCO below -{repair_sigma:g} sigma" if pd.notna(mco_z) and mco_z < -repair_sigma else f"MCSI {mcsi_state.lower()}"] + else: + regime, reasons = "CORRECTION", ["SET/SET100EW confirmation absent"] + if net > 0: + reasons.append("breadth net advances positive") + regimes.append(regime) + reasons_all.append(reasons) + previous = regime + out["regime"], out["regime_reasons"] = regimes, reasons_all + out["new_risk_allowed"] = ~out["regime"].isin(["CORRECTION", "OVERBOUGHT", "BREAKDOWN"]) + return out diff --git a/primethai/login.html b/primethai/login.html new file mode 100644 index 0000000..9ca2c8c --- /dev/null +++ b/primethai/login.html @@ -0,0 +1,51 @@ + + + + + + + เข้าสู่ระบบ · PrimeThai + + + +
+
PrimeThai · Portfolio
+

เข้าสู่ระบบ

+

ลงชื่อเข้าใช้เพื่อดูสัญญาณและข้อมูลพอร์ต

+
+ + + + +
+
+

ตั้งค่าชื่อผู้ใช้และรหัสผ่านได้ในไฟล์ .env ของระบบ

+
+ + + diff --git a/primethai/market_data_store.py b/primethai/market_data_store.py new file mode 100644 index 0000000..7ac54a2 --- /dev/null +++ b/primethai/market_data_store.py @@ -0,0 +1,226 @@ +"""Durable, source-tracked storage for daily market-wide EOD bars.""" + +from __future__ import annotations + +from collections.abc import Mapping +from datetime import datetime, timezone +import csv +import io +import json +import os +from pathlib import Path +import re +import zipfile + + +EOD_DATE = re.compile(r"set-history_EOD_(\d{4}-\d{2}-\d{2})\.csv$", re.IGNORECASE) +_TICKER = re.compile(r"[A-Z0-9]{1,5}$") + + +def _manifest_path(archive_path: Path) -> Path: + return archive_path.with_suffix(".sources.json") + + +def _read_manifest(archive_path: Path) -> dict[str, object]: + try: + value = json.loads(_manifest_path(archive_path).read_text(encoding="utf-8")) + except (FileNotFoundError, json.JSONDecodeError, OSError): + return {"schema_version": 1, "sessions": {}} + if not isinstance(value, dict) or not isinstance(value.get("sessions"), dict): + return {"schema_version": 1, "sessions": {}} + return value + + +def _write_manifest(archive_path: Path, manifest: dict[str, object]) -> None: + path = _manifest_path(archive_path) + temporary = path.with_name(path.name + ".tmp") + temporary.write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temporary, path) + + +def _archive_members(archive_path: Path) -> tuple[dict[str, tuple[str, bytes]], dict[str, bytes]]: + daily: dict[str, tuple[str, bytes]] = {} + other: dict[str, bytes] = {} + if not archive_path.is_file(): + return daily, other + with zipfile.ZipFile(archive_path) as archive: + for info in archive.infolist(): + if info.is_dir(): + continue + match = EOD_DATE.search(Path(info.filename).name) + content = archive.read(info) + if match: + daily[match.group(1)] = (info.filename, content) + else: + other[info.filename] = content + return daily, other + + +def merge_daily_entries( + archive_path: str | Path, + incoming: Mapping[str, bytes], + *, + source: str, + fetched_at: str | None = None, + metadata_by_date: Mapping[str, Mapping[str, object]] | None = None, +) -> dict[str, object]: + """Append daily CSVs to a cumulative ZIP without discarding earlier sessions. + + A primary SiamChart row replaces an existing row for the same date. A backup + row only fills a date that has not already been stored, preserving source + precedence when both providers eventually report the same session. + """ + path = Path(archive_path) + path.parent.mkdir(parents=True, exist_ok=True) + daily, other = _archive_members(path) + manifest = _read_manifest(path) + sessions = manifest.setdefault("sessions", {}) + if not isinstance(sessions, dict): + sessions = {} + manifest["sessions"] = sessions + fetched = fetched_at or datetime.now(timezone.utc).isoformat() + + for day, content in sorted(incoming.items()): + try: + datetime.strptime(day, "%Y-%m-%d") + except ValueError as exc: + raise ValueError(f"invalid EOD date in daily archive: {day}") from exc + if not isinstance(content, bytes) or not content: + raise ValueError(f"empty EOD record for {day}") + member_name = f"set-history_EOD_{day}.csv" + previous = sessions.get(day) + previous_source = str(previous.get("source", "")) if isinstance(previous, dict) else "" + if source.casefold().startswith("yahoo") and day in daily: + continue + daily[day] = (member_name, content) + meta = dict((metadata_by_date or {}).get(day, {})) + try: + text = content.decode("utf-8-sig") + record_count = max(0, sum(1 for _ in csv.DictReader(io.StringIO(text)))) + except (UnicodeDecodeError, csv.Error): + record_count = 0 + sessions[day] = { + "source": source, + "fetched_at": fetched, + "records": record_count, + **meta, + } + + if not sessions: + sessions = { + day: {"source": "SiamChart (existing archive)", "records": 0} + for day in daily + } + manifest["sessions"] = sessions + else: + for day in daily: + sessions.setdefault(day, {"source": "SiamChart (existing archive)", "records": 0}) + + temporary = path.with_name(path.name + ".merge") + try: + with zipfile.ZipFile(temporary, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=6) as output: + for name, content in sorted(other.items()): + output.writestr(name, content) + for day, (name, content) in sorted(daily.items()): + output.writestr(name, content) + os.replace(temporary, path) + manifest["schema_version"] = 1 + manifest["updated_at"] = fetched + _write_manifest(path, manifest) + finally: + temporary.unlink(missing_ok=True) + + return archive_summary(path) + + +def latest_daily_symbols(archives: list[str | Path], latest_date: str) -> list[str]: + """Return equity-like tickers with an EOD row on the requested session.""" + selected: tuple[Path, str] | None = None + for archive_name in archives: + path = Path(archive_name) + if not path.is_file(): + continue + try: + with zipfile.ZipFile(path) as archive: + for info in archive.infolist(): + match = EOD_DATE.search(Path(info.filename).name) + if match and match.group(1) == latest_date: + selected = path, info.filename + except (OSError, zipfile.BadZipFile): + continue + if selected is None: + return [] + symbols: set[str] = set() + path, member = selected + with zipfile.ZipFile(path) as archive: + with archive.open(member) as binary, io.TextIOWrapper( + binary, encoding="utf-8-sig", newline="" + ) as stream: + for row in csv.DictReader(stream): + symbol = str(row.get("") or "").strip().upper() + if symbol != "SET" and _TICKER.fullmatch(symbol): + symbols.add(symbol) + return sorted(symbols) + + +def archive_summary(archive_path: str | Path | None) -> dict[str, object]: + """Return a compact view of stored history and its latest source.""" + if archive_path is None: + return {"sessions": 0, "first_date": None, "last_date": None, "latest_source": None} + path = Path(archive_path) + if not path.is_file(): + return {"sessions": 0, "first_date": None, "last_date": None, "latest_source": None} + try: + with zipfile.ZipFile(path) as archive: + dates = sorted({match.group(1) for info in archive.infolist() + if (match := EOD_DATE.search(Path(info.filename).name))}) + except (OSError, zipfile.BadZipFile): + return {"sessions": 0, "first_date": None, "last_date": None, "latest_source": None} + manifest = _read_manifest(path) + sessions = manifest.get("sessions", {}) + latest = sessions.get(dates[-1], {}) if dates and isinstance(sessions, dict) else {} + providers = sorted({str(value.get("source")) for value in sessions.values() + if isinstance(value, dict) and value.get("source")}) if isinstance(sessions, dict) else [] + return { + "sessions": len(dates), + "first_date": dates[0] if dates else None, + "last_date": dates[-1] if dates else None, + "latest_source": latest.get("source") if isinstance(latest, dict) else None, + "sources": providers, + } + + +def archive_collection_summary(archive_paths: list[str | Path] | tuple[str | Path, ...]) -> dict[str, object]: + """Summarize unique sessions across historical and rolling daily archives.""" + dates: set[str] = set() + source_by_date: dict[str, str] = {} + for archive_name in archive_paths: + path = Path(archive_name) + if not path.is_file(): + continue + manifest = _read_manifest(path) + sessions = manifest.get("sessions", {}) + try: + with zipfile.ZipFile(path) as archive: + for info in archive.infolist(): + match = EOD_DATE.search(Path(info.filename).name) + if not match: + continue + day = match.group(1) + dates.add(day) + metadata = sessions.get(day, {}) if isinstance(sessions, dict) else {} + if isinstance(metadata, dict) and metadata.get("source"): + source_by_date[day] = str(metadata["source"]) + else: + source_by_date[day] = "SiamChart historical archive" + except (OSError, zipfile.BadZipFile): + continue + ordered = sorted(dates) + sources = sorted(set(source_by_date.values())) + return { + "sessions": len(ordered), + "first_date": ordered[0] if ordered else None, + "last_date": ordered[-1] if ordered else None, + "latest_source": source_by_date.get(ordered[-1]) if ordered else None, + "sources": sources, + } diff --git a/primethai/model_lab.py b/primethai/model_lab.py new file mode 100644 index 0000000..3cfeb2d --- /dev/null +++ b/primethai/model_lab.py @@ -0,0 +1,1230 @@ +"""Long-history, point-in-time model screening on SiamChart market-wide EOD archives. + +This lab is deliberately separate from the production PrimeThai signal engine. It +uses an as-of-date liquidity universe and daily target weights to compare model +families before any candidate is considered for the main engine. +""" + +from __future__ import annotations + +from collections import defaultdict +from dataclasses import dataclass +from datetime import date +import csv +import io +import json +import math +from pathlib import Path +import re +import zipfile +import zlib + +import numpy as np +import pandas as pd + +from .config import TradingCosts + + +_EOD_NAME = re.compile(r"set-history_EOD_(\d{4}-\d{2}-\d{2})\.csv$", re.IGNORECASE) +_TICKER = re.compile(r"[A-Z0-9]{1,5}$") +_NON_STOCK_TICKERS = { + "SET", "SET50", "SET100", "SETWB", "SSET", "SETHD", "SETCLMV", "SETESG", "MAI", + "1DIV", "TDEX", "GLD", "BSET100", "ESET50", "EBANK", "ECOMM", "EFOOD", "ENERG", + "CHINA", "DIF", "TFFIF", "3BBIF", "BTSGIF", "JASIF", "JDF", "JPF", "ABFTH", +} +_SPLIT_FACTORS = np.array([100.0, 50.0, 20.0, 10.0, 6.25, 5.0, 4.0, 3.0, 2.5, 2.0, + 1 / 2.0, 1 / 2.5, 1 / 3.0, 1 / 4.0, 1 / 5.0, 1 / 6.25, + 1 / 10.0, 1 / 20.0, 1 / 50.0, 1 / 100.0]) + + +@dataclass +class MarketPanel: + dates: pd.DatetimeIndex + symbols: list[str] + open: pd.DataFrame + high: pd.DataFrame + low: pd.DataFrame + close: pd.DataFrame + volume: pd.DataFrame + has_bar: pd.DataFrame + adj_open: pd.DataFrame + adj_high: pd.DataFrame + adj_low: pd.DataFrame + adj_close: pd.DataFrame + split_ratios: pd.DataFrame + dividends: pd.DataFrame + set_close: pd.Series + split_events: list[dict[str, object]] + action_symbols: int + dividend_events: int + issues: list[str] + + +def _action_maps(action_dir: str | Path, + set_reference: str | Path | None) -> tuple[dict[str, dict[pd.Timestamp, float]], + dict[str, list[tuple[pd.Timestamp, float]]]]: + dividends: dict[str, dict[pd.Timestamp, float]] = defaultdict(dict) + par_changes: dict[str, list[tuple[pd.Timestamp, float]]] = defaultdict(list) + directory = Path(action_dir) + for path in directory.glob("*_dividends.csv") if directory.exists() else []: + try: + frame = pd.read_csv(path) + except (OSError, pd.errors.ParserError): + continue + required = {"symbol", "ex_date", "dividend_per_share"} + if not required.issubset(frame.columns): + continue + for row in frame.itertuples(index=False): + symbol = str(row.symbol).strip().upper() + event_date = pd.to_datetime(row.ex_date, errors="coerce") + amount = pd.to_numeric(row.dividend_per_share, errors="coerce") + if symbol and pd.notna(event_date) and pd.notna(amount) and float(amount) > 0: + dividends[symbol][pd.Timestamp(event_date).normalize()] = float(amount) + for path in directory.glob("*_par_changes.csv") if directory.exists() else []: + try: + frame = pd.read_csv(path) + except (OSError, pd.errors.ParserError): + continue + required = {"symbol", "effective_date", "potential_share_ratio"} + if not required.issubset(frame.columns): + continue + for row in frame.itertuples(index=False): + symbol = str(row.symbol).strip().upper() + event_date = pd.to_datetime(row.effective_date, errors="coerce") + ratio = pd.to_numeric(row.potential_share_ratio, errors="coerce") + if (symbol and pd.notna(event_date) and pd.notna(ratio) + and float(ratio) > 0 and not math.isclose(float(ratio), 1.0)): + par_changes[symbol].append((pd.Timestamp(event_date).normalize(), float(ratio))) + if set_reference and Path(set_reference).exists(): + reference = pd.read_csv(set_reference, dtype={"symbol": str, "action_type": str}) + required = {"symbol", "action_type", "date", "value"} + if required.issubset(reference.columns): + reference = reference.loc[reference["action_type"].str.lower().eq("dividend")] + for row in reference.itertuples(index=False): + symbol = str(row.symbol).strip().upper() + event_date = pd.to_datetime(row.date, errors="coerce") + amount = pd.to_numeric(row.value, errors="coerce") + if symbol and pd.notna(event_date) and pd.notna(amount) and float(amount) > 0: + dividends[symbol][pd.Timestamp(event_date).normalize()] = float(amount) + return dict(dividends), dict(par_changes) + + +def load_market_panel(archives: list[str | Path], *, start: str, end: str, + action_dir: str | Path = "data/raw/actions", + set_index_csv: str | Path = "data/raw/^SET.csv", + set_reference: str | Path | None = "data/reference/set_actions_2025_2026_h1.csv") -> MarketPanel: + """Read market-wide daily records into aligned prices, with an as-of-date universe. + + ``start`` is inclusive and ``end`` is exclusive. A limited list of index and + known fund tickers is omitted; remaining instruments are filtered by historical + turnover at signal time. Missing archive entries are recorded, not repaired by + looking at future index membership. + """ + daily_entries: dict[str, tuple[Path, str]] = {} + for archive_name in archives: + path = Path(archive_name) + if not path.is_file(): + raise FileNotFoundError(f"SiamChart archive not found: {path}") + try: + with zipfile.ZipFile(path) as archive: + for entry in archive.infolist(): + match = _EOD_NAME.search(Path(entry.filename).name) + if not match: + continue + day = match.group(1) + if start <= day < end: + daily_entries[day] = (path, entry.filename) + except (OSError, zipfile.BadZipFile) as exc: + raise ValueError(f"cannot read SiamChart archive {path}: {exc}") from exc + if not daily_entries: + raise ValueError("no daily EOD files overlap the requested period") + + days = sorted(daily_entries) + date_index = pd.DatetimeIndex(pd.to_datetime(days), name="date") + date_position = {day: i for i, day in enumerate(days)} + records: dict[str, list[tuple[int, float, float, float, float, float]]] = defaultdict(list) + set_records: dict[pd.Timestamp, float] = {} + issues: list[str] = [] + for day in days: + archive_path, member = daily_entries[day] + try: + with zipfile.ZipFile(archive_path) as archive: + with archive.open(member) as binary, io.TextIOWrapper( + binary, encoding="utf-8-sig", newline="") as stream: + reader = csv.DictReader(stream) + columns = set(reader.fieldnames or []) + required = {"", "", "", "", "", "", ""} + if not required.issubset(columns): + raise ValueError("unexpected daily CSV columns") + for row in reader: + symbol = (row.get("") or "").strip().upper() + try: + op, hi, lo, cl, vol = (float(row[key]) for key in + ("", "", "", "", "")) + except (TypeError, ValueError): + continue + if not all(math.isfinite(v) for v in (op, hi, lo, cl, vol)) or cl <= 0: + continue + if symbol == "SET": + set_records[pd.Timestamp(day)] = cl + elif (_TICKER.fullmatch(symbol) and symbol not in _NON_STOCK_TICKERS + and not (symbol.endswith("80") and len(symbol) >= 5)): + records[symbol].append((date_position[day], op, hi, lo, cl, max(vol, 0.0))) + except (OSError, EOFError, UnicodeDecodeError, csv.Error, ValueError, + zipfile.BadZipFile, zlib.error) as exc: + issues.append(f"{archive_path.name}:{member}: {exc}") + + if not records: + raise ValueError("no equity-like records found in the selected archives") + # A symbol that never cleared the model's minimum historical liquidity/price + # gate cannot enter the target universe. Pruning it here keeps the long-history + # matrices small without changing the as-of-date top-100 selection later. + active_symbols: list[str] = [] + for symbol, rows in records.items(): + values = np.asarray(rows, dtype=np.float64) + positions = values[:, 0].astype(np.int32) + close = np.full(len(date_index), np.nan, dtype=np.float64) + turnover = np.zeros(len(date_index), dtype=np.float64) + close[positions] = values[:, 4] + turnover[positions] = values[:, 4] * values[:, 5] + average = pd.Series(turnover).rolling(20, min_periods=20).mean().to_numpy() + if np.any((average >= 20_000_000.0) & (close >= 2.0)): + active_symbols.append(symbol) + records = {symbol: records[symbol] for symbol in active_symbols} + if not records: + raise ValueError("no securities met the historical liquidity and price gates") + symbols = sorted(records) + shape = (len(date_index), len(symbols)) + raw_open = np.full(shape, np.nan, dtype=np.float64) + raw_high = np.full(shape, np.nan, dtype=np.float64) + raw_low = np.full(shape, np.nan, dtype=np.float64) + raw_close = np.full(shape, np.nan, dtype=np.float64) + raw_volume = np.zeros(shape, dtype=np.float64) + column = {symbol: j for j, symbol in enumerate(symbols)} + for symbol, rows in records.items(): + j = column[symbol] + values = np.asarray(rows, dtype=np.float64) + idx = values[:, 0].astype(np.int32) + raw_open[idx, j], raw_high[idx, j], raw_low[idx, j], raw_close[idx, j], raw_volume[idx, j] = values[:, 1:].T + has_bar = np.isfinite(raw_close) + dividend_map, par_map = _action_maps(action_dir, set_reference) + + set_close = pd.Series(set_records, dtype=float).reindex(date_index) + if set_index_csv and Path(set_index_csv).exists(): + try: + source = pd.read_csv(set_index_csv) + date_col = next((c for c in source.columns if str(c).strip().lower() == "date"), None) + close_col = next((c for c in source.columns if str(c).strip().lower() == "close"), None) + if date_col and close_col: + fallback = pd.Series(pd.to_numeric(source[close_col], errors="coerce").to_numpy(), + index=pd.to_datetime(source[date_col], errors="coerce")).sort_index() + fallback = fallback.loc[~fallback.index.isna()] + set_close = set_close.fillna(fallback.reindex(date_index)) + except (OSError, ValueError, pd.errors.ParserError): + pass + set_close.name = "SET" + + close_ff = pd.DataFrame(raw_close, index=date_index, columns=symbols).ffill() + split_matrix = np.ones(shape, dtype=np.float64) + dividend_matrix = np.zeros(shape, dtype=np.float64) + split_events: list[dict[str, object]] = [] + # Resolve documented par-value candidates only when raw prices corroborate a + # share-count conversion. Also detect unlisted large-ratio events when both + # price and volume move in the same direction as a split. + for symbol, j in column.items(): + raw = raw_close[:, j] + vol = raw_volume[:, j] + observed = np.flatnonzero(has_bar[:, j]) + observed_pos = {int(v): k for k, v in enumerate(observed)} + resolved: dict[int, tuple[float, str]] = {} + for event_date, ratio in par_map.get(symbol, []): + center = date_index.searchsorted(event_date) + candidates = [i for i in range(max(1, center - 4), min(len(date_index), center + 5)) + if has_bar[i, j] and has_bar[i - 1, j]] + fitting = [] + for i in candidates: + move = raw[i] / raw[i - 1] + adj_move = move * ratio + if (move < 0.67 or move > 1.5) and 0.72 <= adj_move <= 1.28: + fitting.append((abs(math.log(adj_move)), i)) + if fitting: + _, i = min(fitting) + resolved[i] = (ratio, "par_change_price_confirmed") + for k in range(1, len(observed)): + i, prev_i = int(observed[k]), int(observed[k - 1]) + if i in resolved or raw[prev_i] <= 0 or raw[i] <= 0: + continue + price_ratio = raw[i] / raw[prev_i] + if 0.35 <= price_ratio <= 2.85: + continue + ratio = float(_SPLIT_FACTORS[np.argmin(np.abs(np.log(_SPLIT_FACTORS * price_ratio)))]) + adjusted_move = price_ratio * ratio + previous_volume = vol[prev_i] + volume_ratio = vol[i] / previous_volume if previous_volume > 0 else np.nan + volume_support = (pd.notna(volume_ratio) and ratio > 0 + and 0.25 <= volume_ratio / ratio <= 4.0) + if 0.72 <= adjusted_move <= 1.28 and volume_support: + resolved[i] = (ratio, "price_and_volume_inferred") + for i, (ratio, source) in resolved.items(): + split_matrix[i, j] = ratio + split_events.append({"symbol": symbol, "date": date_index[i].date().isoformat(), + "share_ratio": ratio, "source": source}) + for event_date, amount in dividend_map.get(symbol, {}).items(): + i = date_index.get_indexer([event_date])[0] + if i >= 0: + dividend_matrix[i, j] = amount + + adj_factor = np.ones(shape, dtype=np.float64) + for j, symbol in enumerate(symbols): + factor = 1.0 + last_close = np.nan + for i in range(len(date_index)): + if has_bar[i, j]: + current = raw_close[i, j] + split = split_matrix[i, j] + dividend = dividend_matrix[i, j] + if math.isfinite(last_close) and last_close > 0: + factor *= split * (1.0 + dividend / current) + elif split != 1: + factor *= split + last_close = current + elif dividend_matrix[i, j] > 0 and math.isfinite(last_close) and last_close > 0: + factor *= 1.0 + dividend_matrix[i, j] / last_close + adj_factor[i, j] = factor + + close_df = pd.DataFrame(close_ff, index=date_index, columns=symbols) + open_df = pd.DataFrame(raw_open, index=date_index, columns=symbols).where(has_bar, close_df) + high_df = pd.DataFrame(raw_high, index=date_index, columns=symbols).where(has_bar, close_df) + low_df = pd.DataFrame(raw_low, index=date_index, columns=symbols).where(has_bar, close_df) + volume_df = pd.DataFrame(raw_volume, index=date_index, columns=symbols) + has_bar_df = pd.DataFrame(has_bar, index=date_index, columns=symbols) + factor_df = pd.DataFrame(adj_factor, index=date_index, columns=symbols) + return MarketPanel( + dates=date_index, symbols=symbols, open=open_df, high=high_df, low=low_df, + close=close_df, volume=volume_df, has_bar=has_bar_df, + adj_open=open_df * factor_df, adj_high=high_df * factor_df, + adj_low=low_df * factor_df, adj_close=close_df * factor_df, + split_ratios=pd.DataFrame(split_matrix, index=date_index, columns=symbols), + dividends=pd.DataFrame(dividend_matrix, index=date_index, columns=symbols), + set_close=set_close, split_events=split_events, + action_symbols=len(dividend_map), + dividend_events=sum(len(events) for events in dividend_map.values()), + issues=issues, + ) + + +def _features(panel: MarketPanel) -> dict[str, pd.DataFrame | pd.Series]: + turnover = panel.close.mul(panel.volume.where(panel.has_bar, 0.0)) + avg_turnover20 = turnover.rolling(20, min_periods=20).mean() + ranks = avg_turnover20.rank(axis=1, ascending=False, method="first") + universe = (panel.has_bar & panel.close.ge(2.0) & avg_turnover20.ge(20_000_000.0) + & ranks.le(100)) + sma200 = panel.adj_close.rolling(200, min_periods=200).mean() + sma5 = panel.adj_close.rolling(5, min_periods=5).mean() + momentum_12_1 = panel.adj_close.shift(21) / panel.adj_close.shift(252) - 1 + momentum_6_1 = panel.adj_close.shift(21) / panel.adj_close.shift(126) - 1 + daily_returns = panel.adj_close.pct_change() + volatility_126 = daily_returns.rolling(126, min_periods=100).std(ddof=1) + risk_adjusted_momentum = momentum_12_1 / volatility_126.replace(0, np.nan) + ema50 = panel.adj_close.ewm(span=50, adjust=False, min_periods=50).mean() + ema200 = panel.adj_close.ewm(span=200, adjust=False, min_periods=200).mean() + ema50_200_up = ema50.gt(ema200) + ema50_200_cross_up = ema50_200_up & ~ema50_200_up.shift(1, fill_value=False) + ema50_200_cross_down = ~ema50_200_up & ema50_200_up.shift(1, fill_value=False) + delta = panel.adj_close.diff() + gain = delta.clip(lower=0).rolling(2, min_periods=2).mean() + loss = (-delta.clip(upper=0)).rolling(2, min_periods=2).mean() + rsi2 = 100 - 100 / (1 + gain / loss.replace(0, np.nan)) + rsi2 = rsi2.mask(loss.eq(0), 100.0).mask(gain.eq(0) & loss.gt(0), 0.0) + prior_high55 = panel.adj_high.shift(1).rolling(55, min_periods=55).max() + prior_low20 = panel.adj_low.shift(1).rolling(20, min_periods=20).min() + set_sma200 = panel.set_close.rolling(200, min_periods=200).mean() + market_up = panel.set_close.gt(set_sma200) & panel.set_close.notna() + stock_up = panel.adj_close.gt(sma200) + return { + "universe": universe, "avg_turnover20": avg_turnover20, + "sma200": sma200, "sma5": sma5, "momentum_12_1": momentum_12_1, + "momentum_6_1": momentum_6_1, "risk_adjusted_momentum": risk_adjusted_momentum, + "ema50_200_cross_up": ema50_200_cross_up, + "ema50_200_cross_down": ema50_200_cross_down, "rsi2": rsi2, + "prior_high55": prior_high55, "prior_low20": prior_low20, + "market_up": market_up, "stock_up": stock_up, + } + + +def _select_top(scores: pd.Series, mask: pd.Series, count: int) -> list[str]: + values = scores.loc[mask & scores.notna()].sort_values(ascending=False, kind="mergesort") + return values.head(count).index.tolist() + + +def build_model_targets(panel: MarketPanel, features: dict[str, pd.DataFrame | pd.Series], *, + model: str, start: str) -> pd.DataFrame: + """Create desired holdings by signal close; rows are applied at that session's open. + + The signal for row ``t`` was formed at the preceding close, preserving next-open + execution. Entries use only bars and rolling information available by that close. + """ + dates, symbols = panel.dates, panel.symbols + idx_start = max(1, int(dates.searchsorted(pd.Timestamp(start)))) + target = pd.DataFrame(False, index=dates, columns=symbols, dtype=bool) + universe = features["universe"] + stock_up = features["stock_up"] + market_up = features["market_up"] + avg_turnover = features["avg_turnover20"] + if model == "momentum_12_1_monthly": + scores = features["momentum_12_1"] + held: list[str] = [] + for i in range(idx_start, len(dates)): + if dates[i].month != dates[i - 1].month: + signal_i = i - 1 + if bool(market_up.iloc[signal_i]): + mask = universe.iloc[signal_i] & stock_up.iloc[signal_i] + held = _select_top(scores.iloc[signal_i], mask, 10) + else: + held = [] + if held: + target.loc[dates[i], held] = True + return target + + if model == "momentum_12_1_low_vol_monthly": + raw_momentum = features["momentum_12_1"] + scores = features["risk_adjusted_momentum"] + held: list[str] = [] + for i in range(idx_start, len(dates)): + if dates[i].month != dates[i - 1].month: + signal_i = i - 1 + if bool(market_up.iloc[signal_i]): + mask = (universe.iloc[signal_i] & stock_up.iloc[signal_i] + & raw_momentum.iloc[signal_i].gt(0)) + held = _select_top(scores.iloc[signal_i], mask, 10) + else: + held = [] + if held: + target.loc[dates[i], held] = True + return target + + if model == "ema50_200_cross": + held: list[str] = [] + cross_up = features["ema50_200_cross_up"] + cross_down = features["ema50_200_cross_down"] + for i in range(idx_start, len(dates)): + if held: + target.loc[dates[i], held] = True + keep = [symbol for symbol in held + if bool(market_up.iloc[i]) and not bool(cross_down.iloc[i][symbol])] + held = keep + if bool(market_up.iloc[i]) and len(held) < 10: + candidates = (universe.iloc[i] & stock_up.iloc[i] & cross_up.iloc[i]) + ranked = _select_top(features["momentum_12_1"].iloc[i], candidates, 100) + for symbol in ranked: + if symbol not in held: + held.append(symbol) + if len(held) >= 10: + break + return target + + if model == "rsi2_pullback": + held: list[str] = [] + ages: dict[str, int] = {} + for i in range(idx_start, len(dates)): + # These names were selected at the prior close and are held from today's open. + if held: + target.loc[dates[i], held] = True + keep: list[str] = [] + exited_today: set[str] = set() + for symbol in held: + j = panel.close.columns.get_loc(symbol) + should_exit = (not bool(market_up.iloc[i]) + or (bool(panel.has_bar.iloc[i, j]) and ( + bool(panel.adj_close.iloc[i, j] > features["sma5"].iloc[i, j]) + or bool(features["rsi2"].iloc[i, j] > 70) + or ages.get(symbol, 0) >= 10))) + if should_exit: + exited_today.add(symbol) + ages.pop(symbol, None) + else: + keep.append(symbol) + ages[symbol] = ages.get(symbol, 0) + 1 + held = keep + if bool(market_up.iloc[i]) and len(held) < 10: + close_i = panel.close.iloc[i] + candidates = universe.iloc[i] & stock_up.iloc[i] & features["rsi2"].iloc[i].lt(10) + if exited_today: + candidates.loc[list(exited_today)] = False + # Prefer the deepest short-term pullback, then the more liquid name. + ranks = pd.DataFrame({"rsi": features["rsi2"].iloc[i], + "turnover": avg_turnover.iloc[i]}) + ranks = ranks.loc[candidates & ranks.rsi.notna()].sort_values( + ["rsi", "turnover"], ascending=[True, False], kind="mergesort") + for symbol in ranks.index: + if symbol not in held: + held.append(symbol) + ages[symbol] = 0 + if len(held) >= 10: + break + return target + + if model == "donchian_55_20": + held: list[str] = [] + for i in range(idx_start, len(dates)): + if held: + target.loc[dates[i], held] = True + keep: list[str] = [] + for symbol in held: + j = panel.close.columns.get_loc(symbol) + exit_now = (not bool(market_up.iloc[i]) + or (bool(panel.has_bar.iloc[i, j]) + and bool(panel.adj_close.iloc[i, j] < features["prior_low20"].iloc[i, j]))) + if not exit_now: + keep.append(symbol) + held = keep + if bool(market_up.iloc[i]) and len(held) < 10: + breakout = (universe.iloc[i] & stock_up.iloc[i] + & panel.adj_close.iloc[i].gt(features["prior_high55"].iloc[i])) + candidates = _select_top(features["momentum_6_1"].iloc[i], breakout, 100) + for symbol in candidates: + if symbol not in held: + held.append(symbol) + if len(held) >= 10: + break + return target + raise ValueError(f"unknown model: {model}") + + +def build_union_target_weights(*targets: pd.DataFrame) -> pd.DataFrame: + """Equal-weight unique symbols selected by any input model on each session.""" + if not targets: + raise ValueError("at least one model target is required") + index, columns = targets[0].index, targets[0].columns + active = pd.DataFrame(False, index=index, columns=columns) + for target in targets: + if not target.index.equals(index) or not target.columns.equals(columns): + raise ValueError("model target frames must have matching dates and symbols") + active |= target.astype(bool) + counts = active.sum(axis=1).replace(0, np.nan) + return active.div(counts, axis=0).fillna(0.0) + + +def _append_last_row(frame: pd.DataFrame | pd.Series, next_date: pd.Timestamp) -> pd.DataFrame | pd.Series: + future = frame.iloc[[-1]].copy() + future.index = pd.DatetimeIndex([next_date], name=frame.index.name) + return pd.concat([frame, future]) + + +def build_next_session_union_signals( + panel: MarketPanel, + features: dict[str, pd.DataFrame | pd.Series], + *, + start: str, + initial_equity: float, + board_lot: int = 100, +) -> pd.DataFrame: + """Build the next-open union targets from the latest observed close.""" + if not len(panel.dates) or initial_equity <= 0 or board_lot < 1: + raise ValueError("market data, positive equity and board lot are required") + next_date = panel.dates[-1] + pd.Timedelta(days=1) + future_panel = MarketPanel( + dates=panel.dates.append(pd.DatetimeIndex([next_date])), + symbols=panel.symbols, + open=_append_last_row(panel.open, next_date), + high=_append_last_row(panel.high, next_date), + low=_append_last_row(panel.low, next_date), + close=_append_last_row(panel.close, next_date), + volume=_append_last_row(panel.volume, next_date), + has_bar=_append_last_row(panel.has_bar, next_date), + adj_open=_append_last_row(panel.adj_open, next_date), + adj_high=_append_last_row(panel.adj_high, next_date), + adj_low=_append_last_row(panel.adj_low, next_date), + adj_close=_append_last_row(panel.adj_close, next_date), + split_ratios=_append_last_row(panel.split_ratios, next_date), + dividends=_append_last_row(panel.dividends, next_date), + set_close=_append_last_row(panel.set_close, next_date), + split_events=panel.split_events, + action_symbols=panel.action_symbols, + dividend_events=panel.dividend_events, + issues=panel.issues, + ) + future_features = {name: _append_last_row(values, next_date) + for name, values in features.items()} + donchian = build_model_targets( + future_panel, future_features, model="donchian_55_20", start=start + ).iloc[-1] + ema = build_model_targets( + future_panel, future_features, model="ema50_200_cross", start=start + ).iloc[-1] + active = donchian | ema + selected = active.index[active] + if not len(selected): + return pd.DataFrame(columns=[ + "signal_as_of", "for_session", "symbol", "source_model", "target_weight", + "reference_close", "indicative_shares_at_last_close", "last_bar_date", "stale_sessions", + ]) + weight = 1.0 / len(selected) + latest_date = panel.dates[-1] + rows: list[dict[str, object]] = [] + for symbol in selected: + price = float(panel.close.at[latest_date, symbol]) + estimated_shares = (math.floor((initial_equity * weight) / price / board_lot) * board_lot + if math.isfinite(price) and price > 0 else 0) + source = [] + if bool(donchian[symbol]): + source.append("donchian_55_20") + if bool(ema[symbol]): + source.append("ema50_200_cross") + bar_dates = panel.dates[panel.has_bar[symbol].to_numpy(dtype=bool)] + last_bar = bar_dates[-1] if len(bar_dates) else pd.NaT + stale_sessions = int((panel.dates > last_bar).sum()) if not pd.isna(last_bar) else len(panel.dates) + rows.append({ + "signal_as_of": latest_date.date().isoformat(), + "for_session": "next trading session", + "symbol": symbol, + "source_model": "+".join(source), + "target_weight": weight, + "reference_close": price, + "indicative_shares_at_last_close": estimated_shares, + "last_bar_date": last_bar.date().isoformat() if not pd.isna(last_bar) else "", + "stale_sessions": stale_sessions, + }) + return pd.DataFrame(rows).sort_values( + ["source_model", "symbol"], kind="mergesort" + ).reset_index(drop=True) + + +def simulate_target_weights(panel: MarketPanel, target: pd.DataFrame, *, + costs: TradingCosts | None = None, + initial_equity: float = 1_000_000.0, + max_positions: int = 10) -> tuple[pd.DataFrame, dict[str, float]]: + """Next-open target-weight simulation with dividend/split-adjusted market values. + + Fractional portfolio weights are used for this broad-market screening pass. The + final candidate check should use whole SET board lots and the production engine. + """ + costs = costs or TradingCosts() + if initial_equity <= 0 or max_positions < 1: + raise ValueError("initial_equity and max_positions must be positive") + n_dates, n_symbols = target.shape + if n_dates < 2: + raise ValueError("at least two sessions are required") + target_values = target.to_numpy(dtype=bool, copy=False) + has_bar = panel.has_bar.to_numpy(dtype=bool, copy=False) + adj_open = panel.adj_open.to_numpy(dtype=float, copy=False) + adj_close = panel.adj_close.to_numpy(dtype=float, copy=False) + desired_all = np.zeros_like(target_values, dtype=np.float64) + for i in range(n_dates): + selected = np.flatnonzero(target_values[i]) + if len(selected) > max_positions: + selected = selected[:max_positions] + if len(selected): + desired_all[i, selected] = 1.0 / max_positions + nav = float(initial_equity) + prior_weights = np.zeros(n_symbols, dtype=np.float64) + prior_cash_weight = 1.0 + equity_rows: list[dict[str, object]] = [] + total_buy_notional = total_sell_notional = total_costs = 0.0 + symbol_gross_pnl = np.zeros(n_symbols, dtype=np.float64) + symbol_trade_costs = np.zeros(n_symbols, dtype=np.float64) + total_fills = 0 + current_count = 0 + for i, day in enumerate(panel.dates): + if i == 0: + equity_rows.append({"date": day, "equity": nav, "cash": nav, + "exposure": 0.0, "open_positions": 0, "cost": 0.0}) + continue + overnight_return = np.zeros(n_symbols, dtype=np.float64) + available_prev = np.isfinite(adj_close[i - 1]) + available_today = np.isfinite(adj_open[i]) + valid = available_prev & available_today & (adj_close[i - 1] > 0) + overnight_return[valid] = adj_open[i, valid] / adj_close[i - 1, valid] - 1.0 + overnight_return[~has_bar[i]] = 0.0 + overnight_pnl = nav * prior_weights * overnight_return + open_values = prior_weights * (1.0 + overnight_return) + opening_value_factor = prior_cash_weight + float(open_values.sum()) + nav_open = nav * opening_value_factor + if nav_open <= 0 or not math.isfinite(nav_open): + raise ValueError(f"non-positive portfolio value at {day.date()}") + # ``open_values`` and cash are portfolio weights, while ``nav_open`` is + # currency. Normalize by the dimensionless opening portfolio value first. + open_weights = open_values / opening_value_factor + desired = desired_all[i].copy() + unavailable = ~has_bar[i] + stuck = unavailable & (open_weights > 0) + new_unavailable = unavailable & (desired > 0) & (open_weights <= 0) + desired[new_unavailable] = 0.0 + desired[stuck] = open_weights[stuck] + tradable = ~unavailable + stuck_total = float(desired[stuck].sum()) + other_total = float(desired[tradable].sum()) + capacity = max(0.0, 1.0 - stuck_total) + if other_total > capacity and other_total > 0: + desired[tradable] *= capacity / other_total + desired_cash = max(0.0, 1.0 - float(desired.sum())) + delta = desired - open_weights + buys = float(delta.clip(min=0).sum()) + sells = float((-delta).clip(min=0).sum()) + buy_rate = costs.commission_rate + costs.fees_rate + sell_rate = costs.commission_rate + costs.fees_rate + costs.sell_tax_rate + slippage = costs.slippage_bps / 10_000 + buy_costs = nav_open * delta.clip(min=0) * (buy_rate + slippage) + sell_costs = nav_open * (-delta).clip(min=0) * (sell_rate + slippage) + day_cost = float(buy_costs.sum() + sell_costs.sum()) + symbol_trade_costs += buy_costs + sell_costs + nav_after_trading = nav_open - day_cost + intraday_return = np.zeros(n_symbols, dtype=np.float64) + valid = has_bar[i] & np.isfinite(adj_open[i]) & np.isfinite(adj_close[i]) & (adj_open[i] > 0) + intraday_return[valid] = adj_close[i, valid] / adj_open[i, valid] - 1.0 + symbol_gross_pnl += overnight_pnl + nav_after_trading * desired * intraday_return + growth = desired_cash + float(np.dot(desired, 1.0 + intraday_return)) + nav_close = nav_after_trading * growth + if nav_close <= 0 or not math.isfinite(nav_close): + raise ValueError(f"non-positive portfolio value at close {day.date()}") + prior_weights = desired * (1.0 + intraday_return) / growth + prior_cash_weight = desired_cash / growth + nav = nav_close + total_buy_notional += buys * nav_open + total_sell_notional += sells * nav_open + total_costs += day_cost + total_fills += int((delta > 1e-8).sum() + (delta < -1e-8).sum()) + current_count = int((prior_weights > 1e-8).sum()) + equity_rows.append({"date": day, "equity": nav, "cash": nav * prior_cash_weight, + "exposure": float(prior_weights.sum()), "open_positions": current_count, + "cost": day_cost}) + curve = pd.DataFrame(equity_rows).set_index("date") + attribution = { + symbol: {"gross_pnl": float(symbol_gross_pnl[j]), + "trading_costs": float(symbol_trade_costs[j]), + "net_pnl": float(symbol_gross_pnl[j] - symbol_trade_costs[j])} + for j, symbol in enumerate(panel.symbols) + if symbol_gross_pnl[j] != 0 or symbol_trade_costs[j] != 0 + } + return curve, {"buy_notional": total_buy_notional, "sell_notional": total_sell_notional, + "transaction_costs": total_costs, "fills": float(total_fills), + "symbol_attribution": attribution} + + +def simulate_board_lots(panel: MarketPanel, target: pd.DataFrame, *, + costs: TradingCosts | None = None, + initial_equity: float = 1_000_000.0, + max_positions: int = 10, + board_lot: int = 100, + target_weights: pd.DataFrame | None = None, + stale_writeoff_sessions: int | None = None) -> tuple[pd.DataFrame, dict[str, float]]: + """Validate targets with whole shares and the standard 100-share SET lot. + + Split ratios change share quantities, dividends are credited as cash, and orders + use raw open/close prices. A 50-share high-price lot is intentionally not assumed. + """ + costs = costs or TradingCosts() + if (initial_equity <= 0 or max_positions < 1 or board_lot < 1 + or (stale_writeoff_sessions is not None and stale_writeoff_sessions < 1)): + raise ValueError("equity, position count and board lot must be positive") + target_values = target.to_numpy(dtype=bool, copy=False) + weight_values = None + if target_weights is not None: + if not target_weights.index.equals(target.index) or not target_weights.columns.equals(target.columns): + raise ValueError("target weights must have the same dates and symbols as targets") + weight_values = target_weights.to_numpy(dtype=float, copy=False) + if (not np.isfinite(weight_values).all() or (weight_values < 0).any() + or (weight_values.sum(axis=1) > 1.0 + 1e-9).any()): + raise ValueError("target weights must be finite, non-negative and sum to at most one") + has_bar = panel.has_bar.to_numpy(dtype=bool, copy=False) + raw_open = panel.open.to_numpy(dtype=float, copy=False) + raw_close = panel.close.to_numpy(dtype=float, copy=False) + split_ratios = panel.split_ratios.to_numpy(dtype=float, copy=False) + dividends = panel.dividends.to_numpy(dtype=float, copy=False) + n_dates, n_symbols = target.shape + cash = float(initial_equity) + quantity = np.zeros(n_symbols, dtype=np.float64) + curve_rows: list[dict[str, object]] = [] + total_buy = total_sell = total_cost = 0.0 + total_fills = stale_position_days = 0 + total_stale_writeoff = 0.0 + no_bar_streak = np.zeros(n_symbols, dtype=np.int32) + buy_rate = costs.commission_rate + costs.fees_rate + sell_rate = costs.commission_rate + costs.fees_rate + costs.sell_tax_rate + slippage = costs.slippage_bps / 10_000 + for i, day in enumerate(panel.dates): + if i == 0: + curve_rows.append({"date": day, "equity": cash, "cash": cash, + "exposure": 0.0, "open_positions": 0, "cost": 0.0}) + continue + quantity *= split_ratios[i] + cash += float(np.dot(quantity, dividends[i])) + stale_position_days += int(((quantity > 0) & ~has_bar[i]).sum()) + valid_open = has_bar[i] & np.isfinite(raw_open[i]) & (raw_open[i] > 0) + mark_open = np.nan_to_num( + np.where(np.isfinite(raw_open[i]), raw_open[i], raw_close[i]), nan=0.0, posinf=0.0, neginf=0.0 + ) + no_bar_streak = np.where( + has_bar[i], 0, + np.where(quantity > 0, no_bar_streak + 1, 0), + ).astype(np.int32) + if stale_writeoff_sessions is not None: + writeoff = (quantity > 0) & ~has_bar[i] & (no_bar_streak >= stale_writeoff_sessions) + total_stale_writeoff += float(np.dot(quantity[writeoff], mark_open[writeoff])) + quantity[writeoff] = 0.0 + nav_open = cash + float(np.dot(quantity, mark_open)) + if nav_open <= 0 or not math.isfinite(nav_open): + raise ValueError(f"non-positive portfolio value at {day.date()}") + if weight_values is None: + selected = np.flatnonzero(target_values[i]) + if len(selected) > max_positions: + selected = selected[:max_positions] + desired = np.zeros(n_symbols, dtype=np.float64) + if len(selected): + desired[selected] = 1.0 / max_positions + else: + desired = weight_values[i].copy() + selected = np.flatnonzero(desired > 0) + unavailable_holds = ~has_bar[i] & (quantity > 0) + desired_qty = np.zeros(n_symbols, dtype=np.float64) + desired_qty[unavailable_holds] = quantity[unavailable_holds] + tradable_selected = selected[valid_open[selected]] + for j in tradable_selected: + notional = nav_open * desired[j] + lots = math.floor(notional / raw_open[i, j] / board_lot) + desired_qty[j] = lots * board_lot + + day_cost = 0.0 + sell_qty = np.maximum(quantity - desired_qty, 0.0) + sell_qty[~valid_open] = 0.0 + if sell_qty.any(): + sell_notional = sell_qty[valid_open] * raw_open[i, valid_open] + sell_fill_notional = sell_qty[valid_open] * raw_open[i, valid_open] * (1.0 - slippage) + sell_cost = float((sell_fill_notional * sell_rate).sum()) + cash += float(sell_fill_notional.sum()) - sell_cost + day_cost += float((sell_qty[valid_open] * raw_open[i, valid_open]).sum() + - sell_fill_notional.sum()) + sell_cost + total_sell += float(sell_notional.sum()) + total_fills += int((sell_qty > 1e-8).sum()) + quantity -= sell_qty + + buy_qty = np.maximum(desired_qty - quantity, 0.0) + for j in np.flatnonzero((buy_qty > 0) & valid_open): + unit_cash = raw_open[i, j] * (1.0 + slippage) * (1.0 + buy_rate) + affordable_lots = math.floor(max(cash, 0.0) / (unit_cash * board_lot)) + lots = min(math.floor(buy_qty[j] / board_lot), affordable_lots) + actual_qty = lots * board_lot + if actual_qty <= 0: + continue + notional = actual_qty * raw_open[i, j] + fill_notional = notional * (1.0 + slippage) + buy_cost = fill_notional * buy_rate + cash -= fill_notional + buy_cost + day_cost += (fill_notional - notional) + buy_cost + total_buy += notional + total_fills += 1 + quantity[j] += actual_qty + no_bar_streak[quantity <= 0] = 0 + + mark_close = np.nan_to_num(raw_close[i], nan=0.0, posinf=0.0, neginf=0.0) + equity = cash + float(np.dot(quantity, mark_close)) + if equity <= 0 or not math.isfinite(equity): + raise ValueError(f"non-positive portfolio value at close {day.date()}") + market_value = float(np.dot(quantity, mark_close)) + curve_rows.append({"date": day, "equity": equity, "cash": cash, + "exposure": market_value / equity, + "open_positions": int((quantity > 1e-8).sum()), "cost": day_cost}) + total_cost += day_cost + curve = pd.DataFrame(curve_rows).set_index("date") + return curve, {"buy_notional": total_buy, "sell_notional": total_sell, + "transaction_costs": total_cost, "fills": float(total_fills), + "stale_position_days": float(stale_position_days), + "stale_writeoff_value": float(total_stale_writeoff)} + + +def calculate_model_metrics(curve: pd.DataFrame, *, start: str, end: str, + execution: dict[str, float]) -> tuple[dict[str, object], pd.DataFrame]: + sliced = curve.loc[(curve.index >= pd.Timestamp(start)) & (curve.index < pd.Timestamp(end))] + if len(sliced) < 2: + raise ValueError("evaluation window has fewer than two sessions") + equity = sliced["equity"].astype(float) + returns = equity.pct_change().dropna() + years = max((equity.index[-1] - equity.index[0]).days / 365.25, 1 / 252) + dd = equity / equity.cummax() - 1.0 + annual = (1.0 + returns).groupby(returns.index.year).prod() - 1.0 + monthly = (1.0 + returns).groupby(returns.index.to_period("M")).prod() - 1.0 + starting = float(equity.iloc[0]) + ending = float(equity.iloc[-1]) + metrics: dict[str, object] = { + "start": sliced.index[0].date().isoformat(), "end": sliced.index[-1].date().isoformat(), + "sessions": int(len(sliced)), "total_return": ending / starting - 1.0, + "cagr": (ending / starting) ** (1.0 / years) - 1.0 if ending > 0 else -1.0, + "max_drawdown": float(dd.min()), + "sharpe_daily": float(returns.mean() / returns.std(ddof=1) * math.sqrt(252)) + if len(returns) > 1 and returns.std(ddof=1) > 0 else 0.0, + "win_month_pct": float((monthly > 0).mean()) if len(monthly) else 0.0, + "positive_years": int((annual > 0).sum()), "years_observed": int(len(annual)), + "average_exposure": float(sliced["exposure"].mean()), + "average_positions": float(sliced["open_positions"].mean()), + "buy_notional": execution["buy_notional"], "sell_notional": execution["sell_notional"], + "transaction_costs_full_run": execution["transaction_costs"], + "fills_full_run": int(execution["fills"]), + } + annual_frame = annual.rename("return").rename_axis("year").reset_index() + return metrics, annual_frame + + +def _write_targets(target: pd.DataFrame, output: Path) -> None: + active = target.stack().loc[lambda values: values] + active.rename_axis(index=["date", "symbol"]).rename("held").reset_index().to_csv(output, index=False) + + +def _write_weight_targets(target_weights: pd.DataFrame, output: Path) -> None: + active = target_weights.stack().loc[lambda values: values > 0] + active.rename_axis(index=["date", "symbol"]).rename("target_weight").reset_index().to_csv( + output, index=False + ) + + +def run_model_lab(archives: list[str | Path], *, start: str = "2005-01-01", + end: str = "2026-01-01", output_dir: str | Path = "reports/model_lab", + action_dir: str | Path = "data/raw/actions", + set_index_csv: str | Path = "data/raw/^SET.csv", + set_reference: str | Path | None = "data/reference/set_actions_2025_2026_h1.csv", + initial_equity: float = 1_000_000.0) -> pd.DataFrame: + """Run fixed model families with base and stressed costs; save full records.""" + load_start = (pd.Timestamp(start) - pd.Timedelta(days=700)).date().isoformat() + panel = load_market_panel(archives, start=load_start, end=end, action_dir=action_dir, + set_index_csv=set_index_csv, set_reference=set_reference) + features = _features(panel) + model_labels = { + "momentum_12_1_monthly": "12-1 relative momentum, monthly rotation", + "momentum_12_1_low_vol_monthly": "12-1 momentum ranked by realized volatility-adjusted return", + "ema50_200_cross": "EMA 50/200 cross with SET and stock trend gates", + "rsi2_pullback": "RSI(2) pullback in an uptrend", + "donchian_55_20": "55/20 Donchian breakout", + } + scenarios = { + "base_cost": TradingCosts(), + "cost_stress": TradingCosts(commission_rate=0.003, fees_rate=0.0, + sell_tax_rate=0.0, slippage_bps=15.0), + } + root = Path(output_dir) + root.mkdir(parents=True, exist_ok=True) + summary_rows: list[dict[str, object]] = [] + model_targets: dict[str, pd.DataFrame] = {} + data_notes = { + "archive_files": [str(Path(path)) for path in archives], + "evaluation_start": start, "evaluation_end_exclusive": end, + "daily_sessions_loaded": len(panel.dates), "equity_like_symbols": len(panel.symbols), + "action_symbols_with_dividend_history": panel.action_symbols, + "cash_dividend_events_loaded": panel.dividend_events, + "split_events_price_confirmed_or_inferred": len(panel.split_events), + "archive_read_issues": panel.issues, + "universe": "top 100 symbols by trailing 20-session average value turnover, at least THB 20m/day, price >= THB 2, as-of each signal close", + "primary_union": { + "models": ["donchian_55_20", "ema50_200_cross"], + "entry_rule": "hold a symbol selected by either model", + "weighting": "equal weight across unique active symbols; overlapping selections count once", + "maximum_positions": 20, + }, + "known_non_stock_tickers_excluded": sorted(_NON_STOCK_TICKERS), + "notes": [ + "Universe membership is constructed from contemporaneous market-wide EOD records; no current-constituent list is backfilled.", + "A small ticker exclusion list removes indices and known funds; the archive has no instrument-type field, so other equity-like symbols can remain.", + "Cash dividends are applied where SiamChart sidecars exist, with covered-period SET reference values overriding sidecars.", + "Par-value share ratios are used only when price data corroborates a split; missing corporate-action histories remain a data risk.", + "Portfolio weights are fractional and daily value marks are adjusted; board lots and market impact are not modeled in this screening pass.", + "The low-volatility momentum, EMA 50/200 and Donchian finalists also have a separate validation using 100-share board lots, raw prices, split-adjusted quantities, and dividend cash where available.", + "A security with no daily bar cannot be sold in the simulator; its last observed close is carried forward and stale_position_days is reported for board-lot validation.", + "A separate conservative board-lot sensitivity writes the entire position value to zero after 60 consecutive sessions without a bar; this is a delisting-risk stress, not an assumed exchange settlement rule.", + "One unreadable daily archive entry is omitted and listed in archive_read_issues; the source archive is not modified.", + ], + } + (root / "data_notes.json").write_text(json.dumps(data_notes, indent=2, ensure_ascii=False), encoding="utf-8") + pd.DataFrame(panel.split_events).to_csv(root / "split_adjustments.csv", index=False) + feature_scenarios = [ + ("momentum_12_1_monthly", "momentum_12_1_monthly"), + ("momentum_12_1_low_vol_monthly", "momentum_12_1_low_vol_monthly"), + ("ema50_200_cross", "ema50_200_cross"), + ("rsi2_pullback", "rsi2_pullback"), + ("donchian_55_20", "donchian_55_20"), + ] + annual_rows: list[pd.DataFrame] = [] + period_rows: list[dict[str, object]] = [] + attribution_rows: list[pd.DataFrame] = [] + board_lot_rows: list[dict[str, object]] = [] + board_lot_period_rows: list[dict[str, object]] = [] + stale_stress_rows: list[dict[str, object]] = [] + stale_stress_period_rows: list[dict[str, object]] = [] + verify_with_lots = {"momentum_12_1_low_vol_monthly", "ema50_200_cross", "donchian_55_20"} + for model, target_label in feature_scenarios: + target = build_model_targets(panel, features, model=target_label, start=start) + model_targets[model] = target + model_dir = root / model + model_dir.mkdir(parents=True, exist_ok=True) + _write_targets(target, model_dir / "target_holdings.csv") + for scenario, costs in scenarios.items(): + curve, execution = simulate_target_weights(panel, target, costs=costs, + initial_equity=initial_equity, max_positions=10) + scenario_dir = model_dir / scenario + scenario_dir.mkdir(parents=True, exist_ok=True) + curve.to_csv(scenario_dir / "equity_curve.csv", index_label="date") + metrics, annual = calculate_model_metrics(curve, start=start, end=end, execution=execution) + metrics.update({"model": model, "model_label": model_labels[model], "scenario": scenario, + "commission_rate": costs.commission_rate, + "slippage_bps_per_side": costs.slippage_bps, + "turnover_pct_initial": (execution["buy_notional"] + execution["sell_notional"]) + / initial_equity * 100.0}) + attribution = pd.DataFrame.from_dict(execution["symbol_attribution"], orient="index") + if not attribution.empty: + attribution.index.name = "symbol" + attribution = attribution.reset_index() + attribution.insert(0, "scenario", scenario) + attribution.insert(0, "model", model) + positive_pnl = attribution["net_pnl"].clip(lower=0).sum() + largest_five = attribution["net_pnl"].nlargest(5).clip(lower=0).sum() + metrics["top_5_share_of_positive_symbol_pnl"] = ( + float(largest_five / positive_pnl) if positive_pnl > 0 else 0.0 + ) + attribution_rows.append(attribution) + attribution.to_csv(scenario_dir / "symbol_attribution.csv", index=False) + (scenario_dir / "metrics.json").write_text(json.dumps(metrics, indent=2, allow_nan=False), encoding="utf-8") + if model in verify_with_lots: + lot_curve, lot_execution = simulate_board_lots( + panel, target, costs=costs, initial_equity=initial_equity, + max_positions=10, board_lot=100, + ) + lot_dir = scenario_dir / "board_lot_100" + lot_dir.mkdir(parents=True, exist_ok=True) + lot_curve.to_csv(lot_dir / "equity_curve.csv", index_label="date") + lot_metrics, _ = calculate_model_metrics( + lot_curve, start=start, end=end, execution=lot_execution + ) + lot_metrics.update({"model": model, "scenario": scenario, + "board_lot_shares": 100, + "commission_rate": costs.commission_rate, + "slippage_bps_per_side": costs.slippage_bps, + "turnover_pct_initial": (lot_execution["buy_notional"] + + lot_execution["sell_notional"]) / initial_equity * 100.0, + "stale_position_days": int(lot_execution["stale_position_days"])}) + (lot_dir / "metrics.json").write_text( + json.dumps(lot_metrics, indent=2, allow_nan=False), encoding="utf-8" + ) + board_lot_rows.append(lot_metrics) + for period, period_start, period_end in [ + ("early", start, "2013-01-01"), + ("middle", "2013-01-01", "2021-01-01"), + ("recent_holdout", "2021-01-01", end), + ]: + effective_start = max(pd.Timestamp(start), pd.Timestamp(period_start)) + effective_end = min(pd.Timestamp(end), pd.Timestamp(period_end)) + if effective_end <= effective_start: + continue + try: + lot_period_metrics, _ = calculate_model_metrics( + lot_curve, start=effective_start.date().isoformat(), + end=effective_end.date().isoformat(), execution=lot_execution, + ) + except ValueError: + continue + board_lot_period_rows.append({"model": model, "scenario": scenario, + "board_lot_shares": 100, "period": period, + **lot_period_metrics}) + strict_curve, strict_execution = simulate_board_lots( + panel, target, costs=costs, initial_equity=initial_equity, + max_positions=10, board_lot=100, stale_writeoff_sessions=60, + ) + strict_dir = scenario_dir / "board_lot_stale60" + strict_dir.mkdir(parents=True, exist_ok=True) + strict_curve.to_csv(strict_dir / "equity_curve.csv", index_label="date") + strict_metrics, _ = calculate_model_metrics( + strict_curve, start=start, end=end, execution=strict_execution + ) + strict_metrics.update({"model": model, "scenario": scenario, + "board_lot_shares": 100, + "stale_writeoff_after_sessions": 60, + "stale_writeoff_value": strict_execution["stale_writeoff_value"], + "commission_rate": costs.commission_rate, + "slippage_bps_per_side": costs.slippage_bps, + "turnover_pct_initial": (strict_execution["buy_notional"] + + strict_execution["sell_notional"]) / initial_equity * 100.0, + "stale_position_days": int(strict_execution["stale_position_days"])}) + (strict_dir / "metrics.json").write_text( + json.dumps(strict_metrics, indent=2, allow_nan=False), encoding="utf-8" + ) + stale_stress_rows.append(strict_metrics) + for period, period_start, period_end in [ + ("early", start, "2013-01-01"), + ("middle", "2013-01-01", "2021-01-01"), + ("recent_holdout", "2021-01-01", end), + ]: + effective_start = max(pd.Timestamp(start), pd.Timestamp(period_start)) + effective_end = min(pd.Timestamp(end), pd.Timestamp(period_end)) + if effective_end <= effective_start: + continue + try: + strict_period_metrics, _ = calculate_model_metrics( + strict_curve, start=effective_start.date().isoformat(), + end=effective_end.date().isoformat(), execution=strict_execution, + ) + except ValueError: + continue + stale_stress_period_rows.append({"model": model, "scenario": scenario, + "board_lot_shares": 100, + "stale_writeoff_after_sessions": 60, + "period": period, **strict_period_metrics}) + annual.insert(0, "scenario", scenario) + annual.insert(0, "model", model) + annual_rows.append(annual) + summary_rows.append(metrics) + periods = [ + ("early", start, "2013-01-01"), + ("middle", "2013-01-01", "2021-01-01"), + ("recent_holdout", "2021-01-01", end), + ] + for period, period_start, period_end in periods: + effective_start = max(pd.Timestamp(start), pd.Timestamp(period_start)) + effective_end = min(pd.Timestamp(end), pd.Timestamp(period_end)) + if effective_end <= effective_start: + continue + try: + period_metrics, _ = calculate_model_metrics( + curve, start=effective_start.date().isoformat(), + end=effective_end.date().isoformat(), execution=execution, + ) + except ValueError: + continue + period_rows.append({"model": model, "scenario": scenario, + "period": period, **period_metrics}) + + union_model = "primary_union_donchian_ema" + union_weights = build_union_target_weights( + model_targets["donchian_55_20"], model_targets["ema50_200_cross"] + ) + union_targets = union_weights.gt(0) + max_union_positions = int(union_targets.sum(axis=1).max()) + union_dir = root / union_model + union_dir.mkdir(parents=True, exist_ok=True) + _write_weight_targets(union_weights, union_dir / "target_weights.csv") + next_signals = build_next_session_union_signals( + panel, features, start=start, initial_equity=initial_equity, board_lot=100, + ) + next_signals.to_csv(union_dir / "next_session_signals.csv", index=False) + for scenario, costs in scenarios.items(): + scenario_dir = union_dir / scenario + lot_dir = scenario_dir / "board_lot_100" + lot_dir.mkdir(parents=True, exist_ok=True) + union_curve, union_execution = simulate_board_lots( + panel, union_targets, target_weights=union_weights, costs=costs, + initial_equity=initial_equity, max_positions=20, board_lot=100, + ) + union_curve.to_csv(lot_dir / "equity_curve.csv", index_label="date") + union_metrics, union_annual = calculate_model_metrics( + union_curve, start=start, end=end, execution=union_execution + ) + union_metrics.update({ + "model": union_model, + "model_label": "Equal-weight union of Donchian 55/20 and EMA 50/200 signals", + "scenario": scenario, + "board_lot_shares": 100, + "maximum_target_positions": max_union_positions, + "overlapping_model_selections_count_once": True, + "commission_rate": costs.commission_rate, + "slippage_bps_per_side": costs.slippage_bps, + "turnover_pct_initial": (union_execution["buy_notional"] + + union_execution["sell_notional"]) / initial_equity * 100.0, + "stale_position_days": int(union_execution["stale_position_days"]), + }) + (lot_dir / "metrics.json").write_text( + json.dumps(union_metrics, indent=2, allow_nan=False), encoding="utf-8" + ) + board_lot_rows.append(union_metrics) + union_annual.insert(0, "scenario", scenario) + union_annual.insert(0, "model", union_model) + annual_rows.append(union_annual) + for period, period_start, period_end in [ + ("early", start, "2013-01-01"), + ("middle", "2013-01-01", "2021-01-01"), + ("recent_holdout", "2021-01-01", end), + ]: + effective_start = max(pd.Timestamp(start), pd.Timestamp(period_start)) + effective_end = min(pd.Timestamp(end), pd.Timestamp(period_end)) + if effective_end <= effective_start: + continue + try: + union_period_metrics, _ = calculate_model_metrics( + union_curve, start=effective_start.date().isoformat(), + end=effective_end.date().isoformat(), execution=union_execution, + ) + except ValueError: + continue + board_lot_period_rows.append({ + "model": union_model, "scenario": scenario, + "board_lot_shares": 100, "period": period, **union_period_metrics, + }) + + strict_dir = scenario_dir / "board_lot_stale60" + strict_dir.mkdir(parents=True, exist_ok=True) + strict_curve, strict_execution = simulate_board_lots( + panel, union_targets, target_weights=union_weights, costs=costs, + initial_equity=initial_equity, max_positions=20, board_lot=100, + stale_writeoff_sessions=60, + ) + strict_curve.to_csv(strict_dir / "equity_curve.csv", index_label="date") + strict_metrics, strict_annual = calculate_model_metrics( + strict_curve, start=start, end=end, execution=strict_execution + ) + strict_metrics.update({ + "model": union_model, + "model_label": "Equal-weight union of Donchian 55/20 and EMA 50/200 signals", + "scenario": scenario, + "board_lot_shares": 100, + "stale_writeoff_after_sessions": 60, + "stale_writeoff_value": strict_execution["stale_writeoff_value"], + "maximum_target_positions": max_union_positions, + "overlapping_model_selections_count_once": True, + "commission_rate": costs.commission_rate, + "slippage_bps_per_side": costs.slippage_bps, + "turnover_pct_initial": (strict_execution["buy_notional"] + + strict_execution["sell_notional"]) / initial_equity * 100.0, + "stale_position_days": int(strict_execution["stale_position_days"]), + }) + (strict_dir / "metrics.json").write_text( + json.dumps(strict_metrics, indent=2, allow_nan=False), encoding="utf-8" + ) + stale_stress_rows.append(strict_metrics) + strict_annual.insert(0, "scenario", scenario) + strict_annual.insert(0, "model", union_model) + for period, period_start, period_end in [ + ("early", start, "2013-01-01"), + ("middle", "2013-01-01", "2021-01-01"), + ("recent_holdout", "2021-01-01", end), + ]: + effective_start = max(pd.Timestamp(start), pd.Timestamp(period_start)) + effective_end = min(pd.Timestamp(end), pd.Timestamp(period_end)) + if effective_end <= effective_start: + continue + try: + strict_period_metrics, _ = calculate_model_metrics( + strict_curve, start=effective_start.date().isoformat(), + end=effective_end.date().isoformat(), execution=strict_execution, + ) + except ValueError: + continue + stale_stress_period_rows.append({ + "model": union_model, "scenario": scenario, + "board_lot_shares": 100, "stale_writeoff_after_sessions": 60, + "period": period, **strict_period_metrics, + }) + # The annual rows for the strict stale sensitivity are kept in the same + # annual table as the regular execution model under a distinct scenario. + strict_annual["scenario"] = f"{scenario}_stale60" + annual_rows.append(strict_annual) + summary = pd.DataFrame(summary_rows) + summary.to_csv(root / "summary.csv", index=False) + if annual_rows: + pd.concat(annual_rows, ignore_index=True).to_csv(root / "annual_returns.csv", index=False) + if period_rows: + pd.DataFrame(period_rows).to_csv(root / "period_metrics.csv", index=False) + if attribution_rows: + pd.concat(attribution_rows, ignore_index=True).to_csv(root / "symbol_attribution.csv", index=False) + if board_lot_rows: + pd.DataFrame(board_lot_rows).to_csv(root / "board_lot_validation.csv", index=False) + if board_lot_period_rows: + pd.DataFrame(board_lot_period_rows).to_csv(root / "board_lot_period_metrics.csv", index=False) + if stale_stress_rows: + pd.DataFrame(stale_stress_rows).to_csv(root / "board_lot_stale60_validation.csv", index=False) + if stale_stress_period_rows: + pd.DataFrame(stale_stress_period_rows).to_csv(root / "board_lot_stale60_period_metrics.csv", index=False) + return summary diff --git a/primethai/portfolio.py b/primethai/portfolio.py new file mode 100644 index 0000000..6c33d02 --- /dev/null +++ b/primethai/portfolio.py @@ -0,0 +1,83 @@ +"""Thai-aware price/lot rounding, risk sizing and portfolio safety measures.""" + +from __future__ import annotations + +from dataclasses import dataclass +import math + +from .config import ThaiMarketRules + + +def round_to_tick(price: float, rules: ThaiMarketRules, *, direction: str = "nearest", + etf: bool = False) -> float: + if not math.isfinite(price) or price <= 0: + raise ValueError("price must be positive") + if direction not in {"nearest", "up", "down"}: + raise ValueError("direction must be nearest, up or down") + tick = rules.tick_size(price, etf=etf) + units = price / tick + if direction == "up": + rounded = math.ceil(units - 1e-10) + elif direction == "down": + rounded = math.floor(units + 1e-10) + else: + rounded = math.floor(units + 0.5) + return round(rounded * tick, 4) + + +def round_quantity(quantity: float, lot_size: int, *, allow_odd_lot: bool = False, + minimum_quantity: int = 1) -> int: + if not math.isfinite(quantity) or quantity <= 0: + return 0 + if lot_size <= 0: + raise ValueError("lot_size must be positive") + if allow_odd_lot: + return max(minimum_quantity, int(math.floor(quantity + 1e-9))) + return int(math.floor(quantity / lot_size + 1e-9) * lot_size) + + +def size_position(equity: float, risk_pct: float, entry_price: float, stop_price: float, + *, lot_size: int = 100, max_position_pct: float = 0.20, + rules: ThaiMarketRules | None = None) -> dict[str, float | int]: + if not all(math.isfinite(value) for value in (equity, risk_pct, entry_price, stop_price, max_position_pct)): + return {"shares": 0, "risk_per_share": 0.0, "capital_risk": 0.0, + "initial_risk": 0.0, "position_value": 0.0} + if equity <= 0 or entry_price <= 0 or risk_pct <= 0: + return {"shares": 0, "risk_per_share": 0.0, "capital_risk": 0.0, + "initial_risk": 0.0, "position_value": 0.0} + risk_per_share = entry_price - stop_price + if not math.isfinite(risk_per_share) or risk_per_share <= 0: + return {"shares": 0, "risk_per_share": risk_per_share, "capital_risk": equity * risk_pct, + "initial_risk": 0.0, "position_value": 0.0} + risk_budget = equity * risk_pct + by_risk = risk_budget / risk_per_share + by_cap = equity * max_position_pct / entry_price + shares = round_quantity(min(by_risk, by_cap), lot_size, + minimum_quantity=(rules or ThaiMarketRules()).minimum_quantity) + notional = shares * entry_price + return {"shares": shares, "risk_per_share": risk_per_share, + "capital_risk": risk_budget, "initial_risk": shares * risk_per_share, + "position_value": notional} + + +@dataclass +class HeatPosition: + initial_risk: float + market_value: float + cost_basis: float + + @property + def unrealized_profit(self) -> float: + return self.market_value - self.cost_basis + + +def portfolio_heat(positions: list[HeatPosition], equity: float) -> float: + if equity <= 0: + return 0.0 + return sum(max(0.0, position.initial_risk) for position in positions) / equity + + +def portfolio_cushion(positions: list[HeatPosition], equity: float) -> float: + if equity <= 0: + return 0.0 + return sum(position.unrealized_profit for position in positions) / equity diff --git a/primethai/portfolio_book.py b/primethai/portfolio_book.py new file mode 100644 index 0000000..4f4cb02 --- /dev/null +++ b/primethai/portfolio_book.py @@ -0,0 +1,417 @@ +"""Persistent manual portfolio ledger and target-weight trade planner.""" + +from __future__ import annotations + +import csv +from contextlib import closing +from datetime import datetime, timezone +import math +from pathlib import Path +import re +import sqlite3 +import zipfile +from typing import Any + + +DEFAULT_BOOK = Path("data/portfolio/prime_thai_portfolio.sqlite3") +MAX_CASH_AMOUNT = 1_000_000_000_000.0 +_EOD_FILE = re.compile(r"set-history_EOD_(\d{4}-\d{2}-\d{2})\.csv$", re.IGNORECASE) +_SYMBOL = re.compile(r"[A-Z0-9]{1,12}$") + + +def _connect(database: str | Path = DEFAULT_BOOK) -> sqlite3.Connection: + path = Path(database) + path.parent.mkdir(parents=True, exist_ok=True) + connection = sqlite3.connect(path, timeout=10.0, isolation_level=None) + connection.row_factory = sqlite3.Row + connection.execute("PRAGMA foreign_keys = ON") + connection.executescript(""" + CREATE TABLE IF NOT EXISTS cash_events ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + kind TEXT NOT NULL CHECK (kind IN ('deposit', 'withdrawal', 'dividend')), + amount REAL NOT NULL CHECK (amount > 0), + occurred_at TEXT NOT NULL, + note TEXT NOT NULL DEFAULT '' + ); + CREATE TABLE IF NOT EXISTS trades ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + symbol TEXT NOT NULL, + side TEXT NOT NULL CHECK (side IN ('BUY', 'SELL')), + quantity INTEGER NOT NULL CHECK (quantity > 0), + price REAL NOT NULL CHECK (price > 0), + fees REAL NOT NULL DEFAULT 0 CHECK (fees >= 0), + occurred_at TEXT NOT NULL, + note TEXT NOT NULL DEFAULT '', + is_estimated INTEGER NOT NULL DEFAULT 0 CHECK (is_estimated IN (0, 1)), + is_void INTEGER NOT NULL DEFAULT 0 CHECK (is_void IN (0, 1)), + voided_at TEXT + ); + CREATE TABLE IF NOT EXISTS price_marks ( + symbol TEXT PRIMARY KEY, + as_of TEXT NOT NULL, + close REAL NOT NULL CHECK (close > 0) + ); + CREATE INDEX IF NOT EXISTS trades_symbol_id ON trades(symbol, id); + CREATE INDEX IF NOT EXISTS trades_time_id ON trades(occurred_at, id); + CREATE INDEX IF NOT EXISTS cash_events_time_id ON cash_events(occurred_at, id); + """) + # Existing local portfolio databases predate estimated signal trades. + columns = {str(row["name"]) for row in connection.execute("PRAGMA table_info(trades)")} + if "is_estimated" not in columns: + connection.execute("ALTER TABLE trades ADD COLUMN is_estimated INTEGER NOT NULL DEFAULT 0") + if "is_void" not in columns: + connection.execute("ALTER TABLE trades ADD COLUMN is_void INTEGER NOT NULL DEFAULT 0") + if "voided_at" not in columns: + connection.execute("ALTER TABLE trades ADD COLUMN voided_at TEXT") + return connection + + +def read_latest_eod_quotes(archive_path: str | Path) -> tuple[str, dict[str, float]]: + """Read closes from the newest daily CSV in a SiamChart ZIP.""" + path = Path(archive_path) + if not path.is_file(): + raise FileNotFoundError(f"ไม่พบ ZIP EOD: {path}") + with zipfile.ZipFile(path) as archive: + candidates = [(match.group(1), member.filename) + for member in archive.infolist() + if (match := _EOD_FILE.search(Path(member.filename).name))] + if not candidates: + raise ValueError("ZIP ไม่มีไฟล์ SiamChart EOD รายวัน") + as_of, member_name = max(candidates, key=lambda item: item[0]) + with archive.open(member_name) as binary: + import io + with io.TextIOWrapper(binary, encoding="utf-8-sig", newline="") as stream: + reader = csv.DictReader(stream) + required = {"", ""} + if not required.issubset(set(reader.fieldnames or [])): + raise ValueError("คอลัมน์ในไฟล์ EOD ไม่ตรงกับรูปแบบที่รองรับ") + quotes: dict[str, float] = {} + for row in reader: + symbol = (row.get("") or "").strip().upper() + try: + close = float(row[""]) + except (TypeError, ValueError): + continue + if symbol and math.isfinite(close) and close > 0: + quotes[symbol] = close + return as_of, quotes + + +def update_price_marks(as_of: str, quotes: dict[str, float], *, + database: str | Path = DEFAULT_BOOK) -> None: + with closing(_connect(database)) as connection: + connection.executemany(""" + INSERT INTO price_marks(symbol, as_of, close) VALUES (?, ?, ?) + ON CONFLICT(symbol) DO UPDATE SET as_of=excluded.as_of, close=excluded.close + WHERE excluded.as_of >= price_marks.as_of + """, [(symbol.upper(), as_of, float(close)) for symbol, close in quotes.items() + if _SYMBOL.fullmatch(symbol.upper()) and math.isfinite(float(close)) and float(close) > 0]) + + +def add_cash_event(kind: str, amount: float, note: str = "", *, + database: str | Path = DEFAULT_BOOK) -> int: + kind = kind.strip().lower() + if kind not in {"deposit", "withdrawal", "dividend"}: + raise ValueError("รายการเงินสดต้องเป็น deposit, withdrawal หรือ dividend") + value = float(amount) + if not math.isfinite(value) or value <= 0 or value > MAX_CASH_AMOUNT: + raise ValueError("จำนวนเงินต้องมากกว่า 0 และไม่เกิน 1,000,000,000,000 บาท") + with closing(_connect(database)) as connection: + cursor = connection.execute( + "INSERT INTO cash_events(kind, amount, occurred_at, note) VALUES (?, ?, ?, ?)", + (kind, value, datetime.now().astimezone().isoformat(), str(note)[:200]), + ) + return int(cursor.lastrowid) + + +def _read_ledger(connection: sqlite3.Connection) -> tuple[list[sqlite3.Row], list[sqlite3.Row]]: + cash_events = connection.execute("SELECT * FROM cash_events ORDER BY id").fetchall() + trades = connection.execute("SELECT * FROM trades ORDER BY id").fetchall() + return cash_events, trades + + +def _calculate_book(cash_events: list[sqlite3.Row], trades: list[sqlite3.Row]) -> dict[str, Any]: + cash = 0.0 + net_contributed = 0.0 + deposits = 0 + for event in cash_events: + amount = float(event["amount"]) + if event["kind"] == "deposit": + cash += amount + net_contributed += amount + deposits += 1 + elif event["kind"] == "withdrawal": + cash -= amount + net_contributed -= amount + elif event["kind"] == "dividend": + cash += amount + + positions: dict[str, dict[str, float | int]] = {} + for trade in trades: + if "is_void" in trade.keys() and bool(trade["is_void"]): + continue + symbol = str(trade["symbol"]) + position = positions.setdefault(symbol, {"quantity": 0, "cost_basis": 0.0, "realized_pnl": 0.0}) + quantity = int(trade["quantity"]) + price = float(trade["price"]) + fees = float(trade["fees"]) + gross = quantity * price + if trade["side"] == "BUY": + cash -= gross + fees + position["quantity"] = int(position["quantity"]) + quantity + position["cost_basis"] = float(position["cost_basis"]) + gross + fees + else: + held = int(position["quantity"]) + average_cost = float(position["cost_basis"]) / held if held else 0.0 + cash += gross - fees + position["realized_pnl"] = float(position["realized_pnl"]) + gross - fees - average_cost * quantity + position["cost_basis"] = max(0.0, float(position["cost_basis"]) - average_cost * quantity) + position["quantity"] = held - quantity + positions = {symbol: position for symbol, position in positions.items() + if int(position["quantity"]) > 0} + return {"cash": cash, "net_contributed": net_contributed, + "deposit_count": deposits, "positions": positions} + + +def record_trade(symbol: str, side: str, quantity: int, price: float, fees: float = 0.0, + note: str = "", *, estimated: bool = False, + database: str | Path = DEFAULT_BOOK) -> int: + symbol = symbol.strip().upper() + side = side.strip().upper() + if not _SYMBOL.fullmatch(symbol): + raise ValueError("รหัสหุ้นต้องเป็นตัวอักษร A-Z หรือตัวเลข 1–12 ตัว") + if side not in {"BUY", "SELL"}: + raise ValueError("ประเภทรายการต้องเป็น BUY หรือ SELL") + if isinstance(quantity, bool) or int(quantity) != quantity or int(quantity) <= 0: + raise ValueError("จำนวนหุ้นต้องเป็นจำนวนเต็มมากกว่า 0") + quantity = int(quantity) + price, fees = float(price), float(fees) + if not math.isfinite(price) or price <= 0 or not math.isfinite(fees) or fees < 0: + raise ValueError("ราคาต้องมากกว่า 0 และค่าธรรมเนียมต้องไม่ติดลบ") + + with closing(_connect(database)) as connection: + connection.execute("BEGIN IMMEDIATE") + try: + cash_events, trades = _read_ledger(connection) + book = _calculate_book(cash_events, trades) + if side == "BUY" and quantity * price + fees > float(book["cash"]) + 0.005: + raise ValueError(f"เงินสดไม่พอ: มี {float(book['cash']):,.2f} บาท แต่รายการนี้ใช้ {quantity * price + fees:,.2f} บาท") + held = int(book["positions"].get(symbol, {}).get("quantity", 0)) + if side == "SELL" and quantity > held: + raise ValueError(f"หุ้น {symbol} มี {held:,} หุ้น ขายเกินจำนวนที่บันทึกไว้ไม่ได้") + cursor = connection.execute( + "INSERT INTO trades(symbol, side, quantity, price, fees, occurred_at, note, is_estimated) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + (symbol, side, quantity, price, fees, datetime.now().astimezone().isoformat(), + str(note)[:200], int(bool(estimated))), + ) + connection.execute("COMMIT") + return int(cursor.lastrowid) + except Exception: + connection.execute("ROLLBACK") + raise + + +def update_trade(trade_id: int, quantity: int, price: float, fees: float = 0.0, *, + database: str | Path = DEFAULT_BOOK) -> None: + """Edit a previously logged signal trade to match the actual fill.""" + if isinstance(quantity, bool) or int(quantity) != quantity or int(quantity) <= 0: + raise ValueError("จำนวนหุ้นต้องเป็นจำนวนเต็มมากกว่า 0") + quantity = int(quantity) + price, fees = float(price), float(fees) + if not math.isfinite(price) or price <= 0 or not math.isfinite(fees) or fees < 0: + raise ValueError("ราคาต้องมากกว่า 0 และค่าธรรมเนียมต้องไม่ติดลบ") + + with closing(_connect(database)) as connection: + connection.execute("BEGIN IMMEDIATE") + try: + cash_events, trades = _read_ledger(connection) + trade = next((row for row in trades if int(row["id"]) == int(trade_id)), None) + if trade is None or ("is_void" in trade.keys() and bool(trade["is_void"])): + raise ValueError("ไม่พบรายการที่แก้ไขได้ หรือรายการนี้ถูกยกเลิกแล้ว") + symbol, side = str(trade["symbol"]), str(trade["side"]) + old_quantity, old_price, old_fees = int(trade["quantity"]), float(trade["price"]), float(trade["fees"]) + book = _calculate_book(cash_events, trades) + held = int(book["positions"].get(symbol, {}).get("quantity", 0)) + resulting_held = held + (quantity - old_quantity if side == "BUY" else old_quantity - quantity) + if resulting_held < 0: + raise ValueError(f"แก้จำนวนแล้วหุ้น {symbol} จะติดลบ; ตรวจรายการขายภายหลังด้วย") + if side == "BUY": + resulting_cash = float(book["cash"]) + old_quantity * old_price + old_fees - quantity * price - fees + else: + resulting_cash = float(book["cash"]) - old_quantity * old_price + old_fees + quantity * price - fees + if resulting_cash < -0.005: + raise ValueError(f"แก้รายการแล้วเงินสดจะติดลบ {abs(resulting_cash):,.2f} บาท") + connection.execute( + "UPDATE trades SET quantity=?, price=?, fees=?, is_estimated=0 WHERE id=?", + (quantity, price, fees, int(trade_id)), + ) + connection.execute("COMMIT") + except Exception: + connection.execute("ROLLBACK") + raise + + +def void_trade(trade_id: int, *, database: str | Path = DEFAULT_BOOK) -> None: + """Void a signal-estimated trade while preserving it in the audit ledger.""" + with closing(_connect(database)) as connection: + connection.execute("BEGIN IMMEDIATE") + try: + cash_events, trades = _read_ledger(connection) + trade = next((row for row in trades if int(row["id"]) == int(trade_id)), None) + if (trade is None or ("is_void" in trade.keys() and bool(trade["is_void"])) + or not bool(trade["is_estimated"])): + raise ValueError("ยกเลิกได้เฉพาะรายการประมาณการจากสัญญาณที่ยังไม่ถูกยกเลิก") + book = _calculate_book(cash_events, trades) + symbol, side = str(trade["symbol"]), str(trade["side"]) + quantity, price, fees = int(trade["quantity"]), float(trade["price"]), float(trade["fees"]) + held = int(book["positions"].get(symbol, {}).get("quantity", 0)) + resulting_held = held - quantity if side == "BUY" else held + quantity + resulting_cash = float(book["cash"]) + (quantity * price + fees if side == "BUY" + else -quantity * price + fees) + if resulting_held < 0: + raise ValueError(f"ยกเลิกแล้วหุ้น {symbol} จะติดลบ; แก้รายการขายภายหลังก่อน") + if resulting_cash < -0.005: + raise ValueError(f"ยกเลิกแล้วเงินสดจะติดลบ {abs(resulting_cash):,.2f} บาท") + connection.execute( + "UPDATE trades SET is_void=1, voided_at=? WHERE id=?", + (datetime.now().astimezone().isoformat(), int(trade_id)), + ) + connection.execute("COMMIT") + except Exception: + connection.execute("ROLLBACK") + raise + + +def portfolio_view(signals: list[dict[str, str]], *, board_lot: int = 100, + signal_as_of: str | None = None, + database: str | Path = DEFAULT_BOOK) -> dict[str, Any]: + if board_lot <= 0: + raise ValueError("board_lot must be positive") + with closing(_connect(database)) as connection: + cash_events, trades = _read_ledger(connection) + book = _calculate_book(cash_events, trades) + price_rows = connection.execute("SELECT symbol, as_of, close FROM price_marks").fetchall() + recent_cash = connection.execute("SELECT * FROM cash_events ORDER BY id DESC LIMIT 10").fetchall() + recent_trades = connection.execute("SELECT * FROM trades ORDER BY id DESC LIMIT 10").fetchall() + + prices = {str(row["symbol"]): {"as_of": str(row["as_of"]), "close": float(row["close"])} + for row in price_rows} + targets: dict[str, dict[str, float | str]] = {} + for signal in signals: + symbol = str(signal.get("symbol", "")).strip().upper() + try: + weight = float(signal.get("target_weight", "0")) + except (TypeError, ValueError): + continue + if symbol and weight > 0: + targets[symbol] = {"weight": weight, "source_model": str(signal.get("source_model", ""))} + + positions: dict[str, dict[str, float | int]] = book["positions"] + missing_prices = sorted(symbol for symbol in positions if symbol not in prices) + signal_as_of = signal_as_of or max( + (str(row.get("signal_as_of", "")) for row in signals), default="" + ) or max((price["as_of"] for price in prices.values()), default="") + quote_as_of = max((price["as_of"] for price in prices.values()), default="") + signal_quote_mismatch = bool(signal_as_of and quote_as_of and signal_as_of != quote_as_of) + stale_prices = sorted(symbol for symbol in positions + if symbol in prices and signal_as_of and prices[symbol]["as_of"] < signal_as_of) + incomplete_symbols = sorted(set(missing_prices) | set(stale_prices)) + complete = not incomplete_symbols and not signal_quote_mismatch + total_market_value = 0.0 + for symbol, position in positions.items(): + if symbol in prices: + total_market_value += int(position["quantity"]) * prices[symbol]["close"] + cash = float(book["cash"]) + equity = cash + total_market_value + account_exists = int(book["deposit_count"]) > 0 or bool(positions) + rows: list[dict[str, Any]] = [] + buy_value = 0.0 + sell_value = 0.0 + for symbol in sorted(set(targets) | set(positions)): + target = targets.get(symbol, {"weight": 0.0, "source_model": ""}) + mark = prices.get(symbol) + quantity = int(positions.get(symbol, {}).get("quantity", 0)) + cost_basis = float(positions.get(symbol, {}).get("cost_basis", 0.0)) + realized_pnl = float(positions.get(symbol, {}).get("realized_pnl", 0.0)) + close = float(mark["close"]) if mark else None + target_value = equity * float(target["weight"]) if account_exists and complete else None + target_quantity = (int(math.floor(float(target_value) / close / board_lot + 1e-10) * board_lot) + if close and account_exists and complete and target_value is not None else None) + delta = (int(target_quantity) - quantity) if target_quantity is not None else None + market_value = quantity * close if close is not None else None + average_cost = cost_basis / quantity if quantity else None + unrealized = market_value - cost_basis if market_value is not None else None + if mark is None or symbol in stale_prices: + action = "PRICE NEEDED" if quantity else "WAIT" + elif not complete and account_exists: + action = "REVIEW" + elif not account_exists: + action = "WAIT" + elif float(target["weight"]) > 0 and target_quantity == 0 and quantity == 0: + action = "BELOW LOT" + elif delta is not None and delta > 0: + action = "BUY" + buy_value += delta * close # type: ignore[operator] + elif delta is not None and delta < 0: + action = "SELL" + sell_value += -delta * close # type: ignore[operator] + else: + action = "HOLD" + rows.append({ + "symbol": symbol, + "source_model": str(target["source_model"]), + "target_weight": float(target["weight"]), + "quantity_held": quantity, + "target_quantity": target_quantity, + "delta_quantity": delta if action in {"BUY", "SELL"} else 0, + "action": action, + "reference_close": close, + "suggested_limit_price": close, + "price_as_of": str(mark["as_of"]) if mark else None, + "market_value": market_value, + "target_value": target_value, + "average_cost": average_cost, + "unrealized_pnl": unrealized, + "realized_pnl": realized_pnl, + }) + action_order = {"SELL": 0, "BUY": 1, "PRICE NEEDED": 2, "REVIEW": 3, + "BELOW LOT": 4, "HOLD": 5, "WAIT": 6} + rows.sort(key=lambda row: (action_order.get(row["action"], 9), row["symbol"])) + + ledger: list[dict[str, Any]] = [] + for event in recent_cash: + ledger.append({"kind": event["kind"], "amount": float(event["amount"]), + "occurred_at": event["occurred_at"], "note": event["note"]}) + for trade in recent_trades: + ledger.append({"kind": trade["side"], "symbol": trade["symbol"], + "id": int(trade["id"]), + "quantity": int(trade["quantity"]), "price": float(trade["price"]), + "fees": float(trade["fees"]), "occurred_at": trade["occurred_at"], + "note": trade["note"], + "is_estimated": bool(trade["is_estimated"]), + "is_void": bool(trade["is_void"]), + "voided_at": trade["voided_at"]}) + ledger.sort(key=lambda row: str(row["occurred_at"]), reverse=True) + + net_contributed = float(book["net_contributed"]) + return { + "account_exists": account_exists, + "deposit_count": int(book["deposit_count"]), + "cash": cash, + "net_contributed": net_contributed, + "market_value": total_market_value, + "equity": equity, + "profit_loss": equity - net_contributed if complete else None, + "profit_loss_pct": (equity / net_contributed - 1.0) if complete and net_contributed > 0 else None, + "complete": complete, + "unpriced_symbols": missing_prices, + "stale_price_symbols": stale_prices, + "signal_quote_mismatch": signal_quote_mismatch, + "price_as_of": quote_as_of or None, + "buy_value": buy_value, + "sell_value": sell_value, + "cash_after_planned_trades": cash + sell_value - buy_value, + "requires_sell_proceeds": max(0.0, buy_value - cash), + "rows": rows, + "ledger": ledger[:10], + } diff --git a/primethai/research.py b/primethai/research.py new file mode 100644 index 0000000..2768b51 --- /dev/null +++ b/primethai/research.py @@ -0,0 +1,137 @@ +"""Connect raw daily bars to market context, PIT breadth, features and RS.""" + +from __future__ import annotations + +import numpy as np +import pandas as pd + +from .config import RelativeStrengthWeights, ResearchSettings +from .indicators import (breadth_series, equal_weight_index, market_regimes, mcclellan, + price_features, relative_strength) +from .setups import candidate_features +from .universe import PointInTimeUniverse + + +def _equal_weight_bars(bars_by_symbol: dict[str, pd.DataFrame], closes: pd.DataFrame, + members_by_date: dict[pd.Timestamp, list[str]], base: float = 100.0) -> pd.DataFrame: + columns = {field: [] for field in ("open", "high", "low", "close", "volume")} + level = base + previous_close = closes.shift(1) + for date in closes.index: + members = [symbol for symbol in members_by_date.get(pd.Timestamp(date), []) if symbol in bars_by_symbol] + valid = [s for s in members if date in bars_by_symbol[s].index and date in previous_close.index + and pd.notna(previous_close.loc[date, s]) and previous_close.loc[date, s] > 0] + if not valid: + daily_open = daily_high = daily_low = daily_close = 0.0 + volume = 0.0 + else: + fields = {field: [] for field in ("open", "high", "low", "close")} + volume = 0.0 + for symbol in valid: + bar = bars_by_symbol[symbol].loc[date] + previous = float(previous_close.loc[date, symbol]) + for field in fields: + fields[field].append(float(bar[field]) / previous - 1) + volume += float(bar.get("volume", 0.0)) + daily_open, daily_high, daily_low, daily_close = (float(np.nanmean(fields[field])) for field in ("open", "high", "low", "close")) + daily_high = max(daily_high, daily_open, daily_close) + daily_low = min(daily_low, daily_open, daily_close) + if columns["close"]: + opening, high, low, close = (level * (1 + value) for value in (daily_open, daily_high, daily_low, daily_close)) + level = close + else: + opening = high = low = close = level + columns["open"].append(opening) + columns["high"].append(max(high, opening, close)) + columns["low"].append(min(low, opening, close)) + columns["close"].append(close) + columns["volume"].append(volume) + return pd.DataFrame(columns, index=closes.index) + + +def build_market_context(bars_by_symbol: dict[str, pd.DataFrame], set_bars: pd.DataFrame, + membership: PointInTimeUniverse | None = None, + current_members: list[str] | None = None, + *, accurate: bool = False, + research: ResearchSettings | None = None) -> tuple[pd.DataFrame, pd.Series, pd.DataFrame]: + """Build SET, equal-weight proxy, breadth/MCO/MCSI and deterministic regime.""" + research = research or ResearchSettings() + if not bars_by_symbol: + raise ValueError("at least one constituent price series is required") + dates = set_bars.index + stock_closes = pd.DataFrame({symbol: frame["close"].reindex(dates) for symbol, frame in bars_by_symbol.items()}, index=dates) + if accurate: + if membership is None: + raise ValueError("accurate mode requires a point-in-time membership file") + members_by_date = membership.members_by_date(dates) + elif membership is not None: + anchor = dates[-1] + members = membership.members(anchor) + if not members: + members = membership.members(max(row.end for row in membership.periods)) + members_by_date = {pd.Timestamp(date): members for date in dates} + else: + if not current_members: + raise ValueError("fast mode requires a supplied constituent snapshot") + members = [symbol.upper().removesuffix(".BK") for symbol in current_members] + members_by_date = {pd.Timestamp(date): members for date in dates} + closes = stock_closes.copy() + breadth = breadth_series(closes, members_by_date) + ew_bars = _equal_weight_bars(bars_by_symbol, closes, members_by_date) + ew_features = price_features(ew_bars, slope_lookback=research.slope_lookback, + atr_period=research.atr_period, adr_period=research.adr_period) + ew = ew_features["close"].rename("set100ew_close") + set_features = price_features(set_bars, slope_lookback=research.slope_lookback, + atr_period=research.atr_period, adr_period=research.adr_period) + mco_mcsi = mcclellan(breadth, short_span=research.mco_short_span, + long_span=research.mco_long_span, z_window=research.mco_z_window) + context = set_features.join(breadth).join(mco_mcsi) + context["set100ew_close"] = ew + context["set100ew_ema21"] = ew_features["ema21_close"] + context["set100ew_trend_state"] = ew_features["trend_state"] + context["ew_trend_state"] = ew_features["trend_state"] + context = market_regimes(context, overbought_extension_atr=research.overbought_extension_atr, + repair_sigma=research.mco_repair_sigma) + context.attrs["members_by_date"] = members_by_date + return context, ew, breadth + + +def build_feature_panel(bars_by_symbol: dict[str, pd.DataFrame], set_bars: pd.DataFrame, + market: pd.DataFrame, *, rs_weights: RelativeStrengthWeights | None = None, + volume_ratio: float = 1.0, + research: ResearchSettings | None = None) -> dict[str, pd.DataFrame]: + research = research or ResearchSettings() + weights = rs_weights or RelativeStrengthWeights() + close_series = {symbol: frame["close"] for symbol, frame in bars_by_symbol.items()} + set100ew = market["set100ew_close"] + set_close = market["close"] + rs = relative_strength(close_series, set100ew, + weights={"1m": weights.one_month, "3m": weights.three_month, + "12m": weights.twelve_month, "52w": weights.high_52w}, + set_benchmark=set_close, + high_prices={symbol: frame["high"] for symbol, frame in bars_by_symbol.items()}, + members_by_date=market.attrs.get("members_by_date")) + market_columns = ["regime", "regime_reasons", "new_risk_allowed", "net_advances", "advancing", + "declining", "unchanged", "mco", "mco_mean", "mco_std", "mco_z", "mcsi", + "mcsi_10dma", "mcsi_state", "set100ew_close", "set100ew_ema21", "ew_trend_state"] + context_fields = market[[column for column in market_columns if column in market.columns]] + output: dict[str, pd.DataFrame] = {} + for symbol, bars in bars_by_symbol.items(): + features = candidate_features( + price_features(bars, slope_lookback=research.slope_lookback, + atr_period=research.atr_period, adr_period=research.adr_period), + pullback_window=research.pullback_volume_window, + tightness_window=research.tightness_window, + volume_ratio=volume_ratio, pivot_left=research.pivot_left_bars, + pivot_right=research.pivot_right_bars) + features["symbol"] = symbol + rs_columns = [f"{symbol}_{suffix}" for suffix in ( + "rs_percentile", "rs_1m", "rs_3m", "rs_12m", "rs_52w", + "rs_1m_vs_set", "rs_3m_vs_set", "rs_12m_vs_set", + "rs_1m_vs_set100ew", "rs_3m_vs_set100ew", "rs_12m_vs_set100ew", "52w_proximity_pct")] + rs_part = rs[[column for column in rs_columns if column in rs.columns]].rename( + columns={column: column.removeprefix(f"{symbol}_") for column in rs_columns if column in rs.columns}) + features = features.join(rs_part.reindex(features.index)) + features = features.join(context_fields.reindex(features.index)) + output[symbol] = features + return output diff --git a/primethai/scanner.py b/primethai/scanner.py new file mode 100644 index 0000000..6f8d3d4 --- /dev/null +++ b/primethai/scanner.py @@ -0,0 +1,173 @@ +"""Deterministic liquid-leader scanner and local-LLM handoff JSON.""" + +from __future__ import annotations + +from dataclasses import dataclass +import json +from pathlib import Path + +import numpy as np +import pandas as pd + +from .config import AppConfig, StrategyConfig +from .portfolio import size_position +from .setups import DEFAULT_SCORE_WEIGHTS, focus_score, setup_for + + +@dataclass +class ScanResult: + market_summary: dict[str, object] + candidates: pd.DataFrame + + def ai_payload(self, *, portfolio_heat: float = 0.0, portfolio_cushion: float = 0.0) -> list[dict[str, object]]: + payload = [] + for row in self.candidates.to_dict("records"): + payload.append({ + "ticker": row["ticker"], "market_regime": str(self.market_summary["regime"]).lower(), + "rs_percentile": row["rs"], "structure": str(row["structure"]).lower(), + "rs_1m": row["rs_1m"], "rs_3m": row["rs_3m"], "rs_12m": row["rs_12m"], + "rs_1m_vs_set": row["rs_1m_vs_set"], "rs_3m_vs_set": row["rs_3m_vs_set"], + "rs_12m_vs_set": row["rs_12m_vs_set"], + "rs_1m_vs_set100ew": row["rs_1m_vs_set100ew"], + "rs_3m_vs_set100ew": row["rs_3m_vs_set100ew"], + "rs_12m_vs_set100ew": row["rs_12m_vs_set100ew"], + "52w_proximity_pct": row["52w_proximity_pct"], + "distance_atr": row["atr_distance"], "higher_low": row["higher_low"], + "volume_contracting": row["volume_contracting"], "weekly_confluence": row["weekly_confluence"], + "entry_type": row["entry_type"], "portfolio_heat": portfolio_heat, + "portfolio_cushion": portfolio_cushion, "focus_score": row["score"], + "reasons": row["reasons"], "sector": row.get("sector"), + }) + return payload + + def write_json(self, path: str | Path, **portfolio_state: float) -> None: + Path(path).write_text(json.dumps(self.ai_payload(**portfolio_state), indent=2, ensure_ascii=False), encoding="utf-8") + + +def scan_candidates(features_by_symbol: dict[str, pd.DataFrame], market: pd.DataFrame, *, + config: AppConfig | None = None, top_n: int = 10, + score_weights: dict[str, float] | None = None, + sectors: dict[str, str] | None = None, + equity: float | None = None) -> ScanResult: + if top_n < 1: + raise ValueError("top_n must be positive") + config = config or AppConfig() + strategy: StrategyConfig = config.strategy + sectors = sectors or {} + weights = score_weights or config.focus_weights or DEFAULT_SCORE_WEIGHTS + if market.empty: + market_row = pd.Series(dtype=object) + date = None + else: + market_row = market.iloc[-1] + date = market.index[-1] + members_by_date = market.attrs.get("members_by_date") + active_members = (set(members_by_date.get(pd.Timestamp(date), [])) + if members_by_date is not None and date is not None else None) + regime = str(market_row.get("regime", "UNKNOWN")) + market_flag = market_row.get("new_risk_allowed", False) + market_allows_new_risk = pd.notna(market_flag) and bool(market_flag) + blocked_regimes = {"CORRECTION", "BREAKDOWN", "OVERBOUGHT"} + summary = { + "date": date.date().isoformat() if date is not None else None, + "set_trend": market_row.get("trend_state", "UNKNOWN"), + "set100ew_trend": market_row.get("ew_trend_state", "UNKNOWN"), + "mco": float(market_row["mco"]) if pd.notna(market_row.get("mco", np.nan)) else None, + "mco_z": float(market_row["mco_z"]) if pd.notna(market_row.get("mco_z", np.nan)) else None, + "mcsi_state": market_row.get("mcsi_state", "UNKNOWN"), + "breadth": {"advancing": int(market_row.get("advancing", 0) or 0), + "declining": int(market_row.get("declining", 0) or 0), + "unchanged": int(market_row.get("unchanged", 0) or 0), + "net_advances": int(market_row.get("net_advances", 0) or 0)}, + "regime": regime, "reasons": market_row.get("regime_reasons", []), + "new_risk": "ALLOWED" if market_allows_new_risk and regime not in blocked_regimes else "BLOCKED", + } + latest_rows: dict[str, pd.Series] = {} + candidate_frames: dict[str, pd.DataFrame] = {} + for symbol, frame in features_by_symbol.items(): + if active_members is not None and symbol not in active_members: + continue + if date is not None: + frame = frame.loc[frame.index <= date] + frame = frame.dropna(subset=["close"]) + if frame.empty or (date is not None and pd.Timestamp(frame.index[-1]) != pd.Timestamp(date)): + continue + latest = frame.iloc[-1] + avg_turnover = latest.get("avg_value_turnover20", np.nan) + rs = latest.get("rs_percentile", np.nan) + if pd.isna(avg_turnover) or avg_turnover < strategy.min_value_turnover_thb: + continue + if latest["close"] < strategy.min_price or pd.isna(rs): + continue + if strategy.enable_rs and rs < strategy.min_rs_percentile: + continue + latest_rows[symbol] = latest + candidate_frames[symbol] = frame + sector_scores: dict[str, float] = {} + grouped: dict[str, list[float]] = {} + for symbol, row in latest_rows.items(): + sector = sectors.get(symbol) + if sector: + grouped.setdefault(sector, []).append(float(row.get("rs_percentile", 0) or 0)) + sector_scores = {sector: float(np.mean(values)) for sector, values in grouped.items()} + rows = [] + market_blocks_new_risk = (strategy.require_market_support and strategy.enable_market_regime + and (not market_allows_new_risk or regime in blocked_regimes)) + for symbol, row in latest_rows.items(): + frame = candidate_frames[symbol] + previous = frame.iloc[-2] if len(frame) > 1 else None + prior_high = None + if len(frame) > strategy.base_breakout_window: + prior_high = float(frame["high"].iloc[-strategy.base_breakout_window - 1:-1].max()) + setup, entry, stop = setup_for( + row, previous, prior_high=prior_high, + max_extension_atr=strategy.max_extension_atr) + if market_blocks_new_risk: + setup, entry, stop = None, None, None + sector = sectors.get(symbol) + sector_strength = sector_scores.get(sector) if sector else None + score = focus_score(row, sector_strength=sector_strength, weights=weights) + proposed_entry = float(entry if entry is not None else row["close"]) + proposed_stop = float(stop if stop is not None else row.get("ema21_low", np.nan)) + risk_pct = (strategy.weakness_risk_pct if setup == "21DMA Retest" + else strategy.confirmation_risk_pct if setup else 0.0) + size = size_position(equity or strategy.initial_equity, risk_pct, proposed_entry, proposed_stop, + lot_size=config.market_rules.default_board_lot, + max_position_pct=strategy.max_position_pct, rules=config.market_rules) + entry_type = "WEAKNESS" if setup == "21DMA Retest" else "CONFIRMATION" if setup else "WATCH" + reasons = [] + if row.get("trend_state") == "UP": + reasons.append("21DMA structure rising") + if bool(row.get("higher_low", False)): + reasons.append("confirmed higher low") + if bool(row.get("volume_contracting", False)): + reasons.append("pullback volume contracting") + if row.get("weekly_trend_state") == "UP": + reasons.append("weekly structure rising") + if setup: + reasons.append(setup) + if market_blocks_new_risk: + reasons.append("market regime blocks new entries") + rows.append({ + "ticker": symbol, "score": score, "rs": round(float(row["rs_percentile"]), 2), + "rs_1m": row.get("rs_1m"), "rs_3m": row.get("rs_3m"), "rs_12m": row.get("rs_12m"), + "rs_1m_vs_set": row.get("rs_1m_vs_set"), "rs_3m_vs_set": row.get("rs_3m_vs_set"), + "rs_12m_vs_set": row.get("rs_12m_vs_set"), + "rs_1m_vs_set100ew": row.get("rs_1m_vs_set100ew"), + "rs_3m_vs_set100ew": row.get("rs_3m_vs_set100ew"), + "rs_12m_vs_set100ew": row.get("rs_12m_vs_set100ew"), + "52w_proximity_pct": row.get("52w_proximity_pct"), + "atr_distance": round(float(row.get("extension_atr", np.nan)), 2) if pd.notna(row.get("extension_atr", np.nan)) else None, + "structure": row.get("trend_state", "NEUTRAL"), "higher_low": bool(row.get("higher_low", False)), + "volume_contracting": bool(row.get("volume_contracting", False)), + "weekly_confluence": row.get("weekly_trend_state") == "UP", "setup": setup or "Watch", + "entry_type": entry_type, "entry": proposed_entry, "stop": proposed_stop, + "initial_R": proposed_entry - proposed_stop if proposed_entry > proposed_stop else None, + "risk_pct": risk_pct, "shares": int(size["shares"]), "position_value_thb": size["position_value"], + "sector": sector, "sector_strength": sector_strength, + "avg_value_turnover_thb": float(row["avg_value_turnover20"]), "reasons": reasons, + }) + candidates = pd.DataFrame(rows) + if not candidates.empty: + candidates = candidates.sort_values(["score", "rs", "avg_value_turnover_thb"], ascending=False).head(top_n).reset_index(drop=True) + return ScanResult(summary, candidates) diff --git a/primethai/setups.py b/primethai/setups.py new file mode 100644 index 0000000..fd1c77f --- /dev/null +++ b/primethai/setups.py @@ -0,0 +1,129 @@ +"""Confirmed pivots, setup candidates and transparent deterministic focus scores.""" + +from __future__ import annotations + +import numpy as np +import pandas as pd + +from .config import DEFAULT_FOCUS_WEIGHTS + +DEFAULT_SCORE_WEIGHTS = DEFAULT_FOCUS_WEIGHTS + + +def confirmed_pivots(low: pd.Series, *, left_bars: int = 3, right_bars: int = 3) -> pd.DataFrame: + """A low pivot is published only on its confirmation bar, never its pivot bar.""" + if left_bars < 1 or right_bars < 1: + raise ValueError("pivot windows must be positive") + values = low.to_numpy(dtype=float) + pivot_at_confirmation = np.full(len(values), np.nan) + last_pivot = np.full(len(values), np.nan) + higher_low = np.zeros(len(values), dtype=bool) + previous_pivot = np.nan + current_pivot = np.nan + is_higher = False + for i in range(len(values)): + candidate = i - right_bars + if candidate >= left_bars and i < len(values): + window = values[candidate - left_bars : candidate + right_bars + 1] + if np.isfinite(window).all() and values[candidate] == np.min(window): + # Equal lows are ambiguous; confirm only a unique minimum. + if int(np.sum(window == values[candidate])) == 1: + previous_pivot = current_pivot + current_pivot = values[candidate] + is_higher = np.isfinite(previous_pivot) and current_pivot > previous_pivot + pivot_at_confirmation[i] = current_pivot + if np.isfinite(current_pivot): + last_pivot[i] = current_pivot + higher_low[i] = is_higher + return pd.DataFrame({"confirmed_pivot_low": pivot_at_confirmation, + "last_confirmed_pivot_low": last_pivot, + "higher_low": higher_low}, index=low.index) + + +def candidate_features(features: pd.DataFrame, *, pullback_window: int = 5, + tightness_window: int = 10, volume_ratio: float = 1.0, + pivot_left: int = 3, pivot_right: int = 3) -> pd.DataFrame: + out = features.copy().sort_index() + pivots = confirmed_pivots(out["low"], left_bars=pivot_left, right_bars=pivot_right) + out[pivots.columns] = pivots + under_structure = out["close"] < out["ema21_high"] + pullback_volume = out["volume"].where((out["close"] < out["close"].shift(1)) | under_structure) + out["pullback_volume"] = pullback_volume.rolling(pullback_window, min_periods=1).mean() + out["volume_contracting"] = out["pullback_volume"] <= out["avg_volume20"] * volume_ratio + out["atr_contraction"] = out["atr14"] / out["atr14"].rolling(tightness_window, min_periods=max(3, tightness_window // 2)).mean().replace(0, np.nan) + rolling_range = out["high"].rolling(tightness_window, min_periods=max(3, tightness_window // 2)).max() - out["low"].rolling(tightness_window, min_periods=max(3, tightness_window // 2)).min() + out["range_contraction_pct"] = rolling_range / out["close"].replace(0, np.nan) * 100 + out["close_volatility"] = out["close"].pct_change(fill_method=None).rolling(tightness_window, min_periods=max(3, tightness_window // 2)).std(ddof=0) + def trailing_percentile(values: np.ndarray) -> float: + return float(np.mean(values <= values[-1])) + range_rank = out["range_contraction_pct"].rolling(tightness_window, min_periods=max(3, tightness_window // 2)).apply(trailing_percentile, raw=True) + volatility_rank = out["close_volatility"].rolling(tightness_window, min_periods=max(3, tightness_window // 2)).apply(trailing_percentile, raw=True) + out["tightness_score"] = ( + (1 - out["atr_contraction"].clip(0, 2) / 2) * 40 + + (1 - range_rank.fillna(.5)) * 30 + + (1 - volatility_rank.fillna(.5)) * 30 + ).clip(0, 100) + return out + + +def market_alignment_score(regime: str) -> float: + return {"CONFIRMED_UPTREND": 100, "UPTREND_PULLBACK": 100, + "EARLY_UPTREND": 75, "REPAIR": 55, "CORRECTION": 0, + "OVERBOUGHT": 20, "BREAKDOWN": 0}.get(str(regime).upper(), 0) + + +def focus_score(row: pd.Series, *, sector_strength: float | None = None, + weights: dict[str, float] | None = None) -> float: + weights = weights or DEFAULT_SCORE_WEIGHTS + extension = row.get("extension_atr", 0.0) + distance = abs(float(extension)) if pd.notna(extension) else 1.5 + rs_value = row.get("rs_percentile", 0) + tightness_value = row.get("tightness_score", 0) + values = { + "rs": float(rs_value) if pd.notna(rs_value) else 0.0, + "market_alignment": market_alignment_score(row.get("regime", "CORRECTION")), + "structure": 100.0 if row.get("trend_state") == "UP" else 50.0 if row.get("trend_state") == "NEUTRAL" else 0.0, + "higher_low": 100.0 if bool(row.get("higher_low", False)) else 0.0, + "atr_distance": float(np.clip(100 * (1 - min(distance, 1.5) / 1.5), 0, 100)), + "volume_behavior": 100.0 if bool(row.get("volume_contracting", False)) else 0.0, + "tightness": float(tightness_value) if pd.notna(tightness_value) else 0.0, + "sector_strength": sector_strength, + "weekly": 100.0 if row.get("weekly_trend_state") == "UP" else 50.0 if row.get("weekly_trend_state") == "NEUTRAL" else 0.0, + } + active = [(values.get(key), weight) for key, weight in weights.items() + if values.get(key) is not None and pd.notna(values.get(key)) and weight > 0] + if not active: + return 0.0 + total = sum(weight for _, weight in active) + return round(sum(float(value) * weight for value, weight in active) / total, 2) + + +def setup_for(row: pd.Series, previous: pd.Series | None = None, *, + prior_high: float | None = None, + max_extension_atr: float = 1.0) -> tuple[str | None, float | None, float | None]: + """Return (setup, proposed entry reference, structural stop) on known daily data.""" + close, low, high = (row.get(key, np.nan) for key in ("close", "low", "high")) + stop = row.get("ema21_low", np.nan) + if not all(pd.notna(v) for v in (close, low, high, stop)) or stop >= close: + return None, None, None + extension = row.get("extension_atr", 0.0) + if pd.notna(extension) and extension > max_extension_atr: + return None, None, None + touched = low <= row.get("ema21_high", np.inf) and close >= stop + if touched and row.get("trend_state") == "UP": + return "21DMA Retest", float(close), float(stop) + if bool(row.get("higher_low", False)) and close >= row.get("ema21_close", np.inf): + return "Higher Low", float(close), float(stop) + if previous is not None: + if prior_high is not None and pd.notna(prior_high) and close > prior_high: + return "Base Breakout", float(close), float(stop) + if close > previous.get("high", np.inf) and row.get("close", 0) > row.get("open", 0): + return "Prior High Reclaim", float(close), float(stop) + if (close > row.get("ema21_high", np.inf) + and previous.get("close", np.inf) <= previous.get("ema21_high", -np.inf)): + return "21DMA High Reclaim", float(close), float(stop) + if row.get("open", np.nan) < previous.get("close", np.inf) and close > previous.get("close", np.inf): + return "Red-to-Green", float(close), float(stop) + if close > row.get("open", np.inf) and previous.get("close", -np.inf) < previous.get("open", np.inf): + return "Daily Reversal", float(close), float(stop) + return None, None, None diff --git a/primethai/synthetic.py b/primethai/synthetic.py new file mode 100644 index 0000000..0fe2600 --- /dev/null +++ b/primethai/synthetic.py @@ -0,0 +1,42 @@ +"""Reproducible synthetic bars for offline pipeline checks; never market evidence.""" + +from __future__ import annotations + +import numpy as np +import pandas as pd + + +SMOKE_SYMBOLS = ["ADVANC", "AOT", "CPALL", "DELTA", "PTT", "KBANK", "KTB", "TRUE", "GULF", "SCB"] + + +def synthetic_bars(*, seed: int = 19, sessions: int = 650) -> tuple[dict[str, pd.DataFrame], pd.DataFrame]: + rng = np.random.default_rng(seed) + dates = pd.bdate_range("2023-01-02", periods=sessions) + market_regimes = np.full(sessions, 0.0002) + market_regimes[140:175] = -0.0011 + market_regimes[330:355] = -0.0008 + set_returns = market_regimes + rng.normal(0, .006, sessions) + set_close = 1450 * np.cumprod(1 + set_returns) + index = pd.DataFrame({"open": np.r_[set_close[0], set_close[:-1]] * (1 + rng.normal(0, .001, sessions)), + "high": set_close * (1 + rng.uniform(.001, .008, sessions)), + "low": set_close * (1 - rng.uniform(.001, .008, sessions)), + "close": set_close, "adj_close": set_close, + "volume": rng.integers(100_000_000, 180_000_000, sessions), + "dividends": 0.0, "stock_splits": 0.0}, index=dates) + index["high"] = index[["open", "high", "close"]].max(axis=1) + index["low"] = index[["open", "low", "close"]].min(axis=1) + bars: dict[str, pd.DataFrame] = {} + for i, symbol in enumerate(SMOKE_SYMBOLS): + beta = .65 + i * .08 + drift = .00015 + i * .000035 + returns = drift + beta * (set_returns - .0002) + rng.normal(0, .009 + i * .0002, sessions) + returns[140:175] -= .0007 + close = (10 + i * 2.5) * np.cumprod(1 + returns) + open_price = np.r_[close[0], close[:-1]] * (1 + rng.normal(0, .0025, sessions)) + high = np.maximum(open_price, close) * (1 + rng.uniform(.001, .018, sessions)) + low = np.minimum(open_price, close) * (1 - rng.uniform(.001, .018, sessions)) + volume = rng.integers(1_500_000, 2_500_000, sessions) + bars[symbol] = pd.DataFrame({"open": open_price, "high": high, "low": low, + "close": close, "adj_close": close, "volume": volume, + "dividends": 0.0, "stock_splits": 0.0}, index=dates) + return bars, index diff --git a/primethai/universe.py b/primethai/universe.py new file mode 100644 index 0000000..8ccbde1 --- /dev/null +++ b/primethai/universe.py @@ -0,0 +1,113 @@ +"""Dated index membership. No current constituent list is projected backward.""" + +from __future__ import annotations + +from dataclasses import dataclass +from pathlib import Path +import warnings + +import pandas as pd + + +class MembershipError(ValueError): + pass + + +@dataclass(frozen=True) +class MembershipPeriod: + symbol: str + start: pd.Timestamp + end: pd.Timestamp + sector: str | None = None + + +class PointInTimeUniverse: + def __init__(self, periods: list[MembershipPeriod], name: str = "SET100") -> None: + self.periods = periods + self.name = name + if not periods: + raise MembershipError("membership file contains no rows") + grouped: dict[str, list[MembershipPeriod]] = {} + for row in periods: + grouped.setdefault(row.symbol, []).append(row) + for symbol, rows in grouped.items(): + ordered = sorted(rows, key=lambda item: item.start) + for previous, current in zip(ordered, ordered[1:]): + if current.start <= previous.end: + raise MembershipError(f"overlapping membership dates for {symbol}") + + @classmethod + def from_csv(cls, path: str | Path, *, name: str = "SET100") -> "PointInTimeUniverse": + frame = pd.read_csv(path, dtype=str).fillna("") + required = {"symbol", "start_date", "end_date"} + missing = required - set(frame.columns) + if missing: + raise MembershipError(f"membership CSV missing columns: {', '.join(sorted(missing))}") + rows: list[MembershipPeriod] = [] + for item in frame.to_dict("records"): + symbol = str(item.get("symbol", "")).strip().upper() + if not symbol or not str(item["start_date"]).strip() or not str(item["end_date"]).strip(): + raise MembershipError("membership rows require symbol, start_date and end_date") + start, end = pd.Timestamp(item["start_date"]), pd.Timestamp(item["end_date"]) + if pd.isna(start) or pd.isna(end): + raise MembershipError(f"invalid membership date for {symbol}") + if end < start: + raise MembershipError(f"end_date before start_date for {symbol}") + rows.append(MembershipPeriod(symbol, start, end, + str(item.get("sector", "")).strip() or None)) + return cls(rows, name=name) + + def members(self, on_date: str | pd.Timestamp) -> list[str]: + when = pd.Timestamp(on_date) + return sorted({row.symbol for row in self.periods if row.start <= when <= row.end}) + + def sector(self, symbol: str, on_date: str | pd.Timestamp) -> str | None: + when = pd.Timestamp(on_date) + for row in self.periods: + if row.symbol == symbol.upper() and row.start <= when <= row.end: + return row.sector + return None + + def covers(self, dates: pd.Index | list[pd.Timestamp]) -> bool: + return all(bool(self.members(date)) for date in dates) + + def require_coverage(self, dates: pd.Index | list[pd.Timestamp]) -> None: + missing = [pd.Timestamp(date) for date in dates if not self.members(date)] + if missing: + raise MembershipError(f"{self.name} membership does not cover {len(missing)} sessions ({missing[0].date()} to {missing[-1].date()}); accurate backtests require dated constituents for every session") + + def members_by_date(self, dates: pd.Index | list[pd.Timestamp]) -> dict[pd.Timestamp, list[str]]: + self.require_coverage(dates) + return {pd.Timestamp(date): self.members(date) for date in dates} + + +def resolve_universe(name: str, *, membership_file: str | Path | None = None, + symbols: list[str] | None = None, mode: str = "fast", + dates: pd.Index | list[pd.Timestamp] | None = None) -> tuple[list[str] | None, PointInTimeUniverse | None]: + mode = mode.lower() + if mode not in {"fast", "accurate"}: + raise ValueError("mode must be 'fast' or 'accurate'") + if name.lower() == "custom": + if not symbols: + raise ValueError("custom universe requires --symbols") + if mode == "accurate": + raise ValueError("custom accurate mode requires a dated membership CSV") + warnings.warn("SURVIVORSHIP BIAS: custom/current symbols are not historical constituents.", RuntimeWarning) + return sorted({s.upper().removesuffix(".BK") for s in symbols}), None + if membership_file is None: + raise MembershipError(f"{name} needs a constituent CSV. Supply --membership-file; never infer historical membership from today's list.") + universe = PointInTimeUniverse.from_csv(membership_file, name=name.upper()) + if mode == "accurate": + if dates is not None: + universe.require_coverage(dates) + return None, universe + warnings.warn(f"SURVIVORSHIP BIAS: {name} fast mode uses the supplied constituent snapshot for research only.", RuntimeWarning) + if dates is not None and not universe.covers(dates): + warnings.warn("Membership snapshot does not span every requested date; fast-mode historical breadth is incomplete.", RuntimeWarning) + anchor = pd.Timestamp(dates[-1]) if dates is not None and len(dates) else pd.Timestamp.now().normalize() + members = universe.members(anchor) + if not members: + latest = max(row.end for row in universe.periods) + members = universe.members(latest) + warnings.warn(f"No members are effective on {anchor.date()}; using the latest supplied snapshot ending {latest.date()}.", RuntimeWarning) + return members, universe diff --git a/primethai/webui.html b/primethai/webui.html new file mode 100644 index 0000000..07da70c --- /dev/null +++ b/primethai/webui.html @@ -0,0 +1,260 @@ + + + + + + PrimeThai · พอร์ตลงทุน + + + +
+
+
+
PrimeThai · Portfolio
+

พอร์ตลงทุน

+

เป้าหมายจาก Donchian 55/20 + EMA 50/200 · บันทึกเงินและรายการซื้อขายไว้ในเครื่อง

+
+ +
+ +
กดบันทึกจากสัญญาณเพื่อปรับสมุดพอร์ตด้วยราคาอ้างอิง แล้วแก้เป็นจำนวน/ราคาที่ได้จริงภายหลัง · ไม่มีการส่งคำสั่งไปโบรกเกอร์
+ +
+
มูลค่าพอร์ต ณ ราคาปิด
—
กำลังอ่านข้อมูลราคา
+
เงินสดคงเหลือ
—
เงินที่บันทึกไว้หลังรายการที่กรอก
+
เงินลงทุนสุทธิ
—
เงินเพิ่มเข้าพอร์ต หักเงินถอน
+
กำไร/ขาดทุนที่ประเมิน
—
คำนวณจากราคาปิดล่าสุด
+
+ +
+
+

หุ้นที่ถือและเป้าหมายของระบบ

จำนวนเป้าหมายคำนวณตามน้ำหนักโมเดลและมูลค่าพอร์ต · ราคา limit อ้างอิงเท่ากับราคาปิดล่าสุด

+ กำลังโหลดบัญชี +
+
+ + +
หุ้นโมเดลถืออยู่เป้าหมายปรับพอร์ตราคาปิดราคาอ้างอิงจำนวนเปลี่ยนบันทึก
กำลังอ่านสัญญาณและพอร์ต…
+
+
มูลค่าซื้อที่ระบบเสนอ—
+
มูลค่าขายที่ระบบเสนอ—
+
เงินสดหลังรายการขาย/ซื้อเป้าหมาย—
+
+
ประมาณการก่อนค่าธรรมเนียมและเงื่อนไขการใช้เงินจากยอดขายของโบรกเกอร์
+
+
+ +
+
+

เพิ่มเงินลงทุน

ครั้งแรกจะเริ่มพอร์ต; ครั้งต่อไปเพิ่มเงินสดและคำนวณเป้าหมายใหม่

+
+
+ + +
+
+
+
หลังเพิ่มเงิน กดบันทึกซื้อ/ขายในตารางเพื่อปรับสมุดพอร์ตตามแผน ระบบเริ่มด้วยราคาปิดล่าสุดและแก้เป็นราคาที่ได้จริงภายหลังได้
+
+
+ +
+

ประวัติเงินและรายการล่าสุด

ข้อมูลบัญชีเก็บไว้ใน SQLite ภายในโฟลเดอร์โครงการและไม่เชื่อมบัญชีโบรกเกอร์

สูงสุด 10 รายการ
+
+ + +
เวลารายการหุ้นจำนวนราคา/เงินค่าธรรมเนียมจัดการ
ยังไม่มีรายการ
+ +
+
+ + + +
+
+
+
เงินปันผลและเงินถอนจะปรับเงินสดและผลกำไร/ขาดทุนเมื่อบันทึกรายการนี้
+
+ +
+

ข้อมูลและการอัปเดต

เซิร์ฟเวอร์ดึง SiamChart EOD และคำนวณสัญญาณให้อัตโนมัติทุกวันจันทร์–ศุกร์หลัง 20:30 น. เวลาไทย หาก SiamChart ใช้ไม่ได้จะตรวจข้อมูลสำรองจาก Yahoo Finance · ราคาปิดไม่ใช่ราคา real-time

+
กำลังตรวจแหล่งข้อมูล
+
+
+ +
ระบบดึงข้อมูลและคำนวณสัญญาณอัตโนมัติ แต่คำสั่งซื้อขายยังต้องทำกับโบรกเกอร์เอง · ราคาอ้างอิงเป็นราคาปิด ไม่ใช่ real-time · ตรวจราคาและกำลังซื้อก่อนซื้อขาย
+
+ + + diff --git a/primethai/webui.py b/primethai/webui.py new file mode 100644 index 0000000..47eecbe --- /dev/null +++ b/primethai/webui.py @@ -0,0 +1,915 @@ +"""Local portfolio dashboard for the primary Thai equity research models.""" + +from __future__ import annotations + +from datetime import datetime, timedelta, timezone +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from http.cookies import SimpleCookie +import csv +import hmac +import json +import os +from pathlib import Path +from urllib.parse import parse_qs, urlencode, urlsplit +import re +import threading +import time +from zoneinfo import ZoneInfo +from urllib.request import Request, urlopen +import zipfile + +import pandas as pd + +from .auth import ( + COOKIE_NAME, + AuthConfig, + create_session, + expired_session_cookie, + load_auth_config, + session_cookie, + verify_session, +) +from .market_data_store import archive_collection_summary, archive_summary, merge_daily_entries +from .model_lab import run_model_lab +from .portfolio_book import ( + DEFAULT_BOOK, + add_cash_event, + portfolio_view, + read_latest_eod_quotes, + record_trade, + update_trade, + update_price_marks, + void_trade, +) +from .yahoo_backup import YahooBackupError, fetch_yahoo_eod_backup + + +DEFAULT_ARCHIVES = ( + Path("data/raw/siamchart/set-archive_EOD_1970-LAST.zip"), + Path("data/raw/siamchart/set-archive_EOD.zip"), + Path("data/raw/siamchart/set-archive_EOD_LAST.zip"), +) +DEFAULT_REPORT_DIR = Path("reports/primary_union_current") +DEFAULT_START = "2026-01-01" +LATEST_ARCHIVE_URL = "https://siamchart.com/downloads/set-archive_EOD_LAST.zip" +LATEST_FETCH_STATE = Path("data/raw/siamchart/set-archive_EOD_LAST.fetch.json") +BACKUP_FETCH_STATE = Path("data/raw/siamchart/set-archive_EOD_LAST.backup.json") +MAX_LATEST_ARCHIVE_BYTES = 10_000_000 +THAILAND_TZ = ZoneInfo("Asia/Bangkok") +AUTO_REFRESH_HOUR = 20 +AUTO_REFRESH_MINUTE = 30 +FETCH_RETRY_MINUTES = 30 +_EOD_DATE = re.compile(r"set-history_EOD_(\d{4}-\d{2}-\d{2})\.csv$", re.IGNORECASE) +_STATE_LOCK = threading.Lock() +_LOGIN_LOCK = threading.Lock() +_LOGIN_FAILURES: dict[str, tuple[int, float, float]] = {} +_LOGIN_MAX_FAILURES = 5 +_LOGIN_WINDOW_SECONDS = 15 * 60 +_STATE: dict[str, object] = { + "running": False, + "started_at": None, + "completed_at": None, + "error": None, + "data_as_of": None, + "download_status": "ยังไม่อัปเดตข้อมูล", +} + + +def _json_default(value: object) -> object: + if isinstance(value, (pd.Timestamp, datetime)): + return value.isoformat() + if hasattr(value, "item"): + return value.item() # type: ignore[no-any-return] + return str(value) + + +def _login_block_seconds(client: str) -> int: + now = time.time() + with _LOGIN_LOCK: + failures, started_at, blocked_until = _LOGIN_FAILURES.get(client, (0, now, 0.0)) + if blocked_until > now: + return int(blocked_until - now) + 1 + if now - started_at >= _LOGIN_WINDOW_SECONDS: + _LOGIN_FAILURES.pop(client, None) + return 0 + + +def _record_login_failure(client: str) -> None: + now = time.time() + with _LOGIN_LOCK: + if len(_LOGIN_FAILURES) > 4096: + for address, (_, _, blocked_until) in list(_LOGIN_FAILURES.items()): + if blocked_until <= now: + _LOGIN_FAILURES.pop(address, None) + failures, started_at, blocked_until = _LOGIN_FAILURES.get(client, (0, now, 0.0)) + if now - started_at >= _LOGIN_WINDOW_SECONDS: + failures, started_at, blocked_until = 0, now, 0.0 + failures += 1 + if failures >= _LOGIN_MAX_FAILURES: + blocked_until = now + _LOGIN_WINDOW_SECONDS + _LOGIN_FAILURES[client] = (failures, started_at, blocked_until) + + +def _clear_login_failures(client: str) -> None: + with _LOGIN_LOCK: + _LOGIN_FAILURES.pop(client, None) + + +def _read_json(path: Path) -> dict[str, object] | None: + try: + value = json.loads(path.read_text(encoding="utf-8")) + except (FileNotFoundError, json.JSONDecodeError, OSError): + return None + return value if isinstance(value, dict) else None + + +def _read_csv(path: Path) -> list[dict[str, str]]: + try: + with path.open("r", newline="", encoding="utf-8-sig") as handle: + return list(csv.DictReader(handle)) + except (FileNotFoundError, OSError, csv.Error): + return [] + + +def _report_data(report_dir: Path, initial_equity: float, + archives: tuple[Path, ...] = DEFAULT_ARCHIVES) -> dict[str, object]: + union_dir = report_dir / "primary_union_donchian_ema" + base = _read_json(union_dir / "base_cost/board_lot_100/metrics.json") + stress = _read_json(union_dir / "cost_stress/board_lot_100/metrics.json") + notes = _read_json(report_dir / "data_notes.json") + signal_rows = _read_csv(union_dir / "next_session_signals.csv") + curve_rows = _read_csv(union_dir / "base_cost/board_lot_100/equity_curve.csv") + equity_curve: list[dict[str, object]] = [] + if base: + start = str(base.get("start", "")) + end = str(base.get("end", "")) + for row in curve_rows: + if start <= row.get("date", "") <= end: + try: + equity_curve.append({"date": row["date"], "equity": float(row["equity"])}) + except (KeyError, ValueError): + continue + with _STATE_LOCK: + refresh = dict(_STATE) + source_files = notes.get("archive_files", []) if notes else [] + return { + "ready": base is not None, + "model": "Donchian 55/20 + EMA 50/200", + "combination_rule": "ใช้สัญญาณจากโมเดลใดโมเดลหนึ่ง; หุ้นที่ซ้ำกันนับครั้งเดียว; กระจายน้ำหนักเท่ากัน สูงสุด 20 หุ้น", + "execution_status": "Paper/manual review only — ไม่มีการเชื่อมต่อโบรกเกอร์หรือส่งคำสั่ง", + "initial_equity": initial_equity, + "base": base, + "stress": stress, + "signals": signal_rows, + "equity_curve": equity_curve, + "archive_files": source_files, + "archive_issues": notes.get("archive_read_issues", []) if notes else [], + "evaluation_start": notes.get("evaluation_start") if notes else None, + "evaluation_end_exclusive": notes.get("evaluation_end_exclusive") if notes else None, + "refresh": refresh, + "automatic_download": _read_json(LATEST_FETCH_STATE) or {}, + "backup_download": _read_json(BACKUP_FETCH_STATE) or {}, + "price_history": archive_collection_summary(archives), + "data_as_of": refresh.get("data_as_of") or (signal_rows[0].get("signal_as_of") if signal_rows else None), + "download_status": refresh.get("download_status"), + "report_dir": str(report_dir), + } + + +def _portfolio_data(archives: tuple[Path, ...], report_dir: Path) -> dict[str, object]: + union_dir = report_dir / "primary_union_donchian_ema" + signals = _read_csv(union_dir / "next_session_signals.csv") + base_metrics = _read_json(union_dir / "base_cost/board_lot_100/metrics.json") or {} + signal_as_of = (signals[0].get("signal_as_of") if signals else + str(base_metrics.get("end", "")) or None) + quote_error = None + price_as_of = None + daily_archive = _daily_archive_destination(archives) + if daily_archive is not None: + try: + price_as_of, quotes = read_latest_eod_quotes(daily_archive) + update_price_marks(price_as_of, quotes, database=DEFAULT_BOOK) + except Exception as exc: + quote_error = f"{type(exc).__name__}: {exc}" + result = portfolio_view(signals, signal_as_of=signal_as_of, database=DEFAULT_BOOK) + with _STATE_LOCK: + refresh = dict(_STATE) + result.update({ + "signals_ready": (report_dir / "data_notes.json").is_file(), + "signal_as_of": signal_as_of, + "quote_archive_as_of": price_as_of, + "quote_update_error": quote_error, + "refresh": refresh, + "automatic_download": _read_json(LATEST_FETCH_STATE) or {}, + "backup_download": _read_json(BACKUP_FETCH_STATE) or {}, + "price_history": archive_collection_summary(archives), + "ledger_path": str(DEFAULT_BOOK), + "strategy": "Donchian 55/20 + EMA 50/200", + }) + return result + + +def _latest_archive_date(archives: tuple[Path, ...]) -> pd.Timestamp: + latest: pd.Timestamp | None = None + for archive in archives: + if not archive.is_file(): + raise FileNotFoundError(f"ไม่พบไฟล์ข้อมูล: {archive}") + with zipfile.ZipFile(archive) as bundle: + for item in bundle.namelist(): + match = _EOD_DATE.search(Path(item).name) + if match: + date = pd.Timestamp(match.group(1)) + if latest is None or date > latest: + latest = date + if latest is None: + raise ValueError("ไม่พบไฟล์ EOD รายวันใน SiamChart ZIP") + return latest + + +def _today_local() -> str: + return datetime.now(THAILAND_TZ).date().isoformat() + + +def _last_fetch_attempt_date() -> str | None: + state = _read_json(LATEST_FETCH_STATE) + value = state.get("last_attempt_date") if state else None + return str(value) if value else None + + +def _daily_archive_destination(archives: tuple[Path, ...]) -> Path | None: + return next((path for path in archives + if path.name.casefold() == "set-archive_eod_last.zip"), None) + + +def _fetch_latest_archive(destination: Path) -> str: + """Fetch SiamChart EOD and merge every session into the durable local archive.""" + now = datetime.now(THAILAND_TZ) + today = now.date().isoformat() + scheduled_at = now.replace(hour=AUTO_REFRESH_HOUR, minute=AUTO_REFRESH_MINUTE, + second=0, microsecond=0) + after_close = now >= scheduled_at + previous = _read_json(LATEST_FETCH_STATE) or {} + if previous.get("last_attempt_date") == today: + try: + attempted_at = datetime.fromisoformat(str(previous.get("attempted_at", ""))) + if attempted_at.tzinfo is None: + attempted_at = attempted_at.replace(tzinfo=THAILAND_TZ) + if previous.get("result") != "failed" and bool(previous.get("after_close")): + return f"วันนี้ตรวจ SiamChart แล้ว · ข้อมูลถึง {previous.get('downloaded_eod_date', 'วันที่เดิม')}" + if now - attempted_at < timedelta(minutes=FETCH_RETRY_MINUTES): + if previous.get("result") == "failed": + return "SiamChart ยังเชื่อมต่อไม่ได้; ระบบจะลองดึงข้อมูลใหม่อัตโนมัติ" + except (TypeError, ValueError): + pass + + destination.parent.mkdir(parents=True, exist_ok=True) + temporary = destination.with_name(destination.name + ".download") + state: dict[str, object] = { + "last_attempt_date": today, + "attempted_at": now.isoformat(), + "after_close": after_close, + "url": LATEST_ARCHIVE_URL, + } + try: + request = Request( + LATEST_ARCHIVE_URL, + headers={"User-Agent": "PrimeThai/0.1 daily EOD refresh"}, + ) + with urlopen(request, timeout=30) as response: + payload = response.read(MAX_LATEST_ARCHIVE_BYTES + 1) + if not payload or len(payload) > MAX_LATEST_ARCHIVE_BYTES: + raise ValueError("ไฟล์ที่ดาวน์โหลดมีขนาดไม่ถูกต้อง") + temporary.write_bytes(payload) + incoming: dict[str, bytes] = {} + with zipfile.ZipFile(temporary) as bundle: + if bundle.testzip() is not None: + raise ValueError("ไฟล์ ZIP ตรวจสอบความสมบูรณ์ไม่ผ่าน") + for name in bundle.namelist(): + match = _EOD_DATE.search(Path(name).name) + if match: + incoming[match.group(1)] = bundle.read(name) + if not incoming: + raise ValueError("ZIP ไม่มีไฟล์ SiamChart EOD รายวัน") + downloaded_date = max(pd.Timestamp(day) for day in incoming) + existing_summary = archive_summary(destination) + existing_date = pd.Timestamp(str(existing_summary["last_date"])) if existing_summary.get("last_date") else None + stored = merge_daily_entries( + destination, + incoming, + source="SiamChart", + fetched_at=now.isoformat(), + ) + stored_date = pd.Timestamp(str(stored["last_date"])) + result = "source_older_than_local" if existing_date is not None and downloaded_date < existing_date else "downloaded" + state.update({ + "result": result, + "downloaded_eod_date": downloaded_date.date().isoformat(), + "stored_eod_date": stored_date.date().isoformat(), + "stored_sessions": stored["sessions"], + }) + if result == "source_older_than_local": + return (f"SiamChart ส่งข้อมูลถึง {downloaded_date.date()} · " + f"เก็บข้อมูลย้อนหลังไว้ถึง {stored_date.date()}") + return (f"ดึง SiamChart EOD สำเร็จ · เก็บข้อมูลย้อนหลัง {stored['sessions']} วัน " + f"ถึง {stored_date.date()}") + except Exception as exc: + state.update({"result": "failed", "error": f"{type(exc).__name__}: {exc}"}) + return "เชื่อมต่อ SiamChart ไม่สำเร็จ; คงข้อมูลเดิมไว้และจะลองใหม่อัตโนมัติ" + finally: + try: + temporary.unlink(missing_ok=True) + except OSError: + pass + LATEST_FETCH_STATE.parent.mkdir(parents=True, exist_ok=True) + LATEST_FETCH_STATE.write_text(json.dumps(state, ensure_ascii=False, indent=2), encoding="utf-8") + + +def _required_backup_symbols(archives: tuple[Path, ...], report_dir: Path) -> list[str]: + union_dir = report_dir / "primary_union_donchian_ema" + signals = _read_csv(union_dir / "next_session_signals.csv") + signal_as_of = signals[0].get("signal_as_of") if signals else None + view = portfolio_view(signals, signal_as_of=signal_as_of, database=DEFAULT_BOOK) + return sorted({ + str(row.get("symbol", "")).strip().upper() + for row in view.get("rows", []) + if float(row.get("target_weight", 0) or 0) > 0 + or int(row.get("quantity_held", 0) or 0) > 0 + }) + + +def _write_fetch_state(path: Path, state: dict[str, object]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + temporary = path.with_name(path.name + ".tmp") + temporary.write_text(json.dumps(state, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temporary, path) + + +def _fetch_yahoo_backup(archives: tuple[Path, ...], report_dir: Path, + destination: Path) -> str: + """Use Yahoo only when the after-close primary feed did not advance history.""" + now = datetime.now(THAILAND_TZ) + today = now.date().isoformat() + previous = _read_json(BACKUP_FETCH_STATE) or {} + if previous.get("last_attempt_date") == today: + try: + attempted_at = datetime.fromisoformat(str(previous.get("attempted_at", ""))) + if attempted_at.tzinfo is None: + attempted_at = attempted_at.replace(tzinfo=THAILAND_TZ) + if previous.get("result") == "downloaded": + current = _latest_archive_date(archives).date().isoformat() + if str(previous.get("downloaded_through", "")) >= current: + return ("SiamChart ยังใช้ไม่ได้ · ใช้ข้อมูล Yahoo ที่เก็บไว้ถึง " + f"{previous.get('downloaded_through')}") + if now - attempted_at < timedelta(minutes=FETCH_RETRY_MINUTES): + return "SiamChart ยังใช้ไม่ได้ · รอรอบตรวจแหล่งสำรองถัดไป" + except (TypeError, ValueError): + pass + + state: dict[str, object] = { + "last_attempt_date": today, + "attempted_at": now.isoformat(), + "provider": "Yahoo Finance via yfinance", + } + try: + latest = _latest_archive_date(archives).date().isoformat() + entries, metadata = fetch_yahoo_eod_backup( + [str(path) for path in archives], + latest_date=latest, + end_date=now.date(), + required_symbols=_required_backup_symbols(archives, report_dir), + ) + if not entries: + raise YahooBackupError("แหล่งสำรองไม่มี session ใหม่") + stored = merge_daily_entries( + destination, + entries, + source="Yahoo Finance via yfinance", + fetched_at=now.isoformat(), + metadata_by_date=metadata, + ) + last_day = max(entries) + latest_meta = metadata[last_day] + state.update({ + "result": "downloaded", + "downloaded_from": min(entries), + "downloaded_through": last_day, + "sessions": len(entries), + "stored_sessions": stored["sessions"], + "liquid_universe_coverage": latest_meta.get("liquid_universe_coverage"), + "liquid_universe_present": latest_meta.get("liquid_universe_present"), + "liquid_universe_expected": latest_meta.get("liquid_universe_expected"), + }) + return ("SiamChart ยังใช้ไม่ได้ · ใช้ Yahoo Finance สำรองผ่านตรวจ coverage " + f"{float(latest_meta['liquid_universe_coverage']):.0%} · " + f"เก็บข้อมูลถึง {last_day}") + except Exception as exc: + message = str(exc) + no_new_session = "ยังไม่มีข้อมูล SET หลังวันที่เก็บไว้ล่าสุด" in message + state.update({"result": "no_new_session" if no_new_session else "failed", + "error": f"{type(exc).__name__}: {exc}"}) + if no_new_session: + return "ยังไม่พบ session ใหม่จาก SiamChart หรือ Yahoo Finance; คงข้อมูลล่าสุดที่บันทึกไว้" + return "SiamChart และ Yahoo Finance ยังใช้ไม่ได้; คงข้อมูลราคาที่บันทึกไว้และจะลองใหม่อัตโนมัติ" + finally: + _write_fetch_state(BACKUP_FETCH_STATE, state) + + +def _refresh_worker(archives: tuple[Path, ...], report_dir: Path, start: str, + initial_equity: float, fetch_latest: bool = True) -> None: + try: + destination = _daily_archive_destination(archives) + archive_mtime_before = (destination.stat().st_mtime_ns + if destination is not None and destination.is_file() else None) + if fetch_latest and destination is not None: + download_status = _fetch_latest_archive(destination) + primary_state = _read_json(LATEST_FETCH_STATE) or {} + now = datetime.now(THAILAND_TZ) + scheduled_at = now.replace(hour=AUTO_REFRESH_HOUR, minute=AUTO_REFRESH_MINUTE, + second=0, microsecond=0) + primary_has_current_close = ( + primary_state.get("result") == "downloaded" + and bool(primary_state.get("after_close")) + and str(primary_state.get("stored_eod_date", "")) >= today + ) + needs_backup = ( + now.weekday() < 5 + and now >= scheduled_at + and not primary_has_current_close + ) + if needs_backup: + download_status = _fetch_yahoo_backup(archives, report_dir, destination) + elif fetch_latest: + download_status = "ชุดไฟล์นี้ไม่มี ZIP EOD รายวันสำหรับอัปเดต; คำนวณจากไฟล์ที่กำหนด" + else: + download_status = "คำนวณจากไฟล์ EOD ในเครื่อง" + latest = _latest_archive_date(archives) + archive_mtime_after = (destination.stat().st_mtime_ns + if destination is not None and destination.is_file() else None) + reports_exist = (report_dir / "data_notes.json").is_file() + if archive_mtime_before == archive_mtime_after and reports_exist: + with _STATE_LOCK: + _STATE.update({"running": False, "completed_at": datetime.now(timezone.utc).isoformat(), + "error": None, "data_as_of": latest.date().isoformat(), + "download_status": download_status}) + return + end = (latest + pd.Timedelta(days=1)).date().isoformat() + run_model_lab( + [str(path) for path in archives], start=start, end=end, + output_dir=report_dir, action_dir=Path("data/raw/actions"), + set_index_csv=Path("data/raw/^SET.csv"), + set_reference=Path("data/reference/set_actions_2025_2026_h1.csv"), + initial_equity=initial_equity, + ) + with _STATE_LOCK: + _STATE.update({"running": False, "completed_at": datetime.now(timezone.utc).isoformat(), + "error": None, "data_as_of": latest.date().isoformat(), + "download_status": download_status}) + except Exception as exc: # The UI reports local data or calculation errors to the user. + with _STATE_LOCK: + _STATE.update({"running": False, "completed_at": datetime.now(timezone.utc).isoformat(), + "error": f"{type(exc).__name__}: {exc}"}) + + +def _start_refresh(archives: tuple[Path, ...], report_dir: Path, start: str, + initial_equity: float, *, fetch_latest: bool = True) -> tuple[bool, str]: + with _STATE_LOCK: + if bool(_STATE["running"]): + return False, "กำลังคำนวณข้อมูลชุดนี้อยู่แล้ว" + _STATE.update({"running": True, "started_at": datetime.now(timezone.utc).isoformat(), + "completed_at": None, "error": None}) + worker = threading.Thread( + target=_refresh_worker, + args=(archives, report_dir, start, initial_equity, fetch_latest), + name="primethai-model-refresh", + daemon=True, + ) + worker.start() + return True, "เริ่มตรวจข้อมูลล่าสุดและคำนวณสัญญาณแล้ว" + + +def _daily_refresh_loop(archives: tuple[Path, ...], report_dir: Path, start: str, + initial_equity: float) -> None: + """Refresh from SiamChart each Thai trading weekday after the daily EOD release.""" + while True: + now = datetime.now(THAILAND_TZ) + scheduled_today = now.replace(hour=AUTO_REFRESH_HOUR, minute=AUTO_REFRESH_MINUTE, + second=0, microsecond=0) + previous = _read_json(LATEST_FETCH_STATE) or {} + if previous.get("result") == "failed": + try: + attempted_at = datetime.fromisoformat(str(previous.get("attempted_at", ""))) + if attempted_at.tzinfo is None: + attempted_at = attempted_at.replace(tzinfo=THAILAND_TZ) + next_run = max(now, attempted_at + timedelta(minutes=FETCH_RETRY_MINUTES)) + except (TypeError, ValueError): + next_run = now + elif now < scheduled_today: + next_run = scheduled_today + elif (previous.get("last_attempt_date") == now.date().isoformat() + and bool(previous.get("after_close")) and previous.get("result") != "failed"): + next_run = scheduled_today + timedelta(days=1) + else: + next_run = now + time.sleep(max(0.0, (next_run - now).total_seconds())) + run_date = datetime.now(THAILAND_TZ).date() + previous = _read_json(LATEST_FETCH_STATE) or {} + if run_date.weekday() >= 5 and previous.get("result") != "failed": + continue + if (previous.get("last_attempt_date") == run_date.isoformat() + and bool(previous.get("after_close")) and previous.get("result") != "failed"): + continue + while datetime.now(THAILAND_TZ).date() == run_date: + started, message = _start_refresh(archives, report_dir, start, initial_equity) + if not started: + print("[daily-refresh] another refresh is running; waiting") + while True: + with _STATE_LOCK: + running = bool(_STATE["running"]) + if not running: + break + time.sleep(2) + current_fetch = _read_json(LATEST_FETCH_STATE) or {} + if (current_fetch.get("last_attempt_date") == run_date.isoformat() + and bool(current_fetch.get("after_close")) + and current_fetch.get("result") != "failed"): + break + continue + print("[daily-refresh] scheduled refresh started") + while True: + with _STATE_LOCK: + running = bool(_STATE["running"]) + if not running: + break + time.sleep(2) + fetch_state = _read_json(LATEST_FETCH_STATE) or {} + backup_state = _read_json(BACKUP_FETCH_STATE) or {} + with _STATE_LOCK: + refresh_error = _STATE["error"] + primary_ok = ( + fetch_state.get("result") == "downloaded" + and bool(fetch_state.get("after_close")) + and str(fetch_state.get("stored_eod_date", "")) >= run_date.isoformat() + ) + backup_ok = ( + backup_state.get("result") == "downloaded" + and backup_state.get("last_attempt_date") == run_date.isoformat() + and str(backup_state.get("downloaded_through", "")) + >= _latest_archive_date(archives).date().isoformat() + ) + both_sources_confirm_no_new_session = ( + backup_state.get("result") == "no_new_session" + and backup_state.get("last_attempt_date") == run_date.isoformat() + and fetch_state.get("result") == "downloaded" + and bool(fetch_state.get("after_close")) + ) + if (primary_ok or backup_ok or both_sources_confirm_no_new_session) and not refresh_error: + break + retry_at = datetime.now(THAILAND_TZ) + timedelta(minutes=FETCH_RETRY_MINUTES) + if retry_at.date() != run_date: + break + print(f"[daily-refresh] source update failed; retrying around {retry_at:%H:%M} BKK") + time.sleep(FETCH_RETRY_MINUTES * 60) + + +def _handler_factory(archives: tuple[Path, ...], report_dir: Path, + start: str, initial_equity: float, + auth: AuthConfig) -> type[BaseHTTPRequestHandler]: + class DashboardHandler(BaseHTTPRequestHandler): + server_version = "PrimeThaiPortfolio/1.0" + + def _request_path(self) -> str: + return urlsplit(self.path).path + + def _secure_request(self) -> bool: + if auth.cookie_secure == "true": + return True + if auth.cookie_secure == "false": + return False + forwarded = self.headers.get("X-Forwarded-Proto", "").split(",", 1)[0].strip().casefold() + return forwarded == "https" + + def _same_origin(self) -> bool: + origin = self.headers.get("Origin", "") + if not origin: + return False + try: + parsed = urlsplit(origin) + except ValueError: + return False + expected_host = (self.headers.get("X-Forwarded-Host") or self.headers.get("Host", "")) + expected_host = expected_host.split(",", 1)[0].strip().casefold() + forwarded_proto = self.headers.get("X-Forwarded-Proto", "").split(",", 1)[0].strip().casefold() + expected_scheme = forwarded_proto or "http" + return bool( + parsed.scheme.casefold() == expected_scheme + and parsed.netloc.casefold() == expected_host + and not parsed.username + and not parsed.password + and not parsed.path + and not parsed.query + and not parsed.fragment + ) + + @staticmethod + def _safe_next(value: str) -> str: + if not value or any(ord(char) < 32 for char in value): + return "/" + try: + parsed = urlsplit(value) + except ValueError: + return "/" + if parsed.scheme or parsed.netloc or parsed.path not in {"/", "/backtest"}: + return "/" + result = parsed.path + if parsed.query: + result += "?" + parsed.query + if parsed.fragment: + result += "#" + parsed.fragment + return result + + def _authenticated(self) -> bool: + cookie = SimpleCookie() + try: + cookie.load(self.headers.get("Cookie", "")) + morsel = cookie.get(COOKIE_NAME) + except Exception: + return False + return bool(morsel and verify_session(auth, morsel.value)) + + def _send(self, status: int, content_type: str, body: bytes, + headers: dict[str, str] | None = None) -> None: + self.send_response(status) + self.send_header("Content-Type", content_type) + self.send_header("Content-Length", str(len(body))) + self.send_header("Cache-Control", "no-store") + self.send_header("X-Content-Type-Options", "nosniff") + self.send_header("Referrer-Policy", "no-referrer") + self.send_header("X-Frame-Options", "DENY") + self.send_header( + "Content-Security-Policy", + "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; " + "img-src 'self' data:; connect-src 'self'; form-action 'self'; base-uri 'self'; frame-ancestors 'none'", + ) + for name, value in (headers or {}).items(): + self.send_header(name, value) + self.end_headers() + self.wfile.write(body) + + def _redirect(self, location: str, *, cookie: str | None = None) -> None: + self.send_response(303) + self.send_header("Location", location) + self.send_header("Cache-Control", "no-store") + self.send_header("Content-Length", "0") + self.send_header("X-Content-Type-Options", "nosniff") + if cookie: + self.send_header("Set-Cookie", cookie) + self.end_headers() + + def _send_login_page(self) -> None: + page = Path(__file__).with_name("login.html") + try: + self._send(200, "text/html; charset=utf-8", page.read_bytes()) + except OSError: + self._send(500, "text/plain; charset=utf-8", b"Login page is missing") + + def _send_unauthorized(self) -> None: + body = json.dumps({"error": "authentication_required"}).encode("utf-8") + self._send(401, "application/json; charset=utf-8", body, + {"WWW-Authenticate": "Form", "Location": "/login"}) + + def _handle_login(self) -> None: + if not self._same_origin(): + self._send(403, "text/plain; charset=utf-8", b"Login request origin is not allowed") + return + try: + length = int(self.headers.get("Content-Length", "0")) + if length <= 0 or length > 8192: + raise ValueError("คำขอเข้าสู่ระบบไม่ถูกต้อง") + raw = self.rfile.read(length).decode("utf-8") + fields = parse_qs(raw, keep_blank_values=True, max_num_fields=8) + except (ValueError, UnicodeDecodeError): + self._send(400, "text/plain; charset=utf-8", "ข้อมูลเข้าสู่ระบบไม่ถูกต้อง".encode("utf-8")) + return + next_page = self._safe_next(fields.get("next", ["/"])[0]) + client = self.client_address[0] + wait_seconds = _login_block_seconds(client) + if wait_seconds: + query = urlencode({"error": "locked", "next": next_page}) + self._redirect("/login?" + query) + return + supplied_username = fields.get("username", [""])[0] + supplied_password = fields.get("password", [""])[0] + username_ok = hmac.compare_digest(supplied_username.encode("utf-8"), auth.username.encode("utf-8")) + password_ok = hmac.compare_digest(supplied_password.encode("utf-8"), auth.password.encode("utf-8")) + if not (username_ok and password_ok): + _record_login_failure(client) + query = urlencode({"error": "invalid", "next": next_page}) + self._redirect("/login?" + query) + return + _clear_login_failures(client) + token = create_session(auth) + cookie = session_cookie(auth, token, secure=self._secure_request()) + self._redirect(next_page, cookie=cookie) + + def do_GET(self) -> None: # noqa: N802 - BaseHTTPRequestHandler API + path = self._request_path() + if path == "/health": + self._send(200, "application/json; charset=utf-8", b'{"status":"ok"}') + return + if path == "/login": + if self._authenticated(): + query = parse_qs(urlsplit(self.path).query) + self._redirect(self._safe_next(query.get("next", ["/"])[0])) + else: + self._send_login_page() + return + if not self._authenticated(): + if path.startswith("/api/"): + self._send_unauthorized() + else: + next_page = self._safe_next(self.path) + self._redirect("/login?" + urlencode({"next": next_page})) + return + if path in {"/", "/index.html", "/backtest", "/backtest.html"}: + page_name = "backtest.html" if path in {"/backtest", "/backtest.html"} else "webui.html" + page = Path(__file__).with_name(page_name) + try: + self._send(200, "text/html; charset=utf-8", page.read_bytes()) + except OSError: + self._send(500, "text/plain; charset=utf-8", b"Dashboard page is missing") + return + if path == "/api/dashboard": + payload = _report_data(report_dir, initial_equity, archives) + body = json.dumps(payload, ensure_ascii=False, default=_json_default).encode("utf-8") + self._send(200, "application/json; charset=utf-8", body) + return + if path == "/api/portfolio": + try: + payload = _portfolio_data(archives, report_dir) + body = json.dumps(payload, ensure_ascii=False, default=_json_default).encode("utf-8") + self._send(200, "application/json; charset=utf-8", body) + except Exception as exc: + body = json.dumps({"error": f"{type(exc).__name__}: {exc}"}, ensure_ascii=False).encode("utf-8") + self._send(500, "application/json; charset=utf-8", body) + return + self._send(404, "text/plain; charset=utf-8", "ไม่พบหน้านี้".encode("utf-8")) + + def do_POST(self) -> None: # noqa: N802 - BaseHTTPRequestHandler API + path = self._request_path() + if path == "/login": + self._handle_login() + return + if path == "/logout": + if not self._authenticated(): + self._redirect("/login") + return + if not self._same_origin(): + self._send(403, "text/plain; charset=utf-8", b"Logout request origin is not allowed") + return + self._redirect("/login?logged_out=1", + cookie=expired_session_cookie(secure=self._secure_request())) + return + if not self._authenticated(): + self._send_unauthorized() + return + if not self._same_origin(): + body = json.dumps({"ok": False, "message": "คำขอจากเว็บไซต์อื่นถูกปฏิเสธ"}, ensure_ascii=False).encode("utf-8") + self._send(403, "application/json; charset=utf-8", body) + return + if path in { + "/api/portfolio/deposit", "/api/portfolio/cash-event", "/api/portfolio/trade", + "/api/portfolio/signal-trade", "/api/portfolio/edit-trade", "/api/portfolio/void-trade", + }: + try: + length = int(self.headers.get("Content-Length", "0")) + if length <= 0 or length > 16_384: + raise ValueError("ข้อมูลคำขอว่างหรือใหญ่เกินกำหนด") + payload = json.loads(self.rfile.read(length).decode("utf-8")) + if not isinstance(payload, dict): + raise ValueError("รูปแบบข้อมูลไม่ถูกต้อง") + if path.endswith("/deposit"): + event_id = add_cash_event( + "deposit", float(payload.get("amount", 0)), + str(payload.get("note", "")), database=DEFAULT_BOOK, + ) + message = f"บันทึกเงินลงทุน #{event_id} แล้ว" + elif path.endswith("/cash-event"): + kind = str(payload.get("kind", "")) + event_id = add_cash_event( + kind, float(payload.get("amount", 0)), + str(payload.get("note", "")), database=DEFAULT_BOOK, + ) + label = {"dividend": "เงินปันผล", "withdrawal": "เงินถอน"}.get(kind, "รายการเงินสด") + message = f"บันทึก{label} #{event_id} แล้ว" + elif path.endswith("/signal-trade"): + current = _portfolio_data(archives, report_dir) + if not bool(current.get("account_exists")): + raise ValueError("เพิ่มเงินลงทุนครั้งแรกก่อนบันทึกรายการจากสัญญาณ") + symbol = str(payload.get("symbol", "")).strip().upper() + side = str(payload.get("side", "")).strip().upper() + if side not in {"BUY", "SELL"}: + raise ValueError("สัญญาณต้องเป็น BUY หรือ SELL") + row = next((item for item in current["rows"] if item["symbol"] == symbol), None) + if row is None or row["action"] != side: + raise ValueError("สัญญาณเปลี่ยนหรือไม่ตรงกับพอร์ตแล้ว; โหลดหน้าใหม่ก่อน") + quantity = abs(int(row["delta_quantity"] or 0)) + reference_price = row["suggested_limit_price"] + if quantity <= 0 or reference_price is None: + raise ValueError("ไม่มีจำนวนหรือราคาอ้างอิงที่ใช้บันทึกรายการได้") + trade_id = record_trade( + symbol, side, quantity, float(reference_price), 0.0, + f"ประมาณจากสัญญาณ {current.get('signal_as_of') or 'ล่าสุด'}; แก้เป็นราคาจริงได้", + estimated=True, database=DEFAULT_BOOK, + ) + message = (f"บันทึกรายการ{('ซื้อ' if side == 'BUY' else 'ขาย')}ประมาณการ #{trade_id} แล้ว " + "โดยอ้างอิงราคาปิดล่าสุด; ไม่มีการส่งคำสั่งไปโบรกเกอร์") + elif path.endswith("/edit-trade"): + raw_trade_id = float(payload.get("trade_id", 0)) + raw_quantity = float(payload.get("quantity", 0)) + if not raw_trade_id.is_integer() or not raw_quantity.is_integer(): + raise ValueError("รหัสรายการและจำนวนหุ้นต้องเป็นจำนวนเต็ม") + update_trade( + int(raw_trade_id), int(raw_quantity), float(payload.get("price", 0)), + float(payload.get("fees", 0)), database=DEFAULT_BOOK, + ) + message = f"แก้จำนวน/ราคาจริงของรายการ #{int(raw_trade_id)} แล้ว" + elif path.endswith("/void-trade"): + raw_trade_id = float(payload.get("trade_id", 0)) + if not raw_trade_id.is_integer(): + raise ValueError("รหัสรายการต้องเป็นจำนวนเต็ม") + void_trade(int(raw_trade_id), database=DEFAULT_BOOK) + message = f"ยกเลิกรายการประมาณการ #{int(raw_trade_id)} แล้ว; ประวัติยังอยู่ในบัญชี" + else: + raw_quantity = float(payload.get("quantity", 0)) + if not raw_quantity.is_integer(): + raise ValueError("จำนวนหุ้นต้องเป็นจำนวนเต็ม") + trade_id = record_trade( + str(payload.get("symbol", "")), str(payload.get("side", "")), + int(raw_quantity), float(payload.get("price", 0)), + float(payload.get("fees", 0)), str(payload.get("note", "")), + database=DEFAULT_BOOK, + ) + message = f"บันทึก fill จริง #{trade_id} แล้ว" + body = json.dumps({"ok": True, "message": message}, ensure_ascii=False).encode("utf-8") + self._send(200, "application/json; charset=utf-8", body) + except Exception as exc: + body = json.dumps({"ok": False, "message": f"{type(exc).__name__}: {exc}"}, ensure_ascii=False).encode("utf-8") + self._send(400, "application/json; charset=utf-8", body) + return + if path != "/api/refresh": + self._send(404, "text/plain; charset=utf-8", "ไม่พบหน้านี้".encode("utf-8")) + return + started, message = _start_refresh(archives, report_dir, start, initial_equity) + body = json.dumps({"started": started, "message": message}, ensure_ascii=False).encode("utf-8") + self._send(202 if started else 409, "application/json; charset=utf-8", body) + + def log_message(self, format: str, *args: object) -> None: + print(f"[webui] {self.address_string()} {format % args}") + + return DashboardHandler + + +def run_server(*, host: str = "127.0.0.1", port: int = 18900, + archives: list[str | Path] | None = None, + report_dir: str | Path = DEFAULT_REPORT_DIR, start: str = DEFAULT_START, + initial_equity: float = 1_000_000.0) -> None: + if not 1 <= port <= 65535: + raise ValueError("port must be between 1 and 65535") + if initial_equity <= 0: + raise ValueError("initial_equity must be positive") + if pd.Timestamp(start) >= pd.Timestamp.now().normalize() + pd.Timedelta(days=1): + raise ValueError("start must not be in the future") + auth = load_auth_config() + archive_paths = tuple(Path(path).resolve() for path in (archives or DEFAULT_ARCHIVES)) + output_path = Path(report_dir).resolve() + output_path.mkdir(parents=True, exist_ok=True) + handler = _handler_factory(archive_paths, output_path, start, initial_equity, auth) + server = ThreadingHTTPServer((host, port), handler) + display_host = "127.0.0.1" if host == "0.0.0.0" else host + print(f"PrimeThai Web UI: http://{display_host}:{port}") + print(f"Login is enabled for user: {auth.username}; session expires after {auth.session_ttl_seconds // 3600} hours.") + if host == "0.0.0.0": + print("Network bind is public to the host network; put it behind an authenticated HTTPS reverse proxy.") + print(f"Reports: {output_path}") + print(f"Automatic EOD refresh: SiamChart primary, Yahoo Finance backup; weekdays after {AUTO_REFRESH_HOUR:02d}:{AUTO_REFRESH_MINUTE:02d} Asia/Bangkok.") + print("Portfolio edits are local only; no broker orders are sent.") + previous_fetch = _read_json(LATEST_FETCH_STATE) or {} + fetch_on_start = ( + not (previous_fetch.get("last_attempt_date") == _today_local() + and bool(previous_fetch.get("after_close")) + and previous_fetch.get("result") != "failed") + ) + _start_refresh(archive_paths, output_path, start, initial_equity, + fetch_latest=fetch_on_start) + threading.Thread( + target=_daily_refresh_loop, + args=(archive_paths, output_path, start, initial_equity), + name="primethai-daily-refresh", + daemon=True, + ).start() + try: + server.serve_forever() + except KeyboardInterrupt: + print("Stopping PrimeThai Web UI.") + finally: + server.server_close() diff --git a/primethai/yahoo_backup.py b/primethai/yahoo_backup.py new file mode 100644 index 0000000..e879f8e --- /dev/null +++ b/primethai/yahoo_backup.py @@ -0,0 +1,239 @@ +"""Validated Yahoo Finance daily fallback for the active SET liquid universe.""" + +from __future__ import annotations + +from collections.abc import Iterable +from datetime import date, timedelta +import csv +import io +import math +from pathlib import Path +import re + +import pandas as pd + +from .data import yahoo_symbol +from .model_lab import load_market_panel + + +MIN_LIQUID_UNIVERSE_COVERAGE = 0.95 +LIQUID_UNIVERSE_SIZE = 100 +YAHOO_BATCH_SIZE = 100 +_TICKER = re.compile(r"[A-Z0-9]{1,5}$") +_EOD_COLUMNS = ["", "", "", "", "", "", ""] + + +class YahooBackupError(RuntimeError): + """The secondary source could not produce a complete, validated EOD session.""" + + +def _expected_liquid_symbols(archives: list[str | Path], latest_date: str) -> tuple[list[str], list[str]]: + latest = pd.Timestamp(latest_date) + start = (latest - pd.Timedelta(days=120)).date().isoformat() + end = (latest + pd.Timedelta(days=1)).date().isoformat() + panel = load_market_panel( + [str(path) for path in archives], + start=start, + end=end, + action_dir="data/raw/actions", + set_index_csv="data/raw/^SET.csv", + set_reference="data/reference/set_actions_2025_2026_h1.csv", + ) + if not len(panel.dates) or panel.dates[-1].date().isoformat() != latest_date: + raise YahooBackupError("หา session ล่าสุดในไฟล์ราคาที่เก็บไว้เพื่อเทียบ coverage ไม่พบ") + turnover = panel.close.mul(panel.volume.where(panel.has_bar, 0.0)) + average_turnover = turnover.rolling(20, min_periods=20).mean().iloc[-1] + close = panel.close.iloc[-1] + eligible = panel.has_bar.iloc[-1] & close.ge(2.0) & average_turnover.ge(20_000_000.0) + ranked = average_turnover.where(eligible).dropna().sort_values(ascending=False, kind="stable") + expected = [symbol for symbol in ranked.index[:LIQUID_UNIVERSE_SIZE].tolist() + if _TICKER.fullmatch(str(symbol))] + requested = [str(symbol) for symbol in panel.symbols if _TICKER.fullmatch(str(symbol))] + if len(expected) < LIQUID_UNIVERSE_SIZE: + raise YahooBackupError( + f"ชุดราคาที่เก็บไว้มีหุ้นผ่านเกณฑ์สภาพคล่องเพียง {len(expected)} ตัว; ตรวจ Yahoo fallback ไม่ได้" + ) + return expected, requested + + +def _frame_for_ticker(frame: pd.DataFrame, ticker: str, batch: list[str]) -> pd.DataFrame | None: + if not isinstance(frame.columns, pd.MultiIndex): + return frame if len(batch) == 1 else None + wanted = ticker.upper() + for level in range(frame.columns.nlevels): + values = frame.columns.get_level_values(level) + actual = next((str(value) for value in values.unique() if str(value).upper() == wanted), None) + if actual is None: + continue + selected = frame.xs(actual, axis=1, level=level, drop_level=True) + if isinstance(selected.columns, pd.MultiIndex): + selected.columns = [str(column[-1]) for column in selected.columns] + return selected + return None + + +def _date_key(value: object) -> str: + stamp = pd.Timestamp(value) + if stamp.tzinfo is not None: + stamp = stamp.tz_convert("Asia/Bangkok").tz_localize(None) + return stamp.date().isoformat() + + +def _extract_bars(frame: pd.DataFrame | None, *, allow_missing_volume: bool = False) -> dict[str, dict[str, float]]: + if frame is None or frame.empty: + return {} + columns = {str(column).strip().casefold().replace(" ", "_"): column + for column in frame.columns} + required = {"open", "high", "low", "close", "volume"} + if not required.issubset(columns): + return {} + result: dict[str, dict[str, float]] = {} + for index, row in frame.iterrows(): + try: + bar = {field: float(row[columns[field]]) for field in required} + except (TypeError, ValueError, KeyError): + if not allow_missing_volume: + continue + try: + bar = {field: float(row[columns[field]]) for field in required - {"volume"}} + bar["volume"] = 0.0 + except (TypeError, ValueError, KeyError): + continue + if allow_missing_volume and not math.isfinite(bar.get("volume", 0.0)): + bar["volume"] = 0.0 + if not all(math.isfinite(value) for value in bar.values()): + continue + if min(bar["open"], bar["high"], bar["low"], bar["close"]) <= 0 or bar["volume"] < 0: + continue + if bar["high"] + 1e-8 < max(bar["open"], bar["close"], bar["low"]): + continue + if bar["low"] - 1e-8 > min(bar["open"], bar["close"], bar["high"]): + continue + result[_date_key(index)] = bar + return result + + +def _download_yahoo(tickers: list[str], *, start: str, end: str) -> tuple[dict[str, dict[str, dict[str, float]]], list[str]]: + try: + import yfinance as yf + except ImportError as exc: + raise YahooBackupError("ไม่พบ yfinance ใน environment ของแอป") from exc + bars: dict[str, dict[str, dict[str, float]]] = {} + errors: list[str] = [] + unique = list(dict.fromkeys(tickers)) + for offset in range(0, len(unique), YAHOO_BATCH_SIZE): + batch = unique[offset:offset + YAHOO_BATCH_SIZE] + try: + data = yf.download( + batch, + start=start, + end=end, + interval="1d", + auto_adjust=False, + actions=False, + repair=False, + progress=False, + threads=8, + group_by="ticker", + ignore_tz=True, + timeout=20, + multi_level_index=True, + ) + except Exception as exc: + errors.append(f"batch {offset // YAHOO_BATCH_SIZE + 1}: {type(exc).__name__}") + continue + if data is None or data.empty: + errors.append(f"batch {offset // YAHOO_BATCH_SIZE + 1}: no data") + continue + for ticker in batch: + frame = _frame_for_ticker(data, ticker, batch) + normalized = _extract_bars(frame, allow_missing_volume=ticker == "^SET.BK") + if normalized: + bars[ticker] = normalized + return bars, errors + + +def _csv_bytes(day: str, bars: dict[str, dict[str, float]]) -> bytes: + text = io.StringIO(newline="") + writer = csv.DictWriter(text, fieldnames=_EOD_COLUMNS, lineterminator="\n") + writer.writeheader() + yyyymmdd = day.replace("-", "") + for ticker, bar in sorted(bars.items()): + writer.writerow({ + "": ticker, + "": yyyymmdd, + "": format(bar["open"], ".10g"), + "": format(bar["high"], ".10g"), + "": format(bar["low"], ".10g"), + "": format(bar["close"], ".10g"), + "": format(bar["volume"], ".10g"), + }) + return text.getvalue().encode("utf-8") + + +def fetch_yahoo_eod_backup( + archives: list[str | Path], + *, + latest_date: str, + end_date: date, + required_symbols: Iterable[str] = (), +) -> tuple[dict[str, bytes], dict[str, dict[str, object]]]: + """Fetch missing EOD sessions for the liquid model universe from Yahoo. + + The index and at least 95% of the prior session's top-100 liquid universe + must have a valid Yahoo bar for each session before any rows are returned. + """ + expected, requested = _expected_liquid_symbols(archives, latest_date) + extras = {str(symbol).strip().upper() for symbol in required_symbols + if _TICKER.fullmatch(str(symbol).strip().upper())} + symbols = sorted(set(requested) | set(expected) | extras) + ticker_to_symbol = {yahoo_symbol(symbol): symbol for symbol in symbols} + set_ticker = yahoo_symbol("^SET") + start = latest_date + end = (end_date + timedelta(days=1)).isoformat() + index_rows, index_errors = _download_yahoo([set_ticker], start=start, end=end) + index_bars = index_rows.get(set_ticker, {}) + if not index_bars: + detail = "; ".join(index_errors[:2]) + raise YahooBackupError("Yahoo ไม่มีแท่งข้อมูล SET สำหรับยืนยัน session" + (f" ({detail})" if detail else "")) + + symbol_rows, batch_errors = _download_yahoo(list(ticker_to_symbol), start=start, end=end) + market_bars: dict[str, dict[str, dict[str, float]]] = { + ticker_to_symbol[ticker]: rows for ticker, rows in symbol_rows.items() + if ticker in ticker_to_symbol + } + new_days = sorted(day for day in index_bars if day > latest_date) + if not new_days: + raise YahooBackupError("Yahoo ยังไม่มีข้อมูล SET หลังวันที่เก็บไว้ล่าสุด") + + merged: dict[str, bytes] = {} + metadata: dict[str, dict[str, object]] = {} + expected_count = len(expected) + for day in new_days: + available = {symbol for symbol, rows in market_bars.items() if day in rows} + coverage_count = len(set(expected) & available) + coverage = coverage_count / expected_count if expected_count else 0.0 + if coverage < MIN_LIQUID_UNIVERSE_COVERAGE: + raise YahooBackupError( + f"Yahoo coverage หุ้นสภาพคล่องหลัก {coverage_count}/{expected_count} ตัว " + f"({coverage:.0%}); ต้องได้อย่างน้อย {MIN_LIQUID_UNIVERSE_COVERAGE:.0%}" + ) + daily: dict[str, dict[str, float]] = {symbol: rows[day] for symbol, rows in market_bars.items() + if day in rows} + daily["SET"] = index_bars[day] + missing_positions = sorted(symbol for symbol in extras if symbol not in available) + if missing_positions: + raise YahooBackupError( + "Yahoo ไม่มีราคาวันล่าสุดของหุ้นที่ถืออยู่/สัญญาณสำคัญ: " + + ", ".join(missing_positions[:8]) + ) + merged[day] = _csv_bytes(day, daily) + metadata[day] = { + "source": "Yahoo Finance via yfinance", + "records": len(daily), + "liquid_universe_coverage": coverage, + "liquid_universe_present": coverage_count, + "liquid_universe_expected": expected_count, + "batch_errors": batch_errors[:5], + } + return merged, metadata diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..439df74 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,23 @@ +[build-system] +requires = ["setuptools==80.9.0"] +build-backend = "setuptools.build_meta" + +[project] +name = "primethai" +version = "0.1.0" +description = "Thai stock research, scanning and event-driven backtesting toolkit" +requires-python = ">=3.11" +dependencies = ["numpy==2.5.3", "pandas==3.0.5", "yfinance==1.7.0", "scipy==1.18.1", "scikit-learn==1.9.1"] + +[project.optional-dependencies] +dev = ["pytest==9.1.1"] + +[tool.setuptools.packages.find] +include = ["primethai*"] + +[tool.setuptools.package-data] +primethai = ["webui.html", "backtest.html", "login.html"] + +[tool.pytest.ini_options] +testpaths = ["tests"] +addopts = "-q" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0319cec --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +numpy==2.5.3 +pandas==3.0.5 +yfinance==1.7.0 +scipy==1.18.1 +scikit-learn==1.9.1 +pytest==9.1.1 diff --git a/tests/fixtures/cache/ADVANC.BK.csv b/tests/fixtures/cache/ADVANC.BK.csv new file mode 100644 index 0000000..1876552 --- /dev/null +++ b/tests/fixtures/cache/ADVANC.BK.csv @@ -0,0 +1,3 @@ +date,open,high,low,close,adj_close,volume,dividends,stock_splits +2024-01-02,10,11,9,10,9.9,1000,0,0 +2024-01-03,11,12,10,11,11,1100,0,0 diff --git a/tests/fixtures/members.csv b/tests/fixtures/members.csv new file mode 100644 index 0000000..d7d9047 --- /dev/null +++ b/tests/fixtures/members.csv @@ -0,0 +1,3 @@ +symbol,start_date,end_date,sector +A,2024-01-01,2024-06-30,Banking +B,2024-07-01,2024-12-31,Commerce diff --git a/tests/fixtures/overlap.csv b/tests/fixtures/overlap.csv new file mode 100644 index 0000000..d1f877b --- /dev/null +++ b/tests/fixtures/overlap.csv @@ -0,0 +1,3 @@ +symbol,start_date,end_date +A,2024-01-01,2024-07-01 +A,2024-06-01,2024-12-31 diff --git a/tests/fixtures/set_index_csv/^SET.csv b/tests/fixtures/set_index_csv/^SET.csv new file mode 100644 index 0000000..ff1b71f --- /dev/null +++ b/tests/fixtures/set_index_csv/^SET.csv @@ -0,0 +1,3 @@ +Date,Open,High,Low,Close +2026-09-24,1598.00,1605.00,1596.00,1602.58 +2026-09-25,1603.58,1613.45,1603.46,1607.63 diff --git a/tests/fixtures/siamchart_eod_test.zip b/tests/fixtures/siamchart_eod_test.zip new file mode 100644 index 0000000..9cf8985 Binary files /dev/null and b/tests/fixtures/siamchart_eod_test.zip differ diff --git a/tests/test_data_universe.py b/tests/test_data_universe.py new file mode 100644 index 0000000..bde1b55 --- /dev/null +++ b/tests/test_data_universe.py @@ -0,0 +1,102 @@ +import unittest +from pathlib import Path +from unittest.mock import patch + +import pandas as pd + +from primethai.cli import _accurate_context_bars, _read_bars +from primethai.data import SymbolDataError, YFinanceProvider, normalize_bars, read_siamchart_archives, yahoo_symbol +from primethai.universe import MembershipError, MembershipPeriod, PointInTimeUniverse + + +class DataUniverseTests(unittest.TestCase): + def test_yahoo_symbol_mapping_and_normalization_preserve_adjustments_and_actions(self): + self.assertEqual(yahoo_symbol("ADVANC"), "ADVANC.BK") + self.assertEqual(yahoo_symbol("^SET"), "^SET.BK") + idx = pd.DatetimeIndex(["2024-01-02", "2024-01-03"], tz="UTC") + raw = pd.DataFrame({"Open": [10, 5], "High": [11, 5.5], "Low": [9, 4.5], + "Close": [10, 5], "Adj Close": [9.8, 5], "Volume": [100, 200], + "Dividends": [0, .5], "Stock Splits": [0, 2]}, index=idx) + out = normalize_bars(raw) + self.assertIsNone(out.index.tz) + self.assertEqual(out.iloc[1]["adj_close"], 5) + self.assertEqual(out.iloc[1]["adj_open"], 5) + self.assertEqual(out.iloc[1]["dividends"], .5) + self.assertEqual(out.iloc[1]["stock_splits"], 2) + + def test_missing_or_empty_provider_data_is_reported_safely(self): + with self.assertRaisesRegex(SymbolDataError, "no bars"): + normalize_bars(pd.DataFrame()) + with self.assertRaisesRegex(SymbolDataError, "missing columns"): + normalize_bars(pd.DataFrame({"Close": [1]}, index=pd.date_range("2024-01-01", periods=1))) + + def test_local_cache_avoids_second_yahoo_request(self): + root = Path(__file__).resolve().parent / "fixtures" / "cache" + provider = YFinanceProvider(root, retries=0) + result = provider.get_history("ADVANC", start="2024-01-02", end="2024-01-04") + self.assertEqual(len(result), 2) + self.assertEqual(result.iloc[-1]["close"], 11) + + def test_partial_set_benchmark_history_is_refused(self): + class FakeYFinance: + @staticmethod + def download(*args, **kwargs): + index = pd.DatetimeIndex(["2026-09-25"]) + return pd.DataFrame({"Open": [1603.58], "High": [1613.45], + "Low": [1603.46], "Close": [1607.63], + "Volume": [0]}, index=index) + + root = Path(__file__).resolve().parent / "fixtures" / "cache" + provider = YFinanceProvider(root, retries=0) + with patch.dict("sys.modules", {"yfinance": FakeYFinance}): + with self.assertRaisesRegex(SymbolDataError, "incomplete SET benchmark"): + provider.get_history("^SET", start="2024-01-01", end="2024-02-01", refresh=True) + + def test_manual_set_csv_accepts_exchange_style_headers_and_missing_volume(self): + directory = Path(__file__).resolve().parent / "fixtures" / "set_index_csv" + bars = _read_bars(directory, "^SET") + self.assertEqual(len(bars), 2) + self.assertEqual(bars.iloc[-1]["close"], 1607.63) + self.assertEqual(bars["volume"].sum(), 0) + + def test_siamchart_zip_import_selects_symbols_and_reports_bad_daily_files(self): + archive_path = Path(__file__).resolve().parent / "fixtures" / "siamchart_eod_test.zip" + imported, issues = read_siamchart_archives([archive_path], ["^SET", "ADVANC"]) + + self.assertEqual(imported["^SET"].iloc[0]["close"], 1405) + self.assertEqual(imported["ADVANC"].iloc[0]["close"], 201) + self.assertEqual(imported["^SET"].iloc[0]["dividends"], 0) + self.assertEqual(len(issues), 1) + self.assertIn("set-history_EOD_2024-01-03.csv", issues[0]) + + def test_point_in_time_membership_and_coverage(self): + path = Path(__file__).resolve().parent / "fixtures" / "members.csv" + universe = PointInTimeUniverse.from_csv(path) + self.assertEqual(universe.members("2024-03-01"), ["A"]) + self.assertEqual(universe.members("2024-08-01"), ["B"]) + self.assertEqual(universe.sector("A", "2024-03-01"), "Banking") + self.assertFalse(universe.covers(pd.to_datetime(["2023-12-29", "2024-02-01"]))) + with self.assertRaisesRegex(MembershipError, "does not cover"): + universe.require_coverage(pd.to_datetime(["2024-01-02", "2025-01-02"])) + + def test_accurate_context_clips_full_index_to_pit_end_and_keeps_warmup(self): + dates = pd.bdate_range("2024-01-02", periods=300) + pit_end = dates[279] + membership = PointInTimeUniverse([ + MembershipPeriod("A", dates[0], pit_end), + ]) + set_bars = pd.DataFrame({"close": range(len(dates))}, index=dates) + + clipped = _accurate_context_bars( + set_bars, membership, dates[250].date().isoformat(), + (pit_end + pd.Timedelta(days=1)).date().isoformat(), + ) + + self.assertEqual(clipped.index.min(), dates[0]) + self.assertEqual(clipped.index.max(), pit_end) + self.assertEqual(len(clipped), 280) + + def test_overlapping_membership_intervals_are_rejected(self): + path = Path(__file__).resolve().parent / "fixtures" / "overlap.csv" + with self.assertRaisesRegex(MembershipError, "overlapping"): + PointInTimeUniverse.from_csv(path) diff --git a/tests/test_indicators.py b/tests/test_indicators.py new file mode 100644 index 0000000..8d8cf02 --- /dev/null +++ b/tests/test_indicators.py @@ -0,0 +1,89 @@ +import unittest + +import numpy as np +import pandas as pd + +from primethai.indicators import breadth_series, mcclellan, price_features, relative_strength +from primethai.setups import confirmed_pivots + + +def bars(n=280): + idx = pd.bdate_range("2024-01-01", periods=n) + close = pd.Series(np.linspace(10, 20, n), index=idx) + return pd.DataFrame({"open": close - .05, "high": close + .25, + "low": close - .25, "close": close, "volume": 1000.0}, index=idx) + + +class IndicatorTests(unittest.TestCase): + def test_ema21_structure_uses_high_close_low_series(self): + frame = bars() + result = price_features(frame) + expected = frame["high"].ewm(span=21, adjust=False, min_periods=21).mean() + pd.testing.assert_series_equal(result["ema21_high"], expected, check_names=False) + self.assertEqual(result.iloc[-1]["trend_state"], "UP") + self.assertEqual(result.iloc[-1]["price_state"], "ABOVE_STRUCTURE") + + def test_atr_uses_true_range_including_previous_close_gap(self): + frame = pd.DataFrame({"open": [10, 12, 12], "high": [11, 13, 13], + "low": [9, 11, 11], "close": [10, 12, 12], "volume": [1, 1, 1]}, + index=pd.bdate_range("2024-01-01", periods=3)) + result = price_features(frame, atr_period=2, adr_period=2) + self.assertEqual(result["tr"].tolist(), [2, 3, 2]) + self.assertEqual(result["atr14"].iloc[-1], 2.5) + + def test_breadth_mco_and_mcsi_formulas(self): + idx = pd.bdate_range("2024-01-01", periods=40) + closes = pd.DataFrame({"A": np.arange(10, 50), "B": np.arange(50, 10, -1), "C": 10.0}, index=idx) + membership = {pd.Timestamp(date): ["A", "B", "C"] for date in idx} + breadth = breadth_series(closes, membership) + self.assertEqual(breadth.iloc[1]["advancing"], 1) + self.assertEqual(breadth.iloc[1]["declining"], 1) + self.assertEqual(breadth.iloc[1]["unchanged"], 1) + self.assertEqual(breadth.iloc[1]["net_advances"], 0) + out = mcclellan(breadth, short_span=3, long_span=5, z_window=20) + net = breadth["net_advances"].astype(float) + expected = net.ewm(span=3, adjust=False, min_periods=3).mean() - net.ewm(span=5, adjust=False, min_periods=5).mean() + pd.testing.assert_series_equal(out["mco"], expected, check_names=False) + self.assertAlmostEqual(out["mcsi"].iloc[-1], out["mco"].fillna(0).cumsum().iloc[-1]) + self.assertEqual(out["mcsi_10dma"].notna().sum(), 31) + self.assertTrue(set(out["mcsi_state"].dropna()).issubset({"UP", "DOWN", "RECLAIM_10DMA", "LOSE_10DMA", "CURL_UP", "CURL_DOWN"})) + + def test_rs_percentile_and_set_comparison_are_cross_sectional(self): + idx = pd.bdate_range("2023-01-02", periods=300) + benchmark = pd.Series(np.full(300, 100.0), index=idx) + prices = {"LEADER": pd.Series(np.linspace(10, 40, 300), index=idx), + "LAGGARD": pd.Series(np.linspace(40, 10, 300), index=idx), + "FLAT": pd.Series(np.full(300, 20.0), index=idx)} + scores = relative_strength(prices, benchmark, set_benchmark=benchmark) + self.assertGreater(scores.loc[idx[-1], "LEADER_rs_percentile"], scores.loc[idx[-1], "FLAT_rs_percentile"]) + self.assertGreater(scores.loc[idx[-1], "FLAT_rs_percentile"], scores.loc[idx[-1], "LAGGARD_rs_percentile"]) + self.assertGreaterEqual(scores.loc[idx[-1], "LEADER_rs_percentile"], 0) + self.assertLessEqual(scores.loc[idx[-1], "LEADER_rs_percentile"], 100) + self.assertIn("LEADER_rs_1m_vs_set", scores) + + def test_rs_percentile_uses_active_point_in_time_members(self): + idx = pd.bdate_range("2023-01-02", periods=300) + benchmark = pd.Series(np.full(300, 100.0), index=idx) + prices = { + "MEMBER_A": pd.Series(np.linspace(10, 20, 300), index=idx), + "NONMEMBER": pd.Series(np.linspace(10, 100, 300), index=idx), + "MEMBER_C": pd.Series(np.full(300, 20.0), index=idx), + } + members = {pd.Timestamp(date): ["MEMBER_A", "MEMBER_C"] for date in idx} + + scores = relative_strength(prices, benchmark, members_by_date=members) + + # The very strong non-member must not push MEMBER_C below the 50th percentile. + self.assertAlmostEqual(scores.loc[idx[-1], "MEMBER_C_rs_percentile"], 53.75) + + def test_higher_low_is_not_published_until_confirmation(self): + idx = pd.bdate_range("2024-01-01", periods=14) + lows = pd.Series([10, 9, 8, 9, 10, 11, 10, 9, 10, 11, 12, 13, 14, 15], index=idx) + pivots = confirmed_pivots(lows, left_bars=2, right_bars=2) + self.assertTrue(pd.isna(pivots.loc[idx[2], "confirmed_pivot_low"])) + self.assertTrue(pd.isna(pivots.loc[idx[3], "confirmed_pivot_low"])) + self.assertEqual(pivots.loc[idx[4], "confirmed_pivot_low"], 8) + self.assertFalse(bool(pivots.loc[idx[4], "higher_low"])) + self.assertTrue(pd.isna(pivots.loc[idx[8], "confirmed_pivot_low"])) + self.assertEqual(pivots.loc[idx[9], "confirmed_pivot_low"], 9) + self.assertTrue(bool(pivots.loc[idx[9], "higher_low"])) diff --git a/tests/test_portfolio_backtest.py b/tests/test_portfolio_backtest.py new file mode 100644 index 0000000..74fb15d --- /dev/null +++ b/tests/test_portfolio_backtest.py @@ -0,0 +1,124 @@ +import unittest + +import pandas as pd + +from primethai.backtest import run_backtest +from primethai.config import RegimeHeat, StrategyConfig, ThaiMarketRules, TradingCosts +from primethai.portfolio import HeatPosition, portfolio_cushion, portfolio_heat, round_quantity, round_to_tick, size_position + + +def feature_frame(): + idx = pd.bdate_range("2024-01-02", periods=5) + return pd.DataFrame({ + "open": [9.8, 9.9, 10.5, 9.9, 9.3], "high": [10.1, 10.2, 13.1, 10.2, 9.7], + "low": [9.6, 9.8, 10.4, 9.8, 9.0], "close": [9.9, 10.2, 12.8, 9.4, 9.2], + "volume": [1_000_000] * 5, "avg_value_turnover20": [50_000_000] * 5, + "ema21_high": [10.5] * 5, "ema21_low": [9.5] * 5, "ema21_close": [10.0] * 5, + "atr14": [1.0] * 5, "extension_atr": [.2] * 5, "trend_state": ["UP"] * 5, + "price_state": ["INSIDE_STRUCTURE"] * 5, "rs_percentile": [95] * 5, + "regime": ["CONFIRMED_UPTREND"] * 5, "new_risk_allowed": [True] * 5, + "mco_z": [0.0] * 5, "mcsi_state": ["UP"] * 5, "weekly_trend_state": ["UP"] * 5, + "higher_low": [False] * 5, "volume_contracting": [True] * 5, + }, index=idx) + + +def market_frame(index): + return pd.DataFrame({"regime": ["CONFIRMED_UPTREND"] * len(index), + "new_risk_allowed": [True] * len(index)}, index=index) + + +class PortfolioBacktestTests(unittest.TestCase): + def test_position_sizing_respects_risk_and_position_cap(self): + sized = size_position(1_000_000, .005, 20, 18, lot_size=100, max_position_pct=.20) + self.assertEqual(sized["shares"], 2500) + self.assertEqual(sized["initial_risk"], 5000) + self.assertEqual(sized["position_value"], 50_000) + capped = size_position(1_000_000, .005, 20, 19.9, lot_size=100, max_position_pct=.02) + self.assertEqual(capped["shares"], 1000) + self.assertEqual(capped["position_value"], 20_000) + + def test_thai_board_lot_rounding_and_tick_grid(self): + self.assertEqual(round_quantity(199, 100), 100) + self.assertEqual(round_quantity(199, 50), 150) + self.assertEqual(round_to_tick(25.24, ThaiMarketRules(), direction="down"), 25.0) + self.assertEqual(round_to_tick(25.24, ThaiMarketRules(), direction="up"), 25.25) + self.assertEqual(round_to_tick(10.17, ThaiMarketRules(), direction="down"), 10.1) + self.assertEqual(round_to_tick(400.0, ThaiMarketRules()), 400.0) + + def test_portfolio_heat_and_cushion(self): + positions = [HeatPosition(initial_risk=1000, market_value=11000, cost_basis=10000), + HeatPosition(initial_risk=500, market_value=4500, cost_basis=5000)] + self.assertEqual(portfolio_heat(positions, 100_000), .015) + self.assertEqual(portfolio_cushion(positions, 100_000), .005) + + def _run(self, frame, *, market=None, **kwargs): + strategy = StrategyConfig(initial_equity=100_000, weakness_risk_pct=.01, + confirmation_risk_pct=.01, max_position_pct=.5, + min_value_turnover_thb=0, min_price=0, + enable_weekly=False, heat=RegimeHeat(confirmed_uptrend=1.0)) + return run_backtest({"ADVANC": frame}, market if market is not None else market_frame(frame.index), strategy=strategy, + rules=ThaiMarketRules(), costs=TradingCosts(commission_rate=0, slippage_bps=0), **kwargs) + + def test_market_regime_from_market_engine_blocks_new_risk(self): + frame = feature_frame() + blocked = market_frame(frame.index) + blocked["regime"] = "CORRECTION" + blocked["new_risk_allowed"] = False + result = self._run(frame, market=blocked) + self.assertTrue(result.fills.empty) + self.assertTrue(result.trades.empty) + + def test_accurate_membership_gates_new_entries_on_the_signal_date(self): + frame = feature_frame() + market = market_frame(frame.index) + signal_dates = {frame.index[1], frame.index[2]} + market.attrs["members_by_date"] = { + pd.Timestamp(date): ([] if date in signal_dates else ["ADVANC"]) + for date in frame.index + } + + result = self._run(frame, market=market) + + self.assertTrue(result.fills.empty) + self.assertTrue(result.trades.empty) + + def test_next_open_execution_two_r_partial_and_runner_attribution(self): + frame = feature_frame() + result = self._run(frame) + self.assertFalse(result.fills.empty) + buy = result.fills[result.fills["side"] == "BUY"].iloc[0] + self.assertEqual(buy["date"], frame.index[2]) + self.assertEqual(buy["price"], frame.loc[frame.index[2], "open"]) + trade = result.trades.iloc[0] + self.assertTrue(trade["two_r_hit"]) + self.assertGreater(trade["trim_2r_pnl"], 0) + self.assertLess(trade["runner_pnl"], 0) + self.assertAlmostEqual(trade["total_pnl"], trade["trim_2r_pnl"] + trade["runner_pnl"] + trade["dividend_cash"]) + trim = result.fills[result.fills["reason"] == "2R_trim"].iloc[0] + self.assertGreater(trim["shares"], 0) + self.assertLess(trim["shares"], buy["shares"]) + exit_fill = result.fills[result.fills["reason"] == "close_below_ema21_low_next_open"].iloc[0] + self.assertEqual(exit_fill["date"], frame.index[4]) + + def test_dividends_are_credited_while_position_is_open(self): + frame = feature_frame() + frame["dividends"] = [0, 0, 0, .5, 0] + frame["stock_splits"] = [0, 0, 0, 0, 0] + result = self._run(frame) + self.assertGreater(result.trades.iloc[0]["dividend_cash"], 0) + + def test_unadjusted_split_option_adjusts_open_runner_shares(self): + frame = feature_frame() + price_fields = ["open", "high", "low", "close", "ema21_high", "ema21_low", "ema21_close"] + frame.loc[frame.index[3], price_fields] /= 2 + frame.loc[frame.index[3], "stock_splits"] = 2.0 + frame.loc[frame.index[4], price_fields] /= 2 + frame["stock_splits"] = frame["stock_splits"].fillna(0) + result = self._run(frame, yahoo_split_adjusted=False) + buy = result.fills[result.fills["side"] == "BUY"].iloc[0] + trim = result.fills[result.fills["reason"] == "2R_trim"].iloc[0] + runner_exit = result.fills[result.fills["reason"] == "close_below_ema21_low_next_open"].iloc[0] + self.assertEqual(result.trades.iloc[0]["initial_shares"], buy["shares"]) + self.assertEqual(runner_exit["shares"], (buy["shares"] - trim["shares"]) * 2) + self.assertAlmostEqual(result.trades.iloc[0]["total_pnl"], + result.trades.iloc[0]["trim_2r_pnl"] + result.trades.iloc[0]["runner_pnl"]) diff --git a/tests/test_setups_scanner.py b/tests/test_setups_scanner.py new file mode 100644 index 0000000..c12123a --- /dev/null +++ b/tests/test_setups_scanner.py @@ -0,0 +1,94 @@ +import unittest + +import numpy as np +import pandas as pd + +from primethai.config import AppConfig, StrategyConfig +from primethai.scanner import scan_candidates +from primethai.setups import setup_for + + +class SetupScannerTests(unittest.TestCase): + def setUp(self): + self.previous = pd.Series({ + "open": 10.0, "high": 11.0, "low": 9.5, "close": 10.0, + "ema21_high": 10.0, + }) + self.row = pd.Series({ + "open": 10.1, "high": 10.8, "low": 9.7, "close": 10.5, + "ema21_high": 10.2, "ema21_close": 10.0, "ema21_low": 9.0, + "trend_state": "NEUTRAL", "higher_low": False, + "extension_atr": 0.5, + }) + + def test_scanner_setup_classifies_base_breakout_and_ema_reclaim(self): + setup, entry, stop = setup_for(self.row, self.previous, prior_high=10.4) + self.assertEqual(setup, "Base Breakout") + self.assertEqual((entry, stop), (10.5, 9.0)) + + # Keep the current close below yesterday's high so the EMA reclaim is distinct. + prior = self.previous.copy() + prior["high"] = 11.0 + prior["close"] = 10.0 + self.row["close"] = 10.5 + setup, _, _ = setup_for(self.row, prior, prior_high=11.0) + self.assertEqual(setup, "21DMA High Reclaim") + + def test_extension_filter_blocks_scanner_setups(self): + self.row["extension_atr"] = 1.2 + setup, entry, stop = setup_for(self.row, self.previous, prior_high=10.4) + self.assertIsNone(setup) + self.assertIsNone(entry) + self.assertIsNone(stop) + + def test_watch_only_candidate_has_no_position_size(self): + index = pd.bdate_range("2026-01-01", periods=25) + frame = pd.DataFrame({ + "open": 10.0, "high": 10.6, "low": 9.6, "close": 10.0, + "ema21_high": 9.5, "ema21_close": 9.4, "ema21_low": 9.0, + "trend_state": "NEUTRAL", "higher_low": False, + "extension_atr": 0.5, "avg_value_turnover20": 50_000_000.0, + "rs_percentile": 80.0, "volume_contracting": False, + "tightness_score": 50.0, "weekly_trend_state": "NEUTRAL", + "rs_1m": 0.0, "rs_3m": 0.0, "rs_12m": 0.0, + "rs_1m_vs_set": 0.0, "rs_3m_vs_set": 0.0, + "rs_12m_vs_set": 0.0, "rs_1m_vs_set100ew": 0.0, + "rs_3m_vs_set100ew": 0.0, "rs_12m_vs_set100ew": 0.0, + "52w_proximity_pct": 90.0, + }, index=index) + market = pd.DataFrame({ + "regime": ["CONFIRMED_UPTREND"], "new_risk_allowed": [True], + }, index=[index[-1]]) + config = AppConfig(strategy=StrategyConfig()) + + result = scan_candidates({"TEST": frame}, market, config=config) + + self.assertEqual(result.candidates.iloc[0]["entry_type"], "WATCH") + self.assertEqual(result.candidates.iloc[0]["shares"], 0) + self.assertEqual(result.candidates.iloc[0]["risk_pct"], 0.0) + + frame.loc[index[-1], "close"] = 10.8 + frame.loc[index[-1], "high"] = 11.0 + market.loc[index[-1], "new_risk_allowed"] = False + market.loc[index[-1], "regime"] = "CORRECTION" + + blocked = scan_candidates({"TEST": frame}, market, config=config) + + candidate = blocked.candidates.iloc[0] + self.assertEqual(candidate["setup"], "Watch") + self.assertEqual(candidate["entry_type"], "WATCH") + self.assertEqual(candidate["shares"], 0) + self.assertEqual(candidate["risk_pct"], 0.0) + self.assertIn("market regime blocks new entries", candidate["reasons"]) + + market.attrs["members_by_date"] = {pd.Timestamp(index[-1]): ["OTHER"]} + nonmember = scan_candidates({"TEST": frame}, market, config=config) + self.assertTrue(nonmember.candidates.empty) + + market.attrs["members_by_date"] = {pd.Timestamp(index[-1]): ["TEST"]} + stale = scan_candidates({"TEST": frame.iloc[:-1]}, market, config=config) + self.assertTrue(stale.candidates.empty) + + +if __name__ == "__main__": + unittest.main()