Compare commits
4 Commits
b45d136894
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16b9b6832d | ||
|
|
d6a389e0b7 | ||
| cd36d53a47 | |||
|
|
1242794fc4 |
@@ -285,7 +285,10 @@ class KISBroker:
|
||||
await self._rate_limiter.acquire()
|
||||
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 = {
|
||||
"CANO": self._account_no,
|
||||
"ACNT_PRDT_CD": self._product_cd,
|
||||
@@ -330,7 +333,13 @@ class KISBroker:
|
||||
await self._rate_limiter.acquire()
|
||||
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.
|
||||
# ORD_DVSN: "00"=지정가, "01"=시장가
|
||||
|
||||
@@ -175,8 +175,12 @@ class OverseasBroker:
|
||||
await self._broker._rate_limiter.acquire()
|
||||
session = self._broker._get_session()
|
||||
|
||||
# Virtual trading TR_ID for overseas balance inquiry
|
||||
headers = await self._broker._auth_headers("VTTS3012R")
|
||||
# TR_ID: 실전 TTTS3012R, 모의 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 = {
|
||||
"CANO": self._broker._account_no,
|
||||
"ACNT_PRDT_CD": self._broker._product_cd,
|
||||
@@ -229,10 +233,12 @@ class OverseasBroker:
|
||||
await self._broker._rate_limiter.acquire()
|
||||
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) — '해외주식 주문' 시트
|
||||
# VTTT1002U: 모의투자 미국 매수, VTTT1001U: 모의투자 미국 매도
|
||||
tr_id = "VTTT1002U" if order_type == "BUY" else "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"
|
||||
|
||||
body = {
|
||||
"CANO": self._broker._account_no,
|
||||
|
||||
@@ -13,7 +13,7 @@ class Settings(BaseSettings):
|
||||
KIS_APP_KEY: str
|
||||
KIS_APP_SECRET: str
|
||||
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
|
||||
GEMINI_API_KEY: str
|
||||
|
||||
60
src/main.py
60
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")
|
||||
|
||||
# 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(
|
||||
"Overseas cash balance is 0 for %s; using paper fallback %.2f USD",
|
||||
market.exchange_code,
|
||||
@@ -524,6 +530,14 @@ 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",
|
||||
@@ -1033,11 +1047,12 @@ async def run_daily_session(
|
||||
balance_info.get("frcr_buy_amt_smtl", "0") or "0"
|
||||
)
|
||||
# Paper mode fallback: VTS overseas balance API often fails for many accounts.
|
||||
if total_cash <= 0 and settings.PAPER_OVERSEAS_CASH > 0:
|
||||
total_cash = settings.PAPER_OVERSEAS_CASH
|
||||
|
||||
# VTS overseas balance API often returns 0; use paper fallback.
|
||||
if total_cash <= 0 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.MODE == "paper"
|
||||
and settings.PAPER_OVERSEAS_CASH > 0
|
||||
):
|
||||
total_cash = settings.PAPER_OVERSEAS_CASH
|
||||
|
||||
# Calculate daily P&L %
|
||||
@@ -1076,6 +1091,33 @@ async def run_daily_session(
|
||||
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
|
||||
context_snapshot = {
|
||||
"L1": {
|
||||
@@ -1944,6 +1986,10 @@ async def run(settings: Settings) -> None:
|
||||
)
|
||||
except CircuitBreakerTripped:
|
||||
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()
|
||||
break
|
||||
except Exception as exc:
|
||||
@@ -2261,6 +2307,8 @@ async def run(settings: Settings) -> None:
|
||||
except TimeoutError:
|
||||
pass # Normal — timeout means it's time for next cycle
|
||||
finally:
|
||||
# Notify shutdown before closing resources
|
||||
await telegram.notify_system_shutdown("Normal shutdown")
|
||||
# Clean up resources
|
||||
await command_handler.stop_polling()
|
||||
await broker.close()
|
||||
|
||||
@@ -572,4 +572,156 @@ class TestSendOrderTickRounding:
|
||||
order_call = mock_post.call_args_list[1]
|
||||
body = order_call[1].get("json", {})
|
||||
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"
|
||||
|
||||
@@ -3001,3 +3001,185 @@ async def test_buy_proceeds_when_no_open_position() -> None:
|
||||
|
||||
# 포지션이 없으므로 해외 주문이 실행되어야 함
|
||||
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()
|
||||
|
||||
@@ -640,4 +640,176 @@ class TestPaperOverseasCash:
|
||||
GEMINI_API_KEY="g",
|
||||
)
|
||||
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