fix(P0): 인증 누락·레거시 DT 엔진 병존·혼재 종목 재태깅·자정 후 스테일 청산 무력화
감사 CRITICAL 4건:
- /daytrading/{watchlist,execute,close-all,cycle} 에 require_api_key 누락 → 인증 없이
전량 청산 가능했음 (CLAUDE.md §4 위반)
- 레거시 day_trading._positions 가 엔진 day_trading_state 와 별도로 기동 시 복원돼
같은 포지션이 두 엔진에 존재 → /close-all 시 이중 매도. 복원 제거 + close-all/cycle 을
엔진에 위임, /execute 실매수는 410(엔진 미관리 포지션 방지)
- reflect_buy 가 기존 행을 daytrading 으로 재태깅 → 같은 종목 스윙 보유분이
get_swing_holdings 에서 빠져 손절·익절 정지. 신규 행일 때만 태깅
- close_stale_positions 가 인메모리만 순회 → 자정 reset 후 대상이 사라져 무력.
ActiveTrade(day_trade/active/전일) 조회 기반으로 변경하고 인메모리가 비어도 실행
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,41 @@ async def test_today_position_untouched():
|
||||
exec_mock.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stale_position_found_from_db_after_midnight_reset():
|
||||
"""자정 reset 으로 인메모리가 비어도 DB(ActiveTrade)에서 전일 포지션을 찾아 청산한다."""
|
||||
from types import SimpleNamespace
|
||||
from datetime import datetime as _dt
|
||||
|
||||
day_trading_state.positions = {}
|
||||
row = SimpleNamespace(
|
||||
stock_code="064400", stock_name="LG씨엔에스", entry_price=70800.0, quantity=4,
|
||||
highest_since_entry=71200.0, created_at=_dt.now().replace(microsecond=0) - timedelta(days=1),
|
||||
order_id="1", strategy_name="day_trade", status="active",
|
||||
)
|
||||
|
||||
class _Result:
|
||||
def scalars(self):
|
||||
return SimpleNamespace(all=lambda: [row])
|
||||
|
||||
class _Sess:
|
||||
async def __aenter__(self): return self
|
||||
async def __aexit__(self, *a): return False
|
||||
async def execute(self, *a, **k): return _Result()
|
||||
|
||||
exec_mock = AsyncMock(return_value={"stock_code": "064400", "quantity": 4})
|
||||
with patch.object(es, "is_market_open", return_value=True), \
|
||||
patch("app.core.database.AsyncSessionLocal", _Sess), \
|
||||
patch.object(es.korea_investment_api, "get_current_price",
|
||||
AsyncMock(return_value={"output": {"stck_prpr": "70300"}})), \
|
||||
patch.object(es, "_execute_exit", exec_mock):
|
||||
out = await es.close_stale_positions()
|
||||
|
||||
assert len(out) == 1
|
||||
assert exec_mock.await_args.args[1]["quantity"] == 4
|
||||
assert exec_mock.await_args.kwargs["sell_pct"] == 1.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_action_when_market_closed():
|
||||
day_trading_state.positions["064400"] = _pos(es._kst_now() - timedelta(days=1))
|
||||
|
||||
Reference in New Issue
Block a user