feat: add backtest module with DB cache and scenario comparison
Backtest improvements: - Add backtest.py with Oracle DB-backed OHLCV cache (no repeated API calls) - Add backtest_trades table to cache simulation results by params hash (same params -> instant load, skip re-simulation) - Add walk-forward scenario comparison (--walkforward-cmp) - Add trend ceiling filter (--trend-cmp, max gain threshold) - Add ticker win-rate filter (--ticker-cmp, SQL-based instant analysis) - Precompute daily_features once per data load (not per scenario) Live bot fixes: - monitor: add hard stop-loss from buy price (in addition to trailing) - strategy: fix re-entry condition to require +1% above last sell price - price_collector: add 48h backfill on startup for trend calculation - main: call backfill_prices() at startup Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,7 @@ TIME_STOP_MIN_GAIN_PCT = float(os.getenv("TIME_STOP_MIN_GAIN_PCT", "3"))
|
||||
|
||||
|
||||
def _check_trailing_stop(ticker: str, pos: dict, current: float) -> bool:
|
||||
"""트레일링 스탑 체크. 매도 시 True 반환."""
|
||||
"""트레일링 스탑(최고가 기준) + 고정 스탑(매수가 기준) 체크. 매도 시 True 반환."""
|
||||
trader.update_peak(ticker, current)
|
||||
|
||||
pos = trader.get_positions().get(ticker)
|
||||
@@ -27,15 +27,24 @@ def _check_trailing_stop(ticker: str, pos: dict, current: float) -> bool:
|
||||
return False
|
||||
|
||||
peak = pos["peak_price"]
|
||||
buy_price = pos["buy_price"]
|
||||
drop_from_peak = (peak - current) / peak
|
||||
drop_from_buy = (buy_price - current) / buy_price # 구매가 대비 하락률
|
||||
|
||||
if drop_from_peak >= STOP_LOSS_PCT:
|
||||
reason = (
|
||||
f"트레일링스탑 | 최고가={peak:,.0f}원 → "
|
||||
f"현재={current:,.0f}원 ({drop_from_peak:.1%} 하락)"
|
||||
)
|
||||
trader.sell(ticker, reason=reason)
|
||||
return True
|
||||
return trader.sell(ticker, reason=reason)
|
||||
|
||||
if drop_from_buy >= STOP_LOSS_PCT:
|
||||
reason = (
|
||||
f"스탑로스 | 매수가={buy_price:,.0f}원 → "
|
||||
f"현재={current:,.0f}원 ({drop_from_buy:.1%} 하락)"
|
||||
)
|
||||
return trader.sell(ticker, reason=reason)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@@ -70,15 +79,17 @@ def _check_position(ticker: str, pos: dict) -> None:
|
||||
if current is None:
|
||||
return
|
||||
|
||||
pnl = (current - pos["buy_price"]) / pos["buy_price"] * 100
|
||||
buy_price = pos["buy_price"]
|
||||
pnl = (current - buy_price) / buy_price * 100
|
||||
peak = pos["peak_price"]
|
||||
drop_from_peak = (peak - current) / peak
|
||||
drop_from_buy = (buy_price - current) / buy_price
|
||||
entry_time = pos.get("entry_time", datetime.now())
|
||||
elapsed_hours = (datetime.now() - entry_time).total_seconds() / 3600
|
||||
|
||||
logger.info(
|
||||
f"[감시] {ticker} 현재={current:,.0f} | 최고={peak:,.0f} | "
|
||||
f"하락={drop_from_peak:.1%} | 수익률={pnl:+.1f}% | "
|
||||
f"[감시] {ticker} 현재={current:,.0f} | 매수가={buy_price:,.0f} | 최고={peak:,.0f} | "
|
||||
f"수익률={pnl:+.1f}% | peak하락={drop_from_peak:.1%} | buy하락={drop_from_buy:.1%} | "
|
||||
f"보유={elapsed_hours:.1f}h"
|
||||
)
|
||||
|
||||
@@ -94,7 +105,7 @@ def run_monitor(interval: int = CHECK_INTERVAL) -> None:
|
||||
"""전체 포지션 감시 루프."""
|
||||
logger.info(
|
||||
f"모니터 시작 | 체크={interval}초 | "
|
||||
f"트레일링스탑={STOP_LOSS_PCT:.0%} | "
|
||||
f"트레일링스탑={STOP_LOSS_PCT:.1%} | "
|
||||
f"타임스탑={TIME_STOP_HOURS:.0f}h/{TIME_STOP_MIN_GAIN_PCT:+.0f}%"
|
||||
)
|
||||
while True:
|
||||
|
||||
Reference in New Issue
Block a user