Compare commits
5 Commits
d01f4d93a3
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e27000760 | ||
| 5a41f86112 | |||
|
|
ff9c4d6082 | ||
| 25ad4776c9 | |||
|
|
9339824e22 |
78
src/main.py
78
src/main.py
@@ -860,21 +860,23 @@ async def trading_cycle(
|
||||
price=0, # market order
|
||||
)
|
||||
else:
|
||||
# For overseas orders:
|
||||
# - KIS VTS only accepts limit orders (지정가만 가능)
|
||||
# - BUY: use 0.5% premium over last price to improve fill probability
|
||||
# (ask price is typically slightly above last, and VTS won't fill below ask)
|
||||
# - SELL: use last price as the limit
|
||||
# For overseas orders, always use limit orders (지정가):
|
||||
# - KIS market orders (ORD_DVSN=01) calculate quantity based on upper limit
|
||||
# price (상한가 기준), resulting in only 60-80% of intended cash being used.
|
||||
# - BUY: +0.2% above last price — tight enough to minimise overpayment while
|
||||
# achieving >90% fill rate on large-cap US stocks.
|
||||
# - SELL: -0.2% below last price — ensures fill even when price dips slightly
|
||||
# (placing at exact last price risks no-fill if the bid is just below).
|
||||
if decision.action == "BUY":
|
||||
order_price = round(current_price * 1.005, 4)
|
||||
order_price = round(current_price * 1.002, 4)
|
||||
else:
|
||||
order_price = current_price
|
||||
order_price = round(current_price * 0.998, 4)
|
||||
result = await overseas_broker.send_overseas_order(
|
||||
exchange_code=market.exchange_code,
|
||||
stock_code=stock_code,
|
||||
order_type=decision.action,
|
||||
quantity=quantity,
|
||||
price=order_price, # limit order — KIS VTS rejects market orders
|
||||
price=order_price, # limit order
|
||||
)
|
||||
# Check if KIS rejected the order (rt_cd != "0")
|
||||
if result.get("rt_cd", "") != "0":
|
||||
@@ -990,18 +992,30 @@ async def run_daily_session(
|
||||
telegram: TelegramClient,
|
||||
settings: Settings,
|
||||
smart_scanner: SmartVolatilityScanner | None = None,
|
||||
) -> None:
|
||||
daily_start_eval: float = 0.0,
|
||||
) -> float:
|
||||
"""Execute one daily trading session.
|
||||
|
||||
V2 proactive strategy: 1 Gemini call for playbook generation,
|
||||
then local scenario evaluation per stock (0 API calls).
|
||||
|
||||
Args:
|
||||
daily_start_eval: Portfolio evaluation at the start of the trading day.
|
||||
Used to compute intra-day P&L for the Circuit Breaker.
|
||||
Pass 0.0 on the first session of each day; the function will set
|
||||
it from the first balance query and return it for subsequent
|
||||
sessions.
|
||||
|
||||
Returns:
|
||||
The daily_start_eval value that should be forwarded to the next
|
||||
session of the same trading day.
|
||||
"""
|
||||
# Get currently open markets
|
||||
open_markets = get_open_markets(settings.enabled_market_list)
|
||||
|
||||
if not open_markets:
|
||||
logger.info("No markets open for this session")
|
||||
return
|
||||
return daily_start_eval
|
||||
|
||||
logger.info("Starting daily trading session for %d markets", len(open_markets))
|
||||
|
||||
@@ -1203,12 +1217,27 @@ async def run_daily_session(
|
||||
):
|
||||
total_cash = settings.PAPER_OVERSEAS_CASH
|
||||
|
||||
# Calculate daily P&L %
|
||||
pnl_pct = (
|
||||
((total_eval - purchase_total) / purchase_total * 100)
|
||||
if purchase_total > 0
|
||||
else 0.0
|
||||
)
|
||||
# Capture the day's opening portfolio value on the first market processed
|
||||
# in this session. Used to compute intra-day P&L for the CB instead of
|
||||
# the cumulative purchase_total which spans the entire account history.
|
||||
if daily_start_eval <= 0 and total_eval > 0:
|
||||
daily_start_eval = total_eval
|
||||
logger.info(
|
||||
"Daily CB baseline set: total_eval=%.2f (first balance of the day)",
|
||||
daily_start_eval,
|
||||
)
|
||||
|
||||
# Daily P&L: compare current eval vs start-of-day eval.
|
||||
# Falls back to purchase_total if daily_start_eval is unavailable (e.g. paper
|
||||
# mode where balance API returns 0 for all values).
|
||||
if daily_start_eval > 0:
|
||||
pnl_pct = (total_eval - daily_start_eval) / daily_start_eval * 100
|
||||
else:
|
||||
pnl_pct = (
|
||||
((total_eval - purchase_total) / purchase_total * 100)
|
||||
if purchase_total > 0
|
||||
else 0.0
|
||||
)
|
||||
portfolio_data = {
|
||||
"portfolio_pnl_pct": pnl_pct,
|
||||
"total_cash": total_cash,
|
||||
@@ -1477,6 +1506,7 @@ async def run_daily_session(
|
||||
)
|
||||
|
||||
logger.info("Daily trading session completed")
|
||||
return daily_start_eval
|
||||
|
||||
|
||||
async def _handle_market_close(
|
||||
@@ -2118,13 +2148,26 @@ async def run(settings: Settings) -> None:
|
||||
|
||||
session_interval = settings.SESSION_INTERVAL_HOURS * 3600 # Convert to seconds
|
||||
|
||||
# daily_start_eval: portfolio eval captured at the first session of each
|
||||
# trading day. Reset on calendar-date change so the CB measures only
|
||||
# today's drawdown, not cumulative account history.
|
||||
_cb_daily_start_eval: float = 0.0
|
||||
_cb_last_date: str = ""
|
||||
|
||||
while not shutdown.is_set():
|
||||
# Wait for trading to be unpaused
|
||||
await pause_trading.wait()
|
||||
_run_context_scheduler(context_scheduler, now=datetime.now(UTC))
|
||||
|
||||
# Reset intra-day CB baseline on a new calendar date
|
||||
today_str = datetime.now(UTC).date().isoformat()
|
||||
if today_str != _cb_last_date:
|
||||
_cb_last_date = today_str
|
||||
_cb_daily_start_eval = 0.0
|
||||
logger.info("New trading day %s — daily CB baseline reset", today_str)
|
||||
|
||||
try:
|
||||
await run_daily_session(
|
||||
_cb_daily_start_eval = await run_daily_session(
|
||||
broker,
|
||||
overseas_broker,
|
||||
scenario_engine,
|
||||
@@ -2138,6 +2181,7 @@ async def run(settings: Settings) -> None:
|
||||
telegram,
|
||||
settings,
|
||||
smart_scanner=smart_scanner,
|
||||
daily_start_eval=_cb_daily_start_eval,
|
||||
)
|
||||
except CircuitBreakerTripped:
|
||||
logger.critical("Circuit breaker tripped — shutting down")
|
||||
|
||||
@@ -22,6 +22,7 @@ from src.main import (
|
||||
_run_context_scheduler,
|
||||
_run_evolution_loop,
|
||||
_start_dashboard_server,
|
||||
run_daily_session,
|
||||
safe_float,
|
||||
sync_positions_from_broker,
|
||||
trading_cycle,
|
||||
@@ -1104,10 +1105,11 @@ class TestOverseasBalanceParsing:
|
||||
mock_telegram: MagicMock,
|
||||
mock_overseas_market: MagicMock,
|
||||
) -> None:
|
||||
"""Overseas BUY order must use current_price (limit), not 0 (market).
|
||||
"""Overseas BUY order must use current_price +0.2% limit, not market order.
|
||||
|
||||
KIS VTS rejects market orders for overseas paper trading.
|
||||
Regression test for issue #149.
|
||||
KIS market orders (ORD_DVSN=01) calculate quantity based on upper limit price
|
||||
(상한가 기준), resulting in only 60-80% of intended cash being used.
|
||||
Regression test for issue #149 / #211.
|
||||
"""
|
||||
mock_telegram.notify_trade_execution = AsyncMock()
|
||||
|
||||
@@ -1128,14 +1130,93 @@ class TestOverseasBalanceParsing:
|
||||
scan_candidates={},
|
||||
)
|
||||
|
||||
# Verify limit order was sent with actual price + 0.5% premium (issue #151), not 0.0
|
||||
# Verify BUY limit order uses +0.2% premium (issue #211)
|
||||
mock_overseas_broker_with_buy_scenario.send_overseas_order.assert_called_once()
|
||||
call_kwargs = mock_overseas_broker_with_buy_scenario.send_overseas_order.call_args
|
||||
sent_price = call_kwargs[1].get("price") or call_kwargs[0][4]
|
||||
expected_price = round(182.5 * 1.005, 4) # 0.5% premium for BUY limit orders
|
||||
expected_price = round(182.5 * 1.002, 4) # 0.2% premium for BUY limit orders
|
||||
assert sent_price == expected_price, (
|
||||
f"Expected limit price {expected_price} (182.5 * 1.005) but got {sent_price}. "
|
||||
"KIS VTS only accepts limit orders; BUY uses 0.5% premium to improve fill rate."
|
||||
f"Expected limit price {expected_price} (182.5 * 1.002) but got {sent_price}. "
|
||||
"BUY uses +0.2% to improve fill rate while minimising overpayment (#211)."
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_overseas_sell_order_uses_limit_price_below_current(
|
||||
self,
|
||||
mock_domestic_broker: 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_overseas_market: MagicMock,
|
||||
) -> None:
|
||||
"""Overseas SELL order must use current_price -0.2% limit (#211).
|
||||
|
||||
Placing SELL at exact last price risks no-fill when the bid is just below.
|
||||
Using -0.2% ensures the order fills even if the price dips slightly.
|
||||
"""
|
||||
sell_price = 182.5
|
||||
|
||||
# Broker mock: returns price data and a balance with 5 AAPL shares held.
|
||||
overseas_broker = MagicMock()
|
||||
overseas_broker.get_overseas_price = AsyncMock(
|
||||
return_value={"output": {"last": str(sell_price), "rate": "1.5", "tvol": "5000000"}}
|
||||
)
|
||||
overseas_broker.get_overseas_balance = AsyncMock(
|
||||
return_value={
|
||||
"output1": [
|
||||
{
|
||||
"ovrs_pdno": "AAPL",
|
||||
"ovrs_cblc_qty": "5",
|
||||
"pchs_avg_pric": "170.0",
|
||||
"evlu_pfls_rt": "7.35",
|
||||
}
|
||||
],
|
||||
"output2": [
|
||||
{
|
||||
"frcr_evlu_tota": "100000.00",
|
||||
"frcr_dncl_amt_2": "50000.00",
|
||||
"frcr_buy_amt_smtl": "50000.00",
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
overseas_broker.send_overseas_order = AsyncMock(
|
||||
return_value={"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
|
||||
sell_engine = MagicMock(spec=ScenarioEngine)
|
||||
sell_engine.evaluate = MagicMock(return_value=_make_sell_match("AAPL"))
|
||||
mock_telegram.notify_trade_execution = AsyncMock()
|
||||
|
||||
with patch("src.main.log_trade"), patch("src.main.get_open_position") as mock_pos:
|
||||
mock_pos.return_value = {"quantity": 5, "stock_code": "AAPL", "price": 170.0}
|
||||
await trading_cycle(
|
||||
broker=mock_domestic_broker,
|
||||
overseas_broker=overseas_broker,
|
||||
scenario_engine=sell_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_overseas_market,
|
||||
stock_code="AAPL",
|
||||
scan_candidates={},
|
||||
)
|
||||
|
||||
overseas_broker.send_overseas_order.assert_called_once()
|
||||
call_kwargs = overseas_broker.send_overseas_order.call_args
|
||||
sent_price = call_kwargs[1].get("price") or call_kwargs[0][4]
|
||||
expected_price = round(sell_price * 0.998, 4) # -0.2% for SELL limit orders
|
||||
assert sent_price == expected_price, (
|
||||
f"Expected SELL limit price {expected_price} (182.5 * 0.998) but got {sent_price}. "
|
||||
"SELL uses -0.2% to ensure fill even when price dips slightly (#211)."
|
||||
)
|
||||
|
||||
|
||||
@@ -3274,6 +3355,245 @@ class TestRetryConnection:
|
||||
assert call_count == 1 # No retry for non-ConnectionError
|
||||
|
||||
|
||||
# run_daily_session — daily CB baseline (daily_start_eval) tests (issue #207)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDailyCBBaseline:
|
||||
"""Tests for run_daily_session's daily_start_eval (CB baseline) behaviour.
|
||||
|
||||
Issue #207: CB P&L should be computed relative to the portfolio value at
|
||||
the start of each trading day, not the cumulative purchase_total.
|
||||
"""
|
||||
|
||||
def _make_settings(self) -> Settings:
|
||||
return Settings(
|
||||
KIS_APP_KEY="test-key",
|
||||
KIS_APP_SECRET="test-secret",
|
||||
KIS_ACCOUNT_NO="12345678-01",
|
||||
GEMINI_API_KEY="test-gemini",
|
||||
MODE="paper",
|
||||
PAPER_OVERSEAS_CASH=0,
|
||||
)
|
||||
|
||||
def _make_domestic_balance(
|
||||
self, tot_evlu_amt: float = 0.0, dnca_tot_amt: float = 50000.0
|
||||
) -> dict:
|
||||
return {
|
||||
"output1": [],
|
||||
"output2": [
|
||||
{
|
||||
"tot_evlu_amt": str(tot_evlu_amt),
|
||||
"dnca_tot_amt": str(dnca_tot_amt),
|
||||
"pchs_amt_smtl_amt": "40000.0",
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_daily_start_eval_when_no_markets_open(self) -> None:
|
||||
"""run_daily_session returns the unchanged daily_start_eval when no markets are open."""
|
||||
with patch("src.main.get_open_markets", return_value=[]):
|
||||
result = await run_daily_session(
|
||||
broker=MagicMock(),
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=MagicMock(),
|
||||
playbook_store=MagicMock(),
|
||||
pre_market_planner=MagicMock(),
|
||||
risk=MagicMock(),
|
||||
db_conn=init_db(":memory:"),
|
||||
decision_logger=MagicMock(),
|
||||
context_store=MagicMock(),
|
||||
criticality_assessor=MagicMock(),
|
||||
telegram=MagicMock(),
|
||||
settings=self._make_settings(),
|
||||
smart_scanner=None,
|
||||
daily_start_eval=12345.0,
|
||||
)
|
||||
assert result == 12345.0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_zero_when_no_markets_and_no_baseline(self) -> None:
|
||||
"""run_daily_session returns 0.0 when no markets are open and daily_start_eval=0."""
|
||||
with patch("src.main.get_open_markets", return_value=[]):
|
||||
result = await run_daily_session(
|
||||
broker=MagicMock(),
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=MagicMock(),
|
||||
playbook_store=MagicMock(),
|
||||
pre_market_planner=MagicMock(),
|
||||
risk=MagicMock(),
|
||||
db_conn=init_db(":memory:"),
|
||||
decision_logger=MagicMock(),
|
||||
context_store=MagicMock(),
|
||||
criticality_assessor=MagicMock(),
|
||||
telegram=MagicMock(),
|
||||
settings=self._make_settings(),
|
||||
smart_scanner=None,
|
||||
daily_start_eval=0.0,
|
||||
)
|
||||
assert result == 0.0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_captures_total_eval_as_baseline_on_first_session(self) -> None:
|
||||
"""When daily_start_eval=0 and balance returns a positive total_eval, the returned
|
||||
value equals total_eval (the captured baseline for the day)."""
|
||||
from src.analysis.smart_scanner import ScanCandidate
|
||||
|
||||
settings = self._make_settings()
|
||||
broker = MagicMock()
|
||||
# Domestic balance: tot_evlu_amt=55000
|
||||
broker.get_balance = AsyncMock(
|
||||
return_value=self._make_domestic_balance(tot_evlu_amt=55000.0)
|
||||
)
|
||||
# Price data for the stock
|
||||
broker.get_current_price = AsyncMock(
|
||||
return_value=(100.0, 1.5, 100.0)
|
||||
)
|
||||
|
||||
market = MagicMock()
|
||||
market.name = "KR"
|
||||
market.code = "KR"
|
||||
market.exchange_code = "KRX"
|
||||
market.is_domestic = True
|
||||
market.timezone = __import__("zoneinfo").ZoneInfo("Asia/Seoul")
|
||||
|
||||
smart_scanner = MagicMock()
|
||||
smart_scanner.scan = AsyncMock(
|
||||
return_value=[
|
||||
ScanCandidate(
|
||||
stock_code="005930",
|
||||
name="Samsung",
|
||||
price=100.0,
|
||||
volume=1_000_000.0,
|
||||
volume_ratio=2.5,
|
||||
rsi=45.0,
|
||||
signal="momentum",
|
||||
score=80.0,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
playbook_store = MagicMock()
|
||||
playbook_store.load = MagicMock(return_value=_make_playbook("KR"))
|
||||
|
||||
scenario_engine = MagicMock(spec=ScenarioEngine)
|
||||
scenario_engine.evaluate = MagicMock(return_value=_make_hold_match("005930"))
|
||||
|
||||
risk = MagicMock()
|
||||
risk.check_circuit_breaker = MagicMock()
|
||||
risk.check_fat_finger = MagicMock()
|
||||
|
||||
telegram = MagicMock()
|
||||
telegram.notify_trade_execution = AsyncMock()
|
||||
telegram.notify_scenario_matched = AsyncMock()
|
||||
|
||||
decision_logger = MagicMock()
|
||||
decision_logger.log_decision = MagicMock(return_value="d1")
|
||||
|
||||
async def _passthrough(fn, *a, label: str = "", **kw): # type: ignore[override]
|
||||
return await fn(*a, **kw)
|
||||
|
||||
with patch("src.main.get_open_markets", return_value=[market]), \
|
||||
patch("src.main._retry_connection", new=_passthrough):
|
||||
result = await run_daily_session(
|
||||
broker=broker,
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=scenario_engine,
|
||||
playbook_store=playbook_store,
|
||||
pre_market_planner=MagicMock(),
|
||||
risk=risk,
|
||||
db_conn=init_db(":memory:"),
|
||||
decision_logger=decision_logger,
|
||||
context_store=MagicMock(),
|
||||
criticality_assessor=MagicMock(),
|
||||
telegram=telegram,
|
||||
settings=settings,
|
||||
smart_scanner=smart_scanner,
|
||||
daily_start_eval=0.0,
|
||||
)
|
||||
|
||||
assert result == 55000.0 # captured from tot_evlu_amt
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_does_not_overwrite_existing_baseline(self) -> None:
|
||||
"""When daily_start_eval > 0, it must not be overwritten even if balance returns
|
||||
a different value (baseline is fixed at the start of each trading day)."""
|
||||
from src.analysis.smart_scanner import ScanCandidate
|
||||
|
||||
settings = self._make_settings()
|
||||
broker = MagicMock()
|
||||
# Balance reports a different eval value (market moved during the day)
|
||||
broker.get_balance = AsyncMock(
|
||||
return_value=self._make_domestic_balance(tot_evlu_amt=58000.0)
|
||||
)
|
||||
broker.get_current_price = AsyncMock(return_value=(100.0, 1.5, 100.0))
|
||||
|
||||
market = MagicMock()
|
||||
market.name = "KR"
|
||||
market.code = "KR"
|
||||
market.exchange_code = "KRX"
|
||||
market.is_domestic = True
|
||||
market.timezone = __import__("zoneinfo").ZoneInfo("Asia/Seoul")
|
||||
|
||||
smart_scanner = MagicMock()
|
||||
smart_scanner.scan = AsyncMock(
|
||||
return_value=[
|
||||
ScanCandidate(
|
||||
stock_code="005930",
|
||||
name="Samsung",
|
||||
price=100.0,
|
||||
volume=1_000_000.0,
|
||||
volume_ratio=2.5,
|
||||
rsi=45.0,
|
||||
signal="momentum",
|
||||
score=80.0,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
playbook_store = MagicMock()
|
||||
playbook_store.load = MagicMock(return_value=_make_playbook("KR"))
|
||||
|
||||
scenario_engine = MagicMock(spec=ScenarioEngine)
|
||||
scenario_engine.evaluate = MagicMock(return_value=_make_hold_match("005930"))
|
||||
|
||||
risk = MagicMock()
|
||||
risk.check_circuit_breaker = MagicMock()
|
||||
|
||||
telegram = MagicMock()
|
||||
telegram.notify_trade_execution = AsyncMock()
|
||||
telegram.notify_scenario_matched = AsyncMock()
|
||||
|
||||
decision_logger = MagicMock()
|
||||
decision_logger.log_decision = MagicMock(return_value="d1")
|
||||
|
||||
async def _passthrough(fn, *a, label: str = "", **kw): # type: ignore[override]
|
||||
return await fn(*a, **kw)
|
||||
|
||||
with patch("src.main.get_open_markets", return_value=[market]), \
|
||||
patch("src.main._retry_connection", new=_passthrough):
|
||||
result = await run_daily_session(
|
||||
broker=broker,
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=scenario_engine,
|
||||
playbook_store=playbook_store,
|
||||
pre_market_planner=MagicMock(),
|
||||
risk=risk,
|
||||
db_conn=init_db(":memory:"),
|
||||
decision_logger=decision_logger,
|
||||
context_store=MagicMock(),
|
||||
criticality_assessor=MagicMock(),
|
||||
telegram=telegram,
|
||||
settings=settings,
|
||||
smart_scanner=smart_scanner,
|
||||
daily_start_eval=55000.0, # existing baseline
|
||||
)
|
||||
|
||||
# Must return the original baseline, NOT the new total_eval (58000)
|
||||
assert result == 55000.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# sync_positions_from_broker — startup DB sync tests (issue #206)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user