feat: Enhance Optuna integration and logging for backtesting framework

Changes:
- Added new API endpoints for continuing and confirming Optuna jobs, allowing for better management of ongoing studies.
- Introduced detailed logging for tick feed tracking and order book processing, improving traceability of vendor performance during backtests.
- Updated database schema to include new fields for managing Optuna study results, enhancing the ability to track study progress and outcomes.
- Refactored existing functions to utilize the new logging and tracking features, ensuring consistency across the backtesting framework.

Impact:
- These enhancements improve the robustness and transparency of the Optuna backtesting process, facilitating better analysis and optimization of trading strategies.
This commit is contained in:
Your Name
2026-08-21 19:05:23 +09:00
parent 0ecac7cb95
commit 0780b2cdd0
76 changed files with 4648 additions and 516 deletions

View File

@@ -296,6 +296,12 @@ def prepare_tail_search_context(
f"유니버스={universe_source} | 매수시간 "
f"{base_params.get('time_start_hm', 930):04d}-{base_params.get('time_end_hm', 1500):04d}"
)
try:
from kis_trader.backtest.optuna_feed_trace import log_bt_feed_chain_banner
log_bt_feed_chain_banner(context="Optuna-TAIL")
except Exception:
pass
rsi_period = int(base_params.get("rsi_period", 14))
candles_by_code, total_candles, has_holding_peak = tbc.load_tail_candles_by_code(
@@ -475,6 +481,8 @@ def run_tail_optuna(
direction=direction,
sampler=sampler,
)
from kis_trader.backtest.optuna_study_store import bind_study_trials, finalize_optuna_export
bind_study_trials(study, n_trials=n_trials, log=logger)
def objective(trial: optuna.Trial) -> float:
if ctx.mode == "tpe":
@@ -646,20 +654,31 @@ def run_tail_optuna(
out_path, strategy="tail", mode=ctx.mode, note="mode_combo params 저장(실측 전)", log=logger,
)
enrich_out_data_with_mode_combo(
out_data,
evaluate_fn=_eval_mode,
grid_keys=ctx.grid_keys,
def _enrich() -> None:
enrich_out_data_with_mode_combo(
out_data,
evaluate_fn=_eval_mode,
grid_keys=ctx.grid_keys,
log=logger,
on_partial_save=_save_partial,
)
try:
with open(out_path, "w", encoding="utf-8") as f:
json.dump(out_data, f, indent=2, ensure_ascii=False)
except OSError as exc:
logger.warning("⚠️ mode_combo 반영 재저장 실패: %s", exc)
announce_optuna_json_path(
out_path, strategy="tail", mode=ctx.mode, note="최종 JSON", log=logger,
)
finalize_optuna_export(
study,
out_data=out_data,
out_path=out_path,
strategy="tail",
mode=ctx.mode,
enrich_fn=_enrich,
log=logger,
on_partial_save=_save_partial,
)
try:
with open(out_path, "w", encoding="utf-8") as f:
json.dump(out_data, f, indent=2, ensure_ascii=False)
except OSError as exc:
logger.warning("⚠️ mode_combo 반영 재저장 실패: %s", exc)
announce_optuna_json_path(
out_path, strategy="tail", mode=ctx.mode, note="최종 JSON", log=logger,
)
if study.best_trial and study.best_value > _FAIL_OBJECTIVE + 1:
@@ -749,6 +768,10 @@ def main() -> None:
"--study-name", default=None, dest="study_name",
help="Study 이름 (미지정 시 OPTUNA_TAIL_STUDY_NAME 또는 tail_{mode}_{start}_{end})",
)
parser.add_argument(
"--study-trials", default=None, type=int, dest="study_trials",
help="이 study 시도 목표(COMPLETE+PRUNED+FAIL). 미지정/0=이번 --trials. 시도 < 목표면 후처리 스킵",
)
parser.add_argument(
"--storage", default=None,
help="Optuna storage URL (미지정 시 MariaDB 141/kis_optuna)",
@@ -848,6 +871,11 @@ def main() -> None:
n_trials = get_env_int("PARAM_SEARCH_OPTUNA_N_TRIALS", 200)
n_trials = max(1, int(n_trials))
from kis_trader.backtest.optuna_study_store import resolve_cli_study_trials
_st_goal = resolve_cli_study_trials(getattr(args, "study_trials", None))
if _st_goal > 0:
os.environ["KIS_OPTUNA_STUDY_TRIALS"] = str(_st_goal)
n_jobs = args.n_jobs
if n_jobs is None:
n_jobs = get_env_int("PARAM_SEARCH_OPTUNA_N_JOBS", 1)