Categories

# Using the Linear Regression Indicator and Momentum to Identify Trends and Counter-trends

A Step-by-Step Guide to Implementing the Linear Regression and Momentum System

The Linear Regression Indicator plots a line through the prices of a stock or commodity in an attempt to minimize the distance between the line and each individual point. The method used to accomplish this is called the “least squares” method. The indicator is based on the theory that prices are pulled back to the regression line after they stray above it or below it.

The Linear Regression Indicator can also be used to monitor the current trend. If it’s rising the trend is up, and if it’s falling the trend is down. For our Linear Regression and Momentum System, we’ll smooth the linear regression line with an exponential moving average so that the indicator does not switch back and forth between uptrends and downtrends too often.

Momentum, the second indicator in this system, compares the current bar’s closing price with the closing price a specified number of bars in the past. To calculate a 5-bar momentum line, for example, subtract the close of 5 bars ago from the current bar’s close. When the 5-bar Momentum Indicator is above its zero line and rising, the 5-bar price changes are positive and increasing — that is, the trend is bullish and accelerating. If the momentum line turns flat, it implies that the 5-bar price changes are about equal during the Momentum Indicator’s period of sideways movement. When the Momentum Indicator begins to decline from above zero, the market’s gains during the past 5 bars are less than the corresponding gains in the preceding bars — that is, the uptrend is decelerating.

When the 5-bar Momentum Indicator falls below its zero line, the current close is below the close 5 bars ago. As the downtrend gains bearish velocity, momentum accelerates downward from the zero line. An upturn of the indicator in negative territory means that the magnitude of 5-bar price declines is decreasing — that is, the downtrend is decelerating. Momentum is a leading indicator — it levels off while prices are still rising in an uptrend or falling in a downtrend, and it reverses its direction when the trend begins to slow. In this system, we’ll use momentum to identify countertrend declines in an uptrend and countertrend rallies in a downtrend. As mentioned previously, the trend will be determined by the direction of a smoothed linear regression line (i.e., an exponential moving average of linear regression).

For a buy setup, we’ll require the smoothed linear regression line to be rising and for momentum to be below zero but rising. For a sell setup, we’ll require the smoothed linear regression line to be falling and for momentum to be above zero but falling.

After a buy setup, we’ll calculate the distance between the high of the setup bar and the smoothed linear regression line. Then we’ll add 50% of that distance to the high of the setup bar to determine our long entry price.

After a sell setup, we’ll calculate the distance between the low of the setup bar and the smoothed linear regression line. Then we’ll subtract 50% of that distance from the low of the setup bar to determine our short entry price.

Once we’ve entered a long position, we’ll set our initial protective stop at the low of the setup bar minus 50% of the 10-bar average true range; once we’ve entered a short position, we’ll set our initial protective stop at the high of the setup bar plus 50% of the 10-bar average true range. As the market moves in our favor, we’ll trail a % risk trailing stop.

To exit when we’re in a long position, we’ll watch for the smoothed linear regression line to turn down. Then we’ll place our sell stop at the lowest low of the last 4 bars. When we’re in a short position, we’ll watch for the smoothed linear regression line to turn up. Then we’ll place our buy stop at the highest high of the last 4 bars.

In this system, we defined both long entries and short entries as well as exit orders. We also did some setup work to calculate the linear regression line, its smoothed average and momentum. The setup, entries and exits are described next:

Setup

• Calculate the linear regression line (using the closing prices and a length of 20) and its exponential average (using a length of 15).
• Calculate the momentum (using the closing prices and a length of 10).

Long Entries

• For a buy setup, we’ll require the smoothed linear regression line to be rising. In other words, its value on the current bar must be greater than its value on the previous bar. Also, momentum must be below zero but rising (greater on this bar than on the previous bar).
• After a buy setup, we’ll calculate the distance between the high of the setup bar and the smoothed linear regression line. Then we’ll add 50% of that distance to the high of the setup bar; this is our long entry price. The order remains active for 4 bars.

Short Entries

• For a sell setup, we’ll require the smoothed linear regression line to be falling. In other words, its value on the current bar must be less than its value on the previous bar. Also, momentum must be greater than zero but falling (less on this bar than on the previous bar).
• After a sell setup, we’ll calculate the distance between the low of the setup bar and the smoothed linear regression line. Then we’ll subtract 50% of that distance from the low of the setup bar; this is our short entry price. The order remains active for 4 bars.

