OPEN-SOURCE SCRIPT
Folded RSI

Folded RSI: Spectral-Adaptive Momentum Oscillator
A cycle-responsive RSI that automatically tunes its calculation period based on real-time spectral correlation analysis, featuring gradient-visualized momentum extremes.
Overview
The Folded RSI revolutionizes traditional momentum analysis by replacing static periods with dynamic, data-driven adaptation. Using phase-invariant spectral correlation, the indicator measures how closely price action aligns with theoretical cyclical patterns, then adjusts the RSI length accordingly. When markets exhibit strong cyclical structure, the RSI compresses to capture rapid oscillations; during chaotic or trendless periods, it expands to filter noise.
Key Features
How It Works
1. Spectral Analysis
The indicator computes correlation between price returns and synthetic sinusoidal basis functions over the Cycle Window. By testing both sine and cosine components simultaneously, it achieves phase-invariance—detecting cyclical presence regardless of whether the cycle is currently at a peak, trough, or zero-crossing.
2. Harmonic Selection
When enabled, the algorithm compares correlation strength at both the Target Period and its octave (2× length), selecting whichever exhibits stronger statistical alignment with price action.
3. Adaptive Length Calculation
The correlation magnitude determines the RSI period through the formula:
4. Dynamic RSI Computation
A custom Wilder-style RSI calculates using the adaptive length, with optional post-smoothing EMA to reduce whipsaws.
Settings Guide
Visual Interpretation
Monitor the correlation magnitude in the diagnostics table to gauge indicator confidence—values above 60% indicate strong cyclical behavior where the adaptive length is optimized for current market conditions. Values below 30% suggest the market is in a non-cyclical state (trending or chaotic), triggering longer, smoother RSI periods.
A cycle-responsive RSI that automatically tunes its calculation period based on real-time spectral correlation analysis, featuring gradient-visualized momentum extremes.
Overview
The Folded RSI revolutionizes traditional momentum analysis by replacing static periods with dynamic, data-driven adaptation. Using phase-invariant spectral correlation, the indicator measures how closely price action aligns with theoretical cyclical patterns, then adjusts the RSI length accordingly. When markets exhibit strong cyclical structure, the RSI compresses to capture rapid oscillations; during chaotic or trendless periods, it expands to filter noise.
Key Features
- []Phase-Invariant Cycle Detection: Calculates Pearson correlation against pure sine/cosine waves to detect cyclical strength regardless of phase position (uses quadrature sum of sin/cos correlations)
[]Dual-Harmonic Analysis: Optionally evaluates both the target period and its 2× harmonic, automatically selecting the stronger correlation for optimal adaptation
[]Nonlinear Length Mapping: Maps correlation magnitude (0-1) to RSI length through a power function—strong cycles produce fast, responsive RSI; weak cycles produce smooth, lagged readings
[]Pure Mathematical Implementation: Custom Wilder RSI using dynamic smoothing factors (alpha = 1/length) and custom EMA—zero dependency on built-in TA functions
[]Gradient Visual System: Dynamic color transitions from neutral blue to hot red (overbought) or cool green (oversold) with gradient fills showing momentum intensity
[]Extreme Level Markers: Automatic visual alerts when RSI crosses above 70 (red markers) or below 30 (green markers) - Real-Time Diagnostics: On-chart table displaying current correlation magnitude, adaptive length, and detected dominant period
How It Works
1. Spectral Analysis
The indicator computes correlation between price returns and synthetic sinusoidal basis functions over the Cycle Window. By testing both sine and cosine components simultaneously, it achieves phase-invariance—detecting cyclical presence regardless of whether the cycle is currently at a peak, trough, or zero-crossing.
2. Harmonic Selection
When enabled, the algorithm compares correlation strength at both the Target Period and its octave (2× length), selecting whichever exhibits stronger statistical alignment with price action.
3. Adaptive Length Calculation
The correlation magnitude determines the RSI period through the formula:
- []High correlation → Shorter length (minimum setting)
[]Low correlation → Longer length (maximum setting) - Adjustable nonlinearity (power) curve to emphasize or flatten the response
4. Dynamic RSI Computation
A custom Wilder-style RSI calculates using the adaptive length, with optional post-smoothing EMA to reduce whipsaws.
Settings Guide
- []Cycle Window: Lookback bars for correlation calculation (40+ recommended for statistical significance)
[]Target Sine Period: Expected dominant cycle in bars (e.g., 20 for monthly cycles on daily charts)
[]RSI Length Min/Max: Bounds for adaptive calculation (5-50 standard range)
[]Nonlinearity (Power): Response curve shape—>1.0 emphasizes strong cycles, <1.0 creates more gradual transitions
[]Invert Mapping: Reverses logic (strong cycles → longer RSI) for contrarian strategies
[]Post Smoothing: EMA period applied to raw RSI output (1 = no smoothing)
Visual Interpretation
- []▼ Red Markers: RSI above 70 (potential overbought)
[]▲ Green Markers: RSI below 30 (potential oversold)
[]Diagnostics Table: Top-right display showing:- []Current RSI value
[]Correlation magnitude (higher % = stronger cyclical structure)
[]Current adaptive length
[]Best detected period (base or harmonic)
- []Current RSI value
Monitor the correlation magnitude in the diagnostics table to gauge indicator confidence—values above 60% indicate strong cyclical behavior where the adaptive length is optimized for current market conditions. Values below 30% suggest the market is in a non-cyclical state (trending or chaotic), triggering longer, smoother RSI periods.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.