Market Context & Correlation
Relevant source files
The following files were used as context for generating this wiki page:
Market Context & Correlation is a core analytical system within the Rabid Raccoon platform designed to detect market regimes, compute cross-asset relationships, and provide a macro-overlay for technical signals. By integrating data from futures markets (MES, NQ), volatility indices (VIX), rates (US10Y), and economic headlines, the system determines whether the current environment is "Risk-On," "Risk-Off," or "Mixed."
This module acts as a filter for the platform's Instant Analysis Engine. It ensures that technical setups are validated against broader market conditions, such as dollar strength, yield spikes, or upcoming high-impact economic events.
Sources: src/lib/market-context.ts:1-13, src/lib/instant-analysis.ts:281-316
Market Regime Detection
The regime detection logic evaluates a set of "Risk-On" and "Risk-Off" factors based on percentage changes across multiple assets. A scoring mechanism aggregates these factors to assign a definitive market regime.
Scoring Logic and Factors
The system calculates a riskOnScore (base 0). Positive movements in equities and technical outperformance increase the score, while volatility expansion and rising yields decrease it.
| Factor Category | Asset | Condition | Impact |
|---|---|---|---|
| Equities | MES | > +0.05% | +2 (Risk-On) |
| Equities | MES | < -0.05% | -2 (Risk-Off) |
| Tech Leadership | NQ vs MES | NQ > MES + 0.1 | +1 (Risk-On) |
| Volatility | VIX (VX) | < -1% | +1 (Risk-On) |
| Volatility | VIX (VX) | > +1% | -2 (Risk-Off) |
| Rates | US10Y | > +1.0% | -1 (Risk-Off) |
| Safe Havens | Gold (GC) | > +0.3% | -1 (Risk-Off) |
Sources: src/lib/market-context.ts:145-207
The diagram shows the flow from raw price data to the final regime classification used in the intermarket narrative.
Cross-Asset Correlations
The system utilizes Pearson Correlation to measure the relationship between the Micro E-mini S&P 500 (MES) and other assets based on 15-minute rolling returns.
Correlation Interpretation
The engine translates numeric correlation values into human-readable insights to help traders identify leadership or divergences.
- MES ↔ VIX: A strong inverse relationship (e.g., -0.7) is considered normal. A positive correlation is flagged as abnormal "hedging stress."
- MES ↔ NQ: High positive correlation indicates tech leadership confirming the equity trend.
- MES ↔ US10Y: Negative correlation suggests rising rates are pressuring equities; positive suggests a growth/risk-on interpretation.
Sources: src/lib/market-context.ts:74-118
Technical Implementation
The correlation is computed using a pearsonCorrelation helper function that requires a minimum of 5 data points and calculates the covariance divided by the product of standard deviations.
function pearsonCorrelation(a: number[], b: number[]): number {
const n = Math.min(a.length, b.length)
if (n < 5) return 0
const aa = a.slice(-n)
const bb = b.slice(-n)
const meanA = aa.reduce((s, v) => s + v, 0) / n
const meanB = bb.reduce((s, v) => s + v, 0) / n
// ... covariance and variance calculation ...
return denom === 0 ? 0 : cov / denom
}
Sources: src/lib/market-context.ts:46-64
VIX Regime Filter
The VIX level significantly impacts the confidence and direction of technical signals. The platform applies a "hard short filter" or "confidence damping" based on VIX thresholds.
Filter Rules
- VIX >= 20: Hard short filter. BUY signals are suppressed or converted to SELL with high confidence (70%+).
- VIX 18-19.99: Long confidence is dampened (typically reduced by 6 points).
- VIX < 16: Long confidence is accelerated (increased by 5 points).
Sources: src/lib/instant-analysis.ts:430-466
Macro Data Integration
The system enriches market context with external data sources, primarily via the Federal Reserve Economic Data (FRED) API and technical leader snapshots.
Economic Theme Scoring
Headlines are parsed for keywords to generate scores (-100 to 100) for specific macro themes:
- Tariffs: Keywords like "trade," "duty," or "escalation."
- Rates: Keywords like "fed," "hawkish," or "inflation."
- AI/Tech: Keywords like "nvidia," "chip," or "guidance."
Sources: src/lib/market-context.ts:265-316
Rate and Yield Context
The YieldContext structure tracks the 10-Year yield and its relationship to the Fed Funds rate, providing a signal on whether rates are "stable," "grinding higher," or "spiking."
| Field | Type | Description |
|---|---|---|
tenYearYield | number | Current US10Y yield |
tenYearChangeBp | number | 1-day change in basis points |
fedFundsRate | number | null | Latest Fed Funds rate from FRED |
spread10yMinusFedBp | number | null | The spread between 10Y and Fed Funds |
Sources: src/lib/market-context.ts:32-40, src/lib/market-context.ts:333-356
Data Flow: Forecast API
The api/forecast route orchestrates the construction of the market context before passing it to the AI for narrative generation.
The diagram illustrates how daily data is transformed into market context to ground AI-generated forecasts.
Sources: src/app/api/forecast/route.ts:114-142
Summary
The Market Context & Correlation module provides the mathematical and macro-economic foundation for Rabid Raccoon's trading signals. By quantifying intermarket relationships and sentiment via Pearson correlations and keyword scoring, the system converts raw data into actionable regime filters. This ensures the platform's AI forecasting is constrained by "math-first" reality, respecting critical levels like VIX volatility regimes and US10Y yield thresholds.
Sources: src/lib/instant-analysis.ts:5-13, src/lib/market-context.ts:1-7