Multi-Timeframe Trend Analysis in Pine Script
This lesson demonstrates how to reference multiple timeframes in a Market Regime filter to gauge long-term momentum and directional bias.
Check out my other free lessons!Source Code
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ZenAndTheArtOfTrading / www.PineScriptMastery.com // @version=5 indicator("Multi-Timeframe Regime Filter") // Get user input res1 = input.timeframe(title="Timeframe #1", defval="240") res2 = input.timeframe(title="Timeframe #2", defval="1D") res3 = input.timeframe(title="Timeframe #3", defval="1W") len = input.int(title="EMA Length", defval=20) market = input.symbol(title="Market", defval="NASDAQ:NDX") // Define custom security function f_sec(_market, _res, _exp) => request.security(_market, _res, _exp[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] // Get EMA values ema = ta.ema(close, len) emaValue1 = f_sec(market, res1, ema) emaValue2= f_sec(market, res2, ema) emaValue3 = f_sec(market, res3, ema) // Get price values marketPrice1 = f_sec(market, res1, close) marketPrice2 = f_sec(market, res2, close) marketPrice3 = f_sec(market, res3, close) // Check filters regimeFilter1 = marketPrice1 > emaValue1 or marketPrice1[1] > emaValue1[1] regimeFilter2 = marketPrice2 > emaValue2 or marketPrice2[1] > emaValue2[1] regimeFilter3 = marketPrice3 > emaValue3 or marketPrice3[1] > emaValue3[1] // Change background color #1 r1 = hline(0, color=color.black) r2 = hline(1, color=color.black) fill(r1, r2, regimeFilter1 ? color.green : color.red) var label r1label = na if barstate.islast label.delete(r1label[1]) r1label := label.new(bar_index, 0.5, "Timeframe #1", color=color.new(color.black,100), textcolor=color.white, style=label.style_label_left) // Change background color #1 r3 = hline(1, color=color.black) r4 = hline(2, color=color.black) fill(r3, r4, regimeFilter2 ? color.green : color.red) var label r2label = na if barstate.islast label.delete(r2label[1]) r2label := label.new(bar_index, 1.5, "Timeframe #2", color=color.new(color.black,100), textcolor=color.white, style=label.style_label_left) // Change background color #1 r5 = hline(2, color=color.black) r6 = hline(3, color=color.black) fill(r5, r6, regimeFilter3 ? color.green : color.red) var label r3label = na if barstate.islast label.delete(r3label[1]) r3label := label.new(bar_index, 2.5, "Timeframe #3", color=color.new(color.black,100), textcolor=color.white, style=label.style_label_left)