File size: 21,426 Bytes
8b7b267 |
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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
/**
* Adaptive Market Regime Detection System
* Identifies market conditions and adapts strategies accordingly
* Regimes: Trending, Ranging, Volatile, Calm, Bullish, Bearish
*/
/**
* Market regimes
*/
export const MARKET_REGIMES = {
TRENDING_BULLISH: 'trending-bullish',
TRENDING_BEARISH: 'trending-bearish',
RANGING: 'ranging',
VOLATILE_BULLISH: 'volatile-bullish',
VOLATILE_BEARISH: 'volatile-bearish',
CALM: 'calm',
BREAKDOWN: 'breakdown',
BREAKOUT: 'breakout',
ACCUMULATION: 'accumulation',
DISTRIBUTION: 'distribution'
};
/**
* Regime characteristics
*/
const REGIME_CHARACTERISTICS = {
[MARKET_REGIMES.TRENDING_BULLISH]: {
name: 'Trending Bullish',
description: 'Strong upward trend with consistent higher highs and higher lows',
bestStrategies: ['ict-market-structure', 'momentum-divergence-hunter', 'supply-demand-zones'],
riskLevel: 'medium',
profitPotential: 'high'
},
[MARKET_REGIMES.TRENDING_BEARISH]: {
name: 'Trending Bearish',
description: 'Strong downward trend with consistent lower highs and lower lows',
bestStrategies: ['ict-market-structure', 'liquidity-sweep-reversal'],
riskLevel: 'high',
profitPotential: 'high'
},
[MARKET_REGIMES.RANGING]: {
name: 'Ranging',
description: 'Sideways movement between support and resistance',
bestStrategies: ['supply-demand-zones', 'liquidity-sweep-reversal', 'mean-reversion-momentum'],
riskLevel: 'low',
profitPotential: 'medium'
},
[MARKET_REGIMES.VOLATILE_BULLISH]: {
name: 'Volatile Bullish',
description: 'Upward movement with high volatility and large swings',
bestStrategies: ['volatility-breakout-pro', 'fair-value-gap-strategy'],
riskLevel: 'very-high',
profitPotential: 'very-high'
},
[MARKET_REGIMES.VOLATILE_BEARISH]: {
name: 'Volatile Bearish',
description: 'Downward movement with high volatility',
bestStrategies: ['volatility-breakout-pro', 'liquidity-sweep-reversal'],
riskLevel: 'very-high',
profitPotential: 'very-high'
},
[MARKET_REGIMES.CALM]: {
name: 'Calm',
description: 'Low volatility with minimal price movement',
bestStrategies: ['ranging', 'supply-demand-zones'],
riskLevel: 'very-low',
profitPotential: 'low'
},
[MARKET_REGIMES.BREAKOUT]: {
name: 'Breakout',
description: 'Price breaking above resistance',
bestStrategies: ['volatility-breakout-pro', 'ict-market-structure', 'momentum-divergence-hunter'],
riskLevel: 'high',
profitPotential: 'very-high'
},
[MARKET_REGIMES.BREAKDOWN]: {
name: 'Breakdown',
description: 'Price breaking below support',
bestStrategies: ['liquidity-sweep-reversal', 'ict-market-structure'],
riskLevel: 'high',
profitPotential: 'high'
},
[MARKET_REGIMES.ACCUMULATION]: {
name: 'Accumulation',
description: 'Smart money accumulating positions',
bestStrategies: ['wyckoff-accumulation', 'supply-demand-zones', 'market-maker-profile'],
riskLevel: 'medium',
profitPotential: 'very-high'
},
[MARKET_REGIMES.DISTRIBUTION]: {
name: 'Distribution',
description: 'Smart money distributing positions',
bestStrategies: ['wyckoff-accumulation', 'liquidity-sweep-reversal'],
riskLevel: 'high',
profitPotential: 'medium'
}
};
/**
* Adaptive Regime Detector
*/
export class AdaptiveRegimeDetector {
constructor(config = {}) {
this.lookbackPeriod = config.lookbackPeriod || 100;
this.volatilityPeriod = config.volatilityPeriod || 20;
this.trendPeriod = config.trendPeriod || 50;
this.currentRegime = null;
this.regimeHistory = [];
this.confidence = 0;
}
/**
* Detect current market regime
* @param {Array<Object>} ohlcvData - OHLCV data
* @returns {Object} Regime detection results
*/
detectRegime(ohlcvData) {
if (!ohlcvData || ohlcvData.length < this.lookbackPeriod) {
return {
regime: MARKET_REGIMES.CALM,
confidence: 0,
error: 'Insufficient data'
};
}
const metrics = this.calculateMetrics(ohlcvData);
const regime = this.classifyRegime(metrics);
const confidence = this.calculateConfidence(metrics, regime);
// Update history
this.currentRegime = regime;
this.confidence = confidence;
this.regimeHistory.push({
regime,
confidence,
timestamp: Date.now(),
metrics
});
// Keep only recent history
if (this.regimeHistory.length > 50) {
this.regimeHistory.shift();
}
return {
regime,
confidence,
characteristics: REGIME_CHARACTERISTICS[regime],
metrics,
transition: this.detectRegimeTransition(),
timestamp: Date.now()
};
}
/**
* Calculate market metrics
* @param {Array<Object>} ohlcvData - OHLCV data
* @returns {Object} Metrics
*/
calculateMetrics(ohlcvData) {
const closes = ohlcvData.map(c => c.close);
const highs = ohlcvData.map(c => c.high);
const lows = ohlcvData.map(c => c.low);
const volumes = ohlcvData.map(c => c.volume);
return {
volatility: this.calculateVolatility(closes),
trend: this.calculateTrend(closes),
trendStrength: this.calculateTrendStrength(highs, lows, closes),
momentum: this.calculateMomentum(closes),
volume: this.analyzeVolume(volumes),
range: this.calculateRange(highs, lows, closes),
structure: this.analyzeMarketStructure(highs, lows),
phase: this.detectWyckoffPhase(ohlcvData)
};
}
/**
* Calculate volatility (ATR-based)
* @param {Array<number>} closes - Close prices
* @returns {number} Volatility percentage
*/
calculateVolatility(closes) {
const period = Math.min(this.volatilityPeriod, closes.length - 1);
const returns = [];
for (let i = 1; i <= period; i++) {
const ret = (closes[closes.length - i] - closes[closes.length - i - 1]) / closes[closes.length - i - 1];
returns.push(ret);
}
const mean = returns.reduce((a, b) => a + b, 0) / returns.length;
const variance = returns.reduce((sum, r) => sum + Math.pow(r - mean, 2), 0) / returns.length;
const stdDev = Math.sqrt(variance);
return stdDev * 100; // Convert to percentage
}
/**
* Calculate trend direction
* @param {Array<number>} closes - Close prices
* @returns {Object} Trend info
*/
calculateTrend(closes) {
const period = Math.min(this.trendPeriod, closes.length);
const recentPrices = closes.slice(-period);
// Linear regression
const { slope, r2 } = this.linearRegression(recentPrices);
let direction = 'neutral';
if (slope > 0.001) direction = 'up';
else if (slope < -0.001) direction = 'down';
return {
direction,
slope,
strength: r2 * 100 // R² as percentage
};
}
/**
* Linear regression
* @param {Array<number>} values - Values
* @returns {Object} Slope and R²
*/
linearRegression(values) {
const n = values.length;
const indices = Array.from({ length: n }, (_, i) => i);
const sumX = indices.reduce((a, b) => a + b, 0);
const sumY = values.reduce((a, b) => a + b, 0);
const sumXY = indices.reduce((sum, x, i) => sum + x * values[i], 0);
const sumX2 = indices.reduce((sum, x) => sum + x * x, 0);
const sumY2 = values.reduce((sum, y) => sum + y * y, 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
// Calculate R²
const meanY = sumY / n;
const ssTotal = values.reduce((sum, y) => sum + Math.pow(y - meanY, 2), 0);
const ssResidual = values.reduce((sum, y, i) => {
const predicted = slope * i + intercept;
return sum + Math.pow(y - predicted, 2);
}, 0);
const r2 = 1 - (ssResidual / ssTotal);
return { slope, intercept, r2: Math.max(0, r2) };
}
/**
* Calculate trend strength (ADX-like)
* @param {Array<number>} highs - High prices
* @param {Array<number>} lows - Low prices
* @param {Array<number>} closes - Close prices
* @returns {number} Trend strength (0-100)
*/
calculateTrendStrength(highs, lows, closes) {
const period = Math.min(14, closes.length - 1);
let plusDM = 0;
let minusDM = 0;
for (let i = closes.length - period; i < closes.length; i++) {
const highDiff = highs[i] - highs[i - 1];
const lowDiff = lows[i - 1] - lows[i];
if (highDiff > lowDiff && highDiff > 0) {
plusDM += highDiff;
} else if (lowDiff > highDiff && lowDiff > 0) {
minusDM += lowDiff;
}
}
const totalDM = plusDM + minusDM;
if (totalDM === 0) return 0;
const dx = Math.abs(plusDM - minusDM) / totalDM * 100;
return Math.min(100, dx);
}
/**
* Calculate momentum
* @param {Array<number>} closes - Close prices
* @returns {Object} Momentum info
*/
calculateMomentum(closes) {
const period = 10;
const current = closes[closes.length - 1];
const past = closes[closes.length - period];
const momentum = ((current - past) / past) * 100;
let state = 'neutral';
if (momentum > 2) state = 'strong-positive';
else if (momentum > 0.5) state = 'positive';
else if (momentum < -2) state = 'strong-negative';
else if (momentum < -0.5) state = 'negative';
return { value: momentum, state };
}
/**
* Analyze volume
* @param {Array<number>} volumes - Volume data
* @returns {Object} Volume analysis
*/
analyzeVolume(volumes) {
const period = 20;
const recentVolumes = volumes.slice(-period);
const avgVolume = recentVolumes.reduce((a, b) => a + b, 0) / recentVolumes.length;
const currentVolume = volumes[volumes.length - 1];
const ratio = currentVolume / avgVolume;
let state = 'normal';
if (ratio > 2) state = 'very-high';
else if (ratio > 1.5) state = 'high';
else if (ratio < 0.5) state = 'very-low';
else if (ratio < 0.75) state = 'low';
return {
current: currentVolume,
average: avgVolume,
ratio,
state
};
}
/**
* Calculate price range
* @param {Array<number>} highs - High prices
* @param {Array<number>} lows - Low prices
* @param {Array<number>} closes - Close prices
* @returns {Object} Range info
*/
calculateRange(highs, lows, closes) {
const period = 20;
const recentHighs = highs.slice(-period);
const recentLows = lows.slice(-period);
const highestHigh = Math.max(...recentHighs);
const lowestLow = Math.min(...recentLows);
const currentPrice = closes[closes.length - 1];
const rangeSize = highestHigh - lowestLow;
const rangePercent = (rangeSize / currentPrice) * 100;
const position = (currentPrice - lowestLow) / rangeSize;
let state = 'ranging';
if (rangePercent < 3) state = 'tight';
else if (rangePercent > 10) state = 'wide';
return {
high: highestHigh,
low: lowestLow,
size: rangeSize,
percent: rangePercent,
position,
state
};
}
/**
* Analyze market structure
* @param {Array<number>} highs - High prices
* @param {Array<number>} lows - Low prices
* @returns {Object} Structure analysis
*/
analyzeMarketStructure(highs, lows) {
const swingPeriod = 5;
const recentHighs = highs.slice(-20);
const recentLows = lows.slice(-20);
// Find swing points
const swingHighIndices = [];
const swingLowIndices = [];
for (let i = swingPeriod; i < recentHighs.length - swingPeriod; i++) {
let isSwingHigh = true;
let isSwingLow = true;
for (let j = i - swingPeriod; j <= i + swingPeriod; j++) {
if (j !== i) {
if (recentHighs[j] >= recentHighs[i]) isSwingHigh = false;
if (recentLows[j] <= recentLows[i]) isSwingLow = false;
}
}
if (isSwingHigh) swingHighIndices.push(i);
if (isSwingLow) swingLowIndices.push(i);
}
// Analyze structure
let structure = 'neutral';
if (swingHighIndices.length >= 2 && swingLowIndices.length >= 2) {
const lastTwoHighs = swingHighIndices.slice(-2).map(i => recentHighs[i]);
const lastTwoLows = swingLowIndices.slice(-2).map(i => recentLows[i]);
const higherHighs = lastTwoHighs[1] > lastTwoHighs[0];
const higherLows = lastTwoLows[1] > lastTwoLows[0];
const lowerHighs = lastTwoHighs[1] < lastTwoHighs[0];
const lowerLows = lastTwoLows[1] < lastTwoLows[0];
if (higherHighs && higherLows) structure = 'bullish';
else if (lowerHighs && lowerLows) structure = 'bearish';
else if (higherHighs && lowerLows) structure = 'distribution';
else if (lowerHighs && higherLows) structure = 'accumulation';
}
return {
structure,
swingHighs: swingHighIndices.length,
swingLows: swingLowIndices.length
};
}
/**
* Detect Wyckoff phase
* @param {Array<Object>} ohlcvData - OHLCV data
* @returns {string} Wyckoff phase
*/
detectWyckoffPhase(ohlcvData) {
const volumes = ohlcvData.map(c => c.volume);
const closes = ohlcvData.map(c => c.close);
const highs = ohlcvData.map(c => c.high);
const lows = ohlcvData.map(c => c.low);
const priceRange = Math.max(...highs.slice(-20)) - Math.min(...lows.slice(-20));
const priceRangePercent = (priceRange / closes[closes.length - 1]) * 100;
const avgVolume = volumes.slice(-20).reduce((a, b) => a + b, 0) / 20;
const recentVolume = volumes.slice(-5).reduce((a, b) => a + b, 0) / 5;
const volumeRatio = recentVolume / avgVolume;
const priceChange = ((closes[closes.length - 1] - closes[closes.length - 20]) / closes[closes.length - 20]) * 100;
// Accumulation: Low range + High volume + Flat price
if (priceRangePercent < 5 && volumeRatio > 1.2 && Math.abs(priceChange) < 3) {
return 'accumulation';
}
// Distribution: Low range + High volume + Flat/Declining price
if (priceRangePercent < 5 && volumeRatio > 1.2 && priceChange < 0) {
return 'distribution';
}
// Markup: Rising price + Increasing volume
if (priceChange > 5 && volumeRatio > 1) {
return 'markup';
}
// Markdown: Falling price + Increasing volume
if (priceChange < -5 && volumeRatio > 1) {
return 'markdown';
}
return 'neutral';
}
/**
* Classify regime based on metrics
* @param {Object} metrics - Market metrics
* @returns {string} Market regime
*/
classifyRegime(metrics) {
const { volatility, trend, trendStrength, momentum, volume, range, structure, phase } = metrics;
// Wyckoff phases take priority
if (phase === 'accumulation') {
return MARKET_REGIMES.ACCUMULATION;
}
if (phase === 'distribution') {
return MARKET_REGIMES.DISTRIBUTION;
}
// Volatile regimes
if (volatility > 5) {
if (trend.direction === 'up' || momentum.state.includes('positive')) {
return MARKET_REGIMES.VOLATILE_BULLISH;
}
if (trend.direction === 'down' || momentum.state.includes('negative')) {
return MARKET_REGIMES.VOLATILE_BEARISH;
}
}
// Breakout/Breakdown
if (range.position > 0.95 && volume.state === 'high' && momentum.state.includes('positive')) {
return MARKET_REGIMES.BREAKOUT;
}
if (range.position < 0.05 && volume.state === 'high' && momentum.state.includes('negative')) {
return MARKET_REGIMES.BREAKDOWN;
}
// Trending regimes
if (trendStrength > 40 && trend.strength > 60) {
if (trend.direction === 'up' || structure.structure === 'bullish') {
return MARKET_REGIMES.TRENDING_BULLISH;
}
if (trend.direction === 'down' || structure.structure === 'bearish') {
return MARKET_REGIMES.TRENDING_BEARISH;
}
}
// Ranging
if (range.state === 'tight' || range.percent < 5) {
if (volatility < 2) {
return MARKET_REGIMES.CALM;
}
return MARKET_REGIMES.RANGING;
}
// Calm market
if (volatility < 2 && trendStrength < 20) {
return MARKET_REGIMES.CALM;
}
// Default to ranging
return MARKET_REGIMES.RANGING;
}
/**
* Calculate confidence in regime classification
* @param {Object} metrics - Market metrics
* @param {string} regime - Classified regime
* @returns {number} Confidence (0-100)
*/
calculateConfidence(metrics, regime) {
let confidence = 50; // Base confidence
const { volatility, trend, trendStrength, volume, range } = metrics;
// Adjust based on trend strength
confidence += trendStrength * 0.3;
// Adjust based on trend R²
confidence += trend.strength * 0.2;
// Adjust based on volume confirmation
if (volume.state === 'high' || volume.state === 'very-high') {
confidence += 10;
}
// Adjust based on range clarity
if (range.state === 'tight') {
confidence += 5;
}
// Regime-specific adjustments
switch (regime) {
case MARKET_REGIMES.TRENDING_BULLISH:
case MARKET_REGIMES.TRENDING_BEARISH:
if (trendStrength > 60) confidence += 15;
break;
case MARKET_REGIMES.RANGING:
case MARKET_REGIMES.CALM:
if (volatility < 2) confidence += 10;
break;
case MARKET_REGIMES.BREAKOUT:
case MARKET_REGIMES.BREAKDOWN:
if (volume.state === 'very-high') confidence += 20;
break;
}
return Math.min(100, Math.max(0, confidence));
}
/**
* Detect regime transitions
* @returns {Object|null} Transition info
*/
detectRegimeTransition() {
if (this.regimeHistory.length < 2) {
return null;
}
const current = this.regimeHistory[this.regimeHistory.length - 1];
const previous = this.regimeHistory[this.regimeHistory.length - 2];
if (current.regime !== previous.regime) {
return {
from: previous.regime,
to: current.regime,
timestamp: current.timestamp,
significance: this.calculateTransitionSignificance(previous.regime, current.regime)
};
}
return null;
}
/**
* Calculate significance of regime transition
* @param {string} from - Previous regime
* @param {string} to - Current regime
* @returns {string} Significance level
*/
calculateTransitionSignificance(from, to) {
const highImpact = [
[MARKET_REGIMES.ACCUMULATION, MARKET_REGIMES.BREAKOUT],
[MARKET_REGIMES.DISTRIBUTION, MARKET_REGIMES.BREAKDOWN],
[MARKET_REGIMES.RANGING, MARKET_REGIMES.TRENDING_BULLISH],
[MARKET_REGIMES.RANGING, MARKET_REGIMES.TRENDING_BEARISH]
];
for (const [fromRegime, toRegime] of highImpact) {
if (from === fromRegime && to === toRegime) {
return 'high';
}
}
return 'medium';
}
/**
* Get recommended strategies for current regime
* @returns {Array<string>} Recommended strategies
*/
getRecommendedStrategies() {
if (!this.currentRegime) {
return ['ict-market-structure'];
}
return REGIME_CHARACTERISTICS[this.currentRegime]?.bestStrategies || ['ict-market-structure'];
}
/**
* Get regime history
* @param {number} limit - Number of items
* @returns {Array<Object>} Regime history
*/
getHistory(limit = 20) {
return this.regimeHistory.slice(-limit);
}
}
export default AdaptiveRegimeDetector;
|