Compare commits
37 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d99d8ec4a | ||
| 641f3e8811 | |||
|
|
ebd0a0297c | ||
| 02a72e0f7e | |||
| 478a659ac2 | |||
|
|
16b9b6832d | ||
|
|
48b87a79f6 | ||
|
|
ad79082dcc | ||
|
|
11dff9d3e5 | ||
|
|
3c5f1752e6 | ||
|
|
d6a389e0b7 | ||
| cd36d53a47 | |||
|
|
1242794fc4 | ||
| b45d136894 | |||
|
|
ce82121f04 | ||
| 0e2987e66d | |||
|
|
cdd5a218a7 | ||
|
|
f3491e94e4 | ||
|
|
342511a6ed | ||
| 2d5912dc08 | |||
|
|
40ea41cf3c | ||
| af5bfbac24 | |||
|
|
7e9a573390 | ||
| 7dbc48260c | |||
|
|
4b883a4fc4 | ||
|
|
98071a8ee3 | ||
|
|
f2ad270e8b | ||
| 04c73a1a06 | |||
|
|
4da22b10eb | ||
| c920b257b6 | |||
| 9927bfa13e | |||
|
|
aceba86186 | ||
|
|
b961c53a92 | ||
| 76a7ee7cdb | |||
|
|
77577f3f4d | ||
| 17112b864a | |||
|
|
39b9f179f4 |
64
.env.example
64
.env.example
@@ -1,36 +1,82 @@
|
|||||||
|
# ============================================================
|
||||||
|
# The Ouroboros — Environment Configuration
|
||||||
|
# ============================================================
|
||||||
|
# Copy this file to .env and fill in your values.
|
||||||
|
# Lines starting with # are comments.
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# Korea Investment Securities API
|
# Korea Investment Securities API
|
||||||
|
# ============================================================
|
||||||
KIS_APP_KEY=your_app_key_here
|
KIS_APP_KEY=your_app_key_here
|
||||||
KIS_APP_SECRET=your_app_secret_here
|
KIS_APP_SECRET=your_app_secret_here
|
||||||
KIS_ACCOUNT_NO=12345678-01
|
KIS_ACCOUNT_NO=12345678-01
|
||||||
KIS_BASE_URL=https://openapivts.koreainvestment.com:9443
|
|
||||||
|
|
||||||
|
# Paper trading (VTS): https://openapivts.koreainvestment.com:29443
|
||||||
|
# Live trading: https://openapi.koreainvestment.com:9443
|
||||||
|
KIS_BASE_URL=https://openapivts.koreainvestment.com:29443
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Trading Mode
|
||||||
|
# ============================================================
|
||||||
|
# paper = 모의투자 (safe for testing), live = 실전투자 (real money)
|
||||||
|
MODE=paper
|
||||||
|
|
||||||
|
# daily = batch per session, realtime = per-stock continuous scan
|
||||||
|
TRADE_MODE=daily
|
||||||
|
|
||||||
|
# Comma-separated market codes: KR, US, JP, HK, CN, VN
|
||||||
|
ENABLED_MARKETS=KR,US
|
||||||
|
|
||||||
|
# Simulated USD cash for paper (VTS) overseas trading.
|
||||||
|
# VTS overseas balance API often returns 0; this value is used as fallback.
|
||||||
|
# Set to 0 to disable fallback (not used in live mode).
|
||||||
|
PAPER_OVERSEAS_CASH=50000.0
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# Google Gemini
|
# Google Gemini
|
||||||
|
# ============================================================
|
||||||
GEMINI_API_KEY=your_gemini_api_key_here
|
GEMINI_API_KEY=your_gemini_api_key_here
|
||||||
GEMINI_MODEL=gemini-pro
|
# Recommended: gemini-2.0-flash-exp or gemini-1.5-pro
|
||||||
|
GEMINI_MODEL=gemini-2.0-flash-exp
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# Risk Management
|
# Risk Management
|
||||||
|
# ============================================================
|
||||||
CIRCUIT_BREAKER_PCT=-3.0
|
CIRCUIT_BREAKER_PCT=-3.0
|
||||||
FAT_FINGER_PCT=30.0
|
FAT_FINGER_PCT=30.0
|
||||||
CONFIDENCE_THRESHOLD=80
|
CONFIDENCE_THRESHOLD=80
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# Database
|
# Database
|
||||||
|
# ============================================================
|
||||||
DB_PATH=data/trade_logs.db
|
DB_PATH=data/trade_logs.db
|
||||||
|
|
||||||
# Rate Limiting (requests per second for KIS API)
|
# ============================================================
|
||||||
# Reduced to 5.0 to avoid "초당 거래건수 초과" errors (EGW00201)
|
# Rate Limiting
|
||||||
RATE_LIMIT_RPS=5.0
|
# ============================================================
|
||||||
|
# KIS API real limit is ~2 RPS. Keep at 2.0 for maximum safety.
|
||||||
|
# Increasing this risks EGW00201 "초당 거래건수 초과" errors.
|
||||||
|
RATE_LIMIT_RPS=2.0
|
||||||
|
|
||||||
# Trading Mode (paper / live)
|
# ============================================================
|
||||||
MODE=paper
|
# External Data APIs (optional)
|
||||||
|
# ============================================================
|
||||||
# External Data APIs (optional — for enhanced decision-making)
|
|
||||||
# NEWS_API_KEY=your_news_api_key_here
|
# NEWS_API_KEY=your_news_api_key_here
|
||||||
# NEWS_API_PROVIDER=alphavantage
|
# NEWS_API_PROVIDER=alphavantage
|
||||||
# MARKET_DATA_API_KEY=your_market_data_key_here
|
# MARKET_DATA_API_KEY=your_market_data_key_here
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# Telegram Notifications (optional)
|
# Telegram Notifications (optional)
|
||||||
|
# ============================================================
|
||||||
# Get bot token from @BotFather on Telegram
|
# Get bot token from @BotFather on Telegram
|
||||||
# Get chat ID from @userinfobot or your chat
|
# Get chat ID from @userinfobot or your chat
|
||||||
# TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
|
# TELEGRAM_BOT_TOKEN=1234567890:ABCdefGHIjklMNOpqrsTUVwxyz
|
||||||
# TELEGRAM_CHAT_ID=123456789
|
# TELEGRAM_CHAT_ID=123456789
|
||||||
# TELEGRAM_ENABLED=true
|
# TELEGRAM_ENABLED=true
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Dashboard (optional)
|
||||||
|
# ============================================================
|
||||||
|
# DASHBOARD_ENABLED=false
|
||||||
|
# DASHBOARD_HOST=127.0.0.1
|
||||||
|
# DASHBOARD_PORT=8080
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ Markets auto-detected based on timezone and enabled in `ENABLED_MARKETS` env var
|
|||||||
- `src/core/risk_manager.py` is **READ-ONLY** — changes require human approval
|
- `src/core/risk_manager.py` is **READ-ONLY** — changes require human approval
|
||||||
- Circuit breaker at -3.0% P&L — may only be made **stricter**
|
- Circuit breaker at -3.0% P&L — may only be made **stricter**
|
||||||
- Fat-finger protection: max 30% of cash per order — always enforced
|
- Fat-finger protection: max 30% of cash per order — always enforced
|
||||||
- Confidence < 80 → force HOLD — cannot be weakened
|
- Confidence 임계값 (market_outlook별, 낮출 수 없음): BEARISH ≥ 90, NEUTRAL/기본 ≥ 80, BULLISH ≥ 75
|
||||||
- All code changes → corresponding tests → coverage ≥ 80%
|
- All code changes → corresponding tests → coverage ≥ 80%
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|||||||
@@ -192,6 +192,27 @@ When `TELEGRAM_COMMANDS_ENABLED=true` (default), the bot accepts these interacti
|
|||||||
|
|
||||||
Commands are only processed from the authorized `TELEGRAM_CHAT_ID`.
|
Commands are only processed from the authorized `TELEGRAM_CHAT_ID`.
|
||||||
|
|
||||||
|
## KIS API TR_ID 참조 문서
|
||||||
|
|
||||||
|
**TR_ID를 추가하거나 수정할 때 반드시 공식 문서를 먼저 확인할 것.**
|
||||||
|
|
||||||
|
공식 문서: `docs/한국투자증권_오픈API_전체문서_20260221_030000.xlsx`
|
||||||
|
|
||||||
|
> ⚠️ 커뮤니티 블로그, GitHub 예제 등 비공식 자료의 TR_ID는 오래되거나 틀릴 수 있음.
|
||||||
|
> 실제로 `VTTT1006U`(미국 매도 — 잘못됨)가 오랫동안 코드에 남아있던 사례가 있음 (Issue #189).
|
||||||
|
|
||||||
|
### 주요 TR_ID 목록
|
||||||
|
|
||||||
|
| 구분 | 모의투자 TR_ID | 실전투자 TR_ID | 시트명 |
|
||||||
|
|------|---------------|---------------|--------|
|
||||||
|
| 해외주식 매수 (미국) | `VTTT1002U` | `TTTT1002U` | 해외주식 주문 |
|
||||||
|
| 해외주식 매도 (미국) | `VTTT1001U` | `TTTT1006U` | 해외주식 주문 |
|
||||||
|
|
||||||
|
새로운 TR_ID가 필요할 때:
|
||||||
|
1. 위 xlsx 파일에서 해당 거래 유형의 시트를 찾는다.
|
||||||
|
2. 모의투자(`VTTT`) / 실전투자(`TTTT`) 컬럼을 구분하여 정확한 값을 사용한다.
|
||||||
|
3. 코드에 출처 주석을 남긴다: `# Source: 한국투자증권_오픈API_전체문서 — '<시트명>' 시트`
|
||||||
|
|
||||||
## Environment Setup
|
## Environment Setup
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -7,6 +7,32 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 2026-02-21
|
||||||
|
|
||||||
|
### 거래 상태 확인 중 발견된 버그 (#187)
|
||||||
|
|
||||||
|
- 거래 상태 점검 요청 → SELL 주문(손절/익절)이 Fat Finger에 막혀 전혀 실행 안 됨 발견
|
||||||
|
- **#187 (Critical)**: SELL 주문에서 Fat Finger 오탐 — `order_amount/total_cash > 30%`가 SELL에도 적용되어 대형 포지션 매도 불가
|
||||||
|
- JELD stop-loss -6.20% → 차단, RXT take-profit +46.13% → 차단
|
||||||
|
- 수정: SELL은 `check_circuit_breaker`만 호출, `validate_order`(Fat Finger 포함) 미호출
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2026-02-20
|
||||||
|
|
||||||
|
### 지속적 모니터링 및 개선점 도출 (이슈 #178~#182)
|
||||||
|
|
||||||
|
- Dashboard 포함해서 실행하며 간헐적 문제 모니터링 및 개선점 자동 도출 요청
|
||||||
|
- 모니터링 결과 발견된 이슈 목록:
|
||||||
|
- **#178**: uvicorn 미설치 → dashboard 미작동 + 오해의 소지 있는 시작 로그 → uvicorn 설치 완료
|
||||||
|
- **#179 (Critical)**: 잔액 부족 주문 실패 후 매 사이클마다 무한 재시도 (MLECW 20분 이상 반복)
|
||||||
|
- **#180**: 다중 인스턴스 실행 시 Telegram 409 충돌
|
||||||
|
- **#181**: implied_rsi 공식 포화 문제 (change_rate≥12.5% → RSI=100)
|
||||||
|
- **#182 (Critical)**: 보유 종목이 SmartScanner 변동성 필터에 걸려 SELL 신호 미생성 → SELL 체결 0건, 잔고 소진
|
||||||
|
- 요구사항: 모니터링 자동화 및 주기적 개선점 리포트 도출
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 2026-02-05
|
## 2026-02-05
|
||||||
|
|
||||||
### API 효율화
|
### API 효율화
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ class SmartVolatilityScanner:
|
|||||||
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
||||||
score = min(100.0, volatility_score + liquidity_score)
|
score = min(100.0, volatility_score + liquidity_score)
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
|
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
@@ -282,7 +282,7 @@ class SmartVolatilityScanner:
|
|||||||
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
||||||
score = min(100.0, volatility_score + liquidity_score)
|
score = min(100.0, volatility_score + liquidity_score)
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
@@ -338,7 +338,7 @@ class SmartVolatilityScanner:
|
|||||||
|
|
||||||
score = min(volatility_pct / 10.0, 1.0) * 100.0
|
score = min(volatility_pct / 10.0, 1.0) * 100.0
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
|
|||||||
@@ -285,7 +285,10 @@ class KISBroker:
|
|||||||
await self._rate_limiter.acquire()
|
await self._rate_limiter.acquire()
|
||||||
session = self._get_session()
|
session = self._get_session()
|
||||||
|
|
||||||
headers = await self._auth_headers("VTTC8434R") # 모의투자 잔고조회
|
# TR_ID: 실전 TTTC8434R, 모의 VTTC8434R
|
||||||
|
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '국내주식 잔고조회' 시트
|
||||||
|
tr_id = "TTTC8434R" if self._settings.MODE == "live" else "VTTC8434R"
|
||||||
|
headers = await self._auth_headers(tr_id)
|
||||||
params = {
|
params = {
|
||||||
"CANO": self._account_no,
|
"CANO": self._account_no,
|
||||||
"ACNT_PRDT_CD": self._product_cd,
|
"ACNT_PRDT_CD": self._product_cd,
|
||||||
@@ -330,7 +333,13 @@ class KISBroker:
|
|||||||
await self._rate_limiter.acquire()
|
await self._rate_limiter.acquire()
|
||||||
session = self._get_session()
|
session = self._get_session()
|
||||||
|
|
||||||
tr_id = "VTTC0802U" if order_type == "BUY" else "VTTC0801U"
|
# TR_ID: 실전 BUY=TTTC0012U SELL=TTTC0011U, 모의 BUY=VTTC0012U SELL=VTTC0011U
|
||||||
|
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '주식주문(현금)' 시트
|
||||||
|
# ※ TTTC0802U/VTTC0802U는 미수매수(증거금40% 계좌 전용) — 현금주문에 사용 금지
|
||||||
|
if self._settings.MODE == "live":
|
||||||
|
tr_id = "TTTC0012U" if order_type == "BUY" else "TTTC0011U"
|
||||||
|
else:
|
||||||
|
tr_id = "VTTC0012U" if order_type == "BUY" else "VTTC0011U"
|
||||||
|
|
||||||
# KRX requires limit orders to be rounded down to the tick unit.
|
# KRX requires limit orders to be rounded down to the tick unit.
|
||||||
# ORD_DVSN: "00"=지정가, "01"=시장가
|
# ORD_DVSN: "00"=지정가, "01"=시장가
|
||||||
|
|||||||
@@ -175,8 +175,12 @@ class OverseasBroker:
|
|||||||
await self._broker._rate_limiter.acquire()
|
await self._broker._rate_limiter.acquire()
|
||||||
session = self._broker._get_session()
|
session = self._broker._get_session()
|
||||||
|
|
||||||
# Virtual trading TR_ID for overseas balance inquiry
|
# TR_ID: 실전 TTTS3012R, 모의 VTTS3012R
|
||||||
headers = await self._broker._auth_headers("VTTS3012R")
|
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '해외주식 잔고조회' 시트
|
||||||
|
balance_tr_id = (
|
||||||
|
"TTTS3012R" if self._broker._settings.MODE == "live" else "VTTS3012R"
|
||||||
|
)
|
||||||
|
headers = await self._broker._auth_headers(balance_tr_id)
|
||||||
params = {
|
params = {
|
||||||
"CANO": self._broker._account_no,
|
"CANO": self._broker._account_no,
|
||||||
"ACNT_PRDT_CD": self._broker._product_cd,
|
"ACNT_PRDT_CD": self._broker._product_cd,
|
||||||
@@ -229,8 +233,12 @@ class OverseasBroker:
|
|||||||
await self._broker._rate_limiter.acquire()
|
await self._broker._rate_limiter.acquire()
|
||||||
session = self._broker._get_session()
|
session = self._broker._get_session()
|
||||||
|
|
||||||
# Virtual trading TR_IDs for overseas orders
|
# TR_ID: 실전 BUY=TTTT1002U SELL=TTTT1006U, 모의 BUY=VTTT1002U SELL=VTTT1001U
|
||||||
tr_id = "VTTT1002U" if order_type == "BUY" else "VTTT1006U"
|
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '해외주식 주문' 시트
|
||||||
|
if self._broker._settings.MODE == "live":
|
||||||
|
tr_id = "TTTT1002U" if order_type == "BUY" else "TTTT1006U"
|
||||||
|
else:
|
||||||
|
tr_id = "VTTT1002U" if order_type == "BUY" else "VTTT1001U"
|
||||||
|
|
||||||
body = {
|
body = {
|
||||||
"CANO": self._broker._account_no,
|
"CANO": self._broker._account_no,
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ class Settings(BaseSettings):
|
|||||||
KIS_APP_KEY: str
|
KIS_APP_KEY: str
|
||||||
KIS_APP_SECRET: str
|
KIS_APP_SECRET: str
|
||||||
KIS_ACCOUNT_NO: str # format: "XXXXXXXX-XX"
|
KIS_ACCOUNT_NO: str # format: "XXXXXXXX-XX"
|
||||||
KIS_BASE_URL: str = "https://openapivts.koreainvestment.com:9443"
|
KIS_BASE_URL: str = "https://openapivts.koreainvestment.com:29443"
|
||||||
|
|
||||||
# Google Gemini
|
# Google Gemini
|
||||||
GEMINI_API_KEY: str
|
GEMINI_API_KEY: str
|
||||||
GEMINI_MODEL: str = "gemini-pro"
|
GEMINI_MODEL: str = "gemini-2.0-flash"
|
||||||
|
|
||||||
# External Data APIs (optional — for data-driven decisions)
|
# External Data APIs (optional — for data-driven decisions)
|
||||||
NEWS_API_KEY: str | None = None
|
NEWS_API_KEY: str | None = None
|
||||||
|
|||||||
@@ -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,9 +143,80 @@
|
|||||||
.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); } }
|
||||||
|
|
||||||
|
/* Generic panel */
|
||||||
|
.panel {
|
||||||
|
background: var(--panel);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 16px;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Playbook panel - details/summary accordion */
|
||||||
|
.playbook-panel details { border: 1px solid var(--border); border-radius: 4px; margin-bottom: 6px; }
|
||||||
|
.playbook-panel summary { padding: 8px 12px; cursor: pointer; font-weight: 600; background: var(--bg); color: var(--fg); }
|
||||||
|
.playbook-panel summary:hover { color: var(--accent); }
|
||||||
|
.playbook-panel pre { margin: 0; padding: 12px; background: var(--bg); overflow-x: auto;
|
||||||
|
font-size: 11px; color: #a0c4ff; white-space: pre-wrap; }
|
||||||
|
|
||||||
|
/* Scorecard KPI card grid */
|
||||||
|
.scorecard-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
|
||||||
|
.kpi-card { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 12px; text-align: center; }
|
||||||
|
.kpi-card .kpi-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
|
||||||
|
.kpi-card .kpi-value { font-size: 20px; font-weight: 700; color: var(--fg); }
|
||||||
|
|
||||||
|
/* Scenarios table */
|
||||||
|
.scenarios-table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
||||||
|
.scenarios-table th { background: var(--bg); padding: 8px; text-align: left; border-bottom: 1px solid var(--border);
|
||||||
|
color: var(--muted); font-size: 11px; font-weight: 600; white-space: nowrap; }
|
||||||
|
.scenarios-table td { padding: 7px 8px; border-bottom: 1px solid rgba(40,69,95,0.5); }
|
||||||
|
.scenarios-table tr:hover td { background: rgba(255,255,255,0.02); }
|
||||||
|
|
||||||
|
/* Context table */
|
||||||
|
.context-table { width: 100%; border-collapse: collapse; font-size: 12px; }
|
||||||
|
.context-table th { background: var(--bg); padding: 8px; text-align: left; border-bottom: 1px solid var(--border);
|
||||||
|
color: var(--muted); font-size: 11px; font-weight: 600; white-space: nowrap; }
|
||||||
|
.context-table td { padding: 6px 8px; border-bottom: 1px solid rgba(40,69,95,0.5); vertical-align: top; }
|
||||||
|
.context-value { max-height: 60px; overflow-y: auto; color: #a0c4ff; word-break: break-all; }
|
||||||
|
|
||||||
|
/* Common panel select controls */
|
||||||
|
.panel-controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
|
||||||
|
.panel-controls select, .panel-controls input[type="number"] {
|
||||||
|
background: var(--bg); color: var(--fg); border: 1px solid var(--border);
|
||||||
|
border-radius: 4px; padding: 4px 8px; font-size: 13px; font-family: inherit;
|
||||||
|
}
|
||||||
|
.panel-date { color: var(--muted); font-size: 12px; }
|
||||||
|
.empty-msg { color: var(--muted); text-align: center; padding: 20px 0; font-size: 12px; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -134,6 +225,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 +261,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">
|
||||||
@@ -206,6 +328,72 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- playbook panel -->
|
||||||
|
<div class="panel playbook-panel">
|
||||||
|
<div class="panel-header">
|
||||||
|
<span class="panel-title">📋 프리마켓 플레이북</span>
|
||||||
|
<div class="panel-controls">
|
||||||
|
<select id="pb-market-select" onchange="fetchPlaybook()">
|
||||||
|
<option value="KR">KR</option>
|
||||||
|
<option value="US_NASDAQ">US_NASDAQ</option>
|
||||||
|
<option value="US_NYSE">US_NYSE</option>
|
||||||
|
</select>
|
||||||
|
<span id="pb-date" class="panel-date"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="playbook-content"><p class="empty-msg">데이터 없음</p></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- scorecard panel -->
|
||||||
|
<div class="panel">
|
||||||
|
<div class="panel-header">
|
||||||
|
<span class="panel-title">📊 일간 스코어카드</span>
|
||||||
|
<div class="panel-controls">
|
||||||
|
<select id="sc-market-select" onchange="fetchScorecard()">
|
||||||
|
<option value="KR">KR</option>
|
||||||
|
<option value="US_NASDAQ">US_NASDAQ</option>
|
||||||
|
</select>
|
||||||
|
<span id="sc-date" class="panel-date"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="scorecard-grid" class="scorecard-grid"><p class="empty-msg">데이터 없음</p></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- scenarios panel -->
|
||||||
|
<div class="panel">
|
||||||
|
<div class="panel-header">
|
||||||
|
<span class="panel-title">🎯 활성 시나리오 매칭</span>
|
||||||
|
<div class="panel-controls">
|
||||||
|
<select id="scen-market-select" onchange="fetchScenarios()">
|
||||||
|
<option value="KR">KR</option>
|
||||||
|
<option value="US_NASDAQ">US_NASDAQ</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="scenarios-content"><p class="empty-msg">데이터 없음</p></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- context layer panel -->
|
||||||
|
<div class="panel">
|
||||||
|
<div class="panel-header">
|
||||||
|
<span class="panel-title">🧠 컨텍스트 트리</span>
|
||||||
|
<div class="panel-controls">
|
||||||
|
<select id="ctx-layer-select" onchange="fetchContext()">
|
||||||
|
<option value="L7_REALTIME">L7_REALTIME</option>
|
||||||
|
<option value="L6_DAILY">L6_DAILY</option>
|
||||||
|
<option value="L5_WEEKLY">L5_WEEKLY</option>
|
||||||
|
<option value="L4_MONTHLY">L4_MONTHLY</option>
|
||||||
|
<option value="L3_QUARTERLY">L3_QUARTERLY</option>
|
||||||
|
<option value="L2_YEARLY">L2_YEARLY</option>
|
||||||
|
<option value="L1_LIFETIME">L1_LIFETIME</option>
|
||||||
|
</select>
|
||||||
|
<input id="ctx-limit" type="number" value="20" min="1" max="200"
|
||||||
|
style="width:60px;" onchange="fetchContext()">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="context-content"><p class="empty-msg">데이터 없음</p></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@@ -242,6 +430,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 +511,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 {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,13 +632,129 @@
|
|||||||
fetchDecisions(currentMarket);
|
fetchDecisions(currentMarket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function todayStr() {
|
||||||
|
return new Date().toISOString().slice(0, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
function esc(s) {
|
||||||
|
return String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchJSON(url) {
|
||||||
|
const r = await fetch(url);
|
||||||
|
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||||
|
return r.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchPlaybook() {
|
||||||
|
const market = document.getElementById('pb-market-select').value;
|
||||||
|
const date = todayStr();
|
||||||
|
document.getElementById('pb-date').textContent = date;
|
||||||
|
const el = document.getElementById('playbook-content');
|
||||||
|
try {
|
||||||
|
const data = await fetchJSON(`/api/playbook/${date}?market=${market}`);
|
||||||
|
const stocks = data.stock_playbooks ?? [];
|
||||||
|
if (stocks.length === 0) {
|
||||||
|
el.innerHTML = '<p class="empty-msg">오늘 플레이북 없음</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
el.innerHTML = stocks.map(sp =>
|
||||||
|
`<details><summary>${esc(sp.stock_code ?? '?')} — ${esc(sp.signal ?? '')}</summary>` +
|
||||||
|
`<pre>${esc(JSON.stringify(sp, null, 2))}</pre></details>`
|
||||||
|
).join('');
|
||||||
|
} catch {
|
||||||
|
el.innerHTML = '<p class="empty-msg">플레이북 없음 (오늘 미생성 또는 API 오류)</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchScorecard() {
|
||||||
|
const market = document.getElementById('sc-market-select').value;
|
||||||
|
const date = todayStr();
|
||||||
|
document.getElementById('sc-date').textContent = date;
|
||||||
|
const el = document.getElementById('scorecard-grid');
|
||||||
|
try {
|
||||||
|
const data = await fetchJSON(`/api/scorecard/${date}?market=${market}`);
|
||||||
|
const sc = data.scorecard ?? {};
|
||||||
|
const entries = Object.entries(sc);
|
||||||
|
if (entries.length === 0) {
|
||||||
|
el.innerHTML = '<p class="empty-msg">스코어카드 없음</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
el.className = 'scorecard-grid';
|
||||||
|
el.innerHTML = entries.map(([k, v]) => `
|
||||||
|
<div class="kpi-card">
|
||||||
|
<div class="kpi-label">${esc(k)}</div>
|
||||||
|
<div class="kpi-value">${typeof v === 'number' ? v.toFixed(2) : esc(String(v))}</div>
|
||||||
|
</div>`).join('');
|
||||||
|
} catch {
|
||||||
|
el.innerHTML = '<p class="empty-msg">스코어카드 없음 (오늘 미생성 또는 API 오류)</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchScenarios() {
|
||||||
|
const market = document.getElementById('scen-market-select').value;
|
||||||
|
const date = todayStr();
|
||||||
|
const el = document.getElementById('scenarios-content');
|
||||||
|
try {
|
||||||
|
const data = await fetchJSON(`/api/scenarios/active?market=${market}&date_str=${date}&limit=50`);
|
||||||
|
const matches = data.matches ?? [];
|
||||||
|
if (matches.length === 0) {
|
||||||
|
el.innerHTML = '<p class="empty-msg">활성 시나리오 없음</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
el.innerHTML = `<table class="scenarios-table">
|
||||||
|
<thead><tr><th>종목</th><th>신호</th><th>신뢰도</th><th>매칭 조건</th></tr></thead>
|
||||||
|
<tbody>${matches.map(m => `
|
||||||
|
<tr>
|
||||||
|
<td>${esc(m.stock_code)}</td>
|
||||||
|
<td>${esc(m.signal ?? '-')}</td>
|
||||||
|
<td>${esc(m.confidence ?? '-')}</td>
|
||||||
|
<td><code style="font-size:11px">${esc(JSON.stringify(m.scenario_match ?? {}))}</code></td>
|
||||||
|
</tr>`).join('')}
|
||||||
|
</tbody></table>`;
|
||||||
|
} catch {
|
||||||
|
el.innerHTML = '<p class="empty-msg">데이터 없음</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchContext() {
|
||||||
|
const layer = document.getElementById('ctx-layer-select').value;
|
||||||
|
const limit = Math.min(Math.max(parseInt(document.getElementById('ctx-limit').value, 10) || 20, 1), 200);
|
||||||
|
const el = document.getElementById('context-content');
|
||||||
|
try {
|
||||||
|
const data = await fetchJSON(`/api/context/${layer}?limit=${limit}`);
|
||||||
|
const entries = data.entries ?? [];
|
||||||
|
if (entries.length === 0) {
|
||||||
|
el.innerHTML = '<p class="empty-msg">컨텍스트 없음</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
el.innerHTML = `<table class="context-table">
|
||||||
|
<thead><tr><th>timeframe</th><th>key</th><th>value</th><th>updated</th></tr></thead>
|
||||||
|
<tbody>${entries.map(e => `
|
||||||
|
<tr>
|
||||||
|
<td>${esc(e.timeframe)}</td>
|
||||||
|
<td>${esc(e.key)}</td>
|
||||||
|
<td><div class="context-value">${esc(JSON.stringify(e.value ?? e.raw_value))}</div></td>
|
||||||
|
<td style="font-size:11px;color:var(--muted)">${esc((e.updated_at ?? '').slice(0, 16))}</td>
|
||||||
|
</tr>`).join('')}
|
||||||
|
</tbody></table>`;
|
||||||
|
} catch {
|
||||||
|
el.innerHTML = '<p class="empty-msg">데이터 없음</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function refreshAll() {
|
async function refreshAll() {
|
||||||
document.getElementById('last-updated').textContent = '업데이트 중...';
|
document.getElementById('last-updated').textContent = '업데이트 중...';
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fetchStatus(),
|
fetchStatus(),
|
||||||
fetchPerformance(),
|
fetchPerformance(),
|
||||||
|
fetchPositions(),
|
||||||
fetchPnlHistory(currentDays),
|
fetchPnlHistory(currentDays),
|
||||||
fetchDecisions(currentMarket),
|
fetchDecisions(currentMarket),
|
||||||
|
fetchPlaybook(),
|
||||||
|
fetchScorecard(),
|
||||||
|
fetchScenarios(),
|
||||||
|
fetchContext(),
|
||||||
]);
|
]);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const timeStr = now.toLocaleTimeString('ko-KR', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
|
const timeStr = now.toLocaleTimeString('ko-KR', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
|
||||||
|
|||||||
40
src/db.py
40
src/db.py
@@ -14,6 +14,11 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
if db_path != ":memory:":
|
if db_path != ":memory:":
|
||||||
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
|
# Enable WAL mode for concurrent read/write (dashboard + trading loop).
|
||||||
|
# WAL does not apply to in-memory databases.
|
||||||
|
if db_path != ":memory:":
|
||||||
|
conn.execute("PRAGMA journal_mode=WAL")
|
||||||
|
conn.execute("PRAGMA busy_timeout=5000")
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"""
|
"""
|
||||||
CREATE TABLE IF NOT EXISTS trades (
|
CREATE TABLE IF NOT EXISTS trades (
|
||||||
@@ -28,12 +33,13 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
pnl REAL DEFAULT 0.0,
|
pnl REAL DEFAULT 0.0,
|
||||||
market TEXT DEFAULT 'KR',
|
market TEXT DEFAULT 'KR',
|
||||||
exchange_code TEXT DEFAULT 'KRX',
|
exchange_code TEXT DEFAULT 'KRX',
|
||||||
decision_id TEXT
|
decision_id TEXT,
|
||||||
|
mode TEXT DEFAULT 'paper'
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# Migration: Add market and exchange_code columns if they don't exist
|
# Migration: Add columns if they don't exist (backward-compatible schema upgrades)
|
||||||
cursor = conn.execute("PRAGMA table_info(trades)")
|
cursor = conn.execute("PRAGMA table_info(trades)")
|
||||||
columns = {row[1] for row in cursor.fetchall()}
|
columns = {row[1] for row in cursor.fetchall()}
|
||||||
|
|
||||||
@@ -45,6 +51,8 @@ def init_db(db_path: str) -> sqlite3.Connection:
|
|||||||
conn.execute("ALTER TABLE trades ADD COLUMN selection_context TEXT")
|
conn.execute("ALTER TABLE trades ADD COLUMN selection_context TEXT")
|
||||||
if "decision_id" not in columns:
|
if "decision_id" not in columns:
|
||||||
conn.execute("ALTER TABLE trades ADD COLUMN decision_id TEXT")
|
conn.execute("ALTER TABLE trades ADD COLUMN decision_id TEXT")
|
||||||
|
if "mode" not in columns:
|
||||||
|
conn.execute("ALTER TABLE trades ADD COLUMN mode TEXT DEFAULT 'paper'")
|
||||||
|
|
||||||
# Context tree tables for multi-layered memory management
|
# Context tree tables for multi-layered memory management
|
||||||
conn.execute(
|
conn.execute(
|
||||||
@@ -131,6 +139,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
|
||||||
|
|
||||||
@@ -148,6 +175,7 @@ def log_trade(
|
|||||||
exchange_code: str = "KRX",
|
exchange_code: str = "KRX",
|
||||||
selection_context: dict[str, any] | None = None,
|
selection_context: dict[str, any] | None = None,
|
||||||
decision_id: str | None = None,
|
decision_id: str | None = None,
|
||||||
|
mode: str = "paper",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Insert a trade record into the database.
|
"""Insert a trade record into the database.
|
||||||
|
|
||||||
@@ -163,6 +191,8 @@ def log_trade(
|
|||||||
market: Market code
|
market: Market code
|
||||||
exchange_code: Exchange code
|
exchange_code: Exchange code
|
||||||
selection_context: Scanner selection data (RSI, volume_ratio, signal, score)
|
selection_context: Scanner selection data (RSI, volume_ratio, signal, score)
|
||||||
|
decision_id: Unique decision identifier for audit linking
|
||||||
|
mode: Trading mode ('paper' or 'live') for data separation
|
||||||
"""
|
"""
|
||||||
# Serialize selection context to JSON
|
# Serialize selection context to JSON
|
||||||
context_json = json.dumps(selection_context) if selection_context else None
|
context_json = json.dumps(selection_context) if selection_context else None
|
||||||
@@ -171,9 +201,10 @@ def log_trade(
|
|||||||
"""
|
"""
|
||||||
INSERT INTO trades (
|
INSERT INTO trades (
|
||||||
timestamp, stock_code, action, confidence, rationale,
|
timestamp, stock_code, action, confidence, rationale,
|
||||||
quantity, price, pnl, market, exchange_code, selection_context, decision_id
|
quantity, price, pnl, market, exchange_code, selection_context, decision_id,
|
||||||
|
mode
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(
|
(
|
||||||
datetime.now(UTC).isoformat(),
|
datetime.now(UTC).isoformat(),
|
||||||
@@ -188,6 +219,7 @@ def log_trade(
|
|||||||
exchange_code,
|
exchange_code,
|
||||||
context_json,
|
context_json,
|
||||||
decision_id,
|
decision_id,
|
||||||
|
mode,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
170
src/main.py
170
src/main.py
@@ -81,6 +81,7 @@ def safe_float(value: str | float | None, default: float = 0.0) -> float:
|
|||||||
TRADE_INTERVAL_SECONDS = 60
|
TRADE_INTERVAL_SECONDS = 60
|
||||||
SCAN_INTERVAL_SECONDS = 60 # Scan markets every 60 seconds
|
SCAN_INTERVAL_SECONDS = 60 # Scan markets every 60 seconds
|
||||||
MAX_CONNECTION_RETRIES = 3
|
MAX_CONNECTION_RETRIES = 3
|
||||||
|
_BUY_COOLDOWN_SECONDS = 600 # 10-minute cooldown after insufficient-balance rejection
|
||||||
|
|
||||||
# Daily trading mode constants (for Free tier API efficiency)
|
# Daily trading mode constants (for Free tier API efficiency)
|
||||||
DAILY_TRADE_SESSIONS = 4 # Number of trading sessions per day
|
DAILY_TRADE_SESSIONS = 4 # Number of trading sessions per day
|
||||||
@@ -298,6 +299,7 @@ async def trading_cycle(
|
|||||||
stock_code: str,
|
stock_code: str,
|
||||||
scan_candidates: dict[str, dict[str, ScanCandidate]],
|
scan_candidates: dict[str, dict[str, ScanCandidate]],
|
||||||
settings: Settings | None = None,
|
settings: Settings | None = None,
|
||||||
|
buy_cooldown: dict[str, float] | None = None,
|
||||||
) -> 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()
|
||||||
@@ -338,7 +340,13 @@ async def trading_cycle(
|
|||||||
purchase_total = safe_float(balance_info.get("frcr_buy_amt_smtl", "0") or "0")
|
purchase_total = safe_float(balance_info.get("frcr_buy_amt_smtl", "0") or "0")
|
||||||
|
|
||||||
# Paper mode fallback: VTS overseas balance API often fails for many accounts.
|
# Paper mode fallback: VTS overseas balance API often fails for many accounts.
|
||||||
if total_cash <= 0 and settings and settings.PAPER_OVERSEAS_CASH > 0:
|
# Only activate in paper mode — live mode must use real balance from KIS.
|
||||||
|
if (
|
||||||
|
total_cash <= 0
|
||||||
|
and settings
|
||||||
|
and settings.MODE == "paper"
|
||||||
|
and settings.PAPER_OVERSEAS_CASH > 0
|
||||||
|
):
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"Overseas cash balance is 0 for %s; using paper fallback %.2f USD",
|
"Overseas cash balance is 0 for %s; using paper fallback %.2f USD",
|
||||||
market.exchange_code,
|
market.exchange_code,
|
||||||
@@ -428,6 +436,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,
|
||||||
@@ -508,6 +527,33 @@ async def trading_cycle(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# BUY 결정 전 기존 포지션 체크 (중복 매수 방지)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
existing_position = get_open_position(db_conn, stock_code, market.code)
|
||||||
|
if not existing_position and not market.is_domestic:
|
||||||
|
# SELL 지정가 접수 후 미체결 시 DB는 종료로 기록되나 브로커는 여전히 보유 중.
|
||||||
|
# 이중 매수 방지를 위해 라이브 브로커 잔고를 authoritative source로 사용.
|
||||||
|
broker_qty = _extract_held_qty_from_balance(
|
||||||
|
balance_data, stock_code, is_domestic=False
|
||||||
|
)
|
||||||
|
if broker_qty > 0:
|
||||||
|
existing_position = {"price": 0.0, "quantity": broker_qty}
|
||||||
|
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:
|
||||||
@@ -642,8 +688,28 @@ async def trading_cycle(
|
|||||||
return
|
return
|
||||||
order_amount = current_price * quantity
|
order_amount = current_price * quantity
|
||||||
|
|
||||||
# 4. Risk check BEFORE order
|
# 4. Check BUY cooldown (set when a prior BUY failed due to insufficient balance)
|
||||||
|
if decision.action == "BUY" and buy_cooldown is not None:
|
||||||
|
cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
cooldown_until = buy_cooldown.get(cooldown_key, 0.0)
|
||||||
|
now = asyncio.get_event_loop().time()
|
||||||
|
if now < cooldown_until:
|
||||||
|
remaining = int(cooldown_until - now)
|
||||||
|
logger.info(
|
||||||
|
"Skip BUY %s (%s): insufficient-balance cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 5a. Risk check BEFORE order
|
||||||
|
# SELL orders do not consume cash (they receive it), so fat-finger check
|
||||||
|
# is skipped for SELLs — only circuit breaker applies.
|
||||||
try:
|
try:
|
||||||
|
if decision.action == "SELL":
|
||||||
|
risk.check_circuit_breaker(pnl_pct)
|
||||||
|
else:
|
||||||
risk.validate_order(
|
risk.validate_order(
|
||||||
current_pnl_pct=pnl_pct,
|
current_pnl_pct=pnl_pct,
|
||||||
order_amount=order_amount,
|
order_amount=order_amount,
|
||||||
@@ -690,11 +756,23 @@ async def trading_cycle(
|
|||||||
# Check if KIS rejected the order (rt_cd != "0")
|
# Check if KIS rejected the order (rt_cd != "0")
|
||||||
if result.get("rt_cd", "") != "0":
|
if result.get("rt_cd", "") != "0":
|
||||||
order_succeeded = False
|
order_succeeded = False
|
||||||
|
msg1 = result.get("msg1") or ""
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
||||||
stock_code,
|
stock_code,
|
||||||
result.get("rt_cd"),
|
result.get("rt_cd"),
|
||||||
result.get("msg1"),
|
msg1,
|
||||||
|
)
|
||||||
|
# Set BUY cooldown when the rejection is due to insufficient balance
|
||||||
|
if decision.action == "BUY" and buy_cooldown is not None and "주문가능금액" in msg1:
|
||||||
|
cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
buy_cooldown[cooldown_key] = (
|
||||||
|
asyncio.get_event_loop().time() + _BUY_COOLDOWN_SECONDS
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY cooldown set for %s: %.0fs (insufficient balance)",
|
||||||
|
stock_code,
|
||||||
|
_BUY_COOLDOWN_SECONDS,
|
||||||
)
|
)
|
||||||
logger.info("Order result: %s", result.get("msg1", "OK"))
|
logger.info("Order result: %s", result.get("msg1", "OK"))
|
||||||
|
|
||||||
@@ -750,6 +828,7 @@ async def trading_cycle(
|
|||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
selection_context=selection_context,
|
selection_context=selection_context,
|
||||||
decision_id=decision_id,
|
decision_id=decision_id,
|
||||||
|
mode=settings.MODE if settings else "paper",
|
||||||
)
|
)
|
||||||
|
|
||||||
# 7. Latency monitoring
|
# 7. Latency monitoring
|
||||||
@@ -803,6 +882,9 @@ async def run_daily_session(
|
|||||||
|
|
||||||
logger.info("Starting daily trading session for %d markets", len(open_markets))
|
logger.info("Starting daily trading session for %d markets", len(open_markets))
|
||||||
|
|
||||||
|
# BUY cooldown: prevents retrying stocks rejected for insufficient balance
|
||||||
|
daily_buy_cooldown: dict[str, float] = {} # "{market_code}:{stock_code}" -> expiry timestamp
|
||||||
|
|
||||||
# Process each open market
|
# Process each open market
|
||||||
for market in open_markets:
|
for market in open_markets:
|
||||||
# Use market-local date for playbook keying
|
# Use market-local date for playbook keying
|
||||||
@@ -966,11 +1048,12 @@ async def run_daily_session(
|
|||||||
balance_info.get("frcr_buy_amt_smtl", "0") or "0"
|
balance_info.get("frcr_buy_amt_smtl", "0") or "0"
|
||||||
)
|
)
|
||||||
# Paper mode fallback: VTS overseas balance API often fails for many accounts.
|
# Paper mode fallback: VTS overseas balance API often fails for many accounts.
|
||||||
if total_cash <= 0 and settings.PAPER_OVERSEAS_CASH > 0:
|
# Only activate in paper mode — live mode must use real balance from KIS.
|
||||||
total_cash = settings.PAPER_OVERSEAS_CASH
|
if (
|
||||||
|
total_cash <= 0
|
||||||
# VTS overseas balance API often returns 0; use paper fallback.
|
and settings.MODE == "paper"
|
||||||
if total_cash <= 0 and settings.PAPER_OVERSEAS_CASH > 0:
|
and settings.PAPER_OVERSEAS_CASH > 0
|
||||||
|
):
|
||||||
total_cash = settings.PAPER_OVERSEAS_CASH
|
total_cash = settings.PAPER_OVERSEAS_CASH
|
||||||
|
|
||||||
# Calculate daily P&L %
|
# Calculate daily P&L %
|
||||||
@@ -1009,6 +1092,33 @@ async def run_daily_session(
|
|||||||
decision.confidence,
|
decision.confidence,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# BUY 중복 방지: 브로커 잔고 기반 (미체결 SELL 리밋 주문 보호)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
daily_existing = get_open_position(db_conn, stock_code, market.code)
|
||||||
|
if not daily_existing and not market.is_domestic:
|
||||||
|
# SELL 지정가 접수 후 미체결 시 DB는 종료로 기록되나 브로커는 여전히 보유 중.
|
||||||
|
# 이중 매수 방지를 위해 라이브 브로커 잔고를 authoritative source로 사용.
|
||||||
|
broker_qty = _extract_held_qty_from_balance(
|
||||||
|
balance_data, stock_code, is_domestic=False
|
||||||
|
)
|
||||||
|
if broker_qty > 0:
|
||||||
|
daily_existing = {"price": 0.0, "quantity": broker_qty}
|
||||||
|
if daily_existing:
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=decision.confidence,
|
||||||
|
rationale=(
|
||||||
|
f"Already holding {stock_code} "
|
||||||
|
f"(entry={daily_existing['price']:.4f}, "
|
||||||
|
f"qty={daily_existing['quantity']})"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): already holding open position",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
)
|
||||||
|
|
||||||
# Log decision
|
# Log decision
|
||||||
context_snapshot = {
|
context_snapshot = {
|
||||||
"L1": {
|
"L1": {
|
||||||
@@ -1075,8 +1185,28 @@ async def run_daily_session(
|
|||||||
continue
|
continue
|
||||||
order_amount = stock_data["current_price"] * quantity
|
order_amount = stock_data["current_price"] * quantity
|
||||||
|
|
||||||
|
# Check BUY cooldown (insufficient balance)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
daily_cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
daily_cooldown_until = daily_buy_cooldown.get(daily_cooldown_key, 0.0)
|
||||||
|
now = asyncio.get_event_loop().time()
|
||||||
|
if now < daily_cooldown_until:
|
||||||
|
remaining = int(daily_cooldown_until - now)
|
||||||
|
logger.info(
|
||||||
|
"Skip BUY %s (%s): insufficient-balance cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
# Risk check
|
# Risk check
|
||||||
|
# SELL orders do not consume cash (they receive it), so fat-finger
|
||||||
|
# check is skipped for SELLs — only circuit breaker applies.
|
||||||
try:
|
try:
|
||||||
|
if decision.action == "SELL":
|
||||||
|
risk.check_circuit_breaker(pnl_pct)
|
||||||
|
else:
|
||||||
risk.validate_order(
|
risk.validate_order(
|
||||||
current_pnl_pct=pnl_pct,
|
current_pnl_pct=pnl_pct,
|
||||||
order_amount=order_amount,
|
order_amount=order_amount,
|
||||||
@@ -1131,11 +1261,22 @@ async def run_daily_session(
|
|||||||
)
|
)
|
||||||
if result.get("rt_cd", "") != "0":
|
if result.get("rt_cd", "") != "0":
|
||||||
order_succeeded = False
|
order_succeeded = False
|
||||||
|
daily_msg1 = result.get("msg1") or ""
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
||||||
stock_code,
|
stock_code,
|
||||||
result.get("rt_cd"),
|
result.get("rt_cd"),
|
||||||
result.get("msg1"),
|
daily_msg1,
|
||||||
|
)
|
||||||
|
if decision.action == "BUY" and "주문가능금액" in daily_msg1:
|
||||||
|
daily_cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
daily_buy_cooldown[daily_cooldown_key] = (
|
||||||
|
asyncio.get_event_loop().time() + _BUY_COOLDOWN_SECONDS
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY cooldown set for %s: %.0fs (insufficient balance)",
|
||||||
|
stock_code,
|
||||||
|
_BUY_COOLDOWN_SECONDS,
|
||||||
)
|
)
|
||||||
logger.info("Order result: %s", result.get("msg1", "OK"))
|
logger.info("Order result: %s", result.get("msg1", "OK"))
|
||||||
|
|
||||||
@@ -1185,6 +1326,7 @@ async def run_daily_session(
|
|||||||
market=market.code,
|
market=market.code,
|
||||||
exchange_code=market.exchange_code,
|
exchange_code=market.exchange_code,
|
||||||
decision_id=decision_id,
|
decision_id=decision_id,
|
||||||
|
mode=settings.MODE,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info("Daily trading session completed")
|
logger.info("Daily trading session completed")
|
||||||
@@ -1763,6 +1905,9 @@ async def run(settings: Settings) -> None:
|
|||||||
# Active stocks per market (dynamically discovered by scanner)
|
# Active stocks per market (dynamically discovered by scanner)
|
||||||
active_stocks: dict[str, list[str]] = {} # market_code -> [stock_codes]
|
active_stocks: dict[str, list[str]] = {} # market_code -> [stock_codes]
|
||||||
|
|
||||||
|
# BUY cooldown: prevents retrying a stock rejected for insufficient balance
|
||||||
|
buy_cooldown: dict[str, float] = {} # "{market_code}:{stock_code}" -> expiry timestamp
|
||||||
|
|
||||||
# Initialize latency control system
|
# Initialize latency control system
|
||||||
criticality_assessor = CriticalityAssessor(
|
criticality_assessor = CriticalityAssessor(
|
||||||
critical_pnl_threshold=-2.5, # Near circuit breaker at -3.0%
|
critical_pnl_threshold=-2.5, # Near circuit breaker at -3.0%
|
||||||
@@ -1843,6 +1988,10 @@ async def run(settings: Settings) -> None:
|
|||||||
)
|
)
|
||||||
except CircuitBreakerTripped:
|
except CircuitBreakerTripped:
|
||||||
logger.critical("Circuit breaker tripped — shutting down")
|
logger.critical("Circuit breaker tripped — shutting down")
|
||||||
|
await telegram.notify_circuit_breaker(
|
||||||
|
pnl_pct=settings.CIRCUIT_BREAKER_PCT,
|
||||||
|
threshold=settings.CIRCUIT_BREAKER_PCT,
|
||||||
|
)
|
||||||
shutdown.set()
|
shutdown.set()
|
||||||
break
|
break
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
@@ -2105,6 +2254,7 @@ async def run(settings: Settings) -> None:
|
|||||||
stock_code,
|
stock_code,
|
||||||
scan_candidates,
|
scan_candidates,
|
||||||
settings,
|
settings,
|
||||||
|
buy_cooldown,
|
||||||
)
|
)
|
||||||
break # Success — exit retry loop
|
break # Success — exit retry loop
|
||||||
except CircuitBreakerTripped as exc:
|
except CircuitBreakerTripped as exc:
|
||||||
@@ -2159,6 +2309,8 @@ async def run(settings: Settings) -> None:
|
|||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
pass # Normal — timeout means it's time for next cycle
|
pass # Normal — timeout means it's time for next cycle
|
||||||
finally:
|
finally:
|
||||||
|
# Notify shutdown before closing resources
|
||||||
|
await telegram.notify_system_shutdown("Normal shutdown")
|
||||||
# Clean up resources
|
# Clean up resources
|
||||||
await command_handler.stop_polling()
|
await command_handler.stop_polling()
|
||||||
await broker.close()
|
await broker.close()
|
||||||
|
|||||||
@@ -604,6 +604,16 @@ class TelegramCommandHandler:
|
|||||||
async with session.post(url, json=payload) as resp:
|
async with session.post(url, json=payload) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
error_text = await resp.text()
|
error_text = await resp.text()
|
||||||
|
if resp.status == 409:
|
||||||
|
# Another bot instance is already polling — stop this poller entirely.
|
||||||
|
# Retrying would keep conflicting with the other instance.
|
||||||
|
self._running = False
|
||||||
|
logger.warning(
|
||||||
|
"Telegram conflict (409): another instance is already polling. "
|
||||||
|
"Disabling Telegram commands for this process. "
|
||||||
|
"Ensure only one instance of The Ouroboros is running at a time.",
|
||||||
|
)
|
||||||
|
else:
|
||||||
logger.error(
|
logger.error(
|
||||||
"getUpdates API error (status=%d): %s", resp.status, error_text
|
"getUpdates API error (status=%d): %s", resp.status, error_text
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -572,4 +572,156 @@ class TestSendOrderTickRounding:
|
|||||||
order_call = mock_post.call_args_list[1]
|
order_call = mock_post.call_args_list[1]
|
||||||
body = order_call[1].get("json", {})
|
body = order_call[1].get("json", {})
|
||||||
assert body["ORD_DVSN"] == "01"
|
assert body["ORD_DVSN"] == "01"
|
||||||
assert body["ORD_UNPR"] == "0"
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# TR_ID live/paper branching (issues #201, #202, #203)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestTRIDBranchingDomestic:
|
||||||
|
"""get_balance and send_order must use correct TR_ID for live vs paper mode."""
|
||||||
|
|
||||||
|
def _make_broker(self, settings, mode: str) -> KISBroker:
|
||||||
|
from src.config import Settings
|
||||||
|
|
||||||
|
s = Settings(
|
||||||
|
KIS_APP_KEY=settings.KIS_APP_KEY,
|
||||||
|
KIS_APP_SECRET=settings.KIS_APP_SECRET,
|
||||||
|
KIS_ACCOUNT_NO=settings.KIS_ACCOUNT_NO,
|
||||||
|
GEMINI_API_KEY=settings.GEMINI_API_KEY,
|
||||||
|
DB_PATH=":memory:",
|
||||||
|
ENABLED_MARKETS="KR",
|
||||||
|
MODE=mode,
|
||||||
|
)
|
||||||
|
b = KISBroker(s)
|
||||||
|
b._access_token = "tok"
|
||||||
|
b._token_expires_at = float("inf")
|
||||||
|
b._rate_limiter.acquire = AsyncMock()
|
||||||
|
return b
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_balance_paper_uses_vttc8434r(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "paper")
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(
|
||||||
|
return_value={"output1": [], "output2": {}}
|
||||||
|
)
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch("aiohttp.ClientSession.get", return_value=mock_resp) as mock_get:
|
||||||
|
await broker.get_balance()
|
||||||
|
|
||||||
|
headers = mock_get.call_args[1].get("headers", {})
|
||||||
|
assert headers["tr_id"] == "VTTC8434R"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_balance_live_uses_tttc8434r(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "live")
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(
|
||||||
|
return_value={"output1": [], "output2": {}}
|
||||||
|
)
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch("aiohttp.ClientSession.get", return_value=mock_resp) as mock_get:
|
||||||
|
await broker.get_balance()
|
||||||
|
|
||||||
|
headers = mock_get.call_args[1].get("headers", {})
|
||||||
|
assert headers["tr_id"] == "TTTC8434R"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_order_buy_paper_uses_vttc0012u(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "paper")
|
||||||
|
mock_hash = AsyncMock()
|
||||||
|
mock_hash.status = 200
|
||||||
|
mock_hash.json = AsyncMock(return_value={"HASH": "h"})
|
||||||
|
mock_hash.__aenter__ = AsyncMock(return_value=mock_hash)
|
||||||
|
mock_hash.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_order = AsyncMock()
|
||||||
|
mock_order.status = 200
|
||||||
|
mock_order.json = AsyncMock(return_value={"rt_cd": "0"})
|
||||||
|
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||||
|
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||||
|
) as mock_post:
|
||||||
|
await broker.send_order("005930", "BUY", 1)
|
||||||
|
|
||||||
|
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||||
|
assert order_headers["tr_id"] == "VTTC0012U"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_order_buy_live_uses_tttc0012u(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "live")
|
||||||
|
mock_hash = AsyncMock()
|
||||||
|
mock_hash.status = 200
|
||||||
|
mock_hash.json = AsyncMock(return_value={"HASH": "h"})
|
||||||
|
mock_hash.__aenter__ = AsyncMock(return_value=mock_hash)
|
||||||
|
mock_hash.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_order = AsyncMock()
|
||||||
|
mock_order.status = 200
|
||||||
|
mock_order.json = AsyncMock(return_value={"rt_cd": "0"})
|
||||||
|
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||||
|
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||||
|
) as mock_post:
|
||||||
|
await broker.send_order("005930", "BUY", 1)
|
||||||
|
|
||||||
|
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||||
|
assert order_headers["tr_id"] == "TTTC0012U"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_order_sell_paper_uses_vttc0011u(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "paper")
|
||||||
|
mock_hash = AsyncMock()
|
||||||
|
mock_hash.status = 200
|
||||||
|
mock_hash.json = AsyncMock(return_value={"HASH": "h"})
|
||||||
|
mock_hash.__aenter__ = AsyncMock(return_value=mock_hash)
|
||||||
|
mock_hash.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_order = AsyncMock()
|
||||||
|
mock_order.status = 200
|
||||||
|
mock_order.json = AsyncMock(return_value={"rt_cd": "0"})
|
||||||
|
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||||
|
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||||
|
) as mock_post:
|
||||||
|
await broker.send_order("005930", "SELL", 1)
|
||||||
|
|
||||||
|
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||||
|
assert order_headers["tr_id"] == "VTTC0011U"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_order_sell_live_uses_tttc0011u(self, settings) -> None:
|
||||||
|
broker = self._make_broker(settings, "live")
|
||||||
|
mock_hash = AsyncMock()
|
||||||
|
mock_hash.status = 200
|
||||||
|
mock_hash.json = AsyncMock(return_value={"HASH": "h"})
|
||||||
|
mock_hash.__aenter__ = AsyncMock(return_value=mock_hash)
|
||||||
|
mock_hash.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_order = AsyncMock()
|
||||||
|
mock_order.status = 200
|
||||||
|
mock_order.json = AsyncMock(return_value={"rt_cd": "0"})
|
||||||
|
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||||
|
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||||
|
) as mock_post:
|
||||||
|
await broker.send_order("005930", "SELL", 1)
|
||||||
|
|
||||||
|
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||||
|
assert order_headers["tr_id"] == "TTTC0011U"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
135
tests/test_db.py
135
tests/test_db.py
@@ -1,5 +1,8 @@
|
|||||||
"""Tests for database helper functions."""
|
"""Tests for database helper functions."""
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
|
||||||
from src.db import get_open_position, init_db, log_trade
|
from src.db import get_open_position, init_db, log_trade
|
||||||
|
|
||||||
|
|
||||||
@@ -58,3 +61,135 @@ def test_get_open_position_returns_none_when_latest_is_sell() -> None:
|
|||||||
def test_get_open_position_returns_none_when_no_trades() -> None:
|
def test_get_open_position_returns_none_when_no_trades() -> None:
|
||||||
conn = init_db(":memory:")
|
conn = init_db(":memory:")
|
||||||
assert get_open_position(conn, "AAPL", "US_NASDAQ") is None
|
assert get_open_position(conn, "AAPL", "US_NASDAQ") is None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# WAL mode tests (issue #210)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_wal_mode_applied_to_file_db() -> None:
|
||||||
|
"""File-based DB must use WAL journal mode for dashboard concurrent reads."""
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
||||||
|
db_path = f.name
|
||||||
|
try:
|
||||||
|
conn = init_db(db_path)
|
||||||
|
cursor = conn.execute("PRAGMA journal_mode")
|
||||||
|
mode = cursor.fetchone()[0]
|
||||||
|
assert mode == "wal", f"Expected WAL mode, got {mode}"
|
||||||
|
conn.close()
|
||||||
|
finally:
|
||||||
|
os.unlink(db_path)
|
||||||
|
# Clean up WAL auxiliary files if they exist
|
||||||
|
for ext in ("-wal", "-shm"):
|
||||||
|
path = db_path + ext
|
||||||
|
if os.path.exists(path):
|
||||||
|
os.unlink(path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_wal_mode_not_applied_to_memory_db() -> None:
|
||||||
|
""":memory: DB must not apply WAL (SQLite does not support WAL for in-memory)."""
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
cursor = conn.execute("PRAGMA journal_mode")
|
||||||
|
mode = cursor.fetchone()[0]
|
||||||
|
# In-memory DBs default to 'memory' journal mode
|
||||||
|
assert mode != "wal", "WAL should not be set on in-memory database"
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# mode column tests (issue #212)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_trade_stores_mode_paper() -> None:
|
||||||
|
"""log_trade must persist mode='paper' in the trades table."""
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
log_trade(
|
||||||
|
conn=conn,
|
||||||
|
stock_code="005930",
|
||||||
|
action="BUY",
|
||||||
|
confidence=85,
|
||||||
|
rationale="test",
|
||||||
|
mode="paper",
|
||||||
|
)
|
||||||
|
row = conn.execute("SELECT mode FROM trades ORDER BY id DESC LIMIT 1").fetchone()
|
||||||
|
assert row is not None
|
||||||
|
assert row[0] == "paper"
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_trade_stores_mode_live() -> None:
|
||||||
|
"""log_trade must persist mode='live' in the trades table."""
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
log_trade(
|
||||||
|
conn=conn,
|
||||||
|
stock_code="005930",
|
||||||
|
action="BUY",
|
||||||
|
confidence=85,
|
||||||
|
rationale="test",
|
||||||
|
mode="live",
|
||||||
|
)
|
||||||
|
row = conn.execute("SELECT mode FROM trades ORDER BY id DESC LIMIT 1").fetchone()
|
||||||
|
assert row is not None
|
||||||
|
assert row[0] == "live"
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_trade_default_mode_is_paper() -> None:
|
||||||
|
"""log_trade without explicit mode must default to 'paper'."""
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
log_trade(
|
||||||
|
conn=conn,
|
||||||
|
stock_code="005930",
|
||||||
|
action="HOLD",
|
||||||
|
confidence=50,
|
||||||
|
rationale="test",
|
||||||
|
)
|
||||||
|
row = conn.execute("SELECT mode FROM trades ORDER BY id DESC LIMIT 1").fetchone()
|
||||||
|
assert row is not None
|
||||||
|
assert row[0] == "paper"
|
||||||
|
|
||||||
|
|
||||||
|
def test_mode_column_exists_in_schema() -> None:
|
||||||
|
"""trades table must have a mode column after init_db."""
|
||||||
|
conn = init_db(":memory:")
|
||||||
|
cursor = conn.execute("PRAGMA table_info(trades)")
|
||||||
|
columns = {row[1] for row in cursor.fetchall()}
|
||||||
|
assert "mode" in columns
|
||||||
|
|
||||||
|
|
||||||
|
def test_mode_migration_adds_column_to_existing_db() -> None:
|
||||||
|
"""init_db must add mode column to existing DBs that lack it (migration)."""
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
||||||
|
db_path = f.name
|
||||||
|
try:
|
||||||
|
# Create DB without mode column (simulate old schema)
|
||||||
|
old_conn = sqlite3.connect(db_path)
|
||||||
|
old_conn.execute(
|
||||||
|
"""CREATE TABLE trades (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
timestamp TEXT NOT NULL,
|
||||||
|
stock_code TEXT NOT NULL,
|
||||||
|
action TEXT NOT NULL,
|
||||||
|
confidence INTEGER NOT NULL,
|
||||||
|
rationale TEXT,
|
||||||
|
quantity INTEGER,
|
||||||
|
price REAL,
|
||||||
|
pnl REAL DEFAULT 0.0,
|
||||||
|
market TEXT DEFAULT 'KR',
|
||||||
|
exchange_code TEXT DEFAULT 'KRX',
|
||||||
|
decision_id TEXT
|
||||||
|
)"""
|
||||||
|
)
|
||||||
|
old_conn.commit()
|
||||||
|
old_conn.close()
|
||||||
|
|
||||||
|
# Run init_db — should add mode column via migration
|
||||||
|
conn = init_db(db_path)
|
||||||
|
cursor = conn.execute("PRAGMA table_info(trades)")
|
||||||
|
columns = {row[1] for row in cursor.fetchall()}
|
||||||
|
assert "mode" in columns
|
||||||
|
conn.close()
|
||||||
|
finally:
|
||||||
|
os.unlink(db_path)
|
||||||
|
|||||||
@@ -631,6 +631,119 @@ class TestTradingCycleTelegramIntegration:
|
|||||||
# Verify no trade notification sent
|
# Verify no trade notification sent
|
||||||
mock_telegram.notify_trade_execution.assert_not_called()
|
mock_telegram.notify_trade_execution.assert_not_called()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_sell_skips_fat_finger_check(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_overseas_broker: MagicMock,
|
||||||
|
mock_scenario_engine: MagicMock,
|
||||||
|
mock_playbook: DayPlaybook,
|
||||||
|
mock_risk: MagicMock,
|
||||||
|
mock_db: MagicMock,
|
||||||
|
mock_decision_logger: MagicMock,
|
||||||
|
mock_context_store: MagicMock,
|
||||||
|
mock_criticality_assessor: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""SELL orders must not be blocked by fat-finger check.
|
||||||
|
|
||||||
|
Even if position value > 30% of cash (e.g. stop-loss on a large holding
|
||||||
|
with low remaining cash), the SELL should proceed — only circuit breaker
|
||||||
|
applies to SELLs.
|
||||||
|
"""
|
||||||
|
# SELL decision with held qty=100 shares @ 50,000 = 5,000,000
|
||||||
|
# cash = 5,000,000 → ratio = 100% which would normally trigger fat finger
|
||||||
|
mock_scenario_engine.evaluate = MagicMock(return_value=_make_sell_match())
|
||||||
|
mock_broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [{"pdno": "005930", "ord_psbl_qty": "100"}],
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"tot_evlu_amt": "10000000",
|
||||||
|
"dnca_tot_amt": "5000000",
|
||||||
|
"pchs_amt_smtl_amt": "5000000",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=mock_scenario_engine,
|
||||||
|
playbook=mock_playbook,
|
||||||
|
risk=mock_risk,
|
||||||
|
db_conn=mock_db,
|
||||||
|
decision_logger=mock_decision_logger,
|
||||||
|
context_store=mock_context_store,
|
||||||
|
criticality_assessor=mock_criticality_assessor,
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# validate_order (which includes fat finger) must NOT be called for SELL
|
||||||
|
mock_risk.validate_order.assert_not_called()
|
||||||
|
# check_circuit_breaker MUST be called for SELL
|
||||||
|
mock_risk.check_circuit_breaker.assert_called_once()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_sell_circuit_breaker_still_applies(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_overseas_broker: MagicMock,
|
||||||
|
mock_scenario_engine: MagicMock,
|
||||||
|
mock_playbook: DayPlaybook,
|
||||||
|
mock_risk: MagicMock,
|
||||||
|
mock_db: MagicMock,
|
||||||
|
mock_decision_logger: MagicMock,
|
||||||
|
mock_context_store: MagicMock,
|
||||||
|
mock_criticality_assessor: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""SELL orders must still respect the circuit breaker."""
|
||||||
|
mock_scenario_engine.evaluate = MagicMock(return_value=_make_sell_match())
|
||||||
|
mock_broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [{"pdno": "005930", "ord_psbl_qty": "100"}],
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"tot_evlu_amt": "10000000",
|
||||||
|
"dnca_tot_amt": "5000000",
|
||||||
|
"pchs_amt_smtl_amt": "5000000",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
mock_risk.check_circuit_breaker.side_effect = CircuitBreakerTripped(
|
||||||
|
pnl_pct=-4.0, threshold=-3.0
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
with pytest.raises(CircuitBreakerTripped):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=mock_scenario_engine,
|
||||||
|
playbook=mock_playbook,
|
||||||
|
risk=mock_risk,
|
||||||
|
db_conn=mock_db,
|
||||||
|
decision_logger=mock_decision_logger,
|
||||||
|
context_store=mock_context_store,
|
||||||
|
criticality_assessor=mock_criticality_assessor,
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_risk.check_circuit_breaker.assert_called_once()
|
||||||
|
mock_risk.validate_order.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
class TestRunFunctionTelegramIntegration:
|
class TestRunFunctionTelegramIntegration:
|
||||||
"""Test telegram notifications in run function."""
|
"""Test telegram notifications in run function."""
|
||||||
@@ -2217,6 +2330,245 @@ def test_start_dashboard_server_returns_none_when_uvicorn_missing() -> None:
|
|||||||
assert thread is None
|
assert thread is None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# BUY cooldown tests (#179)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestBuyCooldown:
|
||||||
|
"""Tests for BUY cooldown after insufficient-balance rejection."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_broker(self) -> MagicMock:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(100.0, 1.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output2": [{"tot_evlu_amt": "1000000", "dnca_tot_amt": "500000",
|
||||||
|
"pchs_amt_smtl_amt": "500000"}]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
return broker
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_market(self) -> MagicMock:
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
return market
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_overseas_market(self) -> MagicMock:
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "NASDAQ"
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
market.exchange_code = "NAS"
|
||||||
|
market.is_domestic = False
|
||||||
|
return market
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_overseas_broker(self) -> MagicMock:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "1.0", "rate": "0.0",
|
||||||
|
"high": "1.05", "low": "0.95", "tvol": "1000000"}}
|
||||||
|
)
|
||||||
|
broker.get_overseas_balance = AsyncMock(return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"frcr_dncl_amt_2": "50000", "frcr_evlu_tota": "50000",
|
||||||
|
"frcr_buy_amt_smtl": "0"}],
|
||||||
|
})
|
||||||
|
broker.send_overseas_order = AsyncMock(
|
||||||
|
return_value={"rt_cd": "1", "msg1": "모의투자 주문가능금액이 부족합니다."}
|
||||||
|
)
|
||||||
|
return broker
|
||||||
|
|
||||||
|
def _make_buy_match_overseas(self, stock_code: str = "MLECW") -> ScenarioMatch:
|
||||||
|
return ScenarioMatch(
|
||||||
|
stock_code=stock_code,
|
||||||
|
matched_scenario=None,
|
||||||
|
action=ScenarioAction.BUY,
|
||||||
|
confidence=85,
|
||||||
|
rationale="Test buy",
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_set_on_insufficient_balance(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY cooldown entry is created after 주문가능금액 rejection."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
buy_cooldown: dict[str, float] = {}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
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=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "US_NASDAQ:MLECW" in buy_cooldown
|
||||||
|
assert buy_cooldown["US_NASDAQ:MLECW"] > 0
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_skips_buy(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY is skipped when cooldown is active for the stock."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
# Set an active cooldown (expires far in the future)
|
||||||
|
buy_cooldown: dict[str, float] = {
|
||||||
|
"US_NASDAQ:MLECW": asyncio.get_event_loop().time() + 600
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
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=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Order should NOT have been sent
|
||||||
|
mock_overseas_broker.send_overseas_order.assert_not_called()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_not_set_on_other_errors(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""Cooldown is NOT set for non-balance-related rejections."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
# Different rejection reason
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "1.0", "rate": "0.0",
|
||||||
|
"high": "1.05", "low": "0.95", "tvol": "1000000"}}
|
||||||
|
)
|
||||||
|
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={"rt_cd": "1", "msg1": "기타 오류 메시지"}
|
||||||
|
)
|
||||||
|
buy_cooldown: dict[str, float] = {}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
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=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Cooldown should NOT be set for non-balance errors
|
||||||
|
assert "US_NASDAQ:MLECW" not in buy_cooldown
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_no_cooldown_param_still_works(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""trading_cycle works normally when buy_cooldown is None (default)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
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=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
# buy_cooldown not passed → defaults to None
|
||||||
|
)
|
||||||
|
|
||||||
|
# Should attempt the order (and fail), but not crash
|
||||||
|
mock_overseas_broker.send_overseas_order.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# market_outlook BUY confidence threshold tests (#173)
|
# market_outlook BUY confidence threshold tests (#173)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -2496,3 +2848,338 @@ 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()
|
||||||
|
|
||||||
|
|
||||||
|
class TestOverseasBrokerIntegration:
|
||||||
|
"""Test overseas broker live-balance gating for double-buy prevention.
|
||||||
|
|
||||||
|
Issue #195: KIS VTS SELL limit orders are accepted (rt_cd=0) immediately
|
||||||
|
but may not fill until the market price reaches the limit. During this window,
|
||||||
|
the DB records the position as closed, causing the next cycle to BUY again.
|
||||||
|
These tests verify that live broker balance is used as the authoritative source.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_overseas_buy_suppressed_by_broker_balance_when_db_shows_closed(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
"""BUY must be suppressed when broker still holds shares even if DB says closed.
|
||||||
|
|
||||||
|
Scenario: SELL limit order was accepted (DB shows closed), but hasn't
|
||||||
|
filled yet — broker balance still shows 10 AAPL shares.
|
||||||
|
Expected: send_overseas_order is NOT called.
|
||||||
|
"""
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
# DB: BUY then SELL recorded → get_open_position returns None (closed)
|
||||||
|
log_trade(
|
||||||
|
conn=db_conn,
|
||||||
|
stock_code="AAPL",
|
||||||
|
action="BUY",
|
||||||
|
confidence=90,
|
||||||
|
rationale="entry",
|
||||||
|
quantity=10,
|
||||||
|
price=180.0,
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="NASD",
|
||||||
|
)
|
||||||
|
log_trade(
|
||||||
|
conn=db_conn,
|
||||||
|
stock_code="AAPL",
|
||||||
|
action="SELL",
|
||||||
|
confidence=90,
|
||||||
|
rationale="sell order accepted",
|
||||||
|
quantity=10,
|
||||||
|
price=182.0,
|
||||||
|
market="US_NASDAQ",
|
||||||
|
exchange_code="NASD",
|
||||||
|
)
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "182.50"}}
|
||||||
|
)
|
||||||
|
# 브로커: 여전히 AAPL 10주 보유 중 (SELL 미체결)
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [{"ovrs_pdno": "AAPL", "ovrs_cblc_qty": "10"}],
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"frcr_dncl_amt_2": "50000.00",
|
||||||
|
"frcr_evlu_tota": "60000.00",
|
||||||
|
"frcr_buy_amt_smtl": "50000.00",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(return_value={"msg1": "주문접수"})
|
||||||
|
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=_make_buy_match("AAPL"))
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
await trading_cycle(
|
||||||
|
broker=MagicMock(),
|
||||||
|
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="AAPL",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# 브로커 잔고에 보유 중이므로 BUY 주문이 억제되어야 함 (이중 매수 방지)
|
||||||
|
overseas_broker.send_overseas_order.assert_not_called()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_overseas_buy_proceeds_when_broker_shows_no_holding(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
"""BUY must proceed when both DB and broker confirm no existing holding.
|
||||||
|
|
||||||
|
Scenario: No prior trades in DB and broker balance shows no AAPL.
|
||||||
|
Expected: send_overseas_order IS called (normal buy flow).
|
||||||
|
"""
|
||||||
|
db_conn = init_db(":memory:")
|
||||||
|
# DB: 레코드 없음 (신규 포지션)
|
||||||
|
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "182.50"}}
|
||||||
|
)
|
||||||
|
# 브로커: AAPL 미보유
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"frcr_dncl_amt_2": "50000.00",
|
||||||
|
"frcr_evlu_tota": "50000.00",
|
||||||
|
"frcr_buy_amt_smtl": "0.00",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(return_value={"msg1": "주문접수"})
|
||||||
|
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=_make_buy_match("AAPL"))
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=MagicMock(),
|
||||||
|
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="AAPL",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# DB도 브로커도 보유 없음 → BUY 주문이 실행되어야 함 (회귀 테스트)
|
||||||
|
overseas_broker.send_overseas_order.assert_called_once()
|
||||||
|
|||||||
@@ -414,7 +414,7 @@ class TestSendOverseasOrder:
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_sell_limit_order(self, overseas_broker: OverseasBroker) -> None:
|
async def test_sell_limit_order(self, overseas_broker: OverseasBroker) -> None:
|
||||||
"""Limit sell order should use VTTT1006U and ORD_DVSN=00."""
|
"""Limit sell order should use VTTT1001U and ORD_DVSN=00."""
|
||||||
mock_resp = AsyncMock()
|
mock_resp = AsyncMock()
|
||||||
mock_resp.status = 200
|
mock_resp.status = 200
|
||||||
mock_resp.json = AsyncMock(return_value={"rt_cd": "0"})
|
mock_resp.json = AsyncMock(return_value={"rt_cd": "0"})
|
||||||
@@ -428,7 +428,7 @@ class TestSendOverseasOrder:
|
|||||||
result = await overseas_broker.send_overseas_order("NYSE", "MSFT", "SELL", 5, price=350.0)
|
result = await overseas_broker.send_overseas_order("NYSE", "MSFT", "SELL", 5, price=350.0)
|
||||||
assert result["rt_cd"] == "0"
|
assert result["rt_cd"] == "0"
|
||||||
|
|
||||||
overseas_broker._broker._auth_headers.assert_called_with("VTTT1006U")
|
overseas_broker._broker._auth_headers.assert_called_with("VTTT1001U")
|
||||||
|
|
||||||
call_args = mock_session.post.call_args
|
call_args = mock_session.post.call_args
|
||||||
body = call_args[1]["json"]
|
body = call_args[1]["json"]
|
||||||
@@ -640,4 +640,176 @@ class TestPaperOverseasCash:
|
|||||||
GEMINI_API_KEY="g",
|
GEMINI_API_KEY="g",
|
||||||
)
|
)
|
||||||
assert settings.PAPER_OVERSEAS_CASH == 0.0
|
assert settings.PAPER_OVERSEAS_CASH == 0.0
|
||||||
del os.environ["PAPER_OVERSEAS_CASH"]
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# TR_ID live/paper branching — overseas (issues #201, #203)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def _make_overseas_broker_with_mode(mode: str) -> OverseasBroker:
|
||||||
|
s = Settings(
|
||||||
|
KIS_APP_KEY="k",
|
||||||
|
KIS_APP_SECRET="s",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="g",
|
||||||
|
DB_PATH=":memory:",
|
||||||
|
MODE=mode,
|
||||||
|
)
|
||||||
|
kis = KISBroker(s)
|
||||||
|
kis._access_token = "tok"
|
||||||
|
kis._token_expires_at = float("inf")
|
||||||
|
kis._rate_limiter.acquire = AsyncMock()
|
||||||
|
return OverseasBroker(kis)
|
||||||
|
|
||||||
|
|
||||||
|
class TestOverseasTRIDBranching:
|
||||||
|
"""get_overseas_balance and send_overseas_order must use correct TR_ID."""
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_overseas_balance_paper_uses_vtts3012r(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("paper")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"output1": [], "output2": []})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.get = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.get_overseas_balance("NASD")
|
||||||
|
assert "VTTS3012R" in captured
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_overseas_balance_live_uses_ttts3012r(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("live")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"output1": [], "output2": []})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.get = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.get_overseas_balance("NASD")
|
||||||
|
assert "TTTS3012R" in captured
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_overseas_order_buy_paper_uses_vttt1002u(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("paper")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
broker._broker._get_hash_key = AsyncMock(return_value="h") # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.send_overseas_order("NASD", "AAPL", "BUY", 1)
|
||||||
|
assert "VTTT1002U" in captured
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_overseas_order_buy_live_uses_tttt1002u(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("live")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
broker._broker._get_hash_key = AsyncMock(return_value="h") # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.send_overseas_order("NASD", "AAPL", "BUY", 1)
|
||||||
|
assert "TTTT1002U" in captured
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_overseas_order_sell_paper_uses_vttt1001u(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("paper")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
broker._broker._get_hash_key = AsyncMock(return_value="h") # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.send_overseas_order("NASD", "AAPL", "SELL", 1)
|
||||||
|
assert "VTTT1001U" in captured
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_send_overseas_order_sell_live_uses_tttt1006u(self) -> None:
|
||||||
|
broker = _make_overseas_broker_with_mode("live")
|
||||||
|
captured: list[str] = []
|
||||||
|
|
||||||
|
async def mock_auth_headers(tr_id: str) -> dict:
|
||||||
|
captured.append(tr_id)
|
||||||
|
return {"tr_id": tr_id, "authorization": "Bearer tok"}
|
||||||
|
|
||||||
|
broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||||
|
broker._broker._get_hash_key = AsyncMock(return_value="h") # type: ignore[method-assign]
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 200
|
||||||
|
mock_resp.json = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
mock_session = MagicMock()
|
||||||
|
mock_session.post = MagicMock(return_value=mock_resp)
|
||||||
|
broker._broker._get_session = MagicMock(return_value=mock_session)
|
||||||
|
|
||||||
|
await broker.send_overseas_order("NASD", "AAPL", "SELL", 1)
|
||||||
|
assert "TTTT1006U" in captured
|
||||||
|
|||||||
@@ -350,6 +350,42 @@ class TestSmartVolatilityScanner:
|
|||||||
assert [c.stock_code for c in candidates] == ["ABCD"]
|
assert [c.stock_code for c in candidates] == ["ABCD"]
|
||||||
|
|
||||||
|
|
||||||
|
class TestImpliedRSIFormula:
|
||||||
|
"""Test the implied_rsi formula in SmartVolatilityScanner (issue #181)."""
|
||||||
|
|
||||||
|
def test_neutral_change_gives_neutral_rsi(self) -> None:
|
||||||
|
"""0% change → implied_rsi = 50 (neutral)."""
|
||||||
|
# formula: 50 + (change_rate * 2.0)
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (0.0 * 2.0)))
|
||||||
|
assert rsi == 50.0
|
||||||
|
|
||||||
|
def test_10pct_change_gives_rsi_70(self) -> None:
|
||||||
|
"""10% upward change → implied_rsi = 70 (momentum signal)."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (10.0 * 2.0)))
|
||||||
|
assert rsi == 70.0
|
||||||
|
|
||||||
|
def test_minus_10pct_gives_rsi_30(self) -> None:
|
||||||
|
"""-10% change → implied_rsi = 30 (oversold signal)."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (-10.0 * 2.0)))
|
||||||
|
assert rsi == 30.0
|
||||||
|
|
||||||
|
def test_saturation_at_25pct(self) -> None:
|
||||||
|
"""Saturation occurs at >=25% change (not 12.5% as with old coefficient 4.0)."""
|
||||||
|
rsi_12pct = max(0.0, min(100.0, 50.0 + (12.5 * 2.0)))
|
||||||
|
rsi_25pct = max(0.0, min(100.0, 50.0 + (25.0 * 2.0)))
|
||||||
|
rsi_30pct = max(0.0, min(100.0, 50.0 + (30.0 * 2.0)))
|
||||||
|
# At 12.5% change: RSI = 75 (not 100, unlike old formula)
|
||||||
|
assert rsi_12pct == 75.0
|
||||||
|
# At 25%+ saturation
|
||||||
|
assert rsi_25pct == 100.0
|
||||||
|
assert rsi_30pct == 100.0 # Capped
|
||||||
|
|
||||||
|
def test_negative_saturation(self) -> None:
|
||||||
|
"""Saturation at -25% gives RSI = 0."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (-25.0 * 2.0)))
|
||||||
|
assert rsi == 0.0
|
||||||
|
|
||||||
|
|
||||||
class TestRSICalculation:
|
class TestRSICalculation:
|
||||||
"""Test RSI calculation in VolatilityAnalyzer."""
|
"""Test RSI calculation in VolatilityAnalyzer."""
|
||||||
|
|
||||||
|
|||||||
@@ -876,6 +876,54 @@ class TestGetUpdates:
|
|||||||
|
|
||||||
assert updates == []
|
assert updates == []
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_updates_409_stops_polling(self) -> None:
|
||||||
|
"""409 Conflict response stops the poller (_running = False) and returns empty list."""
|
||||||
|
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||||
|
handler = TelegramCommandHandler(client)
|
||||||
|
handler._running = True # simulate active poller
|
||||||
|
|
||||||
|
mock_resp = AsyncMock()
|
||||||
|
mock_resp.status = 409
|
||||||
|
mock_resp.text = AsyncMock(
|
||||||
|
return_value='{"ok":false,"error_code":409,"description":"Conflict"}'
|
||||||
|
)
|
||||||
|
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||||
|
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||||
|
|
||||||
|
with patch("aiohttp.ClientSession.post", return_value=mock_resp):
|
||||||
|
updates = await handler._get_updates()
|
||||||
|
|
||||||
|
assert updates == []
|
||||||
|
assert handler._running is False # poller stopped
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_poll_loop_exits_after_409(self) -> None:
|
||||||
|
"""_poll_loop exits naturally after _running is set to False by a 409 response."""
|
||||||
|
import asyncio as _asyncio
|
||||||
|
|
||||||
|
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||||
|
handler = TelegramCommandHandler(client)
|
||||||
|
|
||||||
|
call_count = 0
|
||||||
|
|
||||||
|
async def mock_get_updates_409() -> list[dict]:
|
||||||
|
nonlocal call_count
|
||||||
|
call_count += 1
|
||||||
|
# Simulate 409 stopping the poller
|
||||||
|
handler._running = False
|
||||||
|
return []
|
||||||
|
|
||||||
|
handler._get_updates = mock_get_updates_409 # type: ignore[method-assign]
|
||||||
|
|
||||||
|
handler._running = True
|
||||||
|
task = _asyncio.create_task(handler._poll_loop())
|
||||||
|
await _asyncio.wait_for(task, timeout=2.0)
|
||||||
|
|
||||||
|
# _get_updates called exactly once, then loop exited
|
||||||
|
assert call_count == 1
|
||||||
|
assert handler._running is False
|
||||||
|
|
||||||
|
|
||||||
class TestCommandWithArgs:
|
class TestCommandWithArgs:
|
||||||
"""Test register_command_with_args and argument dispatch."""
|
"""Test register_command_with_args and argument dispatch."""
|
||||||
|
|||||||
Reference in New Issue
Block a user