Files
kis_bot/scripts/run_optuna_4strat_tpe_seq.sh
Your Name 36a3e2b4a1 feat: Enhance trading system with new permanent subscription features and order book management
Changes:
- Added a new API endpoint for managing permanent subscriptions, allowing users to enable or disable subscriptions dynamically.
- Implemented a function to fill candle data from Kiwoom, ensuring that only relevant data is inserted into the database.
- Introduced a mechanism to handle master subscription states, improving the management of subscription statuses.
- Updated the database schema to include new fields for managing subscription states and order book filtering.

Impact:
- These enhancements improve the flexibility and reliability of the trading system, allowing for better management of subscriptions and order book data, while reducing the risk of data inconsistencies.

히스토리 align 제거 븅신같은 초기설계 아예 제거
진입모드에 구멍메움
호가진입을 켜도 호가가 안들어올때 호가 안보고 그냥 사버림
2026-08-15 23:01:14 +09:00

136 lines
4.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}"
# 꼬리 TPE 진입모드 — 한 스터디에 섞지 않음. 공백 구분 시 순차 2회.
# TAIL_OPTUNA_ENTRY_MODES="align limit_atr"
TAIL_OPTUNA_ENTRY_MODES="${TAIL_OPTUNA_ENTRY_MODES:-align}"
# 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 "TAIL_OPTUNA_ENTRY_MODES=$TAIL_OPTUNA_ENTRY_MODES"
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 entry_mode="${2:-}"
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"
if [[ "$strat" == "tail" && -n "$entry_mode" ]]; then
study="${strat}_${entry_mode}_tpe_${START//-/}_${END//-/}_${ts}"
log="logs/optuna_${strat}_${entry_mode}_tpe_${ts}.log"
fi
sort_by="pnl"
case "$strat" in
momentum|us_momentum|scalp) sort_by="score" ;;
esac
{
echo ""
echo "-------- [$strat${entry_mode:+/$entry_mode}] 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
set +e
local cmd_args=(
--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"
)
if [[ "$strat" == "tail" && -n "$entry_mode" ]]; then
cmd_args+=(--entry-mode "$entry_mode")
fi
if [[ -n "${TICK_SOURCE:-}" ]]; then
cmd_args+=("--tick-source" "$TICK_SOURCE")
fi
if [[ -n "${OB_SOURCE:-}" ]]; then
cmd_args+=("--ob-source" "$OB_SOURCE")
fi
"$PY" -u kis_trader/backtest/param_search_optuna.py "${cmd_args[@]}" >"$log" 2>&1
local rc=$?
set -e
{
echo "-------- [$strat${entry_mode:+/$entry_mode}] 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
if [[ "$s" == "tail" ]]; then
for em in $TAIL_OPTUNA_ENTRY_MODES; do
run_one tail "$em"
done
else
run_one "$s"
fi
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"