feat: implement Smart Volatility Scanner with RSI/volume filters (issue #76)
Some checks failed
CI / test (pull_request) Has been cancelled
Some checks failed
CI / test (pull_request) Has been cancelled
Add Python-first scanning pipeline that reduces Gemini API calls by filtering stocks before AI analysis: KIS rankings API -> RSI/volume filter -> AI judgment. ## Implementation - Add RSI calculation (Wilder's smoothing method) to VolatilityAnalyzer - Add KIS API methods: fetch_market_rankings() and get_daily_prices() - Create SmartVolatilityScanner with configurable thresholds - Integrate scanner into main.py realtime mode - Add selection_context logging to trades table for Evolution system ## Configuration - RSI_OVERSOLD_THRESHOLD: 30 (configurable 0-50) - RSI_MOMENTUM_THRESHOLD: 70 (configurable 50-100) - VOL_MULTIPLIER: 2.0 (minimum volume ratio, configurable 1-10) - SCANNER_TOP_N: 3 (max candidates per scan, configurable 1-10) ## Benefits - Reduces Gemini API calls (process 1-3 qualified stocks vs 20-30 ranked) - Python-based technical filtering before expensive AI judgment - Tracks selection criteria (RSI, volume_ratio, signal, score) for strategy optimization - Graceful fallback to static watchlist if ranking API fails ## Tests - 13 new tests for SmartVolatilityScanner and RSI calculation - All existing tests updated and passing - Coverage maintained at 73% Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -124,6 +124,54 @@ class VolatilityAnalyzer:
|
||||
return 1.0
|
||||
return current_volume / avg_volume
|
||||
|
||||
def calculate_rsi(
|
||||
self,
|
||||
close_prices: list[float],
|
||||
period: int = 14,
|
||||
) -> float:
|
||||
"""Calculate Relative Strength Index (RSI) using Wilder's smoothing.
|
||||
|
||||
Args:
|
||||
close_prices: List of closing prices (oldest to newest, minimum period+1 values)
|
||||
period: RSI period (default 14)
|
||||
|
||||
Returns:
|
||||
RSI value between 0 and 100, or 50.0 (neutral) if insufficient data
|
||||
|
||||
Examples:
|
||||
>>> analyzer = VolatilityAnalyzer()
|
||||
>>> prices = [100 - i * 0.5 for i in range(20)] # Downtrend
|
||||
>>> rsi = analyzer.calculate_rsi(prices)
|
||||
>>> assert rsi < 50 # Oversold territory
|
||||
"""
|
||||
if len(close_prices) < period + 1:
|
||||
return 50.0 # Neutral RSI if insufficient data
|
||||
|
||||
# Calculate price changes
|
||||
changes = [close_prices[i] - close_prices[i - 1] for i in range(1, len(close_prices))]
|
||||
|
||||
# Separate gains and losses
|
||||
gains = [max(0.0, change) for change in changes]
|
||||
losses = [max(0.0, -change) for change in changes]
|
||||
|
||||
# Calculate initial average gain/loss (simple average for first period)
|
||||
avg_gain = sum(gains[:period]) / period
|
||||
avg_loss = sum(losses[:period]) / period
|
||||
|
||||
# Apply Wilder's smoothing for remaining periods
|
||||
for i in range(period, len(changes)):
|
||||
avg_gain = (avg_gain * (period - 1) + gains[i]) / period
|
||||
avg_loss = (avg_loss * (period - 1) + losses[i]) / period
|
||||
|
||||
# Calculate RS and RSI
|
||||
if avg_loss == 0:
|
||||
return 100.0 # All gains, maximum RSI
|
||||
|
||||
rs = avg_gain / avg_loss
|
||||
rsi = 100 - (100 / (1 + rs))
|
||||
|
||||
return rsi
|
||||
|
||||
def calculate_pv_divergence(
|
||||
self,
|
||||
price_change: float,
|
||||
|
||||
Reference in New Issue
Block a user