Compare commits
10 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebd0a0297c | ||
| 02a72e0f7e | |||
| 478a659ac2 | |||
|
|
16b9b6832d | ||
|
|
48b87a79f6 | ||
|
|
ad79082dcc | ||
|
|
11dff9d3e5 | ||
|
|
3c5f1752e6 | ||
|
|
d6a389e0b7 | ||
| cd36d53a47 |
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
|
||||||
|
|||||||
@@ -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,9 +233,11 @@ 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
|
||||||
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '해외주식 주문' 시트
|
# Source: 한국투자증권 오픈API 전체문서 (20260221) — '해외주식 주문' 시트
|
||||||
# VTTT1002U: 모의투자 미국 매수, VTTT1001U: 모의투자 미국 매도
|
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"
|
tr_id = "VTTT1002U" if order_type == "BUY" else "VTTT1001U"
|
||||||
|
|
||||||
body = {
|
body = {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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
|
||||||
|
|||||||
21
src/db.py
21
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(
|
||||||
@@ -167,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.
|
||||||
|
|
||||||
@@ -182,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
|
||||||
@@ -190,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(),
|
||||||
@@ -207,6 +219,7 @@ def log_trade(
|
|||||||
exchange_code,
|
exchange_code,
|
||||||
context_json,
|
context_json,
|
||||||
decision_id,
|
decision_id,
|
||||||
|
mode,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
27
src/main.py
27
src/main.py
@@ -340,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,
|
||||||
@@ -822,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
|
||||||
@@ -1041,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 %
|
||||||
@@ -1318,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")
|
||||||
@@ -1979,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:
|
||||||
@@ -2296,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()
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user