Compare commits
8 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdd5a218a7 | ||
|
|
f3491e94e4 | ||
|
|
342511a6ed | ||
| 2d5912dc08 | |||
|
|
40ea41cf3c | ||
| af5bfbac24 | |||
|
|
7e9a573390 | ||
| 7dbc48260c |
@@ -3,8 +3,9 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -79,6 +80,35 @@ def create_dashboard_app(db_path: str) -> FastAPI:
|
|||||||
total_pnl += market_status[market]["total_pnl"]
|
total_pnl += market_status[market]["total_pnl"]
|
||||||
total_decisions += market_status[market]["decision_count"]
|
total_decisions += market_status[market]["decision_count"]
|
||||||
|
|
||||||
|
cb_threshold = float(os.getenv("CIRCUIT_BREAKER_PCT", "-3.0"))
|
||||||
|
pnl_pct_rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT key, value
|
||||||
|
FROM system_metrics
|
||||||
|
WHERE key LIKE 'portfolio_pnl_pct_%'
|
||||||
|
ORDER BY updated_at DESC
|
||||||
|
LIMIT 20
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
current_pnl_pct: float | None = None
|
||||||
|
if pnl_pct_rows:
|
||||||
|
values = [
|
||||||
|
json.loads(row["value"]).get("pnl_pct")
|
||||||
|
for row in pnl_pct_rows
|
||||||
|
if json.loads(row["value"]).get("pnl_pct") is not None
|
||||||
|
]
|
||||||
|
if values:
|
||||||
|
current_pnl_pct = round(min(values), 4)
|
||||||
|
|
||||||
|
if current_pnl_pct is None:
|
||||||
|
cb_status = "unknown"
|
||||||
|
elif current_pnl_pct <= cb_threshold:
|
||||||
|
cb_status = "tripped"
|
||||||
|
elif current_pnl_pct <= cb_threshold + 1.0:
|
||||||
|
cb_status = "warning"
|
||||||
|
else:
|
||||||
|
cb_status = "ok"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"date": today,
|
"date": today,
|
||||||
"markets": market_status,
|
"markets": market_status,
|
||||||
@@ -87,6 +117,11 @@ def create_dashboard_app(db_path: str) -> FastAPI:
|
|||||||
"total_pnl": round(total_pnl, 2),
|
"total_pnl": round(total_pnl, 2),
|
||||||
"decision_count": total_decisions,
|
"decision_count": total_decisions,
|
||||||
},
|
},
|
||||||
|
"circuit_breaker": {
|
||||||
|
"threshold_pct": cb_threshold,
|
||||||
|
"current_pnl_pct": current_pnl_pct,
|
||||||
|
"status": cb_status,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@app.get("/api/playbook/{date_str}")
|
@app.get("/api/playbook/{date_str}")
|
||||||
@@ -341,12 +376,68 @@ def create_dashboard_app(db_path: str) -> FastAPI:
|
|||||||
)
|
)
|
||||||
return {"market": market, "date": date_str, "count": len(matches), "matches": matches}
|
return {"market": market, "date": date_str, "count": len(matches), "matches": matches}
|
||||||
|
|
||||||
|
@app.get("/api/positions")
|
||||||
|
def get_positions() -> dict[str, Any]:
|
||||||
|
"""Return all currently open positions (last trade per symbol is BUY)."""
|
||||||
|
with _connect(db_path) as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT stock_code, market, exchange_code,
|
||||||
|
price AS entry_price, quantity, timestamp AS entry_time,
|
||||||
|
decision_id
|
||||||
|
FROM (
|
||||||
|
SELECT stock_code, market, exchange_code, price, quantity,
|
||||||
|
timestamp, decision_id, action,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY stock_code, market
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
) AS rn
|
||||||
|
FROM trades
|
||||||
|
)
|
||||||
|
WHERE rn = 1 AND action = 'BUY'
|
||||||
|
ORDER BY entry_time DESC
|
||||||
|
"""
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
positions = []
|
||||||
|
for row in rows:
|
||||||
|
entry_time_str = row["entry_time"]
|
||||||
|
try:
|
||||||
|
entry_dt = datetime.fromisoformat(entry_time_str.replace("Z", "+00:00"))
|
||||||
|
held_seconds = int((now - entry_dt).total_seconds())
|
||||||
|
held_hours = held_seconds // 3600
|
||||||
|
held_minutes = (held_seconds % 3600) // 60
|
||||||
|
if held_hours >= 1:
|
||||||
|
held_display = f"{held_hours}h {held_minutes}m"
|
||||||
|
else:
|
||||||
|
held_display = f"{held_minutes}m"
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
held_display = "--"
|
||||||
|
|
||||||
|
positions.append(
|
||||||
|
{
|
||||||
|
"stock_code": row["stock_code"],
|
||||||
|
"market": row["market"],
|
||||||
|
"exchange_code": row["exchange_code"],
|
||||||
|
"entry_price": row["entry_price"],
|
||||||
|
"quantity": row["quantity"],
|
||||||
|
"entry_time": entry_time_str,
|
||||||
|
"held": held_display,
|
||||||
|
"decision_id": row["decision_id"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"count": len(positions), "positions": positions}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
def _connect(db_path: str) -> sqlite3.Connection:
|
def _connect(db_path: str) -> sqlite3.Connection:
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
|
conn.execute("PRAGMA journal_mode=WAL")
|
||||||
|
conn.execute("PRAGMA busy_timeout=8000")
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
--muted: #9fb3c8;
|
--muted: #9fb3c8;
|
||||||
--accent: #3cb371;
|
--accent: #3cb371;
|
||||||
--red: #e05555;
|
--red: #e05555;
|
||||||
|
--warn: #e8a040;
|
||||||
--border: #28455f;
|
--border: #28455f;
|
||||||
}
|
}
|
||||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
@@ -43,6 +44,25 @@
|
|||||||
}
|
}
|
||||||
.refresh-btn:hover { border-color: var(--accent); color: var(--accent); }
|
.refresh-btn:hover { border-color: var(--accent); color: var(--accent); }
|
||||||
|
|
||||||
|
/* CB Gauge */
|
||||||
|
.cb-gauge-wrap {
|
||||||
|
display: flex; align-items: center; gap: 8px;
|
||||||
|
font-size: 11px; color: var(--muted);
|
||||||
|
}
|
||||||
|
.cb-dot {
|
||||||
|
width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.cb-dot.ok { background: var(--accent); }
|
||||||
|
.cb-dot.warning { background: var(--warn); animation: pulse-warn 1.2s ease-in-out infinite; }
|
||||||
|
.cb-dot.tripped { background: var(--red); animation: pulse-warn 0.6s ease-in-out infinite; }
|
||||||
|
.cb-dot.unknown { background: var(--border); }
|
||||||
|
@keyframes pulse-warn {
|
||||||
|
0%, 100% { opacity: 1; }
|
||||||
|
50% { opacity: 0.35; }
|
||||||
|
}
|
||||||
|
.cb-bar-wrap { width: 64px; height: 5px; background: rgba(255,255,255,0.08); border-radius: 3px; overflow: hidden; }
|
||||||
|
.cb-bar-fill { height: 100%; border-radius: 3px; transition: width 0.4s, background 0.4s; }
|
||||||
|
|
||||||
/* Summary cards */
|
/* Summary cards */
|
||||||
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
|
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
|
||||||
@media (max-width: 700px) { .cards { grid-template-columns: repeat(2, 1fr); } }
|
@media (max-width: 700px) { .cards { grid-template-columns: repeat(2, 1fr); } }
|
||||||
@@ -123,6 +143,32 @@
|
|||||||
.rationale-cell { max-width: 200px; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
|
.rationale-cell { max-width: 200px; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
|
||||||
.empty-row td { text-align: center; color: var(--muted); padding: 24px; }
|
.empty-row td { text-align: center; color: var(--muted); padding: 24px; }
|
||||||
|
|
||||||
|
/* Positions panel */
|
||||||
|
.positions-panel {
|
||||||
|
background: var(--panel);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 16px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.positions-table { width: 100%; border-collapse: collapse; margin-top: 14px; }
|
||||||
|
.positions-table th {
|
||||||
|
text-align: left; color: var(--muted); font-size: 11px; font-weight: 600;
|
||||||
|
padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap;
|
||||||
|
}
|
||||||
|
.positions-table td {
|
||||||
|
padding: 8px 8px; border-bottom: 1px solid rgba(40, 69, 95, 0.5);
|
||||||
|
vertical-align: middle; white-space: nowrap;
|
||||||
|
}
|
||||||
|
.positions-table tr:last-child td { border-bottom: none; }
|
||||||
|
.positions-table tr:hover td { background: rgba(255,255,255,0.02); }
|
||||||
|
.pos-empty { color: var(--muted); text-align: center; padding: 20px 0; font-size: 12px; }
|
||||||
|
.pos-count {
|
||||||
|
display: inline-block; background: rgba(60, 179, 113, 0.12);
|
||||||
|
color: var(--accent); font-size: 11px; font-weight: 700;
|
||||||
|
padding: 2px 8px; border-radius: 10px; margin-left: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
/* Spinner */
|
/* Spinner */
|
||||||
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
|
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
|
||||||
@keyframes spin { to { transform: rotate(360deg); } }
|
@keyframes spin { to { transform: rotate(360deg); } }
|
||||||
@@ -134,6 +180,13 @@
|
|||||||
<header>
|
<header>
|
||||||
<h1>🐍 The Ouroboros</h1>
|
<h1>🐍 The Ouroboros</h1>
|
||||||
<div class="header-right">
|
<div class="header-right">
|
||||||
|
<div class="cb-gauge-wrap" id="cb-gauge" title="Circuit Breaker">
|
||||||
|
<span class="cb-dot unknown" id="cb-dot"></span>
|
||||||
|
<span id="cb-label">CB --</span>
|
||||||
|
<div class="cb-bar-wrap">
|
||||||
|
<div class="cb-bar-fill" id="cb-bar" style="width:0%;background:var(--accent)"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<span id="last-updated">--</span>
|
<span id="last-updated">--</span>
|
||||||
<button class="refresh-btn" onclick="refreshAll()">↺ 새로고침</button>
|
<button class="refresh-btn" onclick="refreshAll()">↺ 새로고침</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -163,6 +216,30 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Open Positions -->
|
||||||
|
<div class="positions-panel">
|
||||||
|
<div class="panel-header">
|
||||||
|
<span class="panel-title">
|
||||||
|
현재 보유 포지션
|
||||||
|
<span class="pos-count" id="positions-count">0</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<table class="positions-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>종목</th>
|
||||||
|
<th>시장</th>
|
||||||
|
<th>수량</th>
|
||||||
|
<th>진입가</th>
|
||||||
|
<th>보유 시간</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="positions-body">
|
||||||
|
<tr><td colspan="5" class="pos-empty"><span class="spinner"></span></td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- P&L Chart -->
|
<!-- P&L Chart -->
|
||||||
<div class="chart-panel">
|
<div class="chart-panel">
|
||||||
<div class="panel-header">
|
<div class="panel-header">
|
||||||
@@ -242,6 +319,71 @@
|
|||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fmtPrice(v, market) {
|
||||||
|
if (v === null || v === undefined) return '--';
|
||||||
|
const n = parseFloat(v);
|
||||||
|
const sym = market === 'KR' ? '₩' : market === 'JP' ? '¥' : market === 'HK' ? 'HK$' : '$';
|
||||||
|
return sym + n.toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 4 });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchPositions() {
|
||||||
|
const tbody = document.getElementById('positions-body');
|
||||||
|
const countEl = document.getElementById('positions-count');
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/positions');
|
||||||
|
if (!r.ok) throw new Error('fetch failed');
|
||||||
|
const d = await r.json();
|
||||||
|
countEl.textContent = d.count ?? 0;
|
||||||
|
if (!d.positions || d.positions.length === 0) {
|
||||||
|
tbody.innerHTML = '<tr><td colspan="5" class="pos-empty">현재 보유 중인 포지션 없음</td></tr>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tbody.innerHTML = d.positions.map(p => `
|
||||||
|
<tr>
|
||||||
|
<td><strong>${p.stock_code || '--'}</strong></td>
|
||||||
|
<td><span style="color:var(--muted);font-size:11px">${p.market || '--'}</span></td>
|
||||||
|
<td>${p.quantity ?? '--'}</td>
|
||||||
|
<td>${fmtPrice(p.entry_price, p.market)}</td>
|
||||||
|
<td style="color:var(--muted);font-size:11px">${p.held || '--'}</td>
|
||||||
|
</tr>
|
||||||
|
`).join('');
|
||||||
|
} catch {
|
||||||
|
tbody.innerHTML = '<tr><td colspan="5" class="pos-empty">데이터 로드 실패</td></tr>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderCbGauge(cb) {
|
||||||
|
if (!cb) return;
|
||||||
|
const dot = document.getElementById('cb-dot');
|
||||||
|
const label = document.getElementById('cb-label');
|
||||||
|
const bar = document.getElementById('cb-bar');
|
||||||
|
|
||||||
|
const status = cb.status || 'unknown';
|
||||||
|
const threshold = cb.threshold_pct ?? -3.0;
|
||||||
|
const current = cb.current_pnl_pct;
|
||||||
|
|
||||||
|
// dot color
|
||||||
|
dot.className = `cb-dot ${status}`;
|
||||||
|
|
||||||
|
// label
|
||||||
|
if (current !== null && current !== undefined) {
|
||||||
|
const sign = current > 0 ? '+' : '';
|
||||||
|
label.textContent = `CB ${sign}${current.toFixed(2)}%`;
|
||||||
|
} else {
|
||||||
|
label.textContent = 'CB --';
|
||||||
|
}
|
||||||
|
|
||||||
|
// bar: fill = how much of the threshold has been consumed (0%=safe, 100%=tripped)
|
||||||
|
const colorMap = { ok: 'var(--accent)', warning: 'var(--warn)', tripped: 'var(--red)', unknown: 'var(--border)' };
|
||||||
|
bar.style.background = colorMap[status] || 'var(--border)';
|
||||||
|
if (current !== null && current !== undefined && threshold < 0) {
|
||||||
|
const fillPct = Math.min(Math.max((current / threshold) * 100, 0), 100);
|
||||||
|
bar.style.width = `${fillPct}%`;
|
||||||
|
} else {
|
||||||
|
bar.style.width = '0%';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchStatus() {
|
async function fetchStatus() {
|
||||||
try {
|
try {
|
||||||
const r = await fetch('/api/status');
|
const r = await fetch('/api/status');
|
||||||
@@ -258,6 +400,7 @@
|
|||||||
pnlEl.className = `card-value ${n > 0 ? 'positive' : n < 0 ? 'negative' : 'neutral'}`;
|
pnlEl.className = `card-value ${n > 0 ? 'positive' : n < 0 ? 'negative' : 'neutral'}`;
|
||||||
}
|
}
|
||||||
document.getElementById('card-pnl-sub').textContent = `결정 ${t.decision_count ?? 0}건`;
|
document.getElementById('card-pnl-sub').textContent = `결정 ${t.decision_count ?? 0}건`;
|
||||||
|
renderCbGauge(d.circuit_breaker);
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -383,6 +526,7 @@
|
|||||||
await Promise.all([
|
await Promise.all([
|
||||||
fetchStatus(),
|
fetchStatus(),
|
||||||
fetchPerformance(),
|
fetchPerformance(),
|
||||||
|
fetchPositions(),
|
||||||
fetchPnlHistory(currentDays),
|
fetchPnlHistory(currentDays),
|
||||||
fetchDecisions(currentMarket),
|
fetchDecisions(currentMarket),
|
||||||
]);
|
]);
|
||||||
|
|||||||
19
src/db.py
19
src/db.py
@@ -131,6 +131,25 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE INDEX IF NOT EXISTS idx_decision_logs_confidence ON decision_logs(confidence)"
|
"CREATE INDEX IF NOT EXISTS idx_decision_logs_confidence ON decision_logs(confidence)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Index for open-position queries (partition by stock_code, market, ordered by timestamp)
|
||||||
|
conn.execute(
|
||||||
|
"CREATE INDEX IF NOT EXISTS idx_trades_stock_market_ts"
|
||||||
|
" ON trades (stock_code, market, timestamp DESC)"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Lightweight key-value store for trading system runtime metrics (dashboard use only)
|
||||||
|
# Intentionally separate from the AI context tree to preserve separation of concerns.
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS system_metrics (
|
||||||
|
key TEXT PRIMARY KEY,
|
||||||
|
value TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|||||||
30
src/main.py
30
src/main.py
@@ -430,6 +430,17 @@ async def trading_cycle(
|
|||||||
{"volume_ratio": candidate.volume_ratio},
|
{"volume_ratio": candidate.volume_ratio},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Write pnl_pct to system_metrics (dashboard-only table, separate from AI context tree)
|
||||||
|
db_conn.execute(
|
||||||
|
"INSERT OR REPLACE INTO system_metrics (key, value, updated_at) VALUES (?, ?, ?)",
|
||||||
|
(
|
||||||
|
f"portfolio_pnl_pct_{market.code}",
|
||||||
|
json.dumps({"pnl_pct": round(pnl_pct, 4)}),
|
||||||
|
datetime.now(UTC).isoformat(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
db_conn.commit()
|
||||||
|
|
||||||
# Build portfolio data for global rule evaluation
|
# Build portfolio data for global rule evaluation
|
||||||
portfolio_data = {
|
portfolio_data = {
|
||||||
"portfolio_pnl_pct": pnl_pct,
|
"portfolio_pnl_pct": pnl_pct,
|
||||||
@@ -510,6 +521,25 @@ async def trading_cycle(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# BUY 결정 전 기존 포지션 체크 (중복 매수 방지)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
existing_position = get_open_position(db_conn, stock_code, market.code)
|
||||||
|
if existing_position:
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=(
|
||||||
|
f"Already holding {stock_code} "
|
||||||
|
f"(entry={existing_position['price']:.4f}, "
|
||||||
|
f"qty={existing_position['quantity']})"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): already holding open position",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
)
|
||||||
|
|
||||||
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)
|
||||||
if open_position:
|
if open_position:
|
||||||
|
|||||||
@@ -316,3 +316,100 @@ def test_pnl_history_market_filter(tmp_path: Path) -> None:
|
|||||||
# KR has 1 trade with pnl=2.0
|
# KR has 1 trade with pnl=2.0
|
||||||
assert len(body["labels"]) >= 1
|
assert len(body["labels"]) >= 1
|
||||||
assert body["pnl"][0] == 2.0
|
assert body["pnl"][0] == 2.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_positions_returns_open_buy(tmp_path: Path) -> None:
|
||||||
|
"""BUY가 마지막 거래인 종목은 포지션으로 반환되어야 한다."""
|
||||||
|
app = _app(tmp_path)
|
||||||
|
get_positions = _endpoint(app, "/api/positions")
|
||||||
|
body = get_positions()
|
||||||
|
# seed_db: 005930은 BUY (오픈), AAPL은 SELL (마지막)
|
||||||
|
assert body["count"] == 1
|
||||||
|
pos = body["positions"][0]
|
||||||
|
assert pos["stock_code"] == "005930"
|
||||||
|
assert pos["market"] == "KR"
|
||||||
|
assert pos["quantity"] == 1
|
||||||
|
assert pos["entry_price"] == 70000
|
||||||
|
|
||||||
|
|
||||||
|
def test_positions_excludes_closed_sell(tmp_path: Path) -> None:
|
||||||
|
"""마지막 거래가 SELL인 종목은 포지션에 나타나지 않아야 한다."""
|
||||||
|
app = _app(tmp_path)
|
||||||
|
get_positions = _endpoint(app, "/api/positions")
|
||||||
|
body = get_positions()
|
||||||
|
codes = [p["stock_code"] for p in body["positions"]]
|
||||||
|
assert "AAPL" not in codes
|
||||||
|
|
||||||
|
|
||||||
|
def test_positions_empty_when_no_trades(tmp_path: Path) -> None:
|
||||||
|
"""거래 내역이 없으면 빈 포지션 목록을 반환해야 한다."""
|
||||||
|
db_path = tmp_path / "empty.db"
|
||||||
|
conn = init_db(str(db_path))
|
||||||
|
conn.close()
|
||||||
|
app = create_dashboard_app(str(db_path))
|
||||||
|
get_positions = _endpoint(app, "/api/positions")
|
||||||
|
body = get_positions()
|
||||||
|
assert body["count"] == 0
|
||||||
|
assert body["positions"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def _seed_cb_context(conn: sqlite3.Connection, pnl_pct: float, market: str = "KR") -> None:
|
||||||
|
import json as _json
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR REPLACE INTO system_metrics (key, value, updated_at) VALUES (?, ?, ?)",
|
||||||
|
(
|
||||||
|
f"portfolio_pnl_pct_{market}",
|
||||||
|
_json.dumps({"pnl_pct": pnl_pct}),
|
||||||
|
"2026-02-22T10:00:00+00:00",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def test_status_circuit_breaker_ok(tmp_path: Path) -> None:
|
||||||
|
"""pnl_pct가 -2.0%보다 높으면 status=ok를 반환해야 한다."""
|
||||||
|
db_path = tmp_path / "cb_ok.db"
|
||||||
|
conn = init_db(str(db_path))
|
||||||
|
_seed_cb_context(conn, -1.0)
|
||||||
|
conn.close()
|
||||||
|
app = create_dashboard_app(str(db_path))
|
||||||
|
get_status = _endpoint(app, "/api/status")
|
||||||
|
body = get_status()
|
||||||
|
cb = body["circuit_breaker"]
|
||||||
|
assert cb["status"] == "ok"
|
||||||
|
assert cb["current_pnl_pct"] == -1.0
|
||||||
|
assert cb["threshold_pct"] == -3.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_status_circuit_breaker_warning(tmp_path: Path) -> None:
|
||||||
|
"""pnl_pct가 -2.0% 이하이면 status=warning을 반환해야 한다."""
|
||||||
|
db_path = tmp_path / "cb_warn.db"
|
||||||
|
conn = init_db(str(db_path))
|
||||||
|
_seed_cb_context(conn, -2.5)
|
||||||
|
conn.close()
|
||||||
|
app = create_dashboard_app(str(db_path))
|
||||||
|
get_status = _endpoint(app, "/api/status")
|
||||||
|
body = get_status()
|
||||||
|
assert body["circuit_breaker"]["status"] == "warning"
|
||||||
|
|
||||||
|
|
||||||
|
def test_status_circuit_breaker_tripped(tmp_path: Path) -> None:
|
||||||
|
"""pnl_pct가 임계값(-3.0%) 이하이면 status=tripped를 반환해야 한다."""
|
||||||
|
db_path = tmp_path / "cb_tripped.db"
|
||||||
|
conn = init_db(str(db_path))
|
||||||
|
_seed_cb_context(conn, -3.5)
|
||||||
|
conn.close()
|
||||||
|
app = create_dashboard_app(str(db_path))
|
||||||
|
get_status = _endpoint(app, "/api/status")
|
||||||
|
body = get_status()
|
||||||
|
assert body["circuit_breaker"]["status"] == "tripped"
|
||||||
|
|
||||||
|
|
||||||
|
def test_status_circuit_breaker_unknown_when_no_data(tmp_path: Path) -> None:
|
||||||
|
"""L7 context에 pnl_pct 데이터가 없으면 status=unknown을 반환해야 한다."""
|
||||||
|
app = _app(tmp_path) # seed_db에는 portfolio_pnl_pct 없음
|
||||||
|
get_status = _endpoint(app, "/api/status")
|
||||||
|
body = get_status()
|
||||||
|
cb = body["circuit_breaker"]
|
||||||
|
assert cb["status"] == "unknown"
|
||||||
|
assert cb["current_pnl_pct"] is None
|
||||||
|
|||||||
@@ -2848,3 +2848,156 @@ class TestMarketOutlookConfidenceThreshold:
|
|||||||
call_args = decision_logger.log_decision.call_args
|
call_args = decision_logger.log_decision.call_args
|
||||||
assert call_args is not None
|
assert call_args is not None
|
||||||
assert call_args.kwargs["action"] == "BUY"
|
assert call_args.kwargs["action"] == "BUY"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_buy_suppressed_when_open_position_exists() -> None:
|
||||||
|
"""BUY should be suppressed when an open position already exists for the stock."""
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
decision_logger = DecisionLogger(db_conn)
|
||||||
|
|
||||||
|
# 기존 BUY 포지션 DB에 기록 (중복 매수 상황)
|
||||||
|
buy_decision_id = decision_logger.log_decision(
|
||||||
|
stock_code="NP",
|
||||||
|
market="US",
|
||||||
|
exchange_code="AMS",
|
||||||
|
action="BUY",
|
||||||
|
confidence=90,
|
||||||
|
rationale="initial entry",
|
||||||
|
context_snapshot={},
|
||||||
|
input_data={},
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
conn=db_conn,
|
||||||
|
stock_code="NP",
|
||||||
|
action="BUY",
|
||||||
|
confidence=90,
|
||||||
|
rationale="initial entry",
|
||||||
|
quantity=10,
|
||||||
|
price=50.0,
|
||||||
|
market="US",
|
||||||
|
exchange_code="AMS",
|
||||||
|
decision_id=buy_decision_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "51.0", "rate": "2.0", "high": "52.0", "low": "50.0", "tvol": "1000000"}}
|
||||||
|
)
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"frcr_dncl_amt_2": "10000", "frcr_evlu_tota": "10000", "frcr_buy_amt_smtl": "0"}],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=_make_buy_match(stock_code="NP"))
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "United States"
|
||||||
|
market.code = "US"
|
||||||
|
market.exchange_code = "AMS"
|
||||||
|
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()
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook(market="US"),
|
||||||
|
risk=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="NP",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# 이미 보유 중이므로 주문이 실행되지 않아야 함
|
||||||
|
broker.send_order.assert_not_called()
|
||||||
|
overseas_broker.send_overseas_order.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_buy_proceeds_when_no_open_position() -> None:
|
||||||
|
"""BUY should proceed normally when no open position exists for the stock."""
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
decision_logger = DecisionLogger(db_conn)
|
||||||
|
# DB가 비어있는 상태 — 기존 포지션 없음
|
||||||
|
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "100.0", "rate": "1.0", "high": "101.0", "low": "99.0", "tvol": "500000"}}
|
||||||
|
)
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"frcr_dncl_amt_2": "50000", "frcr_evlu_tota": "50000", "frcr_buy_amt_smtl": "0"}],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=_make_buy_match(stock_code="KNRX"))
|
||||||
|
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "United States"
|
||||||
|
market.code = "US"
|
||||||
|
market.exchange_code = "NAS"
|
||||||
|
market.is_domestic = False
|
||||||
|
|
||||||
|
risk = MagicMock()
|
||||||
|
risk.validate_order = MagicMock()
|
||||||
|
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_trade_execution = AsyncMock()
|
||||||
|
telegram.notify_fat_finger = AsyncMock()
|
||||||
|
telegram.notify_circuit_breaker = AsyncMock()
|
||||||
|
telegram.notify_scenario_matched = AsyncMock()
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook(market="US"),
|
||||||
|
risk=risk,
|
||||||
|
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="KNRX",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# 포지션이 없으므로 해외 주문이 실행되어야 함
|
||||||
|
overseas_broker.send_overseas_order.assert_called_once()
|
||||||
|
|||||||
Reference in New Issue
Block a user