OPEN-SOURCE SCRIPT
Multi-Timeframe MA Forecast

Multi-Timeframe MA Forecast
This indicator combines two things that other MA tools don't: it plots a moving average from any timeframe onto your chart using proper multi-timeframe handling, and it projects that average forward under a simple assumption, that price holds at the current value while the calculation window rolls ahead. The result is a forward view of where the MA is headed if price goes nowhere, which is a different and useful question from where the MA has been.
Why forecast a moving average
An MA's slope isn't entirely driven by "recent price action" in the vague sense traders usually mean. At any given bar, the slope is set by one comparison: the price entering the average versus the price leaving it. A rising 50-day SMA can flatten or roll over not because anything new happened today, but because the price from 50 days ago, now dropping out of the window, was unusually high or low. That effect is knowable in advance since it depends on price action that already happened.
This matters because many systematic and algorithmic strategies key off MA slope and MA crossovers as regime filters. If you can see that a large gap is about to roll out of a 200-period window, you can anticipate a slope change before it shows up on the indicator itself, rather than reacting to it after the fact.
The forecast is not a price prediction. It answers a narrower, mechanical question: given the prices already in the pipeline, what does this average do next if price simply holds still.
Why MA slope matters for support and resistance
Slope changes the character of an MA:
- Price above a rising MA: the MA tends to act as support
- Price below a rising MA: the MA tends to act as a magnet, pulling price back up toward it
- Price below a falling MA: the MA tends to act as resistance
- Price above a falling MA: the MA tends to act as a magnet, pulling price back down toward it
A forecasted slope flip is an early signal that the MA's role relative to price may be about to change.
How the timeframe handling works
Set the Timeframe higher, lower, or equal to your chart's. The average is calculated on that timeframe using TradingView's multi-timeframe request functions, so the current bar always reflects the true live value of the higher-timeframe average, including the period still forming.
When the Timeframe is higher than the chart, history is drawn as a stairstep: once a higher-timeframe period closes, its final value is drawn back across the bars of that period. This is retroactive, the value shown mid-period was not knowable until that period actually closed, and it gives the clean, stepped look most MTF overlays use. When the Timeframe is the same as or lower than the chart, there's no period to wait for, so the line simply reflects the true value on every bar as it happens.
Either way, the forecast always starts from the current, fully up-to-date value of the average.
Forecast styling is separate from the MA's
The moving average uses its own Line Thickness and MA Line Style inputs, and the forecast has a matching, independently-set Forecast Line Style. These are exposed as inputs rather than through the native Style tab because the forecast is built from a drawing object rather than a plotted series, and drawing objects can't be styled from that tab. Giving the MA its own matching inputs, rather than mixing native Style-tab controls with input-based ones, keeps both fully and consistently customizable.
Supported MA types
SMA (simple), EMA (exponential), WMA (weighted), and HMA (Hull) are supported, each forecast using that type's own real recurrence rather than a shared approximation. EMA and SMA are the cheapest to compute; WMA and HMA require tracking the full calculation window at every forecast step.
Memory limitations on extreme timeframe ratios
Pulling multi-timeframe history through TradingView's request functions has a memory cost that scales with the ratio between the chart timeframe and MA timeframe, particularly when the MA timeframe is much lower than the chart's. In practice:
- EMA has no meaningful limit, since it doesn't require historical window data
- SMA can handle roughly double the timeframe ratio or MA length that WMA and HMA can, before hitting TradingView's memory ceiling
- Very extreme combinations, for example a 1-hour MA on a weekly chart with a long length, can hit a runtime memory error
If you see a memory error, try a coarser MA timeframe, a shorter MA length, switching to EMA or SMA, or lowering the Forecast % input, all of which reduce the amount of historical data the script needs to hold.
---
Every request.security() call in this script that uses barmerge.lookahead_on does so only on a history-offset expression, per TradingView's documented pattern for retrieving higher-timeframe data without lookahead bias. No un-offset higher-timeframe value is ever requested with lookahead_on.
This indicator combines two things that other MA tools don't: it plots a moving average from any timeframe onto your chart using proper multi-timeframe handling, and it projects that average forward under a simple assumption, that price holds at the current value while the calculation window rolls ahead. The result is a forward view of where the MA is headed if price goes nowhere, which is a different and useful question from where the MA has been.
Why forecast a moving average
An MA's slope isn't entirely driven by "recent price action" in the vague sense traders usually mean. At any given bar, the slope is set by one comparison: the price entering the average versus the price leaving it. A rising 50-day SMA can flatten or roll over not because anything new happened today, but because the price from 50 days ago, now dropping out of the window, was unusually high or low. That effect is knowable in advance since it depends on price action that already happened.
This matters because many systematic and algorithmic strategies key off MA slope and MA crossovers as regime filters. If you can see that a large gap is about to roll out of a 200-period window, you can anticipate a slope change before it shows up on the indicator itself, rather than reacting to it after the fact.
The forecast is not a price prediction. It answers a narrower, mechanical question: given the prices already in the pipeline, what does this average do next if price simply holds still.
Why MA slope matters for support and resistance
Slope changes the character of an MA:
- Price above a rising MA: the MA tends to act as support
- Price below a rising MA: the MA tends to act as a magnet, pulling price back up toward it
- Price below a falling MA: the MA tends to act as resistance
- Price above a falling MA: the MA tends to act as a magnet, pulling price back down toward it
A forecasted slope flip is an early signal that the MA's role relative to price may be about to change.
How the timeframe handling works
Set the Timeframe higher, lower, or equal to your chart's. The average is calculated on that timeframe using TradingView's multi-timeframe request functions, so the current bar always reflects the true live value of the higher-timeframe average, including the period still forming.
When the Timeframe is higher than the chart, history is drawn as a stairstep: once a higher-timeframe period closes, its final value is drawn back across the bars of that period. This is retroactive, the value shown mid-period was not knowable until that period actually closed, and it gives the clean, stepped look most MTF overlays use. When the Timeframe is the same as or lower than the chart, there's no period to wait for, so the line simply reflects the true value on every bar as it happens.
Either way, the forecast always starts from the current, fully up-to-date value of the average.
Forecast styling is separate from the MA's
The moving average uses its own Line Thickness and MA Line Style inputs, and the forecast has a matching, independently-set Forecast Line Style. These are exposed as inputs rather than through the native Style tab because the forecast is built from a drawing object rather than a plotted series, and drawing objects can't be styled from that tab. Giving the MA its own matching inputs, rather than mixing native Style-tab controls with input-based ones, keeps both fully and consistently customizable.
Supported MA types
SMA (simple), EMA (exponential), WMA (weighted), and HMA (Hull) are supported, each forecast using that type's own real recurrence rather than a shared approximation. EMA and SMA are the cheapest to compute; WMA and HMA require tracking the full calculation window at every forecast step.
Memory limitations on extreme timeframe ratios
Pulling multi-timeframe history through TradingView's request functions has a memory cost that scales with the ratio between the chart timeframe and MA timeframe, particularly when the MA timeframe is much lower than the chart's. In practice:
- EMA has no meaningful limit, since it doesn't require historical window data
- SMA can handle roughly double the timeframe ratio or MA length that WMA and HMA can, before hitting TradingView's memory ceiling
- Very extreme combinations, for example a 1-hour MA on a weekly chart with a long length, can hit a runtime memory error
If you see a memory error, try a coarser MA timeframe, a shorter MA length, switching to EMA or SMA, or lowering the Forecast % input, all of which reduce the amount of historical data the script needs to hold.
---
Every request.security() call in this script that uses barmerge.lookahead_on does so only on a history-offset expression, per TradingView's documented pattern for retrieving higher-timeframe data without lookahead bias. No un-offset higher-timeframe value is ever requested with lookahead_on.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
Technical analysis should be made as simple as possible, but no simpler. --Albert Einstein
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
Technical analysis should be made as simple as possible, but no simpler. --Albert Einstein
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.