Compare commits
29 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42c06929ea | ||
|
|
5facd22ef9 | ||
| 3af62ce598 | |||
|
|
62cd8a81a4 | ||
| dd8549b912 | |||
|
|
8bba85da1e | ||
| fc6083bd2a | |||
|
|
5f53b02da8 | ||
|
|
82808a8493 | ||
| 9456d66de4 | |||
| 33b97f21ac | |||
| 3b135c3080 | |||
| 1b0d5568d3 | |||
|
|
2406a80782 | ||
| b8569d9de1 | |||
|
|
92261da414 | ||
|
|
9267f1fb77 | ||
|
|
fd0246769a | ||
| ea7260d574 | |||
| a2855e286e | |||
| 28ded34441 | |||
|
|
08607eaa56 | ||
|
|
5c107d2435 | ||
|
|
6d7e6557d2 | ||
|
|
11b9ad126f | ||
|
|
2e394cd17c | ||
|
|
c641097fe7 | ||
|
|
2f3b2149d5 | ||
| 13a6d6612a |
@@ -13,6 +13,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
@@ -26,7 +28,18 @@ jobs:
|
|||||||
run: python3 scripts/session_handover_check.py --strict
|
run: python3 scripts/session_handover_check.py --strict
|
||||||
|
|
||||||
- name: Validate governance assets
|
- name: Validate governance assets
|
||||||
run: python3 scripts/validate_governance_assets.py
|
run: |
|
||||||
|
RANGE=""
|
||||||
|
if [ "${{ github.event_name }}" = "pull_request" ] && [ -n "${{ github.event.pull_request.base.sha }}" ]; then
|
||||||
|
RANGE="${{ github.event.pull_request.base.sha }}...${{ github.sha }}"
|
||||||
|
elif [ -n "${{ github.event.before }}" ] && [ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]; then
|
||||||
|
RANGE="${{ github.event.before }}...${{ github.sha }}"
|
||||||
|
fi
|
||||||
|
if [ -n "$RANGE" ]; then
|
||||||
|
python3 scripts/validate_governance_assets.py "$RANGE"
|
||||||
|
else
|
||||||
|
python3 scripts/validate_governance_assets.py
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Validate Ouroboros docs
|
- name: Validate Ouroboros docs
|
||||||
run: python3 scripts/validate_ouroboros_docs.py
|
run: python3 scripts/validate_ouroboros_docs.py
|
||||||
|
|||||||
@@ -3,9 +3,12 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
REQUIREMENTS_REGISTRY = "docs/ouroboros/01_requirements_registry.md"
|
||||||
|
|
||||||
|
|
||||||
def must_contain(path: Path, required: list[str], errors: list[str]) -> None:
|
def must_contain(path: Path, required: list[str], errors: list[str]) -> None:
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
@@ -17,8 +20,64 @@ def must_contain(path: Path, required: list[str], errors: list[str]) -> None:
|
|||||||
errors.append(f"{path}: missing required token -> {token}")
|
errors.append(f"{path}: missing required token -> {token}")
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_changed_path(path: str) -> str:
|
||||||
|
normalized = path.strip().replace("\\", "/")
|
||||||
|
if normalized.startswith("./"):
|
||||||
|
normalized = normalized[2:]
|
||||||
|
return normalized
|
||||||
|
|
||||||
|
|
||||||
|
def is_policy_file(path: str) -> bool:
|
||||||
|
normalized = normalize_changed_path(path)
|
||||||
|
if not normalized.endswith(".md"):
|
||||||
|
return False
|
||||||
|
if not normalized.startswith("docs/ouroboros/"):
|
||||||
|
return False
|
||||||
|
return normalized != REQUIREMENTS_REGISTRY
|
||||||
|
|
||||||
|
|
||||||
|
def load_changed_files(args: list[str], errors: list[str]) -> list[str]:
|
||||||
|
if not args:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Single range input (e.g. BASE..HEAD or BASE...HEAD)
|
||||||
|
if len(args) == 1 and ".." in args[0]:
|
||||||
|
range_spec = args[0]
|
||||||
|
try:
|
||||||
|
completed = subprocess.run(
|
||||||
|
["git", "diff", "--name-only", range_spec],
|
||||||
|
check=True,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
except (subprocess.CalledProcessError, FileNotFoundError) as exc:
|
||||||
|
errors.append(f"failed to load changed files from range '{range_spec}': {exc}")
|
||||||
|
return []
|
||||||
|
return [
|
||||||
|
normalize_changed_path(line)
|
||||||
|
for line in completed.stdout.splitlines()
|
||||||
|
if line.strip()
|
||||||
|
]
|
||||||
|
|
||||||
|
return [normalize_changed_path(path) for path in args if path.strip()]
|
||||||
|
|
||||||
|
|
||||||
|
def validate_registry_sync(changed_files: list[str], errors: list[str]) -> None:
|
||||||
|
if not changed_files:
|
||||||
|
return
|
||||||
|
|
||||||
|
changed_set = set(changed_files)
|
||||||
|
policy_changed = any(is_policy_file(path) for path in changed_set)
|
||||||
|
registry_changed = REQUIREMENTS_REGISTRY in changed_set
|
||||||
|
if policy_changed and not registry_changed:
|
||||||
|
errors.append(
|
||||||
|
"policy file changed without updating docs/ouroboros/01_requirements_registry.md"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
errors: list[str] = []
|
errors: list[str] = []
|
||||||
|
changed_files = load_changed_files(sys.argv[1:], errors)
|
||||||
|
|
||||||
pr_template = Path(".gitea/PULL_REQUEST_TEMPLATE.md")
|
pr_template = Path(".gitea/PULL_REQUEST_TEMPLATE.md")
|
||||||
issue_template = Path(".gitea/ISSUE_TEMPLATE/runtime_verification.md")
|
issue_template = Path(".gitea/ISSUE_TEMPLATE/runtime_verification.md")
|
||||||
@@ -81,6 +140,8 @@ def main() -> int:
|
|||||||
if not handover_script.exists():
|
if not handover_script.exists():
|
||||||
errors.append(f"missing file: {handover_script}")
|
errors.append(f"missing file: {handover_script}")
|
||||||
|
|
||||||
|
validate_registry_sync(changed_files, errors)
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
print("[FAIL] governance asset validation failed")
|
print("[FAIL] governance asset validation failed")
|
||||||
for err in errors:
|
for err in errors:
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ Implements first-touch labeling with upper/lower/time barriers.
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import warnings
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime, timedelta
|
||||||
from typing import Literal, Sequence
|
from typing import Literal, Sequence
|
||||||
|
|
||||||
|
|
||||||
@@ -16,9 +18,18 @@ TieBreakMode = Literal["stop_first", "take_first"]
|
|||||||
class TripleBarrierSpec:
|
class TripleBarrierSpec:
|
||||||
take_profit_pct: float
|
take_profit_pct: float
|
||||||
stop_loss_pct: float
|
stop_loss_pct: float
|
||||||
max_holding_bars: int
|
max_holding_bars: int | None = None
|
||||||
|
max_holding_minutes: int | None = None
|
||||||
tie_break: TieBreakMode = "stop_first"
|
tie_break: TieBreakMode = "stop_first"
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
if self.max_holding_minutes is None and self.max_holding_bars is None:
|
||||||
|
raise ValueError("one of max_holding_minutes or max_holding_bars must be set")
|
||||||
|
if self.max_holding_minutes is not None and self.max_holding_minutes <= 0:
|
||||||
|
raise ValueError("max_holding_minutes must be positive")
|
||||||
|
if self.max_holding_bars is not None and self.max_holding_bars <= 0:
|
||||||
|
raise ValueError("max_holding_bars must be positive")
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class TripleBarrierLabel:
|
class TripleBarrierLabel:
|
||||||
@@ -35,6 +46,7 @@ def label_with_triple_barrier(
|
|||||||
highs: Sequence[float],
|
highs: Sequence[float],
|
||||||
lows: Sequence[float],
|
lows: Sequence[float],
|
||||||
closes: Sequence[float],
|
closes: Sequence[float],
|
||||||
|
timestamps: Sequence[datetime] | None = None,
|
||||||
entry_index: int,
|
entry_index: int,
|
||||||
side: int,
|
side: int,
|
||||||
spec: TripleBarrierSpec,
|
spec: TripleBarrierSpec,
|
||||||
@@ -53,8 +65,6 @@ def label_with_triple_barrier(
|
|||||||
raise ValueError("highs, lows, closes lengths must match")
|
raise ValueError("highs, lows, closes lengths must match")
|
||||||
if entry_index < 0 or entry_index >= len(closes):
|
if entry_index < 0 or entry_index >= len(closes):
|
||||||
raise IndexError("entry_index out of range")
|
raise IndexError("entry_index out of range")
|
||||||
if spec.max_holding_bars <= 0:
|
|
||||||
raise ValueError("max_holding_bars must be positive")
|
|
||||||
|
|
||||||
entry_price = float(closes[entry_index])
|
entry_price = float(closes[entry_index])
|
||||||
if entry_price <= 0:
|
if entry_price <= 0:
|
||||||
@@ -68,13 +78,31 @@ def label_with_triple_barrier(
|
|||||||
upper = entry_price * (1.0 + spec.stop_loss_pct)
|
upper = entry_price * (1.0 + spec.stop_loss_pct)
|
||||||
lower = entry_price * (1.0 - spec.take_profit_pct)
|
lower = entry_price * (1.0 - spec.take_profit_pct)
|
||||||
|
|
||||||
|
if spec.max_holding_minutes is not None:
|
||||||
|
if timestamps is None:
|
||||||
|
raise ValueError("timestamps are required when max_holding_minutes is set")
|
||||||
|
if len(timestamps) != len(closes):
|
||||||
|
raise ValueError("timestamps length must match OHLC lengths")
|
||||||
|
expiry_timestamp = timestamps[entry_index] + timedelta(minutes=spec.max_holding_minutes)
|
||||||
|
last_index = entry_index
|
||||||
|
for idx in range(entry_index + 1, len(closes)):
|
||||||
|
if timestamps[idx] > expiry_timestamp:
|
||||||
|
break
|
||||||
|
last_index = idx
|
||||||
|
else:
|
||||||
|
assert spec.max_holding_bars is not None
|
||||||
|
warnings.warn(
|
||||||
|
"TripleBarrierSpec.max_holding_bars is deprecated; use max_holding_minutes with timestamps instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
last_index = min(len(closes) - 1, entry_index + spec.max_holding_bars)
|
last_index = min(len(closes) - 1, entry_index + spec.max_holding_bars)
|
||||||
for idx in range(entry_index + 1, last_index + 1):
|
for idx in range(entry_index + 1, last_index + 1):
|
||||||
h = float(highs[idx])
|
high_price = float(highs[idx])
|
||||||
l = float(lows[idx])
|
low_price = float(lows[idx])
|
||||||
|
|
||||||
up_touch = h >= upper
|
up_touch = high_price >= upper
|
||||||
down_touch = l <= lower
|
down_touch = low_price <= lower
|
||||||
if not up_touch and not down_touch:
|
if not up_touch and not down_touch:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,16 @@ class Settings(BaseSettings):
|
|||||||
# This value is used as a fallback when the balance API returns 0 in paper mode.
|
# This value is used as a fallback when the balance API returns 0 in paper mode.
|
||||||
PAPER_OVERSEAS_CASH: float = Field(default=50000.0, ge=0.0)
|
PAPER_OVERSEAS_CASH: float = Field(default=50000.0, ge=0.0)
|
||||||
USD_BUFFER_MIN: float = Field(default=1000.0, ge=0.0)
|
USD_BUFFER_MIN: float = Field(default=1000.0, ge=0.0)
|
||||||
|
US_MIN_PRICE: float = Field(default=5.0, ge=0.0)
|
||||||
|
STAGED_EXIT_BE_ARM_PCT: float = Field(default=1.2, gt=0.0, le=30.0)
|
||||||
|
STAGED_EXIT_ARM_PCT: float = Field(default=3.0, gt=0.0, le=100.0)
|
||||||
|
STOPLOSS_REENTRY_COOLDOWN_MINUTES: int = Field(default=120, ge=1, le=1440)
|
||||||
|
KR_ATR_STOP_MULTIPLIER_K: float = Field(default=2.0, ge=0.1, le=10.0)
|
||||||
|
KR_ATR_STOP_MIN_PCT: float = Field(default=-2.0, le=0.0)
|
||||||
|
KR_ATR_STOP_MAX_PCT: float = Field(default=-7.0, le=0.0)
|
||||||
OVERNIGHT_EXCEPTION_ENABLED: bool = True
|
OVERNIGHT_EXCEPTION_ENABLED: bool = True
|
||||||
|
SESSION_RISK_RELOAD_ENABLED: bool = True
|
||||||
|
SESSION_RISK_PROFILES_JSON: str = "{}"
|
||||||
|
|
||||||
# Trading frequency mode (daily = batch API calls, realtime = per-stock calls)
|
# Trading frequency mode (daily = batch API calls, realtime = per-stock calls)
|
||||||
TRADE_MODE: str = Field(default="daily", pattern="^(daily|realtime)$")
|
TRADE_MODE: str = Field(default="daily", pattern="^(daily|realtime)$")
|
||||||
|
|||||||
49
src/db.py
49
src/db.py
@@ -109,6 +109,7 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
stock_code TEXT NOT NULL,
|
stock_code TEXT NOT NULL,
|
||||||
market TEXT NOT NULL,
|
market TEXT NOT NULL,
|
||||||
exchange_code TEXT NOT NULL,
|
exchange_code TEXT NOT NULL,
|
||||||
|
session_id TEXT DEFAULT 'UNKNOWN',
|
||||||
action TEXT NOT NULL,
|
action TEXT NOT NULL,
|
||||||
confidence INTEGER NOT NULL,
|
confidence INTEGER NOT NULL,
|
||||||
rationale TEXT NOT NULL,
|
rationale TEXT NOT NULL,
|
||||||
@@ -121,6 +122,27 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
decision_columns = {
|
||||||
|
row[1]
|
||||||
|
for row in conn.execute("PRAGMA table_info(decision_logs)").fetchall()
|
||||||
|
}
|
||||||
|
if "session_id" not in decision_columns:
|
||||||
|
conn.execute("ALTER TABLE decision_logs ADD COLUMN session_id TEXT DEFAULT 'UNKNOWN'")
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
UPDATE decision_logs
|
||||||
|
SET session_id = 'UNKNOWN'
|
||||||
|
WHERE session_id IS NULL OR session_id = ''
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
if "outcome_pnl" not in decision_columns:
|
||||||
|
conn.execute("ALTER TABLE decision_logs ADD COLUMN outcome_pnl REAL")
|
||||||
|
if "outcome_accuracy" not in decision_columns:
|
||||||
|
conn.execute("ALTER TABLE decision_logs ADD COLUMN outcome_accuracy INTEGER")
|
||||||
|
if "reviewed" not in decision_columns:
|
||||||
|
conn.execute("ALTER TABLE decision_logs ADD COLUMN reviewed INTEGER DEFAULT 0")
|
||||||
|
if "review_notes" not in decision_columns:
|
||||||
|
conn.execute("ALTER TABLE decision_logs ADD COLUMN review_notes TEXT")
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
@@ -290,9 +312,34 @@ def _resolve_session_id(*, market: str, session_id: str | None) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def get_latest_buy_trade(
|
def get_latest_buy_trade(
|
||||||
conn: sqlite3.Connection, stock_code: str, market: str
|
conn: sqlite3.Connection,
|
||||||
|
stock_code: str,
|
||||||
|
market: str,
|
||||||
|
exchange_code: str | None = None,
|
||||||
) -> dict[str, Any] | None:
|
) -> dict[str, Any] | None:
|
||||||
"""Fetch the most recent BUY trade for a stock and market."""
|
"""Fetch the most recent BUY trade for a stock and market."""
|
||||||
|
if exchange_code:
|
||||||
|
cursor = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT decision_id, price, quantity
|
||||||
|
FROM trades
|
||||||
|
WHERE stock_code = ?
|
||||||
|
AND market = ?
|
||||||
|
AND action = 'BUY'
|
||||||
|
AND decision_id IS NOT NULL
|
||||||
|
AND (
|
||||||
|
exchange_code = ?
|
||||||
|
OR exchange_code IS NULL
|
||||||
|
OR exchange_code = ''
|
||||||
|
)
|
||||||
|
ORDER BY
|
||||||
|
CASE WHEN exchange_code = ? THEN 0 ELSE 1 END,
|
||||||
|
timestamp DESC
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
(stock_code, market, exchange_code, exchange_code),
|
||||||
|
)
|
||||||
|
else:
|
||||||
cursor = conn.execute(
|
cursor = conn.execute(
|
||||||
"""
|
"""
|
||||||
SELECT decision_id, price, quantity
|
SELECT decision_id, price, quantity
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ This module:
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import ast
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import sqlite3
|
import sqlite3
|
||||||
@@ -28,24 +29,24 @@ from src.logging.decision_logger import DecisionLogger
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
STRATEGIES_DIR = Path("src/strategies")
|
STRATEGIES_DIR = Path("src/strategies")
|
||||||
STRATEGY_TEMPLATE = textwrap.dedent("""\
|
STRATEGY_TEMPLATE = """\
|
||||||
\"\"\"Auto-generated strategy: {name}
|
\"\"\"Auto-generated strategy: {name}
|
||||||
|
|
||||||
Generated at: {timestamp}
|
Generated at: {timestamp}
|
||||||
Rationale: {rationale}
|
Rationale: {rationale}
|
||||||
\"\"\"
|
\"\"\"
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from src.strategies.base import BaseStrategy
|
from src.strategies.base import BaseStrategy
|
||||||
|
|
||||||
|
|
||||||
class {class_name}(BaseStrategy):
|
class {class_name}(BaseStrategy):
|
||||||
\"\"\"Strategy: {name}\"\"\"
|
\"\"\"Strategy: {name}\"\"\"
|
||||||
|
|
||||||
def evaluate(self, market_data: dict[str, Any]) -> dict[str, Any]:
|
def evaluate(self, market_data: dict[str, Any]) -> dict[str, Any]:
|
||||||
{body}
|
{body}
|
||||||
""")
|
"""
|
||||||
|
|
||||||
|
|
||||||
class EvolutionOptimizer:
|
class EvolutionOptimizer:
|
||||||
@@ -235,7 +236,8 @@ class EvolutionOptimizer:
|
|||||||
file_path = STRATEGIES_DIR / file_name
|
file_path = STRATEGIES_DIR / file_name
|
||||||
|
|
||||||
# Indent the body for the class method
|
# Indent the body for the class method
|
||||||
indented_body = textwrap.indent(body, " ")
|
normalized_body = textwrap.dedent(body).strip()
|
||||||
|
indented_body = textwrap.indent(normalized_body, " ")
|
||||||
|
|
||||||
# Generate rationale from patterns
|
# Generate rationale from patterns
|
||||||
rationale = f"Auto-evolved from {len(failures)} failures. "
|
rationale = f"Auto-evolved from {len(failures)} failures. "
|
||||||
@@ -247,9 +249,16 @@ class EvolutionOptimizer:
|
|||||||
timestamp=datetime.now(UTC).isoformat(),
|
timestamp=datetime.now(UTC).isoformat(),
|
||||||
rationale=rationale,
|
rationale=rationale,
|
||||||
class_name=class_name,
|
class_name=class_name,
|
||||||
body=indented_body.strip(),
|
body=indented_body.rstrip(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
parsed = ast.parse(content, filename=str(file_path))
|
||||||
|
compile(parsed, filename=str(file_path), mode="exec")
|
||||||
|
except SyntaxError as exc:
|
||||||
|
logger.warning("Generated strategy failed syntax validation: %s", exc)
|
||||||
|
return None
|
||||||
|
|
||||||
file_path.write_text(content)
|
file_path.write_text(content)
|
||||||
logger.info("Generated strategy file: %s", file_path)
|
logger.info("Generated strategy file: %s", file_path)
|
||||||
return file_path
|
return file_path
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class DecisionLog:
|
|||||||
stock_code: str
|
stock_code: str
|
||||||
market: str
|
market: str
|
||||||
exchange_code: str
|
exchange_code: str
|
||||||
|
session_id: str
|
||||||
action: str
|
action: str
|
||||||
confidence: int
|
confidence: int
|
||||||
rationale: str
|
rationale: str
|
||||||
@@ -47,6 +48,7 @@ class DecisionLogger:
|
|||||||
rationale: str,
|
rationale: str,
|
||||||
context_snapshot: dict[str, Any],
|
context_snapshot: dict[str, Any],
|
||||||
input_data: dict[str, Any],
|
input_data: dict[str, Any],
|
||||||
|
session_id: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Log a trading decision with full context.
|
"""Log a trading decision with full context.
|
||||||
|
|
||||||
@@ -59,20 +61,22 @@ class DecisionLogger:
|
|||||||
rationale: Reasoning for the decision
|
rationale: Reasoning for the decision
|
||||||
context_snapshot: L1-L7 context snapshot at decision time
|
context_snapshot: L1-L7 context snapshot at decision time
|
||||||
input_data: Market data inputs (price, volume, orderbook, etc.)
|
input_data: Market data inputs (price, volume, orderbook, etc.)
|
||||||
|
session_id: Runtime session identifier
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
decision_id: Unique identifier for this decision
|
decision_id: Unique identifier for this decision
|
||||||
"""
|
"""
|
||||||
decision_id = str(uuid.uuid4())
|
decision_id = str(uuid.uuid4())
|
||||||
timestamp = datetime.now(UTC).isoformat()
|
timestamp = datetime.now(UTC).isoformat()
|
||||||
|
resolved_session = session_id or "UNKNOWN"
|
||||||
|
|
||||||
self.conn.execute(
|
self.conn.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO decision_logs (
|
INSERT INTO decision_logs (
|
||||||
decision_id, timestamp, stock_code, market, exchange_code,
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
action, confidence, rationale, context_snapshot, input_data
|
session_id, action, confidence, rationale, context_snapshot, input_data
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
decision_id,
|
decision_id,
|
||||||
@@ -80,6 +84,7 @@ class DecisionLogger:
|
|||||||
stock_code,
|
stock_code,
|
||||||
market,
|
market,
|
||||||
exchange_code,
|
exchange_code,
|
||||||
|
resolved_session,
|
||||||
action,
|
action,
|
||||||
confidence,
|
confidence,
|
||||||
rationale,
|
rationale,
|
||||||
@@ -106,7 +111,7 @@ class DecisionLogger:
|
|||||||
query = """
|
query = """
|
||||||
SELECT
|
SELECT
|
||||||
decision_id, timestamp, stock_code, market, exchange_code,
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
action, confidence, rationale, context_snapshot, input_data,
|
session_id, action, confidence, rationale, context_snapshot, input_data,
|
||||||
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
||||||
FROM decision_logs
|
FROM decision_logs
|
||||||
WHERE reviewed = 0 AND confidence >= ?
|
WHERE reviewed = 0 AND confidence >= ?
|
||||||
@@ -168,7 +173,7 @@ class DecisionLogger:
|
|||||||
"""
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
decision_id, timestamp, stock_code, market, exchange_code,
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
action, confidence, rationale, context_snapshot, input_data,
|
session_id, action, confidence, rationale, context_snapshot, input_data,
|
||||||
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
||||||
FROM decision_logs
|
FROM decision_logs
|
||||||
WHERE decision_id = ?
|
WHERE decision_id = ?
|
||||||
@@ -196,7 +201,7 @@ class DecisionLogger:
|
|||||||
"""
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
decision_id, timestamp, stock_code, market, exchange_code,
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
action, confidence, rationale, context_snapshot, input_data,
|
session_id, action, confidence, rationale, context_snapshot, input_data,
|
||||||
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
outcome_pnl, outcome_accuracy, reviewed, review_notes
|
||||||
FROM decision_logs
|
FROM decision_logs
|
||||||
WHERE confidence >= ?
|
WHERE confidence >= ?
|
||||||
@@ -223,13 +228,14 @@ class DecisionLogger:
|
|||||||
stock_code=row[2],
|
stock_code=row[2],
|
||||||
market=row[3],
|
market=row[3],
|
||||||
exchange_code=row[4],
|
exchange_code=row[4],
|
||||||
action=row[5],
|
session_id=row[5] or "UNKNOWN",
|
||||||
confidence=row[6],
|
action=row[6],
|
||||||
rationale=row[7],
|
confidence=row[7],
|
||||||
context_snapshot=json.loads(row[8]),
|
rationale=row[8],
|
||||||
input_data=json.loads(row[9]),
|
context_snapshot=json.loads(row[9]),
|
||||||
outcome_pnl=row[10],
|
input_data=json.loads(row[10]),
|
||||||
outcome_accuracy=row[11],
|
outcome_pnl=row[11],
|
||||||
reviewed=bool(row[12]),
|
outcome_accuracy=row[12],
|
||||||
review_notes=row[13],
|
reviewed=bool(row[13]),
|
||||||
|
review_notes=row[14],
|
||||||
)
|
)
|
||||||
|
|||||||
507
src/main.py
507
src/main.py
@@ -70,6 +70,12 @@ BLACKOUT_ORDER_MANAGER = BlackoutOrderManager(
|
|||||||
_SESSION_CLOSE_WINDOWS = {"NXT_AFTER", "US_AFTER"}
|
_SESSION_CLOSE_WINDOWS = {"NXT_AFTER", "US_AFTER"}
|
||||||
_RUNTIME_EXIT_STATES: dict[str, PositionState] = {}
|
_RUNTIME_EXIT_STATES: dict[str, PositionState] = {}
|
||||||
_RUNTIME_EXIT_PEAKS: dict[str, float] = {}
|
_RUNTIME_EXIT_PEAKS: dict[str, float] = {}
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL: dict[str, float] = {}
|
||||||
|
_VOLATILITY_ANALYZER = VolatilityAnalyzer()
|
||||||
|
_SESSION_RISK_PROFILES_RAW = "{}"
|
||||||
|
_SESSION_RISK_PROFILES_MAP: dict[str, dict[str, Any]] = {}
|
||||||
|
_SESSION_RISK_LAST_BY_MARKET: dict[str, str] = {}
|
||||||
|
_SESSION_RISK_OVERRIDES_BY_MARKET: dict[str, dict[str, Any]] = {}
|
||||||
|
|
||||||
|
|
||||||
def safe_float(value: str | float | None, default: float = 0.0) -> float:
|
def safe_float(value: str | float | None, default: float = 0.0) -> float:
|
||||||
@@ -110,6 +116,258 @@ DAILY_TRADE_SESSIONS = 4 # Number of trading sessions per day
|
|||||||
TRADE_SESSION_INTERVAL_HOURS = 6 # Hours between sessions
|
TRADE_SESSION_INTERVAL_HOURS = 6 # Hours between sessions
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_sell_qty_for_pnl(*, sell_qty: int | None, buy_qty: int | None) -> int:
|
||||||
|
"""Choose quantity basis for SELL outcome PnL with safe fallback."""
|
||||||
|
resolved_sell = int(sell_qty or 0)
|
||||||
|
if resolved_sell > 0:
|
||||||
|
return resolved_sell
|
||||||
|
return max(0, int(buy_qty or 0))
|
||||||
|
|
||||||
|
|
||||||
|
def _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
*,
|
||||||
|
market: MarketInfo | None = None,
|
||||||
|
entry_price: float,
|
||||||
|
atr_value: float,
|
||||||
|
fallback_stop_loss_pct: float,
|
||||||
|
settings: Settings | None,
|
||||||
|
) -> float:
|
||||||
|
"""Compute KR dynamic hard-stop threshold in percent."""
|
||||||
|
if entry_price <= 0 or atr_value <= 0:
|
||||||
|
return fallback_stop_loss_pct
|
||||||
|
|
||||||
|
k = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="KR_ATR_STOP_MULTIPLIER_K",
|
||||||
|
default=2.0,
|
||||||
|
)
|
||||||
|
min_pct = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="KR_ATR_STOP_MIN_PCT",
|
||||||
|
default=-2.0,
|
||||||
|
)
|
||||||
|
max_pct = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="KR_ATR_STOP_MAX_PCT",
|
||||||
|
default=-7.0,
|
||||||
|
)
|
||||||
|
if max_pct > min_pct:
|
||||||
|
min_pct, max_pct = max_pct, min_pct
|
||||||
|
|
||||||
|
dynamic_stop_pct = -((k * atr_value) / entry_price) * 100.0
|
||||||
|
return max(max_pct, min(min_pct, dynamic_stop_pct))
|
||||||
|
|
||||||
|
|
||||||
|
def _stoploss_cooldown_key(*, market: MarketInfo, stock_code: str) -> str:
|
||||||
|
return f"{market.code}:{stock_code}"
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_session_risk_profiles(settings: Settings | None) -> dict[str, dict[str, Any]]:
|
||||||
|
if settings is None:
|
||||||
|
return {}
|
||||||
|
global _SESSION_RISK_PROFILES_RAW, _SESSION_RISK_PROFILES_MAP
|
||||||
|
raw = str(getattr(settings, "SESSION_RISK_PROFILES_JSON", "{}") or "{}")
|
||||||
|
if raw == _SESSION_RISK_PROFILES_RAW:
|
||||||
|
return _SESSION_RISK_PROFILES_MAP
|
||||||
|
|
||||||
|
parsed_map: dict[str, dict[str, Any]] = {}
|
||||||
|
try:
|
||||||
|
decoded = json.loads(raw)
|
||||||
|
if isinstance(decoded, dict):
|
||||||
|
for session_id, session_values in decoded.items():
|
||||||
|
if isinstance(session_id, str) and isinstance(session_values, dict):
|
||||||
|
parsed_map[session_id] = session_values
|
||||||
|
except (ValueError, TypeError) as exc:
|
||||||
|
logger.warning("Invalid SESSION_RISK_PROFILES_JSON; using defaults: %s", exc)
|
||||||
|
parsed_map = {}
|
||||||
|
|
||||||
|
_SESSION_RISK_PROFILES_RAW = raw
|
||||||
|
_SESSION_RISK_PROFILES_MAP = parsed_map
|
||||||
|
return _SESSION_RISK_PROFILES_MAP
|
||||||
|
|
||||||
|
|
||||||
|
def _coerce_setting_value(*, value: Any, default: Any) -> Any:
|
||||||
|
if isinstance(default, bool):
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return value
|
||||||
|
if isinstance(value, str):
|
||||||
|
return value.strip().lower() in {"1", "true", "yes", "on"}
|
||||||
|
if isinstance(value, (int, float)):
|
||||||
|
return value != 0
|
||||||
|
return default
|
||||||
|
if isinstance(default, int) and not isinstance(default, bool):
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return default
|
||||||
|
if isinstance(default, float):
|
||||||
|
return safe_float(value, float(default))
|
||||||
|
if isinstance(default, str):
|
||||||
|
return str(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _session_risk_overrides(
|
||||||
|
*,
|
||||||
|
market: MarketInfo | None,
|
||||||
|
settings: Settings | None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
if market is None or settings is None:
|
||||||
|
return {}
|
||||||
|
if not bool(getattr(settings, "SESSION_RISK_RELOAD_ENABLED", True)):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
session_id = get_session_info(market).session_id
|
||||||
|
previous_session = _SESSION_RISK_LAST_BY_MARKET.get(market.code)
|
||||||
|
if previous_session == session_id:
|
||||||
|
return _SESSION_RISK_OVERRIDES_BY_MARKET.get(market.code, {})
|
||||||
|
|
||||||
|
profile_map = _parse_session_risk_profiles(settings)
|
||||||
|
merged: dict[str, Any] = {}
|
||||||
|
default_profile = profile_map.get("default")
|
||||||
|
if isinstance(default_profile, dict):
|
||||||
|
merged.update(default_profile)
|
||||||
|
session_profile = profile_map.get(session_id)
|
||||||
|
if isinstance(session_profile, dict):
|
||||||
|
merged.update(session_profile)
|
||||||
|
|
||||||
|
_SESSION_RISK_LAST_BY_MARKET[market.code] = session_id
|
||||||
|
_SESSION_RISK_OVERRIDES_BY_MARKET[market.code] = merged
|
||||||
|
if previous_session is None:
|
||||||
|
logger.info(
|
||||||
|
"Session risk profile initialized for %s: %s (overrides=%s)",
|
||||||
|
market.code,
|
||||||
|
session_id,
|
||||||
|
",".join(sorted(merged.keys())) if merged else "none",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
"Session risk profile reloaded for %s: %s -> %s (overrides=%s)",
|
||||||
|
market.code,
|
||||||
|
previous_session,
|
||||||
|
session_id,
|
||||||
|
",".join(sorted(merged.keys())) if merged else "none",
|
||||||
|
)
|
||||||
|
return merged
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_market_setting(
|
||||||
|
*,
|
||||||
|
market: MarketInfo | None,
|
||||||
|
settings: Settings | None,
|
||||||
|
key: str,
|
||||||
|
default: Any,
|
||||||
|
) -> Any:
|
||||||
|
if settings is None:
|
||||||
|
return default
|
||||||
|
|
||||||
|
fallback = getattr(settings, key, default)
|
||||||
|
overrides = _session_risk_overrides(market=market, settings=settings)
|
||||||
|
if key not in overrides:
|
||||||
|
return fallback
|
||||||
|
return _coerce_setting_value(value=overrides[key], default=fallback)
|
||||||
|
|
||||||
|
|
||||||
|
def _stoploss_cooldown_minutes(
|
||||||
|
settings: Settings | None,
|
||||||
|
market: MarketInfo | None = None,
|
||||||
|
) -> int:
|
||||||
|
minutes = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="STOPLOSS_REENTRY_COOLDOWN_MINUTES",
|
||||||
|
default=120,
|
||||||
|
)
|
||||||
|
return max(1, int(minutes))
|
||||||
|
|
||||||
|
|
||||||
|
def _estimate_pred_down_prob_from_rsi(rsi: float | str | None) -> float:
|
||||||
|
"""Estimate downside probability from RSI using a simple linear mapping."""
|
||||||
|
if rsi is None:
|
||||||
|
return 0.5
|
||||||
|
rsi_value = max(0.0, min(100.0, safe_float(rsi, 50.0)))
|
||||||
|
return rsi_value / 100.0
|
||||||
|
|
||||||
|
|
||||||
|
async def _compute_kr_atr_value(
|
||||||
|
*,
|
||||||
|
broker: KISBroker,
|
||||||
|
stock_code: str,
|
||||||
|
period: int = 14,
|
||||||
|
) -> float:
|
||||||
|
"""Compute ATR(period) for KR stocks using daily OHLC."""
|
||||||
|
days = max(period + 1, 30)
|
||||||
|
try:
|
||||||
|
daily_prices = await _retry_connection(
|
||||||
|
broker.get_daily_prices,
|
||||||
|
stock_code,
|
||||||
|
days=days,
|
||||||
|
label=f"daily_prices:{stock_code}",
|
||||||
|
)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
logger.warning("ATR source unavailable for %s: %s", stock_code, exc)
|
||||||
|
return 0.0
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Unexpected ATR fetch failure for %s: %s", stock_code, exc)
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
if not isinstance(daily_prices, list):
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
highs: list[float] = []
|
||||||
|
lows: list[float] = []
|
||||||
|
closes: list[float] = []
|
||||||
|
for row in daily_prices:
|
||||||
|
if not isinstance(row, dict):
|
||||||
|
continue
|
||||||
|
high = safe_float(row.get("high"), 0.0)
|
||||||
|
low = safe_float(row.get("low"), 0.0)
|
||||||
|
close = safe_float(row.get("close"), 0.0)
|
||||||
|
if high <= 0 or low <= 0 or close <= 0:
|
||||||
|
continue
|
||||||
|
highs.append(high)
|
||||||
|
lows.append(low)
|
||||||
|
closes.append(close)
|
||||||
|
|
||||||
|
if len(highs) < period + 1 or len(lows) < period + 1 or len(closes) < period + 1:
|
||||||
|
return 0.0
|
||||||
|
return max(0.0, _VOLATILITY_ANALYZER.calculate_atr(highs, lows, closes, period=period))
|
||||||
|
|
||||||
|
|
||||||
|
async def _inject_staged_exit_features(
|
||||||
|
*,
|
||||||
|
market: MarketInfo,
|
||||||
|
stock_code: str,
|
||||||
|
open_position: dict[str, Any] | None,
|
||||||
|
market_data: dict[str, Any],
|
||||||
|
broker: KISBroker | None,
|
||||||
|
) -> None:
|
||||||
|
"""Inject ATR/pred_down_prob used by staged exit evaluation."""
|
||||||
|
if not open_position:
|
||||||
|
return
|
||||||
|
|
||||||
|
if "pred_down_prob" not in market_data:
|
||||||
|
market_data["pred_down_prob"] = _estimate_pred_down_prob_from_rsi(
|
||||||
|
market_data.get("rsi")
|
||||||
|
)
|
||||||
|
|
||||||
|
existing_atr = safe_float(market_data.get("atr_value"), 0.0)
|
||||||
|
if existing_atr > 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
if market.is_domestic and broker is not None:
|
||||||
|
market_data["atr_value"] = await _compute_kr_atr_value(
|
||||||
|
broker=broker,
|
||||||
|
stock_code=stock_code,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
market_data["atr_value"] = 0.0
|
||||||
|
|
||||||
|
|
||||||
async def _retry_connection(coro_factory: Any, *args: Any, label: str = "", **kwargs: Any) -> Any:
|
async def _retry_connection(coro_factory: Any, *args: Any, label: str = "", **kwargs: Any) -> Any:
|
||||||
"""Call an async function retrying on ConnectionError with exponential backoff.
|
"""Call an async function retrying on ConnectionError with exponential backoff.
|
||||||
|
|
||||||
@@ -217,6 +475,7 @@ async def sync_positions_from_broker(
|
|||||||
price=avg_price,
|
price=avg_price,
|
||||||
market=log_market,
|
market=log_market,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=get_session_info(market).session_id,
|
||||||
mode=settings.MODE,
|
mode=settings.MODE,
|
||||||
)
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -452,7 +711,14 @@ def _should_block_overseas_buy_for_fx_buffer(
|
|||||||
):
|
):
|
||||||
return False, total_cash - order_amount, 0.0
|
return False, total_cash - order_amount, 0.0
|
||||||
remaining = total_cash - order_amount
|
remaining = total_cash - order_amount
|
||||||
required = settings.USD_BUFFER_MIN
|
required = float(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="USD_BUFFER_MIN",
|
||||||
|
default=1000.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
return remaining < required, remaining, required
|
return remaining < required, remaining, required
|
||||||
|
|
||||||
|
|
||||||
@@ -468,7 +734,13 @@ def _should_force_exit_for_overnight(
|
|||||||
return True
|
return True
|
||||||
if settings is None:
|
if settings is None:
|
||||||
return False
|
return False
|
||||||
return not settings.OVERNIGHT_EXCEPTION_ENABLED
|
overnight_enabled = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="OVERNIGHT_EXCEPTION_ENABLED",
|
||||||
|
default=True,
|
||||||
|
)
|
||||||
|
return not bool(overnight_enabled)
|
||||||
|
|
||||||
|
|
||||||
def _build_runtime_position_key(
|
def _build_runtime_position_key(
|
||||||
@@ -498,6 +770,7 @@ def _apply_staged_exit_override_for_hold(
|
|||||||
open_position: dict[str, Any] | None,
|
open_position: dict[str, Any] | None,
|
||||||
market_data: dict[str, Any],
|
market_data: dict[str, Any],
|
||||||
stock_playbook: Any | None,
|
stock_playbook: Any | None,
|
||||||
|
settings: Settings | None = None,
|
||||||
) -> TradeDecision:
|
) -> TradeDecision:
|
||||||
"""Apply v2 staged exit semantics for HOLD positions using runtime state."""
|
"""Apply v2 staged exit semantics for HOLD positions using runtime state."""
|
||||||
if decision.action != "HOLD" or not open_position:
|
if decision.action != "HOLD" or not open_position:
|
||||||
@@ -513,6 +786,41 @@ def _apply_staged_exit_override_for_hold(
|
|||||||
if stock_playbook and stock_playbook.scenarios:
|
if stock_playbook and stock_playbook.scenarios:
|
||||||
stop_loss_threshold = stock_playbook.scenarios[0].stop_loss_pct
|
stop_loss_threshold = stock_playbook.scenarios[0].stop_loss_pct
|
||||||
take_profit_threshold = stock_playbook.scenarios[0].take_profit_pct
|
take_profit_threshold = stock_playbook.scenarios[0].take_profit_pct
|
||||||
|
atr_value = safe_float(market_data.get("atr_value"), 0.0)
|
||||||
|
if market.code == "KR":
|
||||||
|
stop_loss_threshold = _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
market=market,
|
||||||
|
entry_price=entry_price,
|
||||||
|
atr_value=atr_value,
|
||||||
|
fallback_stop_loss_pct=stop_loss_threshold,
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
if settings is None:
|
||||||
|
be_arm_pct = max(0.5, take_profit_threshold * 0.4)
|
||||||
|
arm_pct = take_profit_threshold
|
||||||
|
else:
|
||||||
|
be_arm_pct = max(
|
||||||
|
0.1,
|
||||||
|
float(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="STAGED_EXIT_BE_ARM_PCT",
|
||||||
|
default=1.2,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
arm_pct = max(
|
||||||
|
be_arm_pct,
|
||||||
|
float(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="STAGED_EXIT_ARM_PCT",
|
||||||
|
default=3.0,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
runtime_key = _build_runtime_position_key(
|
runtime_key = _build_runtime_position_key(
|
||||||
market_code=market.code,
|
market_code=market.code,
|
||||||
@@ -531,14 +839,14 @@ def _apply_staged_exit_override_for_hold(
|
|||||||
current_state=current_state,
|
current_state=current_state,
|
||||||
config=ExitRuleConfig(
|
config=ExitRuleConfig(
|
||||||
hard_stop_pct=stop_loss_threshold,
|
hard_stop_pct=stop_loss_threshold,
|
||||||
be_arm_pct=max(0.5, take_profit_threshold * 0.4),
|
be_arm_pct=be_arm_pct,
|
||||||
arm_pct=take_profit_threshold,
|
arm_pct=arm_pct,
|
||||||
),
|
),
|
||||||
inp=ExitRuleInput(
|
inp=ExitRuleInput(
|
||||||
current_price=current_price,
|
current_price=current_price,
|
||||||
entry_price=entry_price,
|
entry_price=entry_price,
|
||||||
peak_price=peak_price,
|
peak_price=peak_price,
|
||||||
atr_value=safe_float(market_data.get("atr_value"), 0.0),
|
atr_value=atr_value,
|
||||||
pred_down_prob=safe_float(market_data.get("pred_down_prob"), 0.0),
|
pred_down_prob=safe_float(market_data.get("pred_down_prob"), 0.0),
|
||||||
liquidity_weak=safe_float(market_data.get("volume_ratio"), 1.0) < 1.0,
|
liquidity_weak=safe_float(market_data.get("volume_ratio"), 1.0) < 1.0,
|
||||||
),
|
),
|
||||||
@@ -558,7 +866,7 @@ def _apply_staged_exit_override_for_hold(
|
|||||||
elif exit_eval.reason == "arm_take_profit":
|
elif exit_eval.reason == "arm_take_profit":
|
||||||
rationale = (
|
rationale = (
|
||||||
f"Take-profit triggered ({pnl_pct:.2f}% >= "
|
f"Take-profit triggered ({pnl_pct:.2f}% >= "
|
||||||
f"{take_profit_threshold:.2f}%)"
|
f"{arm_pct:.2f}%)"
|
||||||
)
|
)
|
||||||
elif exit_eval.reason == "atr_trailing_stop":
|
elif exit_eval.reason == "atr_trailing_stop":
|
||||||
rationale = "ATR trailing-stop triggered"
|
rationale = "ATR trailing-stop triggered"
|
||||||
@@ -750,6 +1058,20 @@ async def process_blackout_recovery_orders(
|
|||||||
|
|
||||||
accepted = result.get("rt_cd", "0") == "0"
|
accepted = result.get("rt_cd", "0") == "0"
|
||||||
if accepted:
|
if accepted:
|
||||||
|
runtime_session_id = get_session_info(market).session_id
|
||||||
|
log_trade(
|
||||||
|
conn=db_conn,
|
||||||
|
stock_code=intent.stock_code,
|
||||||
|
action=intent.order_type,
|
||||||
|
confidence=0,
|
||||||
|
rationale=f"[blackout-recovery] {intent.source}",
|
||||||
|
quantity=intent.quantity,
|
||||||
|
price=float(intent.price),
|
||||||
|
pnl=0.0,
|
||||||
|
market=market.code,
|
||||||
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
"Recovered queued order executed: %s %s (%s) qty=%d price=%.4f source=%s",
|
"Recovered queued order executed: %s %s (%s) qty=%d price=%.4f source=%s",
|
||||||
intent.order_type,
|
intent.order_type,
|
||||||
@@ -990,6 +1312,7 @@ async def trading_cycle(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Execute one trading cycle for a single stock."""
|
"""Execute one trading cycle for a single stock."""
|
||||||
cycle_start_time = asyncio.get_event_loop().time()
|
cycle_start_time = asyncio.get_event_loop().time()
|
||||||
|
_session_risk_overrides(market=market, settings=settings)
|
||||||
|
|
||||||
# 1. Fetch market data
|
# 1. Fetch market data
|
||||||
price_output: dict[str, Any] = {} # Populated for overseas markets; used for fallback metrics
|
price_output: dict[str, Any] = {} # Populated for overseas markets; used for fallback metrics
|
||||||
@@ -1239,7 +1562,14 @@ async def trading_cycle(
|
|||||||
|
|
||||||
# 2.1. Apply market_outlook-based BUY confidence threshold
|
# 2.1. Apply market_outlook-based BUY confidence threshold
|
||||||
if decision.action == "BUY":
|
if decision.action == "BUY":
|
||||||
base_threshold = (settings.CONFIDENCE_THRESHOLD if settings else 80)
|
base_threshold = int(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="CONFIDENCE_THRESHOLD",
|
||||||
|
default=80,
|
||||||
|
)
|
||||||
|
)
|
||||||
outlook = playbook.market_outlook
|
outlook = playbook.market_outlook
|
||||||
if outlook == MarketOutlook.BEARISH:
|
if outlook == MarketOutlook.BEARISH:
|
||||||
min_confidence = 90
|
min_confidence = 90
|
||||||
@@ -1291,6 +1621,48 @@ async def trading_cycle(
|
|||||||
stock_code,
|
stock_code,
|
||||||
market.name,
|
market.name,
|
||||||
)
|
)
|
||||||
|
elif market.code.startswith("US"):
|
||||||
|
min_price = float(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="US_MIN_PRICE",
|
||||||
|
default=5.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if current_price <= min_price:
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=(
|
||||||
|
f"US minimum price filter blocked BUY "
|
||||||
|
f"(price={current_price:.4f} <= {min_price:.4f})"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): US min price filter %.4f <= %.4f",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
current_price,
|
||||||
|
min_price,
|
||||||
|
)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
cooldown_key = _stoploss_cooldown_key(market=market, stock_code=stock_code)
|
||||||
|
now_epoch = datetime.now(UTC).timestamp()
|
||||||
|
cooldown_until = _STOPLOSS_REENTRY_COOLDOWN_UNTIL.get(cooldown_key, 0.0)
|
||||||
|
if now_epoch < cooldown_until:
|
||||||
|
remaining = int(cooldown_until - now_epoch)
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=f"Stop-loss reentry cooldown active ({remaining}s remaining)",
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): stop-loss cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
|
|
||||||
if decision.action == "HOLD":
|
if decision.action == "HOLD":
|
||||||
open_position = get_open_position(db_conn, stock_code, market.code)
|
open_position = get_open_position(db_conn, stock_code, market.code)
|
||||||
@@ -1299,6 +1671,13 @@ async def trading_cycle(
|
|||||||
market_code=market.code,
|
market_code=market.code,
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
)
|
)
|
||||||
|
await _inject_staged_exit_features(
|
||||||
|
market=market,
|
||||||
|
stock_code=stock_code,
|
||||||
|
open_position=open_position,
|
||||||
|
market_data=market_data,
|
||||||
|
broker=broker,
|
||||||
|
)
|
||||||
decision = _apply_staged_exit_override_for_hold(
|
decision = _apply_staged_exit_override_for_hold(
|
||||||
decision=decision,
|
decision=decision,
|
||||||
market=market,
|
market=market,
|
||||||
@@ -1306,6 +1685,7 @@ async def trading_cycle(
|
|||||||
open_position=open_position,
|
open_position=open_position,
|
||||||
market_data=market_data,
|
market_data=market_data,
|
||||||
stock_playbook=stock_playbook,
|
stock_playbook=stock_playbook,
|
||||||
|
settings=settings,
|
||||||
)
|
)
|
||||||
if open_position and decision.action == "HOLD" and _should_force_exit_for_overnight(
|
if open_position and decision.action == "HOLD" and _should_force_exit_for_overnight(
|
||||||
market=market,
|
market=market,
|
||||||
@@ -1368,10 +1748,12 @@ async def trading_cycle(
|
|||||||
"pnl_pct": pnl_pct,
|
"pnl_pct": pnl_pct,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime_session_id = get_session_info(market).session_id
|
||||||
decision_id = decision_logger.log_decision(
|
decision_id = decision_logger.log_decision(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
action=decision.action,
|
action=decision.action,
|
||||||
confidence=decision.confidence,
|
confidence=decision.confidence,
|
||||||
rationale=decision.rationale,
|
rationale=decision.rationale,
|
||||||
@@ -1636,6 +2018,7 @@ async def trading_cycle(
|
|||||||
pnl=0.0,
|
pnl=0.0,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
mode=settings.MODE if settings else "paper",
|
mode=settings.MODE if settings else "paper",
|
||||||
)
|
)
|
||||||
logger.info("Order result: %s", result.get("msg1", "OK"))
|
logger.info("Order result: %s", result.get("msg1", "OK"))
|
||||||
@@ -1655,16 +2038,34 @@ async def trading_cycle(
|
|||||||
logger.warning("Telegram notification failed: %s", exc)
|
logger.warning("Telegram notification failed: %s", exc)
|
||||||
|
|
||||||
if decision.action == "SELL" and order_succeeded:
|
if decision.action == "SELL" and order_succeeded:
|
||||||
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
buy_trade = get_latest_buy_trade(
|
||||||
|
db_conn,
|
||||||
|
stock_code,
|
||||||
|
market.code,
|
||||||
|
exchange_code=market.exchange_code,
|
||||||
|
)
|
||||||
if buy_trade and buy_trade.get("price") is not None:
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
buy_price = float(buy_trade["price"])
|
buy_price = float(buy_trade["price"])
|
||||||
buy_qty = int(buy_trade.get("quantity") or 1)
|
buy_qty = int(buy_trade.get("quantity") or 0)
|
||||||
trade_pnl = (trade_price - buy_price) * buy_qty
|
sell_qty = _resolve_sell_qty_for_pnl(sell_qty=quantity, buy_qty=buy_qty)
|
||||||
|
trade_pnl = (trade_price - buy_price) * sell_qty
|
||||||
decision_logger.update_outcome(
|
decision_logger.update_outcome(
|
||||||
decision_id=buy_trade["decision_id"],
|
decision_id=buy_trade["decision_id"],
|
||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
accuracy=1 if trade_pnl > 0 else 0,
|
accuracy=1 if trade_pnl > 0 else 0,
|
||||||
)
|
)
|
||||||
|
if trade_pnl < 0:
|
||||||
|
cooldown_key = _stoploss_cooldown_key(market=market, stock_code=stock_code)
|
||||||
|
cooldown_minutes = _stoploss_cooldown_minutes(settings, market=market)
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL[cooldown_key] = (
|
||||||
|
datetime.now(UTC).timestamp() + cooldown_minutes * 60
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Stop-loss cooldown set for %s (%s): %d minutes",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
cooldown_minutes,
|
||||||
|
)
|
||||||
|
|
||||||
# 6. Log trade with selection context (skip if order was rejected)
|
# 6. Log trade with selection context (skip if order was rejected)
|
||||||
if decision.action in ("BUY", "SELL") and not order_succeeded:
|
if decision.action in ("BUY", "SELL") and not order_succeeded:
|
||||||
@@ -1690,6 +2091,7 @@ async def trading_cycle(
|
|||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
selection_context=selection_context,
|
selection_context=selection_context,
|
||||||
decision_id=decision_id,
|
decision_id=decision_id,
|
||||||
mode=settings.MODE if settings else "paper",
|
mode=settings.MODE if settings else "paper",
|
||||||
@@ -2106,6 +2508,7 @@ async def run_daily_session(
|
|||||||
|
|
||||||
# Process each open market
|
# Process each open market
|
||||||
for market in open_markets:
|
for market in open_markets:
|
||||||
|
_session_risk_overrides(market=market, settings=settings)
|
||||||
await process_blackout_recovery_orders(
|
await process_blackout_recovery_orders(
|
||||||
broker=broker,
|
broker=broker,
|
||||||
overseas_broker=overseas_broker,
|
overseas_broker=overseas_broker,
|
||||||
@@ -2442,6 +2845,48 @@ async def run_daily_session(
|
|||||||
stock_code,
|
stock_code,
|
||||||
market.name,
|
market.name,
|
||||||
)
|
)
|
||||||
|
elif market.code.startswith("US"):
|
||||||
|
min_price = float(
|
||||||
|
_resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="US_MIN_PRICE",
|
||||||
|
default=5.0,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if stock_data["current_price"] <= min_price:
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=(
|
||||||
|
f"US minimum price filter blocked BUY "
|
||||||
|
f"(price={stock_data['current_price']:.4f} <= {min_price:.4f})"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): US min price filter %.4f <= %.4f",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
stock_data["current_price"],
|
||||||
|
min_price,
|
||||||
|
)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
cooldown_key = _stoploss_cooldown_key(market=market, stock_code=stock_code)
|
||||||
|
now_epoch = datetime.now(UTC).timestamp()
|
||||||
|
cooldown_until = _STOPLOSS_REENTRY_COOLDOWN_UNTIL.get(cooldown_key, 0.0)
|
||||||
|
if now_epoch < cooldown_until:
|
||||||
|
remaining = int(cooldown_until - now_epoch)
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=f"Stop-loss reentry cooldown active ({remaining}s remaining)",
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): stop-loss cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
if decision.action == "HOLD":
|
if decision.action == "HOLD":
|
||||||
daily_open = get_open_position(db_conn, stock_code, market.code)
|
daily_open = get_open_position(db_conn, stock_code, market.code)
|
||||||
if not daily_open:
|
if not daily_open:
|
||||||
@@ -2449,6 +2894,13 @@ async def run_daily_session(
|
|||||||
market_code=market.code,
|
market_code=market.code,
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
)
|
)
|
||||||
|
await _inject_staged_exit_features(
|
||||||
|
market=market,
|
||||||
|
stock_code=stock_code,
|
||||||
|
open_position=daily_open,
|
||||||
|
market_data=stock_data,
|
||||||
|
broker=broker,
|
||||||
|
)
|
||||||
decision = _apply_staged_exit_override_for_hold(
|
decision = _apply_staged_exit_override_for_hold(
|
||||||
decision=decision,
|
decision=decision,
|
||||||
market=market,
|
market=market,
|
||||||
@@ -2456,6 +2908,7 @@ async def run_daily_session(
|
|||||||
open_position=daily_open,
|
open_position=daily_open,
|
||||||
market_data=stock_data,
|
market_data=stock_data,
|
||||||
stock_playbook=stock_playbook,
|
stock_playbook=stock_playbook,
|
||||||
|
settings=settings,
|
||||||
)
|
)
|
||||||
if daily_open and decision.action == "HOLD" and _should_force_exit_for_overnight(
|
if daily_open and decision.action == "HOLD" and _should_force_exit_for_overnight(
|
||||||
market=market,
|
market=market,
|
||||||
@@ -2497,10 +2950,12 @@ async def run_daily_session(
|
|||||||
"pnl_pct": pnl_pct,
|
"pnl_pct": pnl_pct,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime_session_id = get_session_info(market).session_id
|
||||||
decision_id = decision_logger.log_decision(
|
decision_id = decision_logger.log_decision(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
action=decision.action,
|
action=decision.action,
|
||||||
confidence=decision.confidence,
|
confidence=decision.confidence,
|
||||||
rationale=decision.rationale,
|
rationale=decision.rationale,
|
||||||
@@ -2752,16 +3207,40 @@ async def run_daily_session(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if decision.action == "SELL" and order_succeeded:
|
if decision.action == "SELL" and order_succeeded:
|
||||||
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
buy_trade = get_latest_buy_trade(
|
||||||
|
db_conn,
|
||||||
|
stock_code,
|
||||||
|
market.code,
|
||||||
|
exchange_code=market.exchange_code,
|
||||||
|
)
|
||||||
if buy_trade and buy_trade.get("price") is not None:
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
buy_price = float(buy_trade["price"])
|
buy_price = float(buy_trade["price"])
|
||||||
buy_qty = int(buy_trade.get("quantity") or 1)
|
buy_qty = int(buy_trade.get("quantity") or 0)
|
||||||
trade_pnl = (trade_price - buy_price) * buy_qty
|
sell_qty = _resolve_sell_qty_for_pnl(
|
||||||
|
sell_qty=quantity,
|
||||||
|
buy_qty=buy_qty,
|
||||||
|
)
|
||||||
|
trade_pnl = (trade_price - buy_price) * sell_qty
|
||||||
decision_logger.update_outcome(
|
decision_logger.update_outcome(
|
||||||
decision_id=buy_trade["decision_id"],
|
decision_id=buy_trade["decision_id"],
|
||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
accuracy=1 if trade_pnl > 0 else 0,
|
accuracy=1 if trade_pnl > 0 else 0,
|
||||||
)
|
)
|
||||||
|
if trade_pnl < 0:
|
||||||
|
cooldown_key = _stoploss_cooldown_key(market=market, stock_code=stock_code)
|
||||||
|
cooldown_minutes = _stoploss_cooldown_minutes(
|
||||||
|
settings,
|
||||||
|
market=market,
|
||||||
|
)
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL[cooldown_key] = (
|
||||||
|
datetime.now(UTC).timestamp() + cooldown_minutes * 60
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Stop-loss cooldown set for %s (%s): %d minutes",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
cooldown_minutes,
|
||||||
|
)
|
||||||
|
|
||||||
# Log trade (skip if order was rejected by API)
|
# Log trade (skip if order was rejected by API)
|
||||||
if decision.action in ("BUY", "SELL") and not order_succeeded:
|
if decision.action in ("BUY", "SELL") and not order_succeeded:
|
||||||
@@ -2777,6 +3256,7 @@ async def run_daily_session(
|
|||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
|
session_id=runtime_session_id,
|
||||||
decision_id=decision_id,
|
decision_id=decision_id,
|
||||||
mode=settings.MODE,
|
mode=settings.MODE,
|
||||||
)
|
)
|
||||||
@@ -3559,6 +4039,7 @@ async def run(settings: Settings) -> None:
|
|||||||
break
|
break
|
||||||
|
|
||||||
session_info = get_session_info(market)
|
session_info = get_session_info(market)
|
||||||
|
_session_risk_overrides(market=market, settings=settings)
|
||||||
logger.info(
|
logger.info(
|
||||||
"Market session active: %s (%s) session=%s",
|
"Market session active: %s (%s) session=%s",
|
||||||
market.code,
|
market.code,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from src.db import get_open_position, init_db, log_trade
|
from src.db import get_latest_buy_trade, get_open_position, init_db, log_trade
|
||||||
|
|
||||||
|
|
||||||
def test_get_open_position_returns_latest_buy() -> None:
|
def test_get_open_position_returns_latest_buy() -> None:
|
||||||
@@ -329,3 +329,89 @@ def test_log_trade_unknown_market_falls_back_to_unknown_session() -> None:
|
|||||||
row = conn.execute("SELECT session_id FROM trades ORDER BY id DESC LIMIT 1").fetchone()
|
row = conn.execute("SELECT session_id FROM trades ORDER BY id DESC LIMIT 1").fetchone()
|
||||||
assert row is not None
|
assert row is not None
|
||||||
assert row[0] == "UNKNOWN"
|
assert row[0] == "UNKNOWN"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_latest_buy_trade_prefers_exchange_code_match() -> None:
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
log_trade(
|
||||||
|
conn=conn,
|
||||||
|
stock_code="AAPL",
|
||||||
|
action="BUY",
|
||||||
|
confidence=80,
|
||||||
|
rationale="legacy",
|
||||||
|
quantity=10,
|
||||||
|
price=120.0,
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="",
|
||||||
|
decision_id="legacy-buy",
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
conn=conn,
|
||||||
|
stock_code="AAPL",
|
||||||
|
action="BUY",
|
||||||
|
confidence=85,
|
||||||
|
rationale="matched",
|
||||||
|
quantity=5,
|
||||||
|
price=125.0,
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="NASD",
|
||||||
|
decision_id="matched-buy",
|
||||||
|
)
|
||||||
|
matched = get_latest_buy_trade(
|
||||||
|
conn,
|
||||||
|
stock_code="AAPL",
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="NASD",
|
||||||
|
)
|
||||||
|
assert matched is not None
|
||||||
|
assert matched["decision_id"] == "matched-buy"
|
||||||
|
|
||||||
|
|
||||||
|
def test_decision_logs_session_id_migration_backfills_unknown() -> None:
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
||||||
|
db_path = f.name
|
||||||
|
try:
|
||||||
|
old_conn = sqlite3.connect(db_path)
|
||||||
|
old_conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE decision_logs (
|
||||||
|
decision_id TEXT PRIMARY KEY,
|
||||||
|
timestamp TEXT NOT NULL,
|
||||||
|
stock_code TEXT NOT NULL,
|
||||||
|
market TEXT NOT NULL,
|
||||||
|
exchange_code TEXT NOT NULL,
|
||||||
|
action TEXT NOT NULL,
|
||||||
|
confidence INTEGER NOT NULL,
|
||||||
|
rationale TEXT NOT NULL,
|
||||||
|
context_snapshot TEXT NOT NULL,
|
||||||
|
input_data TEXT NOT NULL
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
old_conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO decision_logs (
|
||||||
|
decision_id, timestamp, stock_code, market, exchange_code,
|
||||||
|
action, confidence, rationale, context_snapshot, input_data
|
||||||
|
) VALUES (
|
||||||
|
'd1', '2026-01-01T00:00:00+00:00', 'AAPL', 'US_NASDAQ', 'NASD',
|
||||||
|
'BUY', 80, 'legacy row', '{}', '{}'
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
old_conn.commit()
|
||||||
|
old_conn.close()
|
||||||
|
|
||||||
|
conn = init_db(db_path)
|
||||||
|
columns = {row[1] for row in conn.execute("PRAGMA table_info(decision_logs)").fetchall()}
|
||||||
|
assert "session_id" in columns
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT session_id FROM decision_logs WHERE decision_id='d1'"
|
||||||
|
).fetchone()
|
||||||
|
assert row is not None
|
||||||
|
assert row[0] == "UNKNOWN"
|
||||||
|
conn.close()
|
||||||
|
finally:
|
||||||
|
os.unlink(db_path)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ def test_log_decision_creates_record(logger: DecisionLogger, db_conn: sqlite3.Co
|
|||||||
|
|
||||||
# Verify record exists in database
|
# Verify record exists in database
|
||||||
cursor = db_conn.execute(
|
cursor = db_conn.execute(
|
||||||
"SELECT decision_id, action, confidence FROM decision_logs WHERE decision_id = ?",
|
"SELECT decision_id, action, confidence, session_id FROM decision_logs WHERE decision_id = ?",
|
||||||
(decision_id,),
|
(decision_id,),
|
||||||
)
|
)
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
@@ -57,6 +57,7 @@ def test_log_decision_creates_record(logger: DecisionLogger, db_conn: sqlite3.Co
|
|||||||
assert row[0] == decision_id
|
assert row[0] == decision_id
|
||||||
assert row[1] == "BUY"
|
assert row[1] == "BUY"
|
||||||
assert row[2] == 85
|
assert row[2] == 85
|
||||||
|
assert row[3] == "UNKNOWN"
|
||||||
|
|
||||||
|
|
||||||
def test_log_decision_stores_context_snapshot(logger: DecisionLogger) -> None:
|
def test_log_decision_stores_context_snapshot(logger: DecisionLogger) -> None:
|
||||||
@@ -84,6 +85,24 @@ def test_log_decision_stores_context_snapshot(logger: DecisionLogger) -> None:
|
|||||||
assert decision is not None
|
assert decision is not None
|
||||||
assert decision.context_snapshot == context_snapshot
|
assert decision.context_snapshot == context_snapshot
|
||||||
assert decision.input_data == input_data
|
assert decision.input_data == input_data
|
||||||
|
assert decision.session_id == "UNKNOWN"
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_decision_stores_explicit_session_id(logger: DecisionLogger) -> None:
|
||||||
|
decision_id = logger.log_decision(
|
||||||
|
stock_code="AAPL",
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="NASD",
|
||||||
|
action="BUY",
|
||||||
|
confidence=88,
|
||||||
|
rationale="session check",
|
||||||
|
context_snapshot={},
|
||||||
|
input_data={},
|
||||||
|
session_id="US_PRE",
|
||||||
|
)
|
||||||
|
decision = logger.get_decision_by_id(decision_id)
|
||||||
|
assert decision is not None
|
||||||
|
assert decision.session_id == "US_PRE"
|
||||||
|
|
||||||
|
|
||||||
def test_get_unreviewed_decisions(logger: DecisionLogger) -> None:
|
def test_get_unreviewed_decisions(logger: DecisionLogger) -> None:
|
||||||
@@ -278,6 +297,7 @@ def test_decision_log_dataclass() -> None:
|
|||||||
stock_code="005930",
|
stock_code="005930",
|
||||||
market="KR",
|
market="KR",
|
||||||
exchange_code="KRX",
|
exchange_code="KRX",
|
||||||
|
session_id="KRX_REG",
|
||||||
action="BUY",
|
action="BUY",
|
||||||
confidence=85,
|
confidence=85,
|
||||||
rationale="Test",
|
rationale="Test",
|
||||||
@@ -286,6 +306,7 @@ def test_decision_log_dataclass() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert log.decision_id == "test-uuid"
|
assert log.decision_id == "test-uuid"
|
||||||
|
assert log.session_id == "KRX_REG"
|
||||||
assert log.action == "BUY"
|
assert log.action == "BUY"
|
||||||
assert log.confidence == 85
|
assert log.confidence == 85
|
||||||
assert log.reviewed is False
|
assert log.reviewed is False
|
||||||
|
|||||||
@@ -245,6 +245,52 @@ async def test_generate_strategy_creates_file(optimizer: EvolutionOptimizer, tmp
|
|||||||
assert "def evaluate" in strategy_path.read_text()
|
assert "def evaluate" in strategy_path.read_text()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_strategy_saves_valid_python_code(
|
||||||
|
optimizer: EvolutionOptimizer, tmp_path: Path,
|
||||||
|
) -> None:
|
||||||
|
"""Test that syntactically valid generated code is saved."""
|
||||||
|
failures = [{"decision_id": "1", "timestamp": "2024-01-15T09:30:00+00:00"}]
|
||||||
|
|
||||||
|
mock_response = Mock()
|
||||||
|
mock_response.text = (
|
||||||
|
'price = market_data.get("current_price", 0)\n'
|
||||||
|
'if price > 0:\n'
|
||||||
|
' return {"action": "BUY", "confidence": 80, "rationale": "Positive price"}\n'
|
||||||
|
'return {"action": "HOLD", "confidence": 50, "rationale": "No signal"}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||||
|
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||||
|
strategy_path = await optimizer.generate_strategy(failures)
|
||||||
|
|
||||||
|
assert strategy_path is not None
|
||||||
|
assert strategy_path.exists()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_generate_strategy_blocks_invalid_python_code(
|
||||||
|
optimizer: EvolutionOptimizer, tmp_path: Path, caplog: pytest.LogCaptureFixture,
|
||||||
|
) -> None:
|
||||||
|
"""Test that syntactically invalid generated code is not saved."""
|
||||||
|
failures = [{"decision_id": "1", "timestamp": "2024-01-15T09:30:00+00:00"}]
|
||||||
|
|
||||||
|
mock_response = Mock()
|
||||||
|
mock_response.text = (
|
||||||
|
'if market_data.get("current_price", 0) > 0\n'
|
||||||
|
' return {"action": "BUY", "confidence": 80, "rationale": "broken"}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||||
|
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||||
|
with caplog.at_level("WARNING"):
|
||||||
|
strategy_path = await optimizer.generate_strategy(failures)
|
||||||
|
|
||||||
|
assert strategy_path is None
|
||||||
|
assert list(tmp_path.glob("*.py")) == []
|
||||||
|
assert "failed syntax validation" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_generate_strategy_handles_api_error(optimizer: EvolutionOptimizer) -> None:
|
async def test_generate_strategy_handles_api_error(optimizer: EvolutionOptimizer) -> None:
|
||||||
"""Test that generate_strategy handles Gemini API errors gracefully."""
|
"""Test that generate_strategy handles Gemini API errors gracefully."""
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from datetime import UTC, date, datetime
|
|||||||
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import src.main as main_module
|
||||||
|
|
||||||
from src.config import Settings
|
from src.config import Settings
|
||||||
from src.context.layer import ContextLayer
|
from src.context.layer import ContextLayer
|
||||||
@@ -15,6 +16,14 @@ from src.evolution.scorecard import DailyScorecard
|
|||||||
from src.logging.decision_logger import DecisionLogger
|
from src.logging.decision_logger import DecisionLogger
|
||||||
from src.main import (
|
from src.main import (
|
||||||
KILL_SWITCH,
|
KILL_SWITCH,
|
||||||
|
_SESSION_RISK_LAST_BY_MARKET,
|
||||||
|
_SESSION_RISK_OVERRIDES_BY_MARKET,
|
||||||
|
_SESSION_RISK_PROFILES_MAP,
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL,
|
||||||
|
_apply_staged_exit_override_for_hold,
|
||||||
|
_compute_kr_atr_value,
|
||||||
|
_estimate_pred_down_prob_from_rsi,
|
||||||
|
_inject_staged_exit_features,
|
||||||
_RUNTIME_EXIT_PEAKS,
|
_RUNTIME_EXIT_PEAKS,
|
||||||
_RUNTIME_EXIT_STATES,
|
_RUNTIME_EXIT_STATES,
|
||||||
_should_force_exit_for_overnight,
|
_should_force_exit_for_overnight,
|
||||||
@@ -27,9 +36,13 @@ from src.main import (
|
|||||||
_extract_held_qty_from_balance,
|
_extract_held_qty_from_balance,
|
||||||
_handle_market_close,
|
_handle_market_close,
|
||||||
_retry_connection,
|
_retry_connection,
|
||||||
|
_resolve_market_setting,
|
||||||
|
_resolve_sell_qty_for_pnl,
|
||||||
_run_context_scheduler,
|
_run_context_scheduler,
|
||||||
_run_evolution_loop,
|
_run_evolution_loop,
|
||||||
_start_dashboard_server,
|
_start_dashboard_server,
|
||||||
|
_stoploss_cooldown_minutes,
|
||||||
|
_compute_kr_dynamic_stop_loss_pct,
|
||||||
handle_domestic_pending_orders,
|
handle_domestic_pending_orders,
|
||||||
handle_overseas_pending_orders,
|
handle_overseas_pending_orders,
|
||||||
process_blackout_recovery_orders,
|
process_blackout_recovery_orders,
|
||||||
@@ -92,10 +105,20 @@ def _reset_kill_switch_state() -> None:
|
|||||||
KILL_SWITCH.clear_block()
|
KILL_SWITCH.clear_block()
|
||||||
_RUNTIME_EXIT_STATES.clear()
|
_RUNTIME_EXIT_STATES.clear()
|
||||||
_RUNTIME_EXIT_PEAKS.clear()
|
_RUNTIME_EXIT_PEAKS.clear()
|
||||||
|
_SESSION_RISK_LAST_BY_MARKET.clear()
|
||||||
|
_SESSION_RISK_OVERRIDES_BY_MARKET.clear()
|
||||||
|
_SESSION_RISK_PROFILES_MAP.clear()
|
||||||
|
main_module._SESSION_RISK_PROFILES_RAW = "__reset__"
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL.clear()
|
||||||
yield
|
yield
|
||||||
KILL_SWITCH.clear_block()
|
KILL_SWITCH.clear_block()
|
||||||
_RUNTIME_EXIT_STATES.clear()
|
_RUNTIME_EXIT_STATES.clear()
|
||||||
_RUNTIME_EXIT_PEAKS.clear()
|
_RUNTIME_EXIT_PEAKS.clear()
|
||||||
|
_SESSION_RISK_LAST_BY_MARKET.clear()
|
||||||
|
_SESSION_RISK_OVERRIDES_BY_MARKET.clear()
|
||||||
|
_SESSION_RISK_PROFILES_MAP.clear()
|
||||||
|
main_module._SESSION_RISK_PROFILES_RAW = "__reset__"
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL.clear()
|
||||||
|
|
||||||
|
|
||||||
class TestExtractAvgPriceFromBalance:
|
class TestExtractAvgPriceFromBalance:
|
||||||
@@ -119,6 +142,266 @@ class TestExtractAvgPriceFromBalance:
|
|||||||
result = _extract_avg_price_from_balance(balance, "005930", is_domestic=True)
|
result = _extract_avg_price_from_balance(balance, "005930", is_domestic=True)
|
||||||
assert result == 0.0
|
assert result == 0.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_sell_qty_for_pnl_prefers_sell_qty() -> None:
|
||||||
|
assert _resolve_sell_qty_for_pnl(sell_qty=30, buy_qty=100) == 30
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_sell_qty_for_pnl_uses_buy_qty_fallback_when_sell_qty_missing() -> None:
|
||||||
|
assert _resolve_sell_qty_for_pnl(sell_qty=None, buy_qty=12) == 12
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_sell_qty_for_pnl_returns_zero_when_both_missing() -> None:
|
||||||
|
assert _resolve_sell_qty_for_pnl(sell_qty=None, buy_qty=None) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_compute_kr_dynamic_stop_loss_pct_falls_back_without_atr() -> None:
|
||||||
|
out = _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
entry_price=100.0,
|
||||||
|
atr_value=0.0,
|
||||||
|
fallback_stop_loss_pct=-2.0,
|
||||||
|
settings=None,
|
||||||
|
)
|
||||||
|
assert out == -2.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_compute_kr_dynamic_stop_loss_pct_clamps_to_min_and_max() -> None:
|
||||||
|
# Small ATR -> clamp to min (-2%)
|
||||||
|
out_small = _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
entry_price=100.0,
|
||||||
|
atr_value=0.2,
|
||||||
|
fallback_stop_loss_pct=-2.0,
|
||||||
|
settings=None,
|
||||||
|
)
|
||||||
|
assert out_small == -2.0
|
||||||
|
|
||||||
|
# Large ATR -> clamp to max (-7%)
|
||||||
|
out_large = _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
entry_price=100.0,
|
||||||
|
atr_value=10.0,
|
||||||
|
fallback_stop_loss_pct=-2.0,
|
||||||
|
settings=None,
|
||||||
|
)
|
||||||
|
assert out_large == -7.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_compute_kr_dynamic_stop_loss_pct_uses_settings_values() -> None:
|
||||||
|
settings = MagicMock(
|
||||||
|
KR_ATR_STOP_MULTIPLIER_K=3.0,
|
||||||
|
KR_ATR_STOP_MIN_PCT=-1.5,
|
||||||
|
KR_ATR_STOP_MAX_PCT=-6.0,
|
||||||
|
)
|
||||||
|
out = _compute_kr_dynamic_stop_loss_pct(
|
||||||
|
entry_price=100.0,
|
||||||
|
atr_value=1.0,
|
||||||
|
fallback_stop_loss_pct=-2.0,
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
assert out == -3.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_market_setting_uses_session_profile_override() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
SESSION_RISK_PROFILES_JSON='{"US_PRE": {"US_MIN_PRICE": 7.5}}',
|
||||||
|
)
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
|
||||||
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_PRE")):
|
||||||
|
value = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="US_MIN_PRICE",
|
||||||
|
default=5.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert value == pytest.approx(7.5)
|
||||||
|
|
||||||
|
|
||||||
|
def test_stoploss_cooldown_minutes_uses_session_override() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
STOPLOSS_REENTRY_COOLDOWN_MINUTES=120,
|
||||||
|
SESSION_RISK_PROFILES_JSON='{"NXT_AFTER": {"STOPLOSS_REENTRY_COOLDOWN_MINUTES": 45}}',
|
||||||
|
)
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "KR"
|
||||||
|
|
||||||
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="NXT_AFTER")):
|
||||||
|
value = _stoploss_cooldown_minutes(settings, market=market)
|
||||||
|
|
||||||
|
assert value == 45
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_market_setting_ignores_profile_when_reload_disabled() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
US_MIN_PRICE=5.0,
|
||||||
|
SESSION_RISK_RELOAD_ENABLED=False,
|
||||||
|
SESSION_RISK_PROFILES_JSON='{"US_PRE": {"US_MIN_PRICE": 9.5}}',
|
||||||
|
)
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
|
||||||
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_PRE")):
|
||||||
|
value = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="US_MIN_PRICE",
|
||||||
|
default=5.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert value == pytest.approx(5.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_market_setting_falls_back_on_invalid_profile_json() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
US_MIN_PRICE=5.0,
|
||||||
|
SESSION_RISK_PROFILES_JSON="{invalid-json",
|
||||||
|
)
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
|
||||||
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_PRE")):
|
||||||
|
value = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="US_MIN_PRICE",
|
||||||
|
default=5.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert value == pytest.approx(5.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_market_setting_coerces_bool_string_override() -> None:
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
OVERNIGHT_EXCEPTION_ENABLED=True,
|
||||||
|
SESSION_RISK_PROFILES_JSON='{"US_AFTER": {"OVERNIGHT_EXCEPTION_ENABLED": "false"}}',
|
||||||
|
)
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
|
||||||
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_AFTER")):
|
||||||
|
value = _resolve_market_setting(
|
||||||
|
market=market,
|
||||||
|
settings=settings,
|
||||||
|
key="OVERNIGHT_EXCEPTION_ENABLED",
|
||||||
|
default=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert value is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_estimate_pred_down_prob_from_rsi_uses_linear_mapping() -> None:
|
||||||
|
assert _estimate_pred_down_prob_from_rsi(None) == 0.5
|
||||||
|
assert _estimate_pred_down_prob_from_rsi(0.0) == 0.0
|
||||||
|
assert _estimate_pred_down_prob_from_rsi(50.0) == 0.5
|
||||||
|
assert _estimate_pred_down_prob_from_rsi(100.0) == 1.0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_compute_kr_atr_value_returns_zero_on_short_series() -> None:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_daily_prices = AsyncMock(
|
||||||
|
return_value=[{"high": 101.0, "low": 99.0, "close": 100.0}] * 10
|
||||||
|
)
|
||||||
|
|
||||||
|
atr = await _compute_kr_atr_value(broker=broker, stock_code="005930")
|
||||||
|
assert atr == 0.0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_inject_staged_exit_features_sets_pred_down_prob_and_atr_for_kr() -> None:
|
||||||
|
market = MagicMock()
|
||||||
|
market.is_domestic = True
|
||||||
|
stock_data: dict[str, float] = {"rsi": 65.0}
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_daily_prices = AsyncMock(
|
||||||
|
return_value=[
|
||||||
|
{"high": 102.0 + i, "low": 98.0 + i, "close": 100.0 + i}
|
||||||
|
for i in range(40)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
await _inject_staged_exit_features(
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
open_position={"price": 100.0, "quantity": 1},
|
||||||
|
market_data=stock_data,
|
||||||
|
broker=broker,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert stock_data["pred_down_prob"] == pytest.approx(0.65)
|
||||||
|
assert stock_data["atr_value"] > 0.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_apply_staged_exit_uses_independent_arm_threshold_settings() -> None:
|
||||||
|
market = MagicMock()
|
||||||
|
market.code = "KR"
|
||||||
|
market.name = "Korea"
|
||||||
|
|
||||||
|
decision = MagicMock()
|
||||||
|
decision.action = "HOLD"
|
||||||
|
decision.confidence = 70
|
||||||
|
decision.rationale = "hold"
|
||||||
|
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
STAGED_EXIT_BE_ARM_PCT=2.2,
|
||||||
|
STAGED_EXIT_ARM_PCT=5.4,
|
||||||
|
)
|
||||||
|
|
||||||
|
captured: dict[str, float] = {}
|
||||||
|
|
||||||
|
def _fake_eval(**kwargs): # type: ignore[no-untyped-def]
|
||||||
|
cfg = kwargs["config"]
|
||||||
|
captured["be_arm_pct"] = cfg.be_arm_pct
|
||||||
|
captured["arm_pct"] = cfg.arm_pct
|
||||||
|
|
||||||
|
class _Out:
|
||||||
|
should_exit = False
|
||||||
|
reason = "none"
|
||||||
|
state = PositionState.HOLDING
|
||||||
|
|
||||||
|
return _Out()
|
||||||
|
|
||||||
|
with patch("src.main.evaluate_exit", side_effect=_fake_eval):
|
||||||
|
out = _apply_staged_exit_override_for_hold(
|
||||||
|
decision=decision,
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
open_position={"price": 100.0, "quantity": 1, "decision_id": "d1", "timestamp": "t1"},
|
||||||
|
market_data={"current_price": 101.0, "rsi": 60.0, "pred_down_prob": 0.6},
|
||||||
|
stock_playbook=None,
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert out is decision
|
||||||
|
assert captured["be_arm_pct"] == pytest.approx(2.2)
|
||||||
|
assert captured["arm_pct"] == pytest.approx(5.4)
|
||||||
|
|
||||||
def test_returns_zero_when_field_empty_string(self) -> None:
|
def test_returns_zero_when_field_empty_string(self) -> None:
|
||||||
"""Returns 0.0 when pchs_avg_pric is an empty string."""
|
"""Returns 0.0 when pchs_avg_pric is an empty string."""
|
||||||
balance = {"output1": [{"pdno": "005930", "pchs_avg_pric": ""}]}
|
balance = {"output1": [{"pdno": "005930", "pchs_avg_pric": ""}]}
|
||||||
@@ -2040,6 +2323,105 @@ async def test_sell_updates_original_buy_decision_outcome() -> None:
|
|||||||
assert updated_buy is not None
|
assert updated_buy is not None
|
||||||
assert updated_buy.outcome_pnl == 20.0
|
assert updated_buy.outcome_pnl == 20.0
|
||||||
assert updated_buy.outcome_accuracy == 1
|
assert updated_buy.outcome_accuracy == 1
|
||||||
|
assert "KR:005930" not in _STOPLOSS_REENTRY_COOLDOWN_UNTIL
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_stoploss_reentry_cooldown_blocks_buy_when_active() -> None:
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL["KR:005930"] = datetime.now(UTC).timestamp() + 300
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(100.0, 0.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"tot_evlu_amt": "100000", "dnca_tot_amt": "50000", "pchs_amt_smtl_amt": "50000"}],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=MagicMock(evaluate=MagicMock(return_value=_make_buy_match("005930"))),
|
||||||
|
playbook=_make_playbook(),
|
||||||
|
risk=MagicMock(validate_order=MagicMock(), check_circuit_breaker=MagicMock()),
|
||||||
|
db_conn=db_conn,
|
||||||
|
decision_logger=DecisionLogger(db_conn),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None), set_context=MagicMock()),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
settings=MagicMock(POSITION_SIZING_ENABLED=False, CONFIDENCE_THRESHOLD=80, MODE="paper"),
|
||||||
|
)
|
||||||
|
|
||||||
|
broker.send_order.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_stoploss_reentry_cooldown_allows_buy_after_expiry() -> None:
|
||||||
|
_STOPLOSS_REENTRY_COOLDOWN_UNTIL["KR:005930"] = datetime.now(UTC).timestamp() - 10
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(100.0, 0.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"tot_evlu_amt": "100000", "dnca_tot_amt": "50000", "pchs_amt_smtl_amt": "50000"}],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=MagicMock(evaluate=MagicMock(return_value=_make_buy_match("005930"))),
|
||||||
|
playbook=_make_playbook(),
|
||||||
|
risk=MagicMock(validate_order=MagicMock(), check_circuit_breaker=MagicMock()),
|
||||||
|
db_conn=db_conn,
|
||||||
|
decision_logger=DecisionLogger(db_conn),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None), set_context=MagicMock()),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
settings=MagicMock(POSITION_SIZING_ENABLED=False, CONFIDENCE_THRESHOLD=80, MODE="paper"),
|
||||||
|
)
|
||||||
|
|
||||||
|
broker.send_order.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@@ -2750,6 +3132,9 @@ async def test_sell_order_uses_broker_balance_qty_not_db() -> None:
|
|||||||
assert call_kwargs["order_type"] == "SELL"
|
assert call_kwargs["order_type"] == "SELL"
|
||||||
# Must use broker-confirmed qty (5), NOT DB-recorded ordered qty (10)
|
# Must use broker-confirmed qty (5), NOT DB-recorded ordered qty (10)
|
||||||
assert call_kwargs["quantity"] == 5
|
assert call_kwargs["quantity"] == 5
|
||||||
|
updated_buy = decision_logger.get_decision_by_id(buy_decision_id)
|
||||||
|
assert updated_buy is not None
|
||||||
|
assert updated_buy.outcome_pnl == -25.0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@@ -5654,6 +6039,149 @@ async def test_order_policy_rejection_skips_order_execution() -> None:
|
|||||||
broker.send_order.assert_not_called()
|
broker.send_order.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("price", "should_block"),
|
||||||
|
[
|
||||||
|
(4.99, True),
|
||||||
|
(5.00, True),
|
||||||
|
(5.01, False),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_us_min_price_filter_boundary(price: float, should_block: bool) -> None:
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
decision_logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_balance = AsyncMock(return_value={"output1": [], "output2": [{}]})
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": str(price), "rate": "0.0"}}
|
||||||
|
)
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(
|
||||||
|
return_value={"output1": [], "output2": [{"frcr_evlu_tota": "10000", "frcr_buy_amt_smtl": "0"}]}
|
||||||
|
)
|
||||||
|
overseas_broker.get_overseas_buying_power = AsyncMock(
|
||||||
|
return_value={"output": {"ovrs_ord_psbl_amt": "10000"}}
|
||||||
|
)
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "NASDAQ"
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
market.exchange_code = "NASD"
|
||||||
|
market.is_domestic = False
|
||||||
|
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_trade_execution = AsyncMock()
|
||||||
|
telegram.notify_fat_finger = AsyncMock()
|
||||||
|
telegram.notify_circuit_breaker = AsyncMock()
|
||||||
|
telegram.notify_scenario_matched = AsyncMock()
|
||||||
|
|
||||||
|
settings = MagicMock()
|
||||||
|
settings.POSITION_SIZING_ENABLED = False
|
||||||
|
settings.CONFIDENCE_THRESHOLD = 80
|
||||||
|
settings.MODE = "paper"
|
||||||
|
settings.PAPER_OVERSEAS_CASH = 50000
|
||||||
|
settings.US_MIN_PRICE = 5.0
|
||||||
|
settings.USD_BUFFER_MIN = 1000.0
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=MagicMock(evaluate=MagicMock(return_value=_make_buy_match("AAPL"))),
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(validate_order=MagicMock(), check_circuit_breaker=MagicMock()),
|
||||||
|
db_conn=db_conn,
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(
|
||||||
|
get_latest_timeframe=MagicMock(return_value=None),
|
||||||
|
set_context=MagicMock(),
|
||||||
|
),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=telegram,
|
||||||
|
market=market,
|
||||||
|
stock_code="AAPL",
|
||||||
|
scan_candidates={},
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
|
||||||
|
if should_block:
|
||||||
|
overseas_broker.send_overseas_order.assert_not_called()
|
||||||
|
else:
|
||||||
|
overseas_broker.send_overseas_order.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_us_min_price_filter_not_applied_to_kr_market() -> None:
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
decision_logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(4.0, 0.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"tot_evlu_amt": "100000",
|
||||||
|
"dnca_tot_amt": "50000",
|
||||||
|
"pchs_amt_smtl_amt": "50000",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_trade_execution = AsyncMock()
|
||||||
|
telegram.notify_fat_finger = AsyncMock()
|
||||||
|
telegram.notify_circuit_breaker = AsyncMock()
|
||||||
|
telegram.notify_scenario_matched = AsyncMock()
|
||||||
|
|
||||||
|
settings = MagicMock()
|
||||||
|
settings.POSITION_SIZING_ENABLED = False
|
||||||
|
settings.CONFIDENCE_THRESHOLD = 80
|
||||||
|
settings.MODE = "paper"
|
||||||
|
settings.US_MIN_PRICE = 5.0
|
||||||
|
settings.USD_BUFFER_MIN = 1000.0
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=MagicMock(evaluate=MagicMock(return_value=_make_buy_match("005930"))),
|
||||||
|
playbook=_make_playbook(),
|
||||||
|
risk=MagicMock(validate_order=MagicMock(), check_circuit_breaker=MagicMock()),
|
||||||
|
db_conn=db_conn,
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(
|
||||||
|
get_latest_timeframe=MagicMock(return_value=None),
|
||||||
|
set_context=MagicMock(),
|
||||||
|
),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=telegram,
|
||||||
|
market=market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
settings=settings,
|
||||||
|
)
|
||||||
|
|
||||||
|
broker.send_order.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
def test_overnight_policy_prioritizes_killswitch_over_exception() -> None:
|
def test_overnight_policy_prioritizes_killswitch_over_exception() -> None:
|
||||||
market = MagicMock()
|
market = MagicMock()
|
||||||
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_AFTER")):
|
with patch("src.main.get_session_info", return_value=MagicMock(session_id="US_AFTER")):
|
||||||
@@ -5837,6 +6365,7 @@ async def test_process_blackout_recovery_executes_valid_intents() -> None:
|
|||||||
patch("src.main.MARKETS", {"KR": market}),
|
patch("src.main.MARKETS", {"KR": market}),
|
||||||
patch("src.main.get_open_position", return_value=None),
|
patch("src.main.get_open_position", return_value=None),
|
||||||
patch("src.main.validate_order_policy"),
|
patch("src.main.validate_order_policy"),
|
||||||
|
patch("src.main.get_session_info", return_value=MagicMock(session_id="KRX_REG")),
|
||||||
):
|
):
|
||||||
await process_blackout_recovery_orders(
|
await process_blackout_recovery_orders(
|
||||||
broker=broker,
|
broker=broker,
|
||||||
@@ -5845,6 +6374,19 @@ async def test_process_blackout_recovery_executes_valid_intents() -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
broker.send_order.assert_called_once()
|
broker.send_order.assert_called_once()
|
||||||
|
row = db_conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT action, quantity, session_id, rationale
|
||||||
|
FROM trades
|
||||||
|
WHERE stock_code = '005930'
|
||||||
|
ORDER BY id DESC LIMIT 1
|
||||||
|
"""
|
||||||
|
).fetchone()
|
||||||
|
assert row is not None
|
||||||
|
assert row[0] == "BUY"
|
||||||
|
assert row[1] == 1
|
||||||
|
assert row[2] == "KRX_REG"
|
||||||
|
assert row[3].startswith("[blackout-recovery]")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import UTC, datetime, timedelta
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from src.analysis.triple_barrier import TripleBarrierSpec, label_with_triple_barrier
|
from src.analysis.triple_barrier import TripleBarrierSpec, label_with_triple_barrier
|
||||||
|
|
||||||
|
|
||||||
@@ -129,3 +133,52 @@ def test_short_tie_break_modes() -> None:
|
|||||||
)
|
)
|
||||||
assert out_take.label == 1
|
assert out_take.label == 1
|
||||||
assert out_take.touched == "take_profit"
|
assert out_take.touched == "take_profit"
|
||||||
|
|
||||||
|
|
||||||
|
def test_minutes_time_barrier_consistent_across_sampling() -> None:
|
||||||
|
base = datetime(2026, 2, 28, 9, 0, tzinfo=UTC)
|
||||||
|
highs = [100.0, 100.5, 100.6, 100.4]
|
||||||
|
lows = [100.0, 99.6, 99.4, 99.5]
|
||||||
|
closes = [100.0, 100.1, 100.0, 100.0]
|
||||||
|
spec = TripleBarrierSpec(
|
||||||
|
take_profit_pct=0.02,
|
||||||
|
stop_loss_pct=0.02,
|
||||||
|
max_holding_minutes=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
out_1m = label_with_triple_barrier(
|
||||||
|
highs=highs,
|
||||||
|
lows=lows,
|
||||||
|
closes=closes,
|
||||||
|
timestamps=[base + timedelta(minutes=i) for i in range(4)],
|
||||||
|
entry_index=0,
|
||||||
|
side=1,
|
||||||
|
spec=spec,
|
||||||
|
)
|
||||||
|
out_5m = label_with_triple_barrier(
|
||||||
|
highs=highs,
|
||||||
|
lows=lows,
|
||||||
|
closes=closes,
|
||||||
|
timestamps=[base + timedelta(minutes=5 * i) for i in range(4)],
|
||||||
|
entry_index=0,
|
||||||
|
side=1,
|
||||||
|
spec=spec,
|
||||||
|
)
|
||||||
|
assert out_1m.touch_bar == 3
|
||||||
|
assert out_5m.touch_bar == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_bars_mode_emits_deprecation_warning() -> None:
|
||||||
|
highs = [100, 101, 103]
|
||||||
|
lows = [100, 99.6, 100]
|
||||||
|
closes = [100, 100, 102]
|
||||||
|
spec = TripleBarrierSpec(take_profit_pct=0.02, stop_loss_pct=0.01, max_holding_bars=3)
|
||||||
|
with pytest.deprecated_call(match="max_holding_bars is deprecated"):
|
||||||
|
label_with_triple_barrier(
|
||||||
|
highs=highs,
|
||||||
|
lows=lows,
|
||||||
|
closes=closes,
|
||||||
|
entry_index=0,
|
||||||
|
side=1,
|
||||||
|
spec=spec,
|
||||||
|
)
|
||||||
|
|||||||
81
tests/test_validate_governance_assets.py
Normal file
81
tests/test_validate_governance_assets.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
from pathlib import Path
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
|
||||||
|
def _load_module():
|
||||||
|
script_path = Path(__file__).resolve().parents[1] / "scripts" / "validate_governance_assets.py"
|
||||||
|
spec = importlib.util.spec_from_file_location("validate_governance_assets", script_path)
|
||||||
|
assert spec is not None
|
||||||
|
assert spec.loader is not None
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
return module
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_policy_file_detects_ouroboros_policy_docs() -> None:
|
||||||
|
module = _load_module()
|
||||||
|
assert module.is_policy_file("docs/ouroboros/85_loss_recovery_action_plan.md")
|
||||||
|
assert not module.is_policy_file("docs/ouroboros/01_requirements_registry.md")
|
||||||
|
assert not module.is_policy_file("docs/workflow.md")
|
||||||
|
assert not module.is_policy_file("docs/ouroboros/notes.txt")
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_registry_sync_requires_registry_update_when_policy_changes() -> None:
|
||||||
|
module = _load_module()
|
||||||
|
errors: list[str] = []
|
||||||
|
module.validate_registry_sync(
|
||||||
|
["docs/ouroboros/85_loss_recovery_action_plan.md"],
|
||||||
|
errors,
|
||||||
|
)
|
||||||
|
assert errors
|
||||||
|
assert "policy file changed without updating" in errors[0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_registry_sync_passes_when_registry_included() -> None:
|
||||||
|
module = _load_module()
|
||||||
|
errors: list[str] = []
|
||||||
|
module.validate_registry_sync(
|
||||||
|
[
|
||||||
|
"docs/ouroboros/85_loss_recovery_action_plan.md",
|
||||||
|
"docs/ouroboros/01_requirements_registry.md",
|
||||||
|
],
|
||||||
|
errors,
|
||||||
|
)
|
||||||
|
assert errors == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_changed_files_supports_explicit_paths() -> None:
|
||||||
|
module = _load_module()
|
||||||
|
errors: list[str] = []
|
||||||
|
changed = module.load_changed_files(
|
||||||
|
["./docs/ouroboros/85_loss_recovery_action_plan.md", " src/main.py "],
|
||||||
|
errors,
|
||||||
|
)
|
||||||
|
assert errors == []
|
||||||
|
assert changed == [
|
||||||
|
"docs/ouroboros/85_loss_recovery_action_plan.md",
|
||||||
|
"src/main.py",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_changed_files_with_range_uses_git_diff(monkeypatch) -> None:
|
||||||
|
module = _load_module()
|
||||||
|
errors: list[str] = []
|
||||||
|
|
||||||
|
def fake_run(cmd, check, capture_output, text): # noqa: ANN001
|
||||||
|
assert cmd[:3] == ["git", "diff", "--name-only"]
|
||||||
|
assert check is True
|
||||||
|
assert capture_output is True
|
||||||
|
assert text is True
|
||||||
|
return SimpleNamespace(stdout="docs/ouroboros/85_loss_recovery_action_plan.md\nsrc/main.py\n")
|
||||||
|
|
||||||
|
monkeypatch.setattr(module.subprocess, "run", fake_run)
|
||||||
|
changed = module.load_changed_files(["abc...def"], errors)
|
||||||
|
assert errors == []
|
||||||
|
assert changed == [
|
||||||
|
"docs/ouroboros/85_loss_recovery_action_plan.md",
|
||||||
|
"src/main.py",
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user