Files
SET100-Trading-system/primethai/webui.py
T

916 lines
46 KiB
Python

"""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()