[2026-05-29] 재진입 쿨다운 기준 수정

This commit is contained in:
2026-05-29 18:07:46 +09:00
parent 1d242cf77f
commit 3f6ff387e2
2 changed files with 53 additions and 4 deletions
+34
View File
@@ -461,6 +461,7 @@ class StockBot:
self._restore_positions_from_db()
# 당일 SL 종목 복원 (재시작 후에도 재진입 차단 유지)
self._restore_sl_tickers_from_db()
self._restore_reentry_controls_from_db()
await send(f"[시작] 단타봇 가동 | 예수금: {cash:,}원 | "
f"{'모의투자' if self.kis.is_mock else '실거래'}")
@@ -498,6 +499,37 @@ class StockBot:
if self.sl_tickers:
logger.info(f"당일 SL 종목 복원(재진입 차단): {self.sl_tickers}")
def _restore_reentry_controls_from_db(self):
"""재시작 시 오늘 청산 이력 기반 재진입 제한 상태를 복원한다."""
today = datetime.now().strftime("%Y-%m-%d")
with get_conn() as conn:
rows = conn.execute("""
SELECT ticker, exit_time, exit_reason
FROM trades
WHERE date=?
AND exit_time IS NOT NULL
AND exit_reason IN ('TIME', 'FORCE', 'TP1', 'TP2')
ORDER BY exit_time
""", (today,)).fetchall()
restored = []
for ticker, exit_time, reason in rows:
if ticker in self.positions:
continue
try:
exit_dt = datetime.strptime(exit_time, "%H:%M:%S").replace(
year=datetime.now().year,
month=datetime.now().month,
day=datetime.now().day,
)
except (TypeError, ValueError):
continue
self.strategy.mark_final_exit(ticker, reason, exit_dt)
restored.append(f"{ticker}:{reason}")
if restored:
logger.info("재진입 제한 상태 복원: %s", ", ".join(restored))
def _db_save_position(self, ticker: str, pos: dict, target_price: float):
with get_conn() as conn:
conn.execute("""
@@ -963,6 +995,7 @@ class StockBot:
if pos["qty"] <= 0:
del self.positions[ticker]
self._db_delete_position(ticker)
self.strategy.mark_final_exit(ticker, reason)
else:
self._db_save_position(ticker, pos, self.strategy.get_target(ticker))
await notify_tp1(ticker, name, pnl_pct)
@@ -970,6 +1003,7 @@ class StockBot:
elif reason in ("TP2", "SL", "TIME", "FORCE"):
del self.positions[ticker]
self._db_delete_position(ticker)
self.strategy.mark_final_exit(ticker, reason)
if reason == "TP2":
await notify_tp2(ticker, name, pnl_pct)
elif reason == "SL":