Adding a trend filter into your existing strategy can be a good way to eliminate some bad trades and improve the average trade profit and/or reduce the drawdown. But how do you do this? Read on and find out 🙂
Dummy Strategy without Trend Filter
First of all, let us create a dummy strategy which goes long when the RSI is below 40 and exits 3 bars after the entry, without consideration of the current trend state of the market. If we apply this to a typical price series, the equity curve unsurprisingly looks ugly.
Why? Because a lot of losses have been generated during the down move. This is exactly what we want to avoid! So let`s start coding a trend filter.
How to code the Trend Filter
One way would be to implement an additional condition to your existing trading strategy. For example, the buy signal is only triggered, when the trend is up (using a moving average for example), otherwise the signal is ignored. Another, more flexible way is to code a separate module which can be inserted on top of any strategy in your chart. This saves a lot of time, because you don`t have to change the initial strategy code. Here is an example how this filter can be programmed using the SetTradingStatus function in Tradesignal.
- In the input section (lines 5-6) there are two parameters: one for setting SMA period and one switch to turned on/off the filter
- If the filter is activated
- then entry signals for a trading strategy are blocked (line 13)
- unless the market is in a bull mode (line 14)
- If the filter is deactivated, then all signals from a trading strategy are allowed (line 17)
- The function SetTradingStatus is used in line 19 and refers to the variable state from line 13,15 and 17
Applying the Trend Filter to our Dummy Strategy
And here is the result when this trend filter is applied to the same dummy strategy from above. As you can see, there are no trades in a down trend anymore, which improved the equity curve siginficantly.
Tradesignal® is a registered trademark of Tradesignal GmbH. Unauthorized use or misuse is strictly prohibited. Data by Thomson Reuters Eikon.