feat: Enhance trading system with new e_min_chg_pct parameter and related logic

Changes:
- Introduced the `e_min_chg_pct` parameter to define the minimum price change percentage compared to the previous day's close, enhancing the momentum trading strategy.
- Updated various functions and classes to incorporate this new parameter, ensuring it is utilized in both backtesting and live trading scenarios.
- Improved documentation and comments to clarify the purpose and usage of the new parameter across the codebase.

Impact:
- This addition allows for more precise control over trading conditions, potentially increasing the effectiveness of the momentum strategy while maintaining system integrity and performance.
This commit is contained in:
Your Name
2026-08-01 16:19:24 +09:00
parent 7050f788c5
commit cb7e5037a0
30 changed files with 2206 additions and 253 deletions

View File

@@ -60,7 +60,7 @@ def momentum_universe_exit_debounce_sec() -> int:
def momentum_backtest_candle_warmup_bars() -> int:
"""백테 지표·E(전일시가) warm-up — 실매 갭보정(~500)과 맞춰 전일 장시작까지 덮음."""
"""백테 지표·전일종가(K) warm-up — 실매 갭보정(~500)과 맞춰 전일 세션까지 덮음."""
from kis_trader.utils.env import get_env_int
return max(0, int(get_env_int("MOMENTUM_BACKTEST_CANDLE_WARMUP_BARS", 400)))
@@ -120,18 +120,19 @@ def prepend_momentum_candle_warmup(
# 종목×기간일 단위 REST 웜업 캐시 (프로세스 메모리만 — DB 미기록)
_REST_WARMUP_PREFIX_CACHE: Dict[Tuple[str, str], List[Dict[str, Any]]] = {}
# 1차+재시도 후에도 전일 장시작 시가 미확보 → trial마다 재조회·로그 금지
# 1차+재시도 후에도 전일 가 미확보 → trial마다 재조회·로그 금지
_REST_WARMUP_PERM_FAIL: Set[Tuple[str, str]] = set()
def _momentum_rows_have_prev_day(rows: List[Dict], period_day: str) -> bool:
"""기간 시작일 기준 직전 거래일 **장시작 시가** 봉이 있으면 HTS E 해석 가능.
"""기간 시작일 기준 직전 거래일 **가** 봉이 있으면 HTS K(전일종가 대비) 해석 가능.
전일 오후 봉만 있는 경우(웜업 50 등)는 False → REST 웜업으로 보강.
전일 봉이 하나도 없으면 False → REST 웜업으로 보강.
(시가/장시작 판정은 쓰지 않음 — HTS K 는 전일 종가 기준)
"""
from kis_trader.engine.momentum_hts_logic import candles_have_prev_session_open
from kis_trader.engine.momentum_hts_logic import candles_have_prev_session_close
return candles_have_prev_session_open(rows or [], str(period_day or "")[:8])
return candles_have_prev_session_close(rows or [], str(period_day or "")[:8])
def _kiwoom_gap_credentials() -> Tuple[str, str, bool]:
@@ -195,7 +196,7 @@ def inject_momentum_rest_warmup_memory(
DB 전일봉이 없을 때 키움 ka10080 REST를 종목당 호출해 **메모리에만** prepend.
- 1차: MOMENTUM_BACKTEST_REST_WARMUP_BARS (기본 700)
- 전일 장시작 시가 미확보 시에만 2차: MOMENTUM_BACKTEST_REST_WARMUP_BARS_RETRY (기본 1500)
- 전일 가 미확보 시에만 2차: MOMENTUM_BACKTEST_REST_WARMUP_BARS_RETRY (기본 1500)
- DB INSERT 없음. 성공 prefix·영구실패는 프로세스 캐시 (Optuna trial 재조회·로그 스팸 방지).
"""
from kis_trader.utils.env import get_env_bool, get_env_float, get_env_int
@@ -242,7 +243,7 @@ def inject_momentum_rest_warmup_memory(
50,
int(get_env_int("MOMENTUM_BACKTEST_REST_WARMUP_BARS", 700)),
)
# 전일(직전 세션) 장시작이 1차에 안 잡힐 때만 — 평소엔 700만
# 전일(직전 세션) 종가가 1차에 안 잡힐 때만 — 평소엔 700만
n_retry = max(
n_bars,
int(get_env_int("MOMENTUM_BACKTEST_REST_WARMUP_BARS_RETRY", 1500)),
@@ -302,11 +303,11 @@ def inject_momentum_rest_warmup_memory(
stats["fail"] += 1
continue
# 1차로 전일 장시작 미확보 → 봉 수 늘려 1회만 재시도 (중간 거래일 0봉 등)
# 1차로 전일 종가 미확보 → 봉 수 늘려 1회만 재시도 (중간 거래일 0봉 등)
if (not prefix or not _momentum_rows_have_prev_day(prefix, period_day)) and n_retry > n_bars:
stats["retry"] += 1
log.info(
"📡 REST 웜업 재시도 %s: n=%d → n=%d (전일 장시작 미확보)",
"📡 REST 웜업 재시도 %s: n=%d → n=%d (전일 종가 미확보)",
code, n_bars, n_retry,
)
try: