File size: 17,417 Bytes
43f8a47 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | #!/usr/bin/env python3
"""
Market Regime Detection System
Identifies trending vs ranging markets and provides regime-adaptive trading parameters
"""
import pandas as pd
import numpy as np
from enum import Enum
from typing import Dict, List, Tuple, Optional
import warnings
warnings.filterwarnings('ignore')
class MarketRegime(Enum):
"""Market regime classifications"""
STRONG_BULL = "strong_bull"
BULL_TREND = "bull_trend"
BEAR_TREND = "bear_trend"
STRONG_BEAR = "strong_bear"
RANGING = "ranging"
HIGH_VOLATILITY = "high_volatility"
LOW_VOLATILITY = "low_volatility"
class RegimeParameters:
"""Trading parameters optimized for each market regime"""
def __init__(self):
# Base parameters for each regime
self.regime_params = {
MarketRegime.STRONG_BULL: {
'profit_targets': [0.015, 0.03, 0.06, 0.12], # Higher targets in strong trends
'trailing_stop_pct': 0.03, # Tighter stops to capture momentum
'position_size_multiplier': 1.5, # Larger positions in strong trends
'max_holding_time': 48, # Longer holding in trends
'breakeven_trigger': 0.02, # Later breakeven
'min_confidence_threshold': 0.25, # Lower threshold for strong trends
},
MarketRegime.BULL_TREND: {
'profit_targets': [0.012, 0.025, 0.05, 0.10],
'trailing_stop_pct': 0.025,
'position_size_multiplier': 1.2,
'max_holding_time': 36,
'breakeven_trigger': 0.018,
'min_confidence_threshold': 0.3,
},
MarketRegime.BEAR_TREND: {
'profit_targets': [0.012, 0.025, 0.05, 0.10],
'trailing_stop_pct': 0.025,
'position_size_multiplier': 1.2,
'max_holding_time': 36,
'breakeven_trigger': 0.018,
'min_confidence_threshold': 0.3,
},
MarketRegime.STRONG_BEAR: {
'profit_targets': [0.015, 0.03, 0.06, 0.12],
'trailing_stop_pct': 0.03,
'position_size_multiplier': 1.5,
'max_holding_time': 48,
'breakeven_trigger': 0.02,
'min_confidence_threshold': 0.25,
},
MarketRegime.RANGING: {
'profit_targets': [0.008, 0.015, 0.03, 0.06], # Lower targets in ranging markets
'trailing_stop_pct': 0.02, # Tighter stops to protect against whipsaws
'position_size_multiplier': 0.7, # Smaller positions in ranging markets
'max_holding_time': 12, # Shorter holding periods
'breakeven_trigger': 0.012, # Earlier breakeven
'min_confidence_threshold': 0.4, # Higher threshold for ranging markets
},
MarketRegime.HIGH_VOLATILITY: {
'profit_targets': [0.02, 0.04, 0.08, 0.15], # Higher targets for volatility
'trailing_stop_pct': 0.04, # Wider stops for volatility
'position_size_multiplier': 0.6, # Smaller positions in high volatility
'max_holding_time': 8, # Very short holding periods
'breakeven_trigger': 0.025, # Later breakeven for volatility
'min_confidence_threshold': 0.5, # Much higher threshold
},
MarketRegime.LOW_VOLATILITY: {
'profit_targets': [0.005, 0.01, 0.02, 0.04], # Lower targets in low volatility
'trailing_stop_pct': 0.015, # Tighter stops
'position_size_multiplier': 1.0, # Normal positions
'max_holding_time': 24, # Normal holding periods
'breakeven_trigger': 0.008, # Earlier breakeven
'min_confidence_threshold': 0.35, # Moderate threshold
}
}
def get_parameters(self, regime: MarketRegime) -> Dict:
"""Get trading parameters for a specific regime"""
return self.regime_params.get(regime, self.regime_params[MarketRegime.RANGING])
class MarketRegimeDetector:
"""
Advanced market regime detection using multiple indicators
"""
def __init__(self, lookback_periods: List[int] = [20, 50, 100]):
self.lookback_periods = lookback_periods
self.regime_history = []
self.parameters = RegimeParameters()
def detect_regime(self, price_data: pd.DataFrame, current_idx: int = -1) -> Tuple[MarketRegime, Dict]:
"""
Detect current market regime using comprehensive analysis
Args:
price_data: DataFrame with OHLC and technical indicators
current_idx: Current index in the data (default: latest)
Returns:
Tuple of (detected_regime, regime_parameters)
"""
if len(price_data) < max(self.lookback_periods):
return MarketRegime.RANGING, self.parameters.get_parameters(MarketRegime.RANGING)
# Extract current window
if current_idx == -1:
current_idx = len(price_data) - 1
# Calculate regime indicators
trend_strength = self._calculate_trend_strength(price_data, current_idx)
volatility = self._calculate_volatility(price_data, current_idx)
momentum = self._calculate_momentum(price_data, current_idx)
volume_trend = self._calculate_volume_trend(price_data, current_idx)
# Determine primary regime
regime = self._classify_regime(trend_strength, volatility, momentum, volume_trend)
# Get regime-specific parameters
regime_params = self.parameters.get_parameters(regime)
# Store regime history
self.regime_history.append({
'timestamp': price_data.index[current_idx] if hasattr(price_data.index, '__getitem__') else current_idx,
'regime': regime,
'trend_strength': trend_strength,
'volatility': volatility,
'momentum': momentum
})
return regime, regime_params
def _calculate_trend_strength(self, data: pd.DataFrame, idx: int) -> float:
"""Calculate trend strength using ADX-like indicator"""
if 'High' not in data.columns or 'Low' not in data.columns or 'Close' not in data.columns:
# Fallback for data without OHLC
if 'Close' in data.columns:
prices = data['Close'].iloc[max(0, idx-50):idx+1]
if len(prices) > 10:
# Simple trend strength based on slope
x = np.arange(len(prices))
slope = np.polyfit(x, prices.values, 1)[0]
return abs(slope) / prices.std() # Normalized slope
return 0.0
# Calculate True Range
high = data['High'].iloc[max(0, idx-50):idx+1]
low = data['Low'].iloc[max(0, idx-50):idx+1]
close = data['Close'].iloc[max(0, idx-50):idx+1]
tr = np.maximum(high - low,
np.maximum(abs(high - close.shift(1)),
abs(low - close.shift(1))))
# Calculate Directional Movement
dm_plus = np.where((high - high.shift(1)) > (low.shift(1) - low),
np.maximum(high - high.shift(1), 0), 0)
dm_minus = np.where((low.shift(1) - low) > (high - high.shift(1)),
np.maximum(low.shift(1) - low, 0), 0)
# Convert to pandas Series for rolling operations
dm_plus_series = pd.Series(dm_plus, index=high.index)
dm_minus_series = pd.Series(dm_minus, index=high.index)
tr_series = pd.Series(tr, index=high.index)
# Smooth with Wilder's smoothing
atr = tr_series.rolling(14).mean()
di_plus = 100 * (dm_plus_series.rolling(14).mean() / atr)
di_minus = 100 * (dm_minus_series.rolling(14).mean() / atr)
# ADX (trend strength)
dx = 100 * abs(di_plus - di_minus) / (di_plus + di_minus)
adx = dx.rolling(14).mean()
return adx.iloc[-1] if not adx.empty else 0.0
def _calculate_volatility(self, data: pd.DataFrame, idx: int) -> float:
"""Calculate normalized volatility"""
if 'Close' not in data.columns:
return 0.0
prices = data['Close'].iloc[max(0, idx-50):idx+1]
if len(prices) < 10:
return 0.0
# Calculate multiple volatility measures
returns = prices.pct_change().dropna()
# Bollinger Band width (normalized)
sma = prices.rolling(20).mean()
std = prices.rolling(20).std()
bb_width = 2 * std / sma
bb_volatility = bb_width.iloc[-1] if not bb_width.empty else 0
# ATR if available
if 'High' in data.columns and 'Low' in data.columns:
high = data['High'].iloc[max(0, idx-20):idx+1]
low = data['Low'].iloc[max(0, idx-20):idx+1]
close = data['Close'].iloc[max(0, idx-20):idx+1]
tr = np.maximum(high - low,
np.maximum(abs(high - close.shift(1)),
abs(low - close.shift(1))))
atr = tr.rolling(14).mean()
atr_volatility = atr.iloc[-1] / prices.iloc[-1] if not atr.empty else 0
else:
atr_volatility = 0
# Return volatility
ret_volatility = returns.std() * np.sqrt(252) # Annualized
# Combine measures
combined_volatility = (bb_volatility * 0.4 + atr_volatility * 0.4 + ret_volatility * 0.2)
return combined_volatility
def _calculate_momentum(self, data: pd.DataFrame, idx: int) -> float:
"""Calculate momentum indicators"""
if 'Close' not in data.columns:
return 0.0
prices = data['Close'].iloc[max(0, idx-50):idx+1]
if len(prices) < 20:
return 0.0
# RSI
delta = prices.diff()
gain = (delta.where(delta > 0, 0)).rolling(14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(14).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
rsi_value = rsi.iloc[-1] if not rsi.empty else 50
# MACD momentum
ema12 = prices.ewm(span=12).mean()
ema26 = prices.ewm(span=26).mean()
macd = ema12 - ema26
macd_signal = macd.ewm(span=9).mean()
macd_momentum = (macd - macd_signal).iloc[-1] if not macd.empty else 0
# Normalize RSI to momentum score (-1 to 1)
rsi_momentum = (rsi_value - 50) / 50
# Combine momentum indicators
combined_momentum = (rsi_momentum * 0.6 + np.sign(macd_momentum) * 0.4)
return combined_momentum
def _calculate_volume_trend(self, data: pd.DataFrame, idx: int) -> float:
"""Calculate volume trend if volume data is available"""
if 'Volume' not in data.columns:
return 0.0
volume = data['Volume'].iloc[max(0, idx-50):idx+1]
if len(volume) < 20:
return 0.0
# Volume trend
volume_sma = volume.rolling(20).mean()
volume_trend = (volume.iloc[-1] - volume_sma.iloc[-1]) / volume_sma.iloc[-1]
return volume_trend
def _classify_regime(self, trend_strength: float, volatility: float,
momentum: float, volume_trend: float) -> MarketRegime:
"""
Classify market regime based on calculated indicators
"""
# Volatility thresholds
HIGH_VOL_THRESHOLD = 0.05
LOW_VOL_THRESHOLD = 0.015
# Trend strength thresholds
STRONG_TREND_THRESHOLD = 25
MODERATE_TREND_THRESHOLD = 20
# Momentum thresholds
BULL_MOMENTUM_THRESHOLD = 0.3
BEAR_MOMENTUM_THRESHOLD = -0.3
# High volatility regime
if volatility > HIGH_VOL_THRESHOLD:
return MarketRegime.HIGH_VOLATILITY
# Low volatility regime
if volatility < LOW_VOL_THRESHOLD:
return MarketRegime.LOW_VOLATILITY
# Strong trending regimes
if trend_strength > STRONG_TREND_THRESHOLD:
if momentum > BULL_MOMENTUM_THRESHOLD:
return MarketRegime.STRONG_BULL
elif momentum < BEAR_MOMENTUM_THRESHOLD:
return MarketRegime.STRONG_BEAR
# Moderate trending regimes
if trend_strength > MODERATE_TREND_THRESHOLD:
if momentum > 0.1:
return MarketRegime.BULL_TREND
elif momentum < -0.1:
return MarketRegime.BEAR_TREND
# Default to ranging market
return MarketRegime.RANGING
def get_regime_statistics(self) -> Dict:
"""Get statistics about regime detection performance"""
if not self.regime_history:
return {}
df = pd.DataFrame(self.regime_history)
stats = {
'total_observations': len(df),
'regime_distribution': df['regime'].value_counts().to_dict(),
'avg_trend_strength': df['trend_strength'].mean(),
'avg_volatility': df['volatility'].mean(),
'avg_momentum': df['momentum'].mean(),
'regime_transitions': self._calculate_regime_transitions(df)
}
return stats
def _calculate_regime_transitions(self, df: pd.DataFrame) -> Dict:
"""Calculate regime transition frequencies"""
transitions = {}
regimes = df['regime'].values
for i in range(1, len(regimes)):
from_regime = regimes[i-1]
to_regime = regimes[i]
key = f"{from_regime.value}_to_{to_regime.value}"
transitions[key] = transitions.get(key, 0) + 1
return transitions
def get_optimal_parameters_for_regime(self, regime: MarketRegime) -> Dict:
"""Get optimal trading parameters for a specific regime"""
return self.parameters.get_parameters(regime)
def create_sample_regime_analysis():
"""Create a sample analysis showing regime detection in action"""
print("🧠 MARKET REGIME DETECTION SYSTEM")
print("=" * 50)
# Create sample price data
np.random.seed(42)
n_points = 500
# Generate realistic price data with different regimes
base_price = 2000
prices = [base_price]
for i in range(1, n_points):
# Different volatility regimes
if i < 100: # Low volatility ranging
volatility = 0.005
trend = 0.0001
elif i < 200: # High volatility
volatility = 0.02
trend = 0.0005
elif i < 350: # Strong bull trend
volatility = 0.015
trend = 0.002
else: # Bear trend
volatility = 0.012
trend = -0.0015
# Add momentum component
momentum = 0.1 * (prices[-1] - prices[-2]) / prices[-2] if len(prices) > 1 else 0
# Generate price movement
price_change = trend + np.random.normal(0, volatility) + momentum
new_price = prices[-1] * (1 + price_change)
prices.append(max(1800, min(2200, new_price)))
# Create DataFrame
data = pd.DataFrame({
'Close': prices,
'High': [p * (1 + np.random.uniform(0, 0.005)) for p in prices],
'Low': [p * (1 - np.random.uniform(0, 0.005)) for p in prices],
'Volume': np.random.randint(1000, 10000, n_points)
})
# Initialize regime detector
detector = MarketRegimeDetector()
# Analyze regimes
regimes = []
regime_params = []
print("\n🔍 Analyzing market regimes...")
for i in range(50, len(data)): # Start after minimum lookback
regime, params = detector.detect_regime(data, i)
regimes.append(regime)
regime_params.append(params)
if i % 100 == 0:
print(f"Processed {i}/{len(data)} data points...")
# Show regime distribution
regime_counts = pd.Series([r.value for r in regimes]).value_counts()
print("\n📊 Regime Distribution:")
for regime, count in regime_counts.items():
pct = count / len(regimes) * 100
print(f" {regime}: {count} ({pct:.1f}%)")
# Show example parameters for each regime
print("\n🎯 Regime-Specific Parameters:")
unique_regimes = set(regimes)
for regime in unique_regimes:
params = detector.get_optimal_parameters_for_regime(regime)
print(f"\n{regime.value.upper()}:")
print(f" Profit Targets: {[f'{t*100:.1f}%' for t in params['profit_targets']]}")
print(f" Trailing Stop: {params['trailing_stop_pct']*100:.1f}%")
print(f" Position Size Multiplier: {params['position_size_multiplier']:.1f}x")
print(f" Max Holding Time: {params['max_holding_time']} hours")
print(f" Breakeven Trigger: {params['breakeven_trigger']*100:.1f}%")
print("\n✅ Market regime detection system ready!")
print("This will significantly improve trading performance by adapting to market conditions.")
if __name__ == "__main__":
create_sample_regime_analysis() |