A trend filter is one of the ways to trade in the direction of the general trend, filtering out trades that contradict the trend, with the hope (tested during back-testing) that the trend filter filters out more losing trades than winning ones. We, system traders, often find or create an interesting system for trading in trends on a smaller timeframe, for example, on M30, and thanks to back-testing, we can see that the system can provide many opportunities for profit, following the trend, on this relatively short timeframe. However, visually using back-testing, we can also see that the system, although it ultimately brings profit, also opens a lot of unprofitable positions if they are opened against the general trend. Seeing this, we start looking for a trend filter or an alternative indicator, set a longer timeframe or period that can help the system determine a longer market trend, and open positions only in the direction of this larger trend. This helps us if we encode a trend filter with a true / false keyword, so we can easily test this filter or turn it off to see if it helps.
In order to get an idea of the process of creating and implementing a trend filter, let's analyze the design of a moving average trend filter. Using the example of this trend filter, you can easily use many alternative filters based on various indicators for your strategy, until you finally meet the necessary set of trend filters for testing all your trend strategies.
Trend filter moving average
Let's test the moving average filter. If its parameters are set correctly, we will open only long positions when the closing price is above the moving average, and only short positions when the closing price is below the moving average. It's quite simple.
Long positions: When the closing price is above the moving average.
Short positions: When the closing price is below the moving average.
Parameters
MAFilter Specify whether you want to enable the moving average filter. In this case, positions will be opened only in the direction of the moving average: only long positions when the closing prices are above the moving average, and only short positions when the closing prices are below it. The filter is disabled by default.
MAFilterRev Specify whether you want to enable the reverse moving average filter. In this case, positions will be opened exclusively in the opposite direction of the moving average: only long positions when the closing prices are below the moving average, and only short positions when the closing prices are above it. The filter is disabled by default. Although the reverse filter is not often used, it can be useful if during back-testing you find that the use of MAFilter, which is mentioned above, gives serious mistakes in your strategy. Therefore, to improve the situation, you can apply MAFilterRev.
MATime This is the timeframe of your moving average. By default, 0, which means the same timeframe as on the chart. You should test the filter first on the same timeframes, and then switch to longer timeframes, testing on each of them. For example, if your main strategy is designed for the M30 chart, then initially you should test your Moving Average with a default value of 0, i.e. on the M30 chart, then test it on the H1 timeframe, then on H4, and finally on D1.
MAPeriod This is the period of your moving average. By default, the value is set to 50, which is usually a longer period of the moving average to determine the direction of the trend. Another commonly used long period is 200. In order to see which of the longer MA periods can better determine the trend for a given currency pair on each of the timeframes you are testing, I would advise setting the optimal period between 50 and 200, with a step interval of 25 or 50.
MAMethod This is the method or method of the moving average, which by default is 1, the exponential moving average. Keep in mind the values specified in the method parameter: 0 = simple, 1 = exponential, 2 = smoothed, 3 = linearly weighted. I think that 1, or exponential, is the best type of moving average. Another good moving average that you should test is 0, or a simple moving average. Both moving average modes are very popular and effective.
MT4 code snippets
The code block is discussed below, in the external variables section.
The code block is discussed below in the call indicator section.
Example of using the MT4 code
Below is an example of how MAFilter is intertwined with the code of the moving average intersection strategy. Earlier, we discussed in detail how to build a strategy for crossing moving averages, so it should be identical. It can be assumed that adding a MAFilter to the moving average crossing strategy will be superfluous; however, in some cases this is allowed, since this filter can complement this strategy. For example, if the intersection of moving averages was taken as a signal for opening positions in relation to the M30 timeframe, then you can test it on the H4 timeframe in combination with the use of MAFilter 200. This can be beneficial in that we will open positions in the direction of trends on shorter timeframes and smaller periods, which, eventually, will be aligned with the trend on longer timeframes and larger periods.
Explanation
There are 5 blocks of code in the MT4 usage section mentioned above. In the first block of code, I create a boolean value for BuyCondition and a boolean value for SellCondition; by default, there are no values. I did not initially set values, because I want the boolean values being set to be "true". The second and third blocks of the code are set out to determine the conditions for buying and selling, based on the intersection of moving averages. When the fast moving average crosses the slower moving average from the bottom up, then BuyCondition = true; when the fast moving average crosses the slow moving average from the top down, then SellCondition = true. We have already repeated these conditions, studying the basics of the Expert Advisor-the intersection of moving averages. The only difference is that I put these conditions in the values of BuyCondition and SellCondition instead of the values of OpenBuy and OpenSell, which I refer to in the fourth and fifth blocks of the code.
These are the fourth and fifth blocks of code that perform the work of MAFilter. Each one starts with the "if ()" condition, which implies three conditions specified in parentheses. The first condition, BuyCondition or SellCondition, includes the logic of opening a position at the intersection of moving averages.
The second condition, the MAFilter condition, is a compound command in parentheses, separated by the || ("or") symbol. The first part of the statement indicates that if MAFilter is set to "false" (MAFilter = false), then MAFilter does not work. The second part of the statement (after the | | symbol) indicates that if the MAFilter is set to "true" (note that it should not be set to "true", since the value itself means "true"), then you should go to the MAFilter rule. By BuyCondition, the MAFilter rule implies that the Ask value must be greater ( > ) than the mafilter value. Note: The variable for mafilter (in lowercase) is defined in MT4 by a fragment placed in the indicator that calls the section.
The third condition, the MAFilterRev condition, is also a compound command in parentheses, separated by the || ("or") symbol. The first part of the statement indicates that if MAFilterRev is set to "false" (MAFilterRev = false), then MAFilterRev does not work. The second part of the statement (after the | | symbol) indicates that if MAFilterRev is set to "true", then you should go to the MAFilterRev rule. As you can see, the MAFilterRev rule is the opposite of the MAFilter rule. If the MAFilter rule determines that the Ask value should be greater ( > ) than the mafilter value, then MAFilterRev determines that the Ask value should be less ( < ) than the mafilter value.
If all three of the above conditions are met, then EA can initiate OpenBuy = true or OpenSell = true, which gives a signal to open a buy or sell position.
The MAFilter we discussed above is just one of the many possible trend filters that can be built and implemented. I chose MAFilter because it is probably one of the simplest and most effective filters for detecting the direction of a trend.
Other good indicators that can act as trend filters are variations of the moving average: JMA, Hull, NonLagMA, and Slope Directional Line. I use all four indicators as reliable trend filters. You can also experiment with such pulse oscillators as the RSI and the stochastic oscillator. Whichever indicator is used, in order to see which of them is the best filter for a larger trend, it should be tested on different periods and timeframes. The trend only matters in combination with the timeframe, and once you determine the timeframe, you should no longer fantasize about the concept of a trend.
It is not necessary to try to find or build super-fashionable, mathematical trend filters. Many traders are trying to further complicate the problem of determining the trend. In order to more accurately determine whether a trend is ascending or descending, they invent all kinds of fancy mathematical equations and methods based on back-testing of price behavior, and most of such attempts are meaningless. Often the simplest and most popular approaches to determining the trend are the best.
Not every moving average can help to benefit from the presence of a trend filter. Some Expert Advisors include trend indicators that work quite well, without additional trend filters. Other Expert Advisors try to predict trend reversals or are sensors of trend conditions, and can give a signal to enter the market against the trend.
Keep in mind that all filters prevent the occurrence of mostly unprofitable transactions, as we wish. It is usually better to test one filter, and try not to use several of them at once. Your strategy should be good enough to send signals independently, without the need for additional filters. A trend filter should be used in conjunction with a good strategy only as an additional tool that should be tested beforehand. If we find that the filter does not produce a large number of signals or even an equal number of winning trades, this is not a good filter with which to trade. The only acceptable filter would be a filter that could prevent a large percentage of unprofitable transactions during long-term back-testing for 5-10 previous years.