Exit Orders

• Once we’ve entered a long position, we’ll set our initial protective stop at the low of the setup bar minus 50% of the 10-bar average true range.
• Once we’ve entered a short position, we’ll set our initial protective stop at the high of the setup bar plus 50% of the 10-bar average true range.
• To exit when we’re in a long position, we’ll watch for the smoothed linear regression line to turn down (the value on the current bar is less than it was on the previous bar). Then we’ll place a stop order at the lowest low of the last 4 bars.
• When we’re in a short position, we’ll watch for the smoothed linear regression line to turn up (the value on the current bar is greater than it was on the previous bar). Then we’ll place a stop order at the highest high of the last 4 bars.

### Designing & Formatting

This section presents the EasyLanguage instructions and formatting for the system, with the EasyLanguage instructions broken down and explained line by line.

Input

Following is the list of all the inputs we used in this system:

Setup

First, we calculate the exponential moving average of the linear regression. We use the LinearRegValue function to calculate the linear regression and then use the resulting value as an input for the XAverage function in order to determine the exponential average of the linear regression. We store the resulting value in the XLinReg variable.

We also calculate the momentum using the Momentum function and store the resulting value in the Mom variable.

We use two counters, CountL and CountS. We increment these with each bar and reset them each time a buy setup or sell setup occurs, respectively. This way, we can keep a count and use them to keep buy and sell orders active for 4 bars Next, we make the comparisons between the exponential moving averages and the momentum values. The results of these comparisons are used throughout the system. We compare the exponential moving average of the linear regression line to the same value one bar ago. We also compare the value of the momentum calculation to zero and to the same value one bar ago.

First, we check to see if the current value of the exponential average is greater than the value one bar ago. If it is, the variable Condition1 is set to True. Next, we check to see if the momentum value is less than zero and greater than the same value one bar ago. If it is, Condition2 is set to True. After that, we check to see if the exponential average is less than it was one bar ago. If it is, Condition3 is set to True. And finally, we check to see if the momentum value is greater than zero and less than the same value one bar ago. If it is, Condition4 is set to True.

Long Entries

Next, we check to see if Condition1 and Condition2 are true. If they are, and they became true either on the previous bar or more than 4 bars ago, then the high price is stored in the variable SetBarL and the CountL variable, which accumulates the number of bars since the setup, is set to 1.

If the number of bars since Condition1 and Condition2 became true is less than 4, then we place a buy order at a certain percentage above the value stored in SetBarL. We calculate the percentage by subtracting the value of the exponential moving average from the value in SetBarL and multiplying that by the specified percentage (default of .5). We add this to the value in SetBarL and that is our entry price. Notice that we specified a certain number of shares to buy using the NumUnits function. This function is described in detail in the section at the end of this chapter, titled, “Investing a Fixed Dollar Amount.”

Short Entries

Next, we check to see if Condition3 and Condition4 are true. If they are, and they became true either on the previous bar or more than 4 bars ago, then the low price is stored in the variable SetBarS and the CountS variable, which accumulates the number of bars since the setup, is set to 1.

If the number of bars since Condition3 and Condition4 became true is less than 4, then we place a sell order at a certain percentage below the value stored in SetBarS. We calculate the percentage by subtracting the value of the exponential moving average from the value in SetBarS and multiplying that by the specified percentage (default of .5). We subtract this from the value in SetBarS and that is our entry price.

Long & Short Exits

We will exit any long position at or below the value that results when you subtract a percentage (as specified by the input Pcnt) of the 10-bar average of the true range from the low of the buy setup bar.

Conversely, we will exit the short position at or above the value that results when you add a percentage (as specified by the input Pcnt) of the 10-bar average of the true range to the high of the sell setup bar.

Finally, we will place a trailing stop for long positions at the lowest low of the last four bars. Conversely, we will place a trailing stop to cover our short positions at the highest high of the last four bars.

### General System Format

When we apply a system to a chart, we normally use the options in the Format dialog box to format costs, stops, and properties. However, in this system we did not enter an amount for slippage and commission although those costs must certainly be taken into account before a system is traded.

Note: Remember that Commissions are calculated on a per contract/share basis. When you are trading stocks, you would enter the average commission you are charged divided by the number of shares the system is buying and selling.

### Testing & Improving

I want to be honest, I haven’t tested this system properly, I used some underlyings with different timeframes, I remind you that you can download the entire system from my Telegram channel, here are the results: