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:
@@ -205,6 +205,14 @@ def prepare_momentum_search_context(
|
||||
f"총한도 {total_budget_f:,.0f}원 | 매매 {format_session_hm(base_fixed)} | market={mk}"
|
||||
)
|
||||
|
||||
# 실매과 동일 1·2·3차 체인 — 옵투나 로그에서도 벤더 추적 가능
|
||||
try:
|
||||
from kis_trader.backtest.optuna_feed_trace import log_bt_feed_chain_banner
|
||||
|
||||
log_bt_feed_chain_banner(context="Optuna-MOMENTUM")
|
||||
except Exception:
|
||||
pass
|
||||
logger.info("📂 캔들 로드 중 %s~%s (이 동안 Top5는 비어 있음 · trial 0)", start, end)
|
||||
codes_candles = _load_candles_for_search(
|
||||
start, end, base_fixed.get("rsi_period", 3),
|
||||
market=mk if mk in ("US", "KR") else None,
|
||||
@@ -423,6 +431,8 @@ def run_momentum_optuna(
|
||||
direction="maximize",
|
||||
sampler=_make_sampler(sampler_name, seed),
|
||||
)
|
||||
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":
|
||||
@@ -597,17 +607,29 @@ def run_momentum_optuna(
|
||||
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,
|
||||
)
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
json.dump(out_data, f, indent=2, ensure_ascii=False)
|
||||
announce_optuna_json_path(
|
||||
out_path, strategy=_ann_strat, mode=ctx.mode, note="최종 JSON", log=logger,
|
||||
)
|
||||
|
||||
finalize_optuna_export(
|
||||
study,
|
||||
out_data=out_data,
|
||||
out_path=out_path,
|
||||
strategy=_ann_strat,
|
||||
mode=ctx.mode,
|
||||
enrich_fn=_enrich,
|
||||
log=logger,
|
||||
on_partial_save=_save_partial,
|
||||
)
|
||||
with open(out_path, "w", encoding="utf-8") as f:
|
||||
json.dump(out_data, f, indent=2, ensure_ascii=False)
|
||||
announce_optuna_json_path(
|
||||
out_path, strategy=_ann_strat, mode=ctx.mode, note="최종 JSON", log=logger,
|
||||
symbol=_sym,
|
||||
)
|
||||
study._kis_export_path = out_path # type: ignore[attr-defined]
|
||||
return study
|
||||
|
||||
Reference in New Issue
Block a user