OPEN-SOURCE SCRIPT
Trend Context

Trend Context is a market-regime indicator designed to help traders identify whether the current market environment is predominantly bullish, bearish, or neutral.
Rather than relying on a single moving-average crossover or momentum oscillator, the indicator combines several normalized market characteristics into one composite regime model:
• Price location relative to an adaptive baseline
• Direction and strength of the baseline slope
• Recent price momentum
• Position within the recent trading range
• Directional efficiency
• Current volatility relative to its historical average
The objective is not to predict the next candle. Trend Context is designed to provide structured directional context that can be used as a market filter, confirmation layer, or visual framework for an existing trading strategy.
✦ Core Concept
Not every movement above or below a moving average represents a meaningful trend.
Markets frequently move in an irregular, inefficient manner, producing price fluctuations without sustained directional progress. Trend Context evaluates both the direction of price movement and the quality of that movement before confirming a new market regime.
A directional state is activated only when several independent components support the same conclusion.
This helps reduce unnecessary regime changes caused by isolated candles or short-term market noise.
⚙️ How the Indicator Works
1. Directional Efficiency
The indicator first measures how efficiently price has moved over the selected lookback period.
Directional efficiency compares:
• The absolute distance between the current price and the price at the beginning of the measurement period
• The sum of all individual price changes during that same period
A value closer to 1 indicates a relatively direct movement.
A value closer to 0 indicates that price has moved back and forth without making meaningful directional progress.
Directional efficiency influences both the adaptive baseline and the confidence applied to the final regime score.
2. Adaptive Baseline
Trend Context calculates a fast exponential moving average and a slow exponential moving average.
The directional-efficiency value determines how these two averages are blended:
• During efficient directional movement, the baseline shifts closer to the faster average
• During noisy or inefficient movement, the baseline remains closer to the slower average
The blended value is then smoothed again to reduce unnecessary fluctuations.
This creates a baseline that can respond more quickly during directional conditions while remaining more stable during irregular or sideways markets.
3. Volatility-Adjusted Context Zone
The indicator creates an upper and lower context boundary around the adaptive baseline.
The width of this zone is based on Average True Range, or ATR. It is also adjusted by comparing current ATR with its average value over the selected volatility lookback period.
This adjustment is bounded to prevent unusually quiet or volatile conditions from producing excessively narrow or wide zones.
As a result:
• The context zone expands as volatility increases
• The context zone contracts as volatility decreases
The active boundary provides a visual representation of the current directional context. It should not be interpreted as guaranteed support or resistance.
📊 Composite Regime Score
The market regime is calculated using four normalized components.
Price Location
Measures how far price is positioned above or below the adaptive baseline.
The distance is normalized by ATR so that the calculation remains comparable across instruments with different prices and volatility levels.
Baseline Slope
Measures whether the adaptive baseline is rising or falling.
The slope is normalized using ATR and the selected slope period.
Momentum
Measures the change in price over the selected momentum period.
This component is also normalized by ATR.
Range Position
Measures where the current price is located within its recent high-to-low range.
A position near the upper part of the range supports a bullish regime, while a position near the lower part supports a bearish regime.
The four components are combined using configurable weights. The resulting value is then adjusted by directional efficiency.
The final regime score is constrained between -1 and +1:
• Positive values support a bullish regime
• Negative values support a bearish regime
• Values close to zero indicate an uncertain or neutral environment
◇ Confirmed Three-State Regime Model
Trend Context uses three market states:
UP
NEUTRAL
DOWN
UP Regime
An UP regime requires:
• The composite score to exceed the activation threshold
• Price to remain above the adaptive baseline
• Directional efficiency to meet the minimum requirement
• The conditions to remain valid for the selected number of confirmation bars
DOWN Regime
A DOWN regime requires:
• The composite score to move below the negative activation threshold
• Price to remain below the adaptive baseline
• Directional efficiency to meet the minimum requirement
• The conditions to remain valid for the selected number of confirmation bars
Neutral Regime
The indicator returns to a neutral state when the absolute regime score remains inside the defined neutral zone for the required number of bars.
Regime transitions and UP/DOWN labels are confirmed only after the candle closes.
This confirmation process is intended to reduce rapid state changes caused by individual candles.
🎯 Visual Interpretation
UP Context
During an UP regime:
• The lower context boundary is displayed
• The area between price and the lower boundary is highlighted with the selected UP color
• An optional UP label appears when the regime changes into the bullish state
The active lower boundary represents bullish market context. It is not an automatic entry level or guaranteed support zone.
DOWN Context
During a DOWN regime:
• The upper context boundary is displayed
• The area between price and the upper boundary is highlighted with the selected DOWN color
• An optional DOWN label appears when the regime changes into the bearish state
The active upper boundary represents bearish market context. It is not an automatic entry level or guaranteed resistance zone.
Neutral Context
During a neutral regime, neither directional ribbon is active.
This means that the combined calculations do not currently provide sufficient evidence for a confirmed bullish or bearish market state.
How to Use Trend Context
Directional Filter
The indicator can be used to filter trades according to the active market regime.
For example:
• Consider bullish setups while the indicator is in the UP state
• Consider bearish setups while the indicator is in the DOWN state
• Reduce directional exposure or wait for additional confirmation during neutral conditions
Trend Context is not a complete entry system. Entry timing, stop-loss placement, position sizing, and profit targets must be defined separately.
Pullback Context
The active boundary and ribbon can help traders assess pullbacks within the current directional regime.
A return toward the active context zone is not an automatic trade signal.
It may be combined with:
• Market structure
• Support and resistance
• Candlestick confirmation
• Volume analysis
• A separate execution model
Higher-Timeframe Context
Trend Context can also be applied to a higher timeframe to identify the broader market regime while entries are managed on a lower timeframe.
Because market behaviour differs between instruments and timeframes, settings should be tested on the specific market being traded.
⚙️ Main Settings
Adaptive Baseline
Price Source
Selects the price series used in the calculations.
Fast Smoothing Length
Controls the faster exponential moving average.
Slow Smoothing Length
Controls the slower exponential moving average.
Efficiency Measurement Length
Defines the period used to measure directional efficiency.
Final Baseline Smoothing
Controls the final smoothing applied to the adaptive baseline.
Regime Model
Volatility Length
Sets the ATR calculation period.
Volatility Context Length
Defines the period used to compare current ATR with its historical average.
Baseline Slope Length
Controls the period used to calculate the baseline slope.
Momentum Length
Defines the period used for the momentum component.
Range Position Length
Sets the recent high-to-low range used in the range-position calculation.
Context Zone Multiplier
Controls the width of the volatility-adjusted context zone.
Regime Activation Threshold
Determines how strong the composite score must be before a directional regime can be activated.
A higher value produces fewer and more selective regime changes. A lower value increases sensitivity.
Neutral Zone Threshold
Defines how close the composite score must be to zero before the market can return to a neutral state.
Minimum Directional Efficiency
Prevents directional states from being activated when price movement is considered too inefficient.
Direction Confirmation Bars
Defines how many consecutive confirmed bars are required before an UP or DOWN state is activated.
Neutral Confirmation Bars
Defines how many consecutive bars inside the neutral zone are required before the indicator returns to a neutral state.
Component Weights
The indicator allows users to adjust the relative contribution of:
• Price location
• Baseline slope
• Momentum
• Range position
The total weight is normalized automatically, so the settings represent the relative importance of each component.
Visual Settings
Users can independently control:
• UP, DOWN, and neutral colors
• Trend ribbon visibility
• Active boundary visibility
• Adaptive baseline visibility
• Baseline visibility during neutral conditions
• Price-bar coloring
• UP and DOWN transition labels
🔔 Alerts
Alert conditions are available for:
• A confirmed transition into the UP regime
• A confirmed transition into the DOWN regime
These alerts indicate a change in market context. They should not be interpreted as automatic buy or sell orders.
Important Limitations
Trend Context is a reactive indicator based on historical price and volatility data. It does not predict future prices.
Like all trend and market-regime tools, it may react with a delay after sudden reversals and may produce repeated state changes during sideways or unstable conditions.
Increasing the activation threshold, minimum directional efficiency, or confirmation-bar settings may reduce sensitivity, but it will also delay regime transitions.
Decreasing these values will make the indicator respond faster, but may increase the number of short-lived or false regime changes.
Trend Context should be used together with independent risk management, position sizing, and trade validation.
This indicator is provided for analytical and educational purposes and does not constitute financial advice.
Rather than relying on a single moving-average crossover or momentum oscillator, the indicator combines several normalized market characteristics into one composite regime model:
• Price location relative to an adaptive baseline
• Direction and strength of the baseline slope
• Recent price momentum
• Position within the recent trading range
• Directional efficiency
• Current volatility relative to its historical average
The objective is not to predict the next candle. Trend Context is designed to provide structured directional context that can be used as a market filter, confirmation layer, or visual framework for an existing trading strategy.
✦ Core Concept
Not every movement above or below a moving average represents a meaningful trend.
Markets frequently move in an irregular, inefficient manner, producing price fluctuations without sustained directional progress. Trend Context evaluates both the direction of price movement and the quality of that movement before confirming a new market regime.
A directional state is activated only when several independent components support the same conclusion.
This helps reduce unnecessary regime changes caused by isolated candles or short-term market noise.
⚙️ How the Indicator Works
1. Directional Efficiency
The indicator first measures how efficiently price has moved over the selected lookback period.
Directional efficiency compares:
• The absolute distance between the current price and the price at the beginning of the measurement period
• The sum of all individual price changes during that same period
A value closer to 1 indicates a relatively direct movement.
A value closer to 0 indicates that price has moved back and forth without making meaningful directional progress.
Directional efficiency influences both the adaptive baseline and the confidence applied to the final regime score.
2. Adaptive Baseline
Trend Context calculates a fast exponential moving average and a slow exponential moving average.
The directional-efficiency value determines how these two averages are blended:
• During efficient directional movement, the baseline shifts closer to the faster average
• During noisy or inefficient movement, the baseline remains closer to the slower average
The blended value is then smoothed again to reduce unnecessary fluctuations.
This creates a baseline that can respond more quickly during directional conditions while remaining more stable during irregular or sideways markets.
3. Volatility-Adjusted Context Zone
The indicator creates an upper and lower context boundary around the adaptive baseline.
The width of this zone is based on Average True Range, or ATR. It is also adjusted by comparing current ATR with its average value over the selected volatility lookback period.
This adjustment is bounded to prevent unusually quiet or volatile conditions from producing excessively narrow or wide zones.
As a result:
• The context zone expands as volatility increases
• The context zone contracts as volatility decreases
The active boundary provides a visual representation of the current directional context. It should not be interpreted as guaranteed support or resistance.
📊 Composite Regime Score
The market regime is calculated using four normalized components.
Price Location
Measures how far price is positioned above or below the adaptive baseline.
The distance is normalized by ATR so that the calculation remains comparable across instruments with different prices and volatility levels.
Baseline Slope
Measures whether the adaptive baseline is rising or falling.
The slope is normalized using ATR and the selected slope period.
Momentum
Measures the change in price over the selected momentum period.
This component is also normalized by ATR.
Range Position
Measures where the current price is located within its recent high-to-low range.
A position near the upper part of the range supports a bullish regime, while a position near the lower part supports a bearish regime.
The four components are combined using configurable weights. The resulting value is then adjusted by directional efficiency.
The final regime score is constrained between -1 and +1:
• Positive values support a bullish regime
• Negative values support a bearish regime
• Values close to zero indicate an uncertain or neutral environment
◇ Confirmed Three-State Regime Model
Trend Context uses three market states:
UP
NEUTRAL
DOWN
UP Regime
An UP regime requires:
• The composite score to exceed the activation threshold
• Price to remain above the adaptive baseline
• Directional efficiency to meet the minimum requirement
• The conditions to remain valid for the selected number of confirmation bars
DOWN Regime
A DOWN regime requires:
• The composite score to move below the negative activation threshold
• Price to remain below the adaptive baseline
• Directional efficiency to meet the minimum requirement
• The conditions to remain valid for the selected number of confirmation bars
Neutral Regime
The indicator returns to a neutral state when the absolute regime score remains inside the defined neutral zone for the required number of bars.
Regime transitions and UP/DOWN labels are confirmed only after the candle closes.
This confirmation process is intended to reduce rapid state changes caused by individual candles.
🎯 Visual Interpretation
UP Context
During an UP regime:
• The lower context boundary is displayed
• The area between price and the lower boundary is highlighted with the selected UP color
• An optional UP label appears when the regime changes into the bullish state
The active lower boundary represents bullish market context. It is not an automatic entry level or guaranteed support zone.
DOWN Context
During a DOWN regime:
• The upper context boundary is displayed
• The area between price and the upper boundary is highlighted with the selected DOWN color
• An optional DOWN label appears when the regime changes into the bearish state
The active upper boundary represents bearish market context. It is not an automatic entry level or guaranteed resistance zone.
Neutral Context
During a neutral regime, neither directional ribbon is active.
This means that the combined calculations do not currently provide sufficient evidence for a confirmed bullish or bearish market state.
How to Use Trend Context
Directional Filter
The indicator can be used to filter trades according to the active market regime.
For example:
• Consider bullish setups while the indicator is in the UP state
• Consider bearish setups while the indicator is in the DOWN state
• Reduce directional exposure or wait for additional confirmation during neutral conditions
Trend Context is not a complete entry system. Entry timing, stop-loss placement, position sizing, and profit targets must be defined separately.
Pullback Context
The active boundary and ribbon can help traders assess pullbacks within the current directional regime.
A return toward the active context zone is not an automatic trade signal.
It may be combined with:
• Market structure
• Support and resistance
• Candlestick confirmation
• Volume analysis
• A separate execution model
Higher-Timeframe Context
Trend Context can also be applied to a higher timeframe to identify the broader market regime while entries are managed on a lower timeframe.
Because market behaviour differs between instruments and timeframes, settings should be tested on the specific market being traded.
⚙️ Main Settings
Adaptive Baseline
Price Source
Selects the price series used in the calculations.
Fast Smoothing Length
Controls the faster exponential moving average.
Slow Smoothing Length
Controls the slower exponential moving average.
Efficiency Measurement Length
Defines the period used to measure directional efficiency.
Final Baseline Smoothing
Controls the final smoothing applied to the adaptive baseline.
Regime Model
Volatility Length
Sets the ATR calculation period.
Volatility Context Length
Defines the period used to compare current ATR with its historical average.
Baseline Slope Length
Controls the period used to calculate the baseline slope.
Momentum Length
Defines the period used for the momentum component.
Range Position Length
Sets the recent high-to-low range used in the range-position calculation.
Context Zone Multiplier
Controls the width of the volatility-adjusted context zone.
Regime Activation Threshold
Determines how strong the composite score must be before a directional regime can be activated.
A higher value produces fewer and more selective regime changes. A lower value increases sensitivity.
Neutral Zone Threshold
Defines how close the composite score must be to zero before the market can return to a neutral state.
Minimum Directional Efficiency
Prevents directional states from being activated when price movement is considered too inefficient.
Direction Confirmation Bars
Defines how many consecutive confirmed bars are required before an UP or DOWN state is activated.
Neutral Confirmation Bars
Defines how many consecutive bars inside the neutral zone are required before the indicator returns to a neutral state.
Component Weights
The indicator allows users to adjust the relative contribution of:
• Price location
• Baseline slope
• Momentum
• Range position
The total weight is normalized automatically, so the settings represent the relative importance of each component.
Visual Settings
Users can independently control:
• UP, DOWN, and neutral colors
• Trend ribbon visibility
• Active boundary visibility
• Adaptive baseline visibility
• Baseline visibility during neutral conditions
• Price-bar coloring
• UP and DOWN transition labels
🔔 Alerts
Alert conditions are available for:
• A confirmed transition into the UP regime
• A confirmed transition into the DOWN regime
These alerts indicate a change in market context. They should not be interpreted as automatic buy or sell orders.
Important Limitations
Trend Context is a reactive indicator based on historical price and volatility data. It does not predict future prices.
Like all trend and market-regime tools, it may react with a delay after sudden reversals and may produce repeated state changes during sideways or unstable conditions.
Increasing the activation threshold, minimum directional efficiency, or confirmation-bar settings may reduce sensitivity, but it will also delay regime transitions.
Decreasing these values will make the indicator respond faster, but may increase the number of short-lived or false regime changes.
Trend Context should be used together with independent risk management, position sizing, and trade validation.
This indicator is provided for analytical and educational purposes and does not constitute financial advice.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Trading tools by MarketStructureLab. Professional indicators, algorithms & market structure logic. Get access: marketstructurelab.com
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Trading tools by MarketStructureLab. Professional indicators, algorithms & market structure logic. Get access: marketstructurelab.com
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.