feat: 국내주식 지정가 전환 및 미체결 처리 (#232)
- KISBroker에 get_domestic_pending_orders (TTTC0084R, 실전전용) 및 cancel_domestic_order (실전 TTTC0013U / 모의 VTTC0013U) 추가 - main.py 국내 주문 price=0 → 지정가 전환 (2곳): · BUY +0.2% / SELL -0.2%, kr_round_down으로 KRX 틱 반올림 적용 - handle_domestic_pending_orders 함수 추가: · BUY 미체결 → 취소 + buy_cooldown 설정 · SELL 미체결 → 취소 후 -0.4% 재주문 (최대 1회) - daily/realtime 두 모드 market 루프 내 domestic pending 호출 추가 (sell_resubmit_counts는 해외용과 공유, key prefix "KR:" vs 거래소코드) - 테스트 14개 추가: · test_broker.py: TestGetDomesticPendingOrders 3개 + TestCancelDomesticOrder 5개 · test_main.py: TestHandleDomesticPendingOrders 4개 + TestDomesticLimitOrderPrice 2개 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -725,3 +725,195 @@ class TestTRIDBranchingDomestic:
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
assert order_headers["tr_id"] == "TTTC0011U"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Domestic Pending Orders (get_domestic_pending_orders)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetDomesticPendingOrders:
|
||||
"""get_domestic_pending_orders must return [] in paper mode and call TTTC0084R in live."""
|
||||
|
||||
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_paper_mode_returns_empty(self, settings) -> None:
|
||||
"""Paper mode must return [] immediately without any API call."""
|
||||
broker = self._make_broker(settings, "paper")
|
||||
|
||||
with patch("aiohttp.ClientSession.get") as mock_get:
|
||||
result = await broker.get_domestic_pending_orders()
|
||||
|
||||
assert result == []
|
||||
mock_get.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_live_mode_calls_tttc0084r_with_correct_params(
|
||||
self, settings
|
||||
) -> None:
|
||||
"""Live mode must call TTTC0084R with INQR_DVSN_1/2 and paging params."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
pending = [{"odno": "001", "pdno": "005930", "psbl_qty": "10"}]
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(return_value={"output": pending})
|
||||
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:
|
||||
result = await broker.get_domestic_pending_orders()
|
||||
|
||||
assert result == pending
|
||||
headers = mock_get.call_args[1].get("headers", {})
|
||||
assert headers["tr_id"] == "TTTC0084R"
|
||||
params = mock_get.call_args[1].get("params", {})
|
||||
assert params["INQR_DVSN_1"] == "0"
|
||||
assert params["INQR_DVSN_2"] == "0"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_live_mode_connection_error(self, settings) -> None:
|
||||
"""Network error must raise ConnectionError."""
|
||||
import aiohttp as _aiohttp
|
||||
|
||||
broker = self._make_broker(settings, "live")
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.get",
|
||||
side_effect=_aiohttp.ClientError("timeout"),
|
||||
):
|
||||
with pytest.raises(ConnectionError):
|
||||
await broker.get_domestic_pending_orders()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Domestic Order Cancellation (cancel_domestic_order)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCancelDomesticOrder:
|
||||
"""cancel_domestic_order must use correct TR_ID and build body correctly."""
|
||||
|
||||
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
|
||||
|
||||
def _make_post_mocks(self, order_payload: dict) -> tuple:
|
||||
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=order_payload)
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
return mock_hash, mock_order
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_live_uses_tttc0013u(self, settings) -> None:
|
||||
"""Live mode must use TR_ID TTTC0013U."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
assert order_headers["tr_id"] == "TTTC0013U"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_paper_uses_vttc0013u(self, settings) -> None:
|
||||
"""Paper mode must use TR_ID VTTC0013U."""
|
||||
broker = self._make_broker(settings, "paper")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
assert order_headers["tr_id"] == "VTTC0013U"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_sets_rvse_cncl_dvsn_cd_02(self, settings) -> None:
|
||||
"""Body must have RVSE_CNCL_DVSN_CD='02' (취소) and QTY_ALL_ORD_YN='Y'."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
body = mock_post.call_args_list[1][1].get("json", {})
|
||||
assert body["RVSE_CNCL_DVSN_CD"] == "02"
|
||||
assert body["QTY_ALL_ORD_YN"] == "Y"
|
||||
assert body["ORD_UNPR"] == "0"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_sets_krx_fwdg_ord_orgno_in_body(self, settings) -> None:
|
||||
"""Body must include KRX_FWDG_ORD_ORGNO and ORGN_ODNO from arguments."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD123", "BRN456", 3)
|
||||
|
||||
body = mock_post.call_args_list[1][1].get("json", {})
|
||||
assert body["KRX_FWDG_ORD_ORGNO"] == "BRN456"
|
||||
assert body["ORGN_ODNO"] == "ORD123"
|
||||
assert body["ORD_QTY"] == "3"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_sets_hashkey_header(self, settings) -> None:
|
||||
"""Request must include hashkey header (same pattern as send_order)."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 2)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
assert "hashkey" in order_headers
|
||||
assert order_headers["hashkey"] == "h"
|
||||
|
||||
@@ -22,6 +22,7 @@ from src.main import (
|
||||
_run_context_scheduler,
|
||||
_run_evolution_loop,
|
||||
_start_dashboard_server,
|
||||
handle_domestic_pending_orders,
|
||||
handle_overseas_pending_orders,
|
||||
run_daily_session,
|
||||
safe_float,
|
||||
@@ -4058,3 +4059,322 @@ class TestHandleOverseasPendingOrders:
|
||||
# Should be called exactly once with "NASD"
|
||||
assert overseas_broker.get_overseas_pending_orders.call_count == 1
|
||||
overseas_broker.get_overseas_pending_orders.assert_called_once_with("NASD")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Domestic Pending Order Handling
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestHandleDomesticPendingOrders:
|
||||
"""Tests for handle_domestic_pending_orders function."""
|
||||
|
||||
def _make_settings(self) -> Settings:
|
||||
return Settings(
|
||||
KIS_APP_KEY="k",
|
||||
KIS_APP_SECRET="s",
|
||||
KIS_ACCOUNT_NO="12345678-01",
|
||||
GEMINI_API_KEY="g",
|
||||
ENABLED_MARKETS="KR",
|
||||
)
|
||||
|
||||
def _make_telegram(self) -> MagicMock:
|
||||
t = MagicMock()
|
||||
t.notify_unfilled_order = AsyncMock()
|
||||
return t
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_buy_pending_is_cancelled_and_cooldown_set(self) -> None:
|
||||
"""BUY pending order should be cancelled and buy_cooldown should be set."""
|
||||
settings = self._make_settings()
|
||||
telegram = self._make_telegram()
|
||||
|
||||
pending_order = {
|
||||
"pdno": "005930",
|
||||
"orgn_odno": "ORD001",
|
||||
"ord_gno_brno": "BRN01",
|
||||
"sll_buy_dvsn_cd": "02", # BUY
|
||||
"psbl_qty": "3",
|
||||
}
|
||||
broker = MagicMock()
|
||||
broker.get_domestic_pending_orders = AsyncMock(return_value=[pending_order])
|
||||
broker.cancel_domestic_order = AsyncMock(
|
||||
return_value={"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
|
||||
sell_resubmit_counts: dict[str, int] = {}
|
||||
buy_cooldown: dict[str, float] = {}
|
||||
|
||||
await handle_domestic_pending_orders(
|
||||
broker, telegram, settings, sell_resubmit_counts, buy_cooldown
|
||||
)
|
||||
|
||||
broker.cancel_domestic_order.assert_called_once_with(
|
||||
stock_code="005930",
|
||||
orgn_odno="ORD001",
|
||||
krx_fwdg_ord_orgno="BRN01",
|
||||
qty=3,
|
||||
)
|
||||
assert "KR:005930" in buy_cooldown
|
||||
telegram.notify_unfilled_order.assert_called_once()
|
||||
call_kwargs = telegram.notify_unfilled_order.call_args[1]
|
||||
assert call_kwargs["action"] == "BUY"
|
||||
assert call_kwargs["outcome"] == "cancelled"
|
||||
assert call_kwargs["market"] == "KR"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sell_pending_is_cancelled_then_resubmitted(self) -> None:
|
||||
"""First unfilled SELL should be cancelled then resubmitted at -0.4% price."""
|
||||
from src.broker.kis_api import kr_round_down
|
||||
|
||||
settings = self._make_settings()
|
||||
telegram = self._make_telegram()
|
||||
|
||||
pending_order = {
|
||||
"pdno": "005930",
|
||||
"orgn_odno": "ORD002",
|
||||
"ord_gno_brno": "BRN02",
|
||||
"sll_buy_dvsn_cd": "01", # SELL
|
||||
"psbl_qty": "5",
|
||||
}
|
||||
broker = MagicMock()
|
||||
broker.get_domestic_pending_orders = AsyncMock(return_value=[pending_order])
|
||||
broker.cancel_domestic_order = AsyncMock(
|
||||
return_value={"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
broker.get_current_price = AsyncMock(return_value=(50000.0, 0.0, 0.0))
|
||||
broker.send_order = AsyncMock(return_value={"rt_cd": "0"})
|
||||
|
||||
sell_resubmit_counts: dict[str, int] = {}
|
||||
|
||||
await handle_domestic_pending_orders(
|
||||
broker, telegram, settings, sell_resubmit_counts
|
||||
)
|
||||
|
||||
broker.cancel_domestic_order.assert_called_once()
|
||||
broker.send_order.assert_called_once()
|
||||
resubmit_kwargs = broker.send_order.call_args[1]
|
||||
assert resubmit_kwargs["order_type"] == "SELL"
|
||||
expected_price = kr_round_down(50000.0 * 0.996)
|
||||
assert resubmit_kwargs["price"] == expected_price
|
||||
assert sell_resubmit_counts.get("KR:005930") == 1
|
||||
notify_kwargs = telegram.notify_unfilled_order.call_args[1]
|
||||
assert notify_kwargs["outcome"] == "resubmitted"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sell_cancel_failure_skips_resubmit(self) -> None:
|
||||
"""When cancel returns rt_cd != '0', resubmit should NOT be attempted."""
|
||||
settings = self._make_settings()
|
||||
telegram = self._make_telegram()
|
||||
|
||||
pending_order = {
|
||||
"pdno": "005930",
|
||||
"orgn_odno": "ORD003",
|
||||
"ord_gno_brno": "BRN03",
|
||||
"sll_buy_dvsn_cd": "01", # SELL
|
||||
"psbl_qty": "2",
|
||||
}
|
||||
broker = MagicMock()
|
||||
broker.get_domestic_pending_orders = AsyncMock(return_value=[pending_order])
|
||||
broker.cancel_domestic_order = AsyncMock(
|
||||
return_value={"rt_cd": "1", "msg1": "Error"} # failure
|
||||
)
|
||||
broker.send_order = AsyncMock()
|
||||
|
||||
sell_resubmit_counts: dict[str, int] = {}
|
||||
|
||||
await handle_domestic_pending_orders(
|
||||
broker, telegram, settings, sell_resubmit_counts
|
||||
)
|
||||
|
||||
broker.send_order.assert_not_called()
|
||||
telegram.notify_unfilled_order.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sell_already_resubmitted_is_only_cancelled(self) -> None:
|
||||
"""Second unfilled SELL (sell_resubmit_counts >= 1) should only cancel, no resubmit."""
|
||||
settings = self._make_settings()
|
||||
telegram = self._make_telegram()
|
||||
|
||||
pending_order = {
|
||||
"pdno": "005930",
|
||||
"orgn_odno": "ORD004",
|
||||
"ord_gno_brno": "BRN04",
|
||||
"sll_buy_dvsn_cd": "01", # SELL
|
||||
"psbl_qty": "4",
|
||||
}
|
||||
broker = MagicMock()
|
||||
broker.get_domestic_pending_orders = AsyncMock(return_value=[pending_order])
|
||||
broker.cancel_domestic_order = AsyncMock(
|
||||
return_value={"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
broker.send_order = AsyncMock()
|
||||
|
||||
# Already resubmitted once
|
||||
sell_resubmit_counts: dict[str, int] = {"KR:005930": 1}
|
||||
|
||||
await handle_domestic_pending_orders(
|
||||
broker, telegram, settings, sell_resubmit_counts
|
||||
)
|
||||
|
||||
broker.cancel_domestic_order.assert_called_once()
|
||||
broker.send_order.assert_not_called()
|
||||
notify_kwargs = telegram.notify_unfilled_order.call_args[1]
|
||||
assert notify_kwargs["outcome"] == "cancelled"
|
||||
assert notify_kwargs["action"] == "SELL"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Domestic Limit Order Price in trading_cycle
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDomesticLimitOrderPrice:
|
||||
"""trading_cycle must use kr_round_down limit prices for domestic orders."""
|
||||
|
||||
def _make_market(self) -> MagicMock:
|
||||
market = MagicMock()
|
||||
market.name = "Korea"
|
||||
market.code = "KR"
|
||||
market.exchange_code = "KRX"
|
||||
market.is_domestic = True
|
||||
return market
|
||||
|
||||
def _make_broker(self, current_price: float, balance_data: dict) -> MagicMock:
|
||||
broker = MagicMock()
|
||||
broker.get_current_price = AsyncMock(return_value=(current_price, 0.0, 0.0))
|
||||
broker.get_balance = AsyncMock(return_value=balance_data)
|
||||
broker.send_order = AsyncMock(return_value={"rt_cd": "0", "msg1": "OK"})
|
||||
return broker
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_trading_cycle_domestic_buy_uses_limit_price(self) -> None:
|
||||
"""BUY order for domestic stock must use kr_round_down(price * 1.002)."""
|
||||
from src.broker.kis_api import kr_round_down
|
||||
from src.strategy.models import ScenarioAction
|
||||
|
||||
current_price = 70000.0
|
||||
balance_data = {
|
||||
"output2": [
|
||||
{
|
||||
"tot_evlu_amt": "10000000",
|
||||
"dnca_tot_amt": "5000000",
|
||||
"pchs_amt_smtl_amt": "5000000",
|
||||
}
|
||||
]
|
||||
}
|
||||
broker = self._make_broker(current_price, balance_data)
|
||||
market = self._make_market()
|
||||
|
||||
buy_match = ScenarioMatch(
|
||||
stock_code="005930",
|
||||
matched_scenario=None,
|
||||
action=ScenarioAction.BUY,
|
||||
confidence=85,
|
||||
rationale="test",
|
||||
)
|
||||
engine = MagicMock(spec=ScenarioEngine)
|
||||
engine.evaluate = MagicMock(return_value=buy_match)
|
||||
|
||||
risk = MagicMock()
|
||||
risk.validate_order = MagicMock()
|
||||
risk.check_circuit_breaker = MagicMock()
|
||||
telegram = MagicMock()
|
||||
telegram.notify_trade_execution = AsyncMock()
|
||||
telegram.notify_fat_finger = AsyncMock()
|
||||
telegram.notify_circuit_breaker = AsyncMock()
|
||||
telegram.notify_scenario_matched = AsyncMock()
|
||||
|
||||
with patch("src.main.log_trade"):
|
||||
await trading_cycle(
|
||||
broker=broker,
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=engine,
|
||||
playbook=_make_playbook(),
|
||||
risk=risk,
|
||||
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=telegram,
|
||||
market=market,
|
||||
stock_code="005930",
|
||||
scan_candidates={},
|
||||
)
|
||||
|
||||
broker.send_order.assert_called_once()
|
||||
call_kwargs = broker.send_order.call_args[1]
|
||||
expected_price = kr_round_down(current_price * 1.002)
|
||||
assert call_kwargs["price"] == expected_price
|
||||
assert call_kwargs["order_type"] == "BUY"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_trading_cycle_domestic_sell_uses_limit_price(self) -> None:
|
||||
"""SELL order for domestic stock must use kr_round_down(price * 0.998)."""
|
||||
from src.broker.kis_api import kr_round_down
|
||||
from src.strategy.models import ScenarioAction
|
||||
|
||||
current_price = 70000.0
|
||||
stock_code = "005930"
|
||||
balance_data = {
|
||||
"output1": [
|
||||
{"pdno": stock_code, "hldg_qty": "5", "prpr": "70000", "evlu_amt": "350000"}
|
||||
],
|
||||
"output2": [
|
||||
{
|
||||
"tot_evlu_amt": "350000",
|
||||
"dnca_tot_amt": "0",
|
||||
"pchs_amt_smtl_amt": "350000",
|
||||
}
|
||||
],
|
||||
}
|
||||
broker = self._make_broker(current_price, balance_data)
|
||||
market = self._make_market()
|
||||
|
||||
sell_match = ScenarioMatch(
|
||||
stock_code=stock_code,
|
||||
matched_scenario=None,
|
||||
action=ScenarioAction.SELL,
|
||||
confidence=85,
|
||||
rationale="test",
|
||||
)
|
||||
engine = MagicMock(spec=ScenarioEngine)
|
||||
engine.evaluate = MagicMock(return_value=sell_match)
|
||||
|
||||
risk = MagicMock()
|
||||
risk.validate_order = MagicMock()
|
||||
risk.check_circuit_breaker = MagicMock()
|
||||
telegram = MagicMock()
|
||||
telegram.notify_trade_execution = AsyncMock()
|
||||
telegram.notify_fat_finger = AsyncMock()
|
||||
telegram.notify_circuit_breaker = AsyncMock()
|
||||
telegram.notify_scenario_matched = AsyncMock()
|
||||
|
||||
with patch("src.main.log_trade"):
|
||||
await trading_cycle(
|
||||
broker=broker,
|
||||
overseas_broker=MagicMock(),
|
||||
scenario_engine=engine,
|
||||
playbook=_make_playbook(),
|
||||
risk=risk,
|
||||
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=telegram,
|
||||
market=market,
|
||||
stock_code=stock_code,
|
||||
scan_candidates={},
|
||||
)
|
||||
|
||||
broker.send_order.assert_called_once()
|
||||
call_kwargs = broker.send_order.call_args[1]
|
||||
expected_price = kr_round_down(current_price * 0.998)
|
||||
assert call_kwargs["price"] == expected_price
|
||||
assert call_kwargs["order_type"] == "SELL"
|
||||
|
||||
Reference in New Issue
Block a user