fix: use local 5h volume baseline instead of 23h global average
23h average includes high-volume daytime periods, causing false negatives at early morning hours. Now compare last 1h candle against the previous 5h local average (same time-of-day context) with 1.2x multiplier. Also add momentum failure debug logs to show exact reason for rejection. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,7 +17,8 @@ TREND_MIN_GAIN_PCT = float(os.getenv("TREND_MIN_GAIN_PCT", "3"))
|
|||||||
|
|
||||||
# 모멘텀: MA 기간, 거래량 급증 배수
|
# 모멘텀: MA 기간, 거래량 급증 배수
|
||||||
MA_PERIOD = 20
|
MA_PERIOD = 20
|
||||||
VOLUME_MULTIPLIER = 2.0
|
VOLUME_MULTIPLIER = float(os.getenv("VOLUME_MULTIPLIER", "1.2")) # 로컬 5h 평균 대비
|
||||||
|
LOCAL_VOL_HOURS = 5 # 로컬 기준 시간 (h)
|
||||||
|
|
||||||
|
|
||||||
def check_trend(ticker: str) -> bool:
|
def check_trend(ticker: str) -> bool:
|
||||||
@@ -47,9 +48,10 @@ def check_trend(ticker: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def check_momentum(ticker: str) -> bool:
|
def check_momentum(ticker: str) -> bool:
|
||||||
"""모멘텀 조건: 현재가 > MA20(일봉) AND 최근 1h 거래량 > 24h 평균 × 2 (60분봉 기준).
|
"""모멘텀 조건: 현재가 > MA20(일봉) AND 최근 1h 거래량 > 로컬 5h 평균 × 1.2.
|
||||||
|
|
||||||
일봉 거래량은 오전에 항상 미달하므로 60분봉으로 교체.
|
23h 평균은 낮 시간대 고거래량이 포함돼 새벽에 항상 미달하므로,
|
||||||
|
로컬 5h 평균(같은 시간대 컨텍스트)과 비교한다.
|
||||||
"""
|
"""
|
||||||
# MA20: 일봉 기준
|
# MA20: 일봉 기준
|
||||||
df_daily = get_ohlcv(ticker, count=MA_PERIOD + 1)
|
df_daily = get_ohlcv(ticker, count=MA_PERIOD + 1)
|
||||||
@@ -63,27 +65,34 @@ def check_momentum(ticker: str) -> bool:
|
|||||||
|
|
||||||
price_ok = current > ma
|
price_ok = current > ma
|
||||||
if not price_ok:
|
if not price_ok:
|
||||||
|
logger.debug(f"[모멘텀✗] {ticker} 현재={current:,.0f} < MA20={ma:,.0f} (가격 기준 미달)")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 거래량: 60분봉 기준 (최근 1h vs 이전 24h 평균)
|
# 거래량: 60분봉 기준 (최근 1h vs 이전 LOCAL_VOL_HOURS h 로컬 평균)
|
||||||
|
fetch_count = LOCAL_VOL_HOURS + 3 # 여유 있게 fetch
|
||||||
try:
|
try:
|
||||||
df_hour = pyupbit.get_ohlcv(ticker, interval="minute60", count=26)
|
df_hour = pyupbit.get_ohlcv(ticker, interval="minute60", count=fetch_count)
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
if df_hour is None or len(df_hour) < 5:
|
if df_hour is None or len(df_hour) < LOCAL_VOL_HOURS + 1:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
recent_vol = df_hour["volume"].iloc[-2] # 직전 완성된 1h 봉
|
recent_vol = df_hour["volume"].iloc[-2] # 직전 완성된 1h 봉
|
||||||
avg_vol_1h = df_hour["volume"].iloc[-25:-2].mean() # 이전 23h 평균
|
local_avg = df_hour["volume"].iloc[-(LOCAL_VOL_HOURS + 1):-2].mean() # 이전 LOCAL_VOL_HOURS h 평균
|
||||||
vol_ok = avg_vol_1h > 0 and recent_vol > avg_vol_1h * VOLUME_MULTIPLIER
|
vol_ok = local_avg > 0 and recent_vol >= local_avg * VOLUME_MULTIPLIER
|
||||||
|
|
||||||
result = price_ok and vol_ok
|
ratio = recent_vol / local_avg if local_avg > 0 else 0
|
||||||
if result:
|
if vol_ok:
|
||||||
logger.debug(
|
logger.info(
|
||||||
f"[모멘텀] {ticker} 현재={current:,.0f} MA20={ma:,.0f} "
|
f"[모멘텀↑] {ticker} 현재={current:,.0f} MA20={ma:,.0f} "
|
||||||
f"1h거래량={recent_vol:.0f} 평균={avg_vol_1h:.0f}"
|
f"1h거래량={recent_vol:.0f} 로컬{LOCAL_VOL_HOURS}h평균={local_avg:.0f} ({ratio:.2f}x)"
|
||||||
)
|
)
|
||||||
return result
|
else:
|
||||||
|
logger.debug(
|
||||||
|
f"[모멘텀✗] {ticker} 1h거래량={recent_vol:.0f} 로컬{LOCAL_VOL_HOURS}h평균={local_avg:.0f} "
|
||||||
|
f"({ratio:.2f}x < {VOLUME_MULTIPLIER}x)"
|
||||||
|
)
|
||||||
|
return vol_ok
|
||||||
|
|
||||||
|
|
||||||
def should_buy(ticker: str) -> bool:
|
def should_buy(ticker: str) -> bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user