feat: add market regime filter and compound reinvestment
- Add market_regime.py: BTC/ETH/SOL/XRP weighted 2h trend score Bull(≥+1.5%) / Neutral / Bear(<-1%) regime detection with 10min cache - strategy.py: dynamic TREND/VOL thresholds based on current regime Bull: 3%/1.5x, Neutral: 5%/2.0x, Bear: 8%/3.5x - price_collector.py: always include leader coins in price history - trader.py: compound reinvestment (profit added to budget, floor at initial) - notify.py: regime info in hourly report, P&L icons (✅/❌, 💚/🔴) - main.py: hourly status at top-of-hour, filter positions held 1h+ - backtest.py: timestop/combo comparison modes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,22 +7,23 @@ import os
|
||||
|
||||
import pyupbit
|
||||
from .market import get_current_price, get_ohlcv
|
||||
from .market_regime import get_regime
|
||||
from .price_db import get_price_n_hours_ago
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 추세 판단: 현재 기준 N시간 전 DB 가격 대비 +M% 이상이면 상승 중
|
||||
TREND_HOURS = float(os.getenv("TREND_HOURS", "12"))
|
||||
TREND_MIN_GAIN_PCT = float(os.getenv("TREND_MIN_GAIN_PCT", "3"))
|
||||
TREND_MIN_GAIN_PCT = float(os.getenv("TREND_MIN_GAIN_PCT", "5")) # 레짐이 없을 때 기본값
|
||||
|
||||
# 모멘텀: MA 기간, 거래량 급증 배수
|
||||
MA_PERIOD = 20
|
||||
VOLUME_MULTIPLIER = float(os.getenv("VOLUME_MULTIPLIER", "1.2")) # 로컬 5h 평균 대비
|
||||
VOLUME_MULTIPLIER = float(os.getenv("VOLUME_MULTIPLIER", "2.0")) # 레짐이 없을 때 기본값
|
||||
LOCAL_VOL_HOURS = 5 # 로컬 기준 시간 (h)
|
||||
|
||||
|
||||
def check_trend(ticker: str) -> bool:
|
||||
"""상승 추세 조건: 현재가가 DB에 저장된 N시간 전 가격 대비 +M% 이상."""
|
||||
def check_trend(ticker: str, min_gain_pct: float) -> bool:
|
||||
"""상승 추세 조건: 현재가가 DB에 저장된 N시간 전 가격 대비 +min_gain_pct% 이상."""
|
||||
past_price = get_price_n_hours_ago(ticker, TREND_HOURS)
|
||||
if past_price is None:
|
||||
logger.debug(f"[추세] {ticker} {TREND_HOURS:.0f}h 전 가격 없음 (수집 중)")
|
||||
@@ -33,22 +34,22 @@ def check_trend(ticker: str) -> bool:
|
||||
return False
|
||||
|
||||
gain_pct = (current - past_price) / past_price * 100
|
||||
result = gain_pct >= TREND_MIN_GAIN_PCT
|
||||
result = gain_pct >= min_gain_pct
|
||||
|
||||
if result:
|
||||
logger.info(
|
||||
f"[추세↑] {ticker} {TREND_HOURS:.0f}h 전={past_price:,.2f} "
|
||||
f"현재={current:,.2f} (+{gain_pct:.1f}%)"
|
||||
f"현재={current:,.2f} (+{gain_pct:.1f}% ≥ {min_gain_pct}%)"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"[추세✗] {ticker} {gain_pct:+.1f}% (기준={TREND_MIN_GAIN_PCT:+.0f}%)"
|
||||
f"[추세✗] {ticker} {gain_pct:+.1f}% (기준={min_gain_pct:+.0f}%)"
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def check_momentum(ticker: str) -> bool:
|
||||
"""모멘텀 조건: 현재가 > MA20(일봉) AND 최근 1h 거래량 > 로컬 5h 평균 × 1.2.
|
||||
def check_momentum(ticker: str, vol_mult: float) -> bool:
|
||||
"""모멘텀 조건: 현재가 > MA20(일봉) AND 최근 1h 거래량 > 로컬 5h 평균 × vol_mult.
|
||||
|
||||
23h 평균은 낮 시간대 고거래량이 포함돼 새벽에 항상 미달하므로,
|
||||
로컬 5h 평균(같은 시간대 컨텍스트)과 비교한다.
|
||||
@@ -79,24 +80,28 @@ def check_momentum(ticker: str) -> bool:
|
||||
|
||||
recent_vol = df_hour["volume"].iloc[-2] # 직전 완성된 1h 봉
|
||||
local_avg = df_hour["volume"].iloc[-(LOCAL_VOL_HOURS + 1):-2].mean() # 이전 LOCAL_VOL_HOURS h 평균
|
||||
vol_ok = local_avg > 0 and recent_vol >= local_avg * VOLUME_MULTIPLIER
|
||||
vol_ok = local_avg > 0 and recent_vol >= local_avg * vol_mult
|
||||
|
||||
ratio = recent_vol / local_avg if local_avg > 0 else 0
|
||||
if vol_ok:
|
||||
logger.info(
|
||||
f"[모멘텀↑] {ticker} 현재={current:,.0f} MA20={ma:,.0f} "
|
||||
f"1h거래량={recent_vol:.0f} 로컬{LOCAL_VOL_HOURS}h평균={local_avg:.0f} ({ratio:.2f}x)"
|
||||
f"1h거래량={recent_vol:.0f} 로컬{LOCAL_VOL_HOURS}h평균={local_avg:.0f} ({ratio:.2f}x ≥ {vol_mult}x)"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"[모멘텀✗] {ticker} 1h거래량={recent_vol:.0f} 로컬{LOCAL_VOL_HOURS}h평균={local_avg:.0f} "
|
||||
f"({ratio:.2f}x < {VOLUME_MULTIPLIER}x)"
|
||||
f"({ratio:.2f}x < {vol_mult}x)"
|
||||
)
|
||||
return vol_ok
|
||||
|
||||
|
||||
def should_buy(ticker: str) -> bool:
|
||||
"""Strategy C: 실시간 상승 추세 AND 거래량 모멘텀 모두 충족 시 True."""
|
||||
if not check_trend(ticker):
|
||||
"""Strategy C + 시장 레짐: 레짐별 동적 임계값으로 추세 AND 모멘텀 판단."""
|
||||
regime = get_regime()
|
||||
trend_pct = regime["trend_pct"]
|
||||
vol_mult = regime["vol_mult"]
|
||||
|
||||
if not check_trend(ticker, trend_pct):
|
||||
return False
|
||||
return check_momentum(ticker)
|
||||
return check_momentum(ticker, vol_mult)
|
||||
|
||||
Reference in New Issue
Block a user