How To Create A Regime Filter
This lesson demonstrates how you can use Pine Script to create a regime filter. A regime filter is an indicator that analyzes a major index or market leading asset class, and tells you when the overall market is "healthy" or "unhealthy".
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("Regime Filter") // Get user input res = input.timeframe(title="Timeframe", defval="D") len = input.int(title="EMA Length", defval=20) market = input.symbol(title="Market", defval="NASDAQ:NDX") // Define custom security function (that doesn't repaint) f_sec(_market, _res, _exp) => request.security(_market, _res, _exp[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] // Get EMA value ema = ta.ema(close, len) emaValue = f_sec(market, res, ema) // Check if price is above or below EMA filter marketPrice = f_sec(market, res, close) regimeFilter = marketPrice > emaValue or marketPrice[1] > emaValue[1] // Change background color bgcolor(regimeFilter ? color.green : color.red)