Pine Script

Adding a dynamic filter to Pine Script strategies.

Adding a dynamic filter to Pine Script strategies.

Quite often your strategies in TradingView will be complicated and will have multiple conditions at once. You might want to check if certain conditions work or not for your strategy. It’s a good idea to create a checkbox in your parameter that switch on/off certain filters. In this article, I’ll show you a simple example of how you can do that in PineScript. If you want to see a more complicated example you can check my advanced Pine Script course.

Let’s start with a simple MA Crossover strategy:

//@version=5
strategy("MA Strategy", overlay=true)

// Calcuations
ma_fast = ta.sma(close, 14)
ma_slow = ta.sma(close, 28)

longCondition  = ta.crossover(ma_fast, ma_slow)
shortCondition = ta.crossunder(ma_fast, ma_slow)

// Strategy
if (longCondition)
    strategy.entry("Long", strategy.long)

if (shortCondition)
    strategy.entry("Short", strategy.short)

// Plot 
plot(ma_fast, color = color.red, linewidth = 2)
plot(ma_slow, color = color.blue, linewidth = 2)

Let’s assume that we want to add a MACD indicator to this strategy as a filter and we want the possibility to enable/disable it without changing the code. So let’s start by adding an input that will allow to switch in on/off:

// Inputs
use_macd_filter = input.bool(false)

Next let’s compute the MACD that will work as a filter for us:

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

Next let’s compute 2 variables that will work as long and short signals. Long filter will be when macd is above the signal line, opposite position for short filter:

long_filter  = macdLine > signalLine
short_filter = macdLine < signalLine

Next we have to edit out long/short conditions to include logic for the filters. To do that I need to write my code following way:

  • When checkbox checked condition should we counting filter
  • When checkbox is uncheched then filter should be ingnored

To have this issue I’ll use a pretty weird from the first view condition, but if you’ll look closely you’ll unferstand what I’m doing:

longCondition  = ta.crossover(ma_fast, ma_slow)  and (not use_macd_filter or long_filter)
shortCondition = ta.crossunder(ma_fast, ma_slow) and (not use_macd_filter or short_filter)

So to have this result I use condition like: (not use_macd_filter or long_filter). So when use_macd_filter is false, “not use_macd_filter” will be true so entire condition will be true, and because I join it with “and” to the signal it won’t make any difference. If condition is true, “not use_macd_filter” will be false so co result of the entire condition will be equal to the filter value and it will be counted. From the begining it might look complicated for you, but in reality it is very simple.

If you want you can also add a backround on your chart that will represent your filter:

bgcolor(long_filter ? color.new(color.green, 80) : short_filter ? color.new(color.red, 80) : na)

As you can see it’s a pretty simple trick, but it can help you to backtest your strategies better without chaning code too much. Off course you can create many filters and combine them together.

Here is the entire code of resulting straegy:

//@version=5
strategy("MA Strategy With MACD Filter", overlay=true)

// Inputs
use_macd_filter = input.bool(false)

// Calcuations
ma_fast = ta.sma(close, 14)
ma_slow = ta.sma(close, 28)

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

// Signals
long_filter  = macdLine > signalLine
short_filter = macdLine < signalLine

longCondition  = ta.crossover(ma_fast, ma_slow)  and (not use_macd_filter or long_filter)
shortCondition = ta.crossunder(ma_fast, ma_slow) and (not use_macd_filter or short_filter)

// Strategy
if (longCondition)
    strategy.entry("Long", strategy.long)

if (shortCondition)
    strategy.entry("Short", strategy.short)

// Plot 
plot(ma_fast, color = color.red, linewidth = 2)
plot(ma_slow, color = color.blue, linewidth = 2)

bgcolor(long_filter ? color.new(color.green, 80) : short_filter ? color.new(color.red, 80) : na)

Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news