System Trader Success
Share It!

Building A Better Trend Filter

In this article I will create a trend filter (also known as market mode filter or regime filter) that is adaptable to volatility and utilizes some of the basic principles of hysteresis to reduce false signals (whipsaws). As you may know, I often will use the 200-period simple moving average (200-SMA) to determine when a market is within a bull or bear mode on a daily chart. When price closes above our 200-SMA we are in a bull market. Likewise, when price is below our 200-SMA we are in a bear market. Naturally, such rules will create some false signals. By the end of this article you will have a market mode filter that can be used in your system development that may produce better results than a standard 200-SMA filter. To build our better market trend filter we will use the following concepts:

  • Hysteresis
  • Price proxy

Hysteresis Basics

When building trading systems many of the decisions have a binary outcome. For example, the market is bearish or bullish. You take the trade or you don’t. Introducing a “gray area” is not always considered. In this article I’m going to introduce a concept called Hysteresis and how it can be applied to our trading. Hysteresis was used in a previous article on reducing whipsaws within a moving average crossover trading system. While the word Hysteresis was not used specifically in that article, it was a good example.

The common analogy to help understand the concept of hysteresis is to imagine how a thermostat works. Let’s say we are living in a cool weather climate and we are using a thermostat to keep the temperature of a room at 70 degrees F (critical threshold). When the temperature falls below our critical threshold the heaters turn on and begin blowing warm air into the room. Taking this literally as soon as the temperature moves to 69.9 our heater kicks on and begins blowing warm air into the room driving the temperature up. Once the temperature reaches 70.0 our heaters turn off. In a short time the room begins to cool and our heaters must turn on again. What we have is a system that is constantly turning off and on to keep the temperature at 70 degrees. This is inefficient as it produces a lot of wear on the mechanical components and wastes fuel. As you might have guessed, hysteresis is a way to correct this issue. More in just a moment.

The purpose of this article is to improve our market mode filter. Below is the result of buying the S&P cash index when price closes above the 200-SMA and selling when price closes below the 200-SMA. This is similar to our thermostat example. Instead of turning on the furnace to heat a room we are going to open a new position when a critical threshold (200-SMA) is crossed. In order to keep things simple, there is no shorting. For all the examples in this article, we are starting with a $100,000 account and risking $1,000 for each trade. The number of shares is scaled based upon a 20-day ATR calculation. To account for slippage and commissions $30 is deducted for each round trip.

SMA_Line = Average( Close, 200 );
If ( Close > SMA_Line ) then Buy next bar at market;
If ( Close < SMA_Line ) then Sell next bar at market;

In the above image we can see we entered into the market six times before the trade moves significantly in our favor. The first five attempts were closed at a loss as price moved from bull territory back to bear territory. That’s five consecutive losing trades!

Trading Bands

Going back to our thermostat example, how do we fix the problem of the furnace turning on and tuning off so many times? How do we reduce the number of signals? Let’s create a zone around our ideal temperature of 70 degrees. This zone will turn on the heaters when the temperature reaches 69 degrees and turn off when the temperature reaches 71 degrees. Our ideal temperature is in the middle of a band with the upper band at 71 and the lower band at 69. The lower band is when we turn on the furnace and the upper band is when we turn off the furnace. The zone in the middle is our hysteresis.

In our thermostat example we are reducing “whipsaws” or false signals, by providing hysteresis around our ideal temperature of 70 degrees. Let’s use the concept of hysteresis to attempt to remove some of these false signals. But like our ideal temperature we want an upper band and a lower band to designate our “lines in the sand” where we take action. There are many ways to create these bands. For simplicity let’s create the bands from the price extremes for each bar. That is, for our upper band we will use the 200-SMA of the daily highs and for the lower band we will use the 200-SMA of the daily lows. This band floats around our ideal point which is the 200-SMA. Both the upper and lower bands vary based upon the recent past. In short, our system has memory and adjusts to expanding or contracting volatility. The EasyLanguage code for our new system look something like this:

SMA_Line = Average( Close, 200 );
UpperBand = Average( High, 200 );
LowerBand = Average( Low, 200 );
If ( Close crosses over UpperBand ) then Buy next bar at market;
If ( Close crosses under LowerBand ) then Sell next bar at market;

Below is a screen shot showing the effect of opening a new trade after the daily bar closes above the upper band. We have reduced our five consecutive losing trades down to two trades.

Here are the results with using our new bands as trigger points.

Looking at the performance table above we can see an improvement in nearly all aspects of the system’s key performance. Most notably, increased Net Profit, Profit Factor, Percent Winners  and Average Trade net Profit. We also reduced the number of trades and the number of consecutive losing trades. In the end we really end up with about the same amount of net profit but we accomplish this task with fewer, more profitable trades.

It’s interesting to note that our Expectancy Score falls even as we increase the Expectancy value from 1.55 to 1.71. This is due to the reduced number of trading opportunities.

Price Proxy

A price proxy is nothing more than using the result of a price-based indicator instead of price directly. This is often done to smooth price. There are many ways to smooth price. I won’t get into them here. Such a topic is great for another article. For now, we can smooth our daily price by using a fast period exponential moving average (EMA). Let’s pick a 5-day EMA (5-EMA). Each day we compute the 5-EMA and it’s this value that must be above or below our trigger thresholds. By using the EMA as a proxy for our price we are attempting to remove some of the noise of the daily price fluctuations.

Below is what the EasyLanguage code may look like.

SMA_Line = Average( Close, 200 );
UpperBand = Average( High, 200 );
LowerBand = Average( Low, 200 );
PriceProxy = XAverage( Close, 5 );

If ( PriceProxy crosses over UpperBand ) then Buy next bar at market;
If ( PriceProxy crosses under LowerBand ) then Sell next bar at market;

Below is an example of a trade entry. Notice the trade is opened when our price proxy (yellow line) crosses over the upper band. We have also reduced our losing trades to one.

Let’s see how this affects our performance.

Looking at the strategy performance table above we are making slightly less money. However, we are once again being more efficient with our trades by eliminating unprofitable trades. We have reduced the number of consecutive losing trades, increased consecutive winning trades and increase our percent winners to 50%. So, which strategy is better? It all depends on what you want or what you are comfortable with. Some people will wish to simply take as much money as possible. Others will wish to reduce the number of consecutive losing trades.

The above examples are designed to demonstrate the effect a “better” trend filter can have on a simple trading strategy. If we want to use this in a trading system it would be ideal to create a function from this code that would pass back if we are in a bear or bull trend. However, the programming aspect of such a task is really beyond the scope of this article. Nonetheless, below is a quick example of setting two boolean variables (in EasyLanguage) that could be used as trend flags:

BullMarket = PriceProxy > UpperBand;
BearMarket = PriceProxy <= LowerBand;

In this article we have created a dynamic trend filter that smooths price, adapts to market volatility and utilizes hysteresis principles. With just a few lines of code we can significantly reduced the number of false signals commonly associated with this style of trading strategy . This type of filter can be effective in building the trading systems for ETFs, futures and Forex.


TradeStation Strategy Code (ELD file)
TradeStation WorkSpace (TWS file)
Strategy Code (text file) 

About the Author Jeff Swanson

Jeff is the founder of System Trader Success - an inBox magazine dedicated to sharing great ideas and concepts from the world of automated trading systems. Read More Google

follow me on:

Leave a Comment:

Add Your Reply