NexusFi: Find Your Edge


Home Menu

 





Building A Better Trend Filter


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one NW27 with 8 posts (11 thanks)
    2. looks_two Jeff65 with 6 posts (11 thanks)
    3. looks_3 bomberone1 with 4 posts (2 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
      Best Posters
    1. looks_one imPairsonator with 4 thanks per post
    2. looks_two Jeff65 with 1.8 thanks per post
    3. looks_3 NW27 with 1.4 thanks per post
    4. looks_4 bomberone1 with 0.5 thanks per post
    1. trending_up 16,528 views
    2. thumb_up 28 thanks given
    3. group 9 followers
    1. forum 21 posts
    2. attach_file 2 attachments




 
Search this Thread

Building A Better Trend Filter

  #1 (permalink)
Jeff65
Gurnee, IL
 
Posts: 46 since Apr 2010
Thanks Given: 17
Thanks Received: 97

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 produces 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.

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, $50 is deducted from each trade to account for both slippage and commissions.
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;



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;
Here are the results with using our new bands as trigger points.



Looking at the chart above we can see an improvement in all important aspects of the system’s key performance. Most notably, the Band Cross column shows a reduced number of trades and increased the accuracy of the system. This suggests we eliminated unprofitable trades. Just what we want to see. Below is an example of a trade entry example. Notice the trade is opened when our daily bar closes above the upper band. The thick blue line is our 200-SMA.

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 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 in our system. Let’s see how this effects our performance.
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;

Looking at the graph above we once again see a solid improvement in our system’s performance. We continue to reduce the number of losing trades. Our profit per trade has jumped from $7.15 to $23.17. That’s over a 300% increase. Below is an example of a trade entry example. Notice the trade is opened when our price proxy (yellow line) crosses over the upper band.


The above code is a simple trading system designed to show you the benefit of our “better” trend filter. 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. However, 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 by using a simple EMA as price proxy, it adapts to market volatility and utilizes hysteresis principles. With just a few lines of code we dramatically reduced the number of false signals thus, increasing the profitability of the trading system. This type of filter can be effective in building the trading system for ETFs, futures and forex on daily bars.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 NW27 
Newcastle, Australia
 
Experience: Intermediate
Platform: Multicharts 8 - Full Version
Broker: IB
Trading: SPI,FTSE100, 6E, 6A
Posts: 285 since Oct 2010
Thanks Given: 108
Thanks Received: 188


I thought I would throw in my two cents worth but in short fashion given im typing on a phone.
I agree with what you have done and have used this for many years.
However, to coin another traders term, it still doesn't keep you out of barbwire ie sideways action where you can get whipsawed.
A new trend filter i have come up with, is using two moving averages, say a 20ema and a 40ema.
Ie fast>slow for an up trend. Just like this you still get whip sawed, however if you also said (using forex as an example) that the two ma's had to be seperated by 0.0003, you now have a good trend filter that also keeps you out of the barbwire.
In multicharts i have created this is a study and it displays a horizontal line on the bottom of the chart. The line changes color, where green is a up trend, red is down trend and blue is barbwire (sideways).
If people are interested, i will up load it.

Neil.

Sent from my GT-I9100T using Tapatalk 2

Reply With Quote
  #4 (permalink)
 ehlaban 
Netherlands
 
Experience: Advanced
Platform: Ensign, Multicharts
Trading: SP500
Posts: 91 since Nov 2009
Thanks Given: 66
Thanks Received: 57

yes, i'm interested in your trend filter

Reply With Quote
  #5 (permalink)
 NW27 
Newcastle, Australia
 
Experience: Intermediate
Platform: Multicharts 8 - Full Version
Broker: IB
Trading: SPI,FTSE100, 6E, 6A
Posts: 285 since Oct 2010
Thanks Given: 108
Thanks Received: 188

Hi Jeff,

I enjoy your articles keep up the great work. I like your analytical mind and general approach.

I'm looking forward to the rest of this thread.

For those that are interested, here is a screen shot of the Trend ribbon. It is on the chart four times, each using the same MA periods but different MA's ie EMA, WMA, ZMA & TEMA (just for comparisons sake).

Also for comparison is the standard ADX at the bottom with RED lines at 20,25 & 30.

When you combine the color of the trend ribbon with the same color on the price action MA, you get good signals.

Neil.
DownLoad NWT Trend Ribbon

Attached Thumbnails
Click image for larger version

Name:	NWT Trend Ribbon Ver1.png
Views:	529
Size:	75.0 KB
ID:	72187  
Reply With Quote
Thanked by:
  #6 (permalink)
Jeff65
Gurnee, IL
 
Posts: 46 since Apr 2010
Thanks Given: 17
Thanks Received: 97


NW27 View Post
Hi Jeff,

I enjoy your articles keep up the great work. I like your analytical mind and general approach.

I'm looking forward to the rest of this thread.

For those that are interested, here is a screen shot of the Trend ribbon. It is on the chart four times, each using the same MA periods but different MA's ie EMA, WMA, ZMA & TEMA (just for comparisons sake).

Also for comparison is the standard ADX at the bottom with RED lines at 20,25 & 30.

When you combine the color of the trend ribbon with the same color on the price action MA, you get good signals.

Neil.
DownLoad NWT Trend Ribbon

Thanks. Glad you like the articles.

Thank you for providing the link to the Trend ribbon. I'll take a look at it.

Reply With Quote
  #7 (permalink)
Jeff65
Gurnee, IL
 
Posts: 46 since Apr 2010
Thanks Given: 17
Thanks Received: 97


Jeff65 View Post
Thanks. Glad you like the articles.

Thank you for providing the link to the Trend ribbon. I'll take a look at it.

Just realized I can't view the code. Must be an elite member. Oh well...

Reply With Quote
  #8 (permalink)
 NW27 
Newcastle, Australia
 
Experience: Intermediate
Platform: Multicharts 8 - Full Version
Broker: IB
Trading: SPI,FTSE100, 6E, 6A
Posts: 285 since Oct 2010
Thanks Given: 108
Thanks Received: 188


Jeff65 View Post
Just realized I can't view the code. Must be an elite member. Oh well...

I guess you will have to join
Tis only a small donation for the privilege of using a great forum.

Or I could send you the code.

Neil.

Reply With Quote
  #9 (permalink)
superbetz
Bremen + Germany
 
Posts: 1 since May 2012
Thanks Given: 0
Thanks Received: 0

Hi Neil,
I m new to the forum and would be interested in your NWT Trend Ribbon Indi. I intend to translate ist into Equilla, ie Tradesignals language :-).

Could you pls sent me the indicator, since I'm not an Elite Member yet.

Cheers - Iro

Reply With Quote
  #10 (permalink)
Jeff65
Gurnee, IL
 
Posts: 46 since Apr 2010
Thanks Given: 17
Thanks Received: 97



NW27 View Post
I guess you will have to join
Tis only a small donation for the privilege of using a great forum.

Or I could send you the code.

Neil.

This post almost slipped by me. It would be great if you could send me a copy of the code.

I probably should join. Another thing to look into. Item #2343 on the to-do list.

Thanks.

Reply With Quote




Last Updated on June 11, 2012


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts