거래 빠르게 안티에서 병신만든거 커서로
feat: Implement backtest source management and enhance candle data handling Changes: - Introduced a new function `_apply_backtest_source_env_from_request` to manage the environment variables for candle, tick, and order book sources based on incoming requests. - Added a teardown function `_teardown_backtest_source_env` to ensure that environment variables do not persist between requests, enhancing the stability of the backtesting environment. - Refactored existing code to utilize the new source management functions, improving code readability and maintainability. - Added new utility functions in `bt_candle_source.py` for fetching and managing candle data, ensuring consistency with live trading data sources. Impact: - These changes improve the flexibility and reliability of the backtesting framework, allowing for better management of data sources and reducing the risk of cross-request contamination.
This commit is contained in:
@@ -158,6 +158,23 @@ class OrderManager:
|
||||
self.strategy_pnl_provider: Optional[Callable[..., tuple]] = None
|
||||
# 호가 조회 — WSManager.get_orderbook 등 (키움 0D 캐시 우선)
|
||||
self.orderbook_provider: Optional[Callable[..., Optional[dict]]] = None
|
||||
# 체결 알림 시세 표기 — WSManager.get_tick_feed_label(code) -> str
|
||||
self.tick_feed_label_provider: Optional[Callable[[str], str]] = None
|
||||
|
||||
def _tick_feed_mm_line(self, code: str) -> str:
|
||||
"""매수/매도 MM 한 줄: 시세: kiwoom | kis | ls(spill)."""
|
||||
try:
|
||||
if self.tick_feed_label_provider is not None:
|
||||
lab = (self.tick_feed_label_provider(code) or "").strip()
|
||||
if lab:
|
||||
return f"시세: {lab}"
|
||||
except Exception as e:
|
||||
logger.debug("tick_feed_label_provider 실패: %s", e)
|
||||
try:
|
||||
lab = (get_env_from_db("LIVE_TICK_PROVIDER", "kiwoom") or "kiwoom").strip().lower()
|
||||
return f"시세: {lab}"
|
||||
except Exception:
|
||||
return "시세: ?"
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Lock 헬퍼
|
||||
@@ -637,6 +654,7 @@ class OrderManager:
|
||||
f"🔷 **[{log_tag}:{disp}]** {req.name}({req.code})\n"
|
||||
f"{filled_price:,.0f}원 × {filled_qty}주 = {filled_price*filled_qty:,.0f}원\n"
|
||||
f"손절 {req.stop_price:,.0f} / 목표 {req.target_price:,.0f}\n"
|
||||
f"{self._tick_feed_mm_line(req.code)}\n"
|
||||
f"ODNO={ord_no}"
|
||||
)
|
||||
if prev_filled > 0:
|
||||
@@ -781,6 +799,11 @@ class OrderManager:
|
||||
realized_pnl_override=realized_pnl,
|
||||
sell_qty=int(delta_qty),
|
||||
)
|
||||
try:
|
||||
from kis_trader.utils.today_trades_cache import invalidate_today_trades_cache
|
||||
invalidate_today_trades_cache()
|
||||
except Exception:
|
||||
pass
|
||||
# 매수~매도 구간 1분봉 REST 백필 (백테 봉구멍·슬롯 좀비 방지) — 비동기 1회
|
||||
# 부분매도면 포지션 잔존 → 전량 청산 시에만 백필
|
||||
remain_after = self._active_qty(req.strategy_id, req.code)
|
||||
@@ -823,7 +846,8 @@ class OrderManager:
|
||||
f"{emoji} **[매도체결:{disp}]** {req.name}({req.code})\n"
|
||||
f"{sell_price:,.0f}원 × {delta_qty}주\n"
|
||||
f"{req.reason} · 수익률 {req.profit_pct*100:+.2f}%\n"
|
||||
f"실현 {pnl_str} · ODNO={ord_no}"
|
||||
f"실현 {pnl_str} · ODNO={ord_no}\n"
|
||||
f"{self._tick_feed_mm_line(req.code)}"
|
||||
)
|
||||
if remain_after > 0:
|
||||
header += f"\n⚠️ 부분체결 잔량 {remain_after}주"
|
||||
|
||||
Reference in New Issue
Block a user