OPEN-SOURCE SCRIPT

Log Regression Regime Channel

151
Log Regression Regime Channel
A market regime, trend quality, price deviation, and directional momentum indicator built around a unified log-linear regression model.
The indicator is designed to evaluate five connected aspects of market behavior:
1. Is the current market statistically directional or neutral?
2. How well does the regression model describe recent price movement?
3. Where is the current price relative to its fitted trend?
4. Does short-term momentum support or oppose the current market regime?
5. Is the underlying regression trend strengthening, cooling, or remaining stable?
The indicator’s primary purpose is to organize several related measurements into one coherent analytical framework.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📐 Core Calculation: Log-Linear Regression
The script applies ordinary least-squares linear regression to the logarithm of closing prices over the selected Regression Length.
The model has the following form:
log(price) = intercept + slope × time
Using logarithmic prices allows the model to analyze proportional price movement rather than absolute movement in price units.
For example, a move from 10 to 11 is treated consistently with a move from 100 to 110 because both represent a 10% change.
After the regression is calculated in logarithmic space, the fitted values are converted back into regular price values using the exponential function.
The model calculates:
• Regression Slope — determines the direction of the fitted trend.
• Regression Intercept — defines the position of the fitted path.
• Residual Standard Deviation — measures the typical distance between observed log prices and the regression model.
• R² — measures how much of the variation in log prices is explained by the fitted trend.
• Fitted Price Values — represent the model values at the beginning and end of the regression window.
Only positive price observations can be used because the logarithm of zero or a negative value is undefined.
The indicator begins displaying the complete model after enough valid historical bars have accumulated.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Residual-Based Regression Channel
The channel width is not calculated from ATR, a fixed percentage, or the standard deviation of raw prices.
It is based on the standard deviation of regression residuals in logarithmic space.
A residual is the difference between:
the observed logarithmic price
and
the logarithmic price fitted by the regression model.
The channel includes:
• Regression Midline — the centerline of the current fitted model.
• Inner Residual Bands — the inner residual-deviation boundaries.
• Outer Residual Bands — the outer residual-deviation boundaries.
• Channel Ribbons — optional shaded areas between the inner and outer boundaries.
Because the boundaries are symmetrical in logarithmic space, they represent proportional deviations from the fitted model.
After conversion back into regular price values, the upper and lower parts of the channel may not appear perfectly symmetrical on a linear price scale.
Difference Between the Two Length Settings
Regression Length determines how many bars are used to calculate the statistical model.
Visible Channel Length determines only how many recent bars are used to draw the current channel.
Changing Visible Channel Length does not change the underlying regression calculation. It changes only the displayed length of the channel.
This separation makes it possible to use a longer model window without drawing the channel across the entire calculation period.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🎯 Price Deviation From the Model
Current price deviation is calculated as:
current logarithmic residual ÷ residual standard deviation
The result shows the current price’s distance from the regression model in residual standard deviations.
Price Location
Center — price is close to the fitted regression midline.
Upper — price is above the regression model.
Lower — price is below the regression model.
Deviation Zones
Normal — the absolute deviation remains inside the inner boundary.
Extended — the absolute deviation has reached the inner boundary.
Extreme — the absolute deviation has reached the outer boundary.
The Extended and Extreme states describe price location relative to the current model.
They do not automatically imply that price must reverse. During a strong trend, price may remain elevated or depressed relative to the regression path for an extended period.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📈 Trend Quality and Trend Strength
The market is not classified as Bullish or Bearish from the direction of the regression slope alone.
The regime engine evaluates three characteristics together:
• the direction of the log-regression slope;
• Trend Quality;
• standardized Trend Strength.
Trend Quality
Trend Quality is measured using R² and displayed as a percentage.
R² indicates how consistently the movement of log prices is described by the regression model.
A higher value means that the fitted trend explains a larger proportion of the observed log-price variation.
Trend Strength
Trend Strength is calculated as the total fitted logarithmic movement across the regression window divided by the residual standard deviation.
It measures how large the directional move is relative to the normal dispersion of price around the model.
The value is expressed in residual standard deviations.
Why Both Measurements Are Used
A small but orderly slope and a large but irregular price movement do not represent the same market condition.
Using R² and Trend Strength together allows the indicator to evaluate:
• the consistency of the trend;
• the magnitude of the directional move;
• the amount of residual noise around the model.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔄 Market Regime Hysteresis
The script maintains one of three persistent market states:
🟢 Bullish
The regression slope is positive, while trend quality and strength satisfy the required thresholds.
⚪ Neutral
The conditions for a directional regime are not satisfied or are no longer sufficiently stable.
🔴 Bearish
The regression slope is negative, while trend quality and strength satisfy the required thresholds.
Different thresholds are used to enter a new directional regime and to preserve an existing regime.
Entering a New Regime
A new Bullish or Bearish regime must satisfy the stricter:
• Trend Entry Quality threshold;
• Trend Entry Strength threshold.
Preserving an Existing Regime
Once a directional regime has been established, it may remain active using the lower:
• Trend Exit Quality threshold;
• Trend Exit Strength threshold.
This mechanism is known as hysteresis.
It reduces frequent switching between directional and neutral states when the model measurements fluctuate around a single threshold.
Market Regime changes are committed only after the chart bar is confirmed. The persistent regime state therefore does not change while the current bar is still forming.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚡ Directional Momentum
Current Momentum is not based on a standard RSI, MACD, or moving-average crossover.
It combines two normalized components:
1. Logarithmic Price Velocity
Measures the direction and speed of proportional price changes.
2. Residual-Deviation Velocity
Measures whether price is gaining or losing ground relative to the fitted regression path.
This makes it possible to distinguish between:
• absolute price movement;
• movement relative to the current statistical trend.
Each component is normalized by its own typical absolute movement.
Momentum Score Composition
75% — normalized logarithmic price velocity.
25% — normalized residual-deviation velocity.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🧭 Adaptive Momentum Threshold
Momentum is classified using a dynamic threshold.
The script selects the larger of:
• the user-defined Minimum Momentum Threshold;
• the recent standard deviation of Momentum Score multiplied by the Adaptive Noise Multiplier.
When the Momentum Score becomes more unstable, the classification threshold automatically increases.
This helps reduce directional classifications during periods of elevated momentum noise.
Momentum Classification
🟢 Bullish Momentum — Momentum Score is above the positive adaptive threshold.
🔴 Bearish Momentum — Momentum Score is below the negative adaptive threshold.
⚪ Neutral Momentum — Momentum Score remains between the positive and negative thresholds.
Regression Line Color
A green line represents Bullish Momentum.
A red line represents Bearish Momentum.
A neutral-colored line indicates that momentum has not exceeded the adaptive threshold.
The optional Momentum Fill is drawn between the current fitted regression value and its smoothed reference line. Its color follows the current momentum classification.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🚀 Trend Acceleration
Trend Acceleration measures changes in the standardized regression slope.
The regression slope is first divided by the residual standard deviation.
The change in this standardized slope is then:
• smoothed;
• compared with its typical absolute change;
• converted into a normalized acceleration score.
Interpretation During a Bullish Regime
Strengthening — positive acceleration exceeds the selected threshold.
Cooling — negative acceleration exceeds the threshold in absolute terms.
Stable — the change remains within the threshold.
Interpretation During a Bearish Regime
Strengthening — negative acceleration exceeds the selected threshold.
Cooling — positive acceleration exceeds the selected threshold.
Stable — the change remains within the threshold.
Interpretation During a Neutral Regime
Changing — the absolute change in the standardized slope exceeds the threshold.
Stable — the change remains within the threshold.
Trend Acceleration describes changes in the regression trend and is calculated separately from short-term directional momentum.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🧩 Market Phase Engine
Market Phase is an interpretation layer that combines three measurements:
• confirmed Market Regime;
• current Current Momentum;
• current Price Deviation.
Market Phase is not a separate independent indicator and does not use an additional hidden calculation.
It describes the current combination of conditions already calculated by the model.
🟢 Bullish Regime Phases
Bullish Overextension — price has reached an extreme upper deviation during a Bullish regime.
Pullback Recovery — price is in the lower part of the channel, while directional momentum has turned Bullish again.
Bullish Pullback — price has moved into the lower part of the channel while short-term momentum remains Bearish.
Trend Continuation — the Bullish regime is supported by Bullish momentum.
Momentum Cooling — the Bullish regime remains active, but current momentum is Bearish.
Bullish Consolidation — the Bullish regime remains active without a clear directional momentum classification.
🔴 Bearish Regime Phases
Bearish Overextension — price has reached an extreme lower deviation during a Bearish regime.
Bounce Rejection — price is in the upper part of the channel, while directional momentum has turned Bearish again.
Bearish Bounce — price has moved into the upper part of the channel while short-term momentum remains Bullish.
Trend Continuation — the Bearish regime is supported by Bearish momentum.
Countertrend Bounce — the Bearish regime remains active, but current momentum is Bullish.
Bearish Consolidation — the Bearish regime remains active without a clear directional momentum classification.
⚪ Neutral Regime Phases
Upper Price Shock — price has reached an extreme upper deviation without an established directional regime.
Lower Price Shock — price has reached an extreme lower deviation without an established directional regime.
Neutral Upward Impulse — Bullish momentum has developed while the market regime remains Neutral.
Neutral Downward Impulse — Bearish momentum has developed while the market regime remains Neutral.
No Clear Trend — neither a directional regime nor a significant momentum impulse is present.
Market Phase labels describe the current combination of model conditions. They should be interpreted together with the underlying regime, momentum, and deviation measurements.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
💎 Why These Components Are Combined
Log Regression Regime Channel is not a collection of unrelated indicators placed inside one script.
All major components are derived from, or directly connected to, the same log-regression model.
How the Components Are Connected
• The regression slope provides the directional foundation.
• R² evaluates the quality of the same regression model.
• Residual dispersion defines the width of the channel.
• Residual volatility standardizes Trend Strength.
• The current residual produces the Price Deviation measurement.
• Changes in residual deviation contribute to Directional Momentum.
• Changes in the standardized slope produce Trend Acceleration.
• Regime, momentum, and deviation are combined by the Market Phase engine.
The purpose of this architecture is to express direction, model quality, price location, momentum, and acceleration in compatible terms.
This differs from mechanically combining several independent oscillators that use unrelated formulas and scales.
Core Architectural Features
• a unified log-linear regression model;
• a residual-based channel instead of an ATR or fixed-percentage channel;
• combined evaluation of trend quality and trend strength;
• confirmed-bar regime hysteresis;
• normalized price and residual momentum;
• an adaptive momentum threshold;
• a separate trend-acceleration measurement;
• deterministic Market Phase classification.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🖥️ Dashboard
The dashboard contains six primary measurements.
Market Regime
Displays the confirmed market state:
Bullish, Neutral, or Bearish.
Trend Quality
Displays:
• R² as a percentage;
• standardized Trend Strength.
Price Deviation
Displays:
• whether price is above or below the model;
• the current deviation in residual standard deviations;
• the Normal, Extended, or Extreme deviation zone.
Current Momentum
Displays the current short-term directional momentum:
Bullish, Neutral, or Bearish.
Trend Acceleration
Displays the current regression-trend state:
Strengthening, Cooling, Stable, or Changing.
Market Phase
Displays the current interpretation of the combination of:
• market regime;
• directional momentum;
• price deviation.
The channel boundaries and regime label use the color of the confirmed Market Regime.
The rolling regression line uses the color of Current Momentum.
These colors represent different calculations and should not be interpreted as duplicate classifications.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚙️ Default Settings
Regression Model
Regression Length — 120 bars
The number of bars used to calculate the log-linear regression model.
Visible Channel Length — 100 bars
The number of recent bars used to display the current regression channel.
Residual Channel
Inner Residual Band — 1.0σ
The inner channel boundary, measured in residual standard deviations.
Outer Residual Band — 2.0σ
The outer channel boundary, measured in residual standard deviations.
Regime Detection
Trend Entry Quality — 45% R²
The minimum model quality required to enter a Bullish or Bearish regime.
Trend Exit Quality — 35% R²
The minimum model quality required to preserve an existing directional regime.
Trend Entry Strength — 1.25σ
The minimum standardized trend strength required to enter a directional regime.
Trend Exit Strength — 0.90σ
The minimum trend strength required to preserve the current regime.
Directional Momentum
Momentum Length — 5 bars
The smoothing period used for short-term directional momentum.
Momentum Baseline — 18 bars
The period used to estimate typical price and residual movement.
Momentum Noise Window — 40 bars
The period used to estimate variation in the combined Momentum Score.
Minimum Momentum Threshold — 0.35
The minimum normalized threshold required for a directional momentum classification.
Adaptive Noise Multiplier — 0.30
The multiplier used to increase the momentum threshold when Momentum Score becomes more unstable.
Acceleration Threshold — 0.35
The minimum normalized change in slope required for an acceleration classification.
Built-In Parameter Safeguards
The script automatically controls relationships between dependent settings:
• the outer residual band remains wider than the inner band;
• exit thresholds cannot be stricter than their corresponding entry thresholds;
• Momentum Baseline remains longer than Momentum Length.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔍 How to Use the Indicator
The following sequence provides a structured way to interpret the model.
Step 1. Identify Market Regime
Determine whether the model currently recognizes:
• a Bullish regime;
• a Bearish regime;
• a Neutral environment.
Step 2. Evaluate Trend Quality
Use R² and Trend Strength to assess:
• how consistently price is moving;
• how large the fitted trend is relative to residual noise.
Step 3. Determine Price Location
Price Deviation shows:
• whether price is above or below the regression model;
• whether the deviation is Normal, Extended, or Extreme.
Step 4. Compare Momentum With Market Regime
When regime and momentum point in the same direction, the short-term movement is aligned with the broader statistical regime.
When they point in opposite directions, short-term movement is not supporting the current regime.
Step 5. Evaluate Trend Acceleration
Determine whether the current regression trend is:
• Strengthening;
• Cooling;
• Stable;
• Changing.
Step 6. Use Market Phase as the Combined Context
Market Phase combines regime, momentum, and price location into one descriptive classification.
It is best interpreted as a summary of the current market structure rather than separately from its underlying components.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🛠️ How Settings Affect the Model
Regression Length
A longer period generally produces a slower and more stable model.
A shorter period reacts more quickly to price changes but may change market regime more frequently.
Entry Quality and Entry Strength
Higher values require a cleaner and stronger trend before a Bullish or Bearish regime can begin.
Residual Bands
Wider bands require a larger deviation from the regression model before price is classified as Extended or Extreme.
Momentum Thresholds
Higher thresholds reduce the number of Bullish and Bearish momentum classifications.
Acceleration Threshold
A higher threshold reduces the number of Strengthening, Cooling, and Changing classifications.
Settings should be evaluated in relation to the selected instrument, timeframe, and market behavior.
The default values provide a general starting point and are not optimized for every market or timeframe.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠️ Real-Time Behavior and Limitations
The visible channel represents the latest calculated regression model.
Its boundaries, slope, and shaded areas update as new market data becomes available. The current channel should therefore not be interpreted as a fixed historical object that existed in exactly the same position in the past.
The rolling regression line stores, on each historical bar, the value calculated from the information available at that time.
The Following Values Can Change on an Open Bar:
• regression-model values;
• residual-channel position;
• Price Deviation;
• Current Momentum;
• Trend Acceleration;
• Market Phase.
Market Regime changes only after the bar has been confirmed.
The script does not use future data or lookahead calculations.
However, log regression is sensitive to the selected calculation window.
The model can be materially affected by:
• large price gaps;
• sudden price movements;
• structural market changes;
• low liquidity;
• changes in volatility;
• the selected instrument and timeframe.
The indicator is an analytical tool. Its measurements should be considered together with independent market analysis, risk management, and the characteristics of the selected trading instrument.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.