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.
108 lines
3.6 KiB
Bash
Executable File
108 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
# 4전략 Optuna mode=tpe 순차 실행 (병렬 X — RAM/틱 로딩 OOM 방지)
|
|
# 기본 기간: 어제~오늘(호출 시 START/END 오버라이드)
|
|
# --apply-best 없음 — 결과 JSON만. apply 는 사용자 승인 후.
|
|
#
|
|
# 사용:
|
|
# nohup bash scripts/run_optuna_4strat_tpe_seq.sh >> logs/optuna_4strat_tpe_master.log 2>&1 &
|
|
# tail -f logs/optuna_4strat_tpe_latest_master.logpath # 경로만
|
|
# tail -f "$(cat logs/optuna_4strat_tpe_latest_master.logpath)"
|
|
#
|
|
# START=2026-07-20 END=2026-07-21 TRIALS=200 bash scripts/run_optuna_4strat_tpe_seq.sh
|
|
|
|
set -euo pipefail
|
|
cd /home/hoon/kis_bot
|
|
mkdir -p logs kis_trader/backtest/results
|
|
|
|
START="${START:-2026-07-20}"
|
|
END="${END:-2026-07-21}"
|
|
MODE="${MODE:-tpe}"
|
|
TRIALS="${TRIALS:-200}"
|
|
MIN_TRADES="${MIN_TRADES:-1}"
|
|
# 탐색 게이트 OFF — TPE 학습. 사후 후보는 JSON results_gated (REPORT_MIN_*)
|
|
MIN_WIN_RATE="${MIN_WIN_RATE:-0}"
|
|
MIN_PF="${MIN_PF:-0}"
|
|
# 공백 구분 — 기본 국내4 (해외 us_momentum 은 STRATEGIES 에 넣을 때만)
|
|
# STRATEGIES="momentum tail" bash ...
|
|
# STRATEGIES="us_momentum momentum" bash ...
|
|
STRATEGIES="${STRATEGIES:-momentum tail breakout scalp}"
|
|
# kiwoom|ls — 웹 Optuna 이력소스 / CLI UNIVERSE_HISTORY_SOURCE
|
|
UNIVERSE_HISTORY_SOURCE="${UNIVERSE_HISTORY_SOURCE:-${BACKTEST_UNIVERSE_HISTORY_SOURCE:-kiwoom}}"
|
|
PY="${PY:-.venv/bin/python}"
|
|
TS0="$(date +%Y%m%d_%H%M%S)"
|
|
MASTER="logs/optuna_4strat_tpe_${START}_${END}_${TS0}_master.log"
|
|
|
|
{
|
|
echo "======== Optuna 4전략 TPE 순차 시작 $(date -Is) ========"
|
|
echo "START=$START END=$END MODE=$MODE TRIALS=$TRIALS"
|
|
echo "STRATEGIES=$STRATEGIES"
|
|
echo "UNIVERSE_HISTORY_SOURCE=$UNIVERSE_HISTORY_SOURCE"
|
|
echo "min_wr=$MIN_WIN_RATE min_pf=$MIN_PF min_trades=$MIN_TRADES"
|
|
echo "apply-best=OFF orderbook=off n_jobs=1 (사후 results_gated + briefing.md)"
|
|
echo "master_log=$MASTER"
|
|
free -h | sed -n '1,2p'
|
|
df -h / | tail -1
|
|
} | tee -a "$MASTER"
|
|
echo "$MASTER" > logs/optuna_4strat_tpe_latest_master.logpath
|
|
|
|
run_one() {
|
|
local strat="$1"
|
|
local ts study log sort_by
|
|
ts="$(date +%Y%m%d_%H%M%S)"
|
|
study="${strat}_tpe_${START//-/}_${END//-/}_${ts}"
|
|
log="logs/optuna_${strat}_tpe_${ts}.log"
|
|
sort_by="pnl"
|
|
case "$strat" in
|
|
momentum|us_momentum|scalp) sort_by="score" ;;
|
|
esac
|
|
|
|
{
|
|
echo ""
|
|
echo "-------- [$strat] START $(date -Is) study=$study univ=$UNIVERSE_HISTORY_SOURCE --------"
|
|
} | tee -a "$MASTER"
|
|
echo "$log" > "logs/optuna_${strat}_tpe_latest.logpath"
|
|
echo "$study" > "logs/optuna_${strat}_tpe_latest.study"
|
|
|
|
set +e
|
|
"$PY" -u kis_trader/backtest/param_search_optuna.py \
|
|
--strategy "$strat" \
|
|
--mode "$MODE" \
|
|
--start "$START" \
|
|
--end "$END" \
|
|
--trials "$TRIALS" \
|
|
--min_trades "$MIN_TRADES" \
|
|
--min_win_rate "$MIN_WIN_RATE" \
|
|
--min_pf "$MIN_PF" \
|
|
--orderbook-filter off \
|
|
--no-progress \
|
|
--study-name "$study" \
|
|
--sort-by "$sort_by" \
|
|
--universe-history-source "$UNIVERSE_HISTORY_SOURCE" \
|
|
>"$log" 2>&1
|
|
local rc=$?
|
|
set -e
|
|
|
|
{
|
|
echo "-------- [$strat] END rc=$rc $(date -Is) --------"
|
|
echo "LOG=$log"
|
|
grep -E 'OPTUNA_RESULT_JSON=|OPTUNA_BRIEFING_MD=|Best trial|optuna_best|❌|KeyError|Traceback' "$log" | tail -n 24 || true
|
|
} | tee -a "$MASTER"
|
|
|
|
if [[ "$rc" -ne 0 ]]; then
|
|
echo "⚠️ [$strat] 실패 rc=$rc — 다음 전략 계속" | tee -a "$MASTER"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
for s in $STRATEGIES; do
|
|
run_one "$s"
|
|
done
|
|
|
|
{
|
|
echo ""
|
|
echo "======== ALL DONE $(date -Is) ========"
|
|
echo "apply 하지 않음. JSON= kis_trader/backtest/results/optuna_*_tpe_*.json"
|
|
echo "브리핑= 같은 이름 .briefing.md (이전 장 / 앞으로 장)"
|
|
echo "검증: .venv/bin/python scripts/verify_optuna_tpe_apply_dryrun.py"
|
|
} | tee -a "$MASTER"
|