Linear Regression: Indicator Explained + How To Exploit It in a Trading System

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>

Linear regression is used in statistics to approximate the expected value between multiple variables and can be very useful in trading as well.

There is in fact an indicator based on linear regression that lets you approximate the price trend of an instrument with the smallest possible gap. Basically, this indicator is nothing else than a line that follows the movements of the market and can be used to generate signals that can be encoded in our trading systems.

In this video, you’ll learn how this indicator works and how to use it to create an effective trading strategy. 

Here are some of the things you'll learn by watching the video:

- what linear regression is and how to calculate it

- how to code the linear regression indicator in MultiCharts (and TradeStation)

- how to develop an effective strategy using this indicator

Enjoy the video!

Transcription

Hey everyone and welcome back!

One of the coaches at Unger Academy here and today we're going to talk about the theory and use of linear regression in trading.

Hints of Theory

Well, with a view to exploring all the possibilities that systematic trading offers us, today I want to illustrate linear regression. In statistics, linear regression is used to analyze a data set and study its dependence on the mean. Here is an example of how the indicator is plotted on MultiCharts. 

Now we’ll review some notions of statistics. Don't worry if some of the formulas are incomprehensible at first. This is more than normal. It isn’t important to be able to demonstrate the theory in order to trade, but it’s necessary to understand how to use the indicator. I’m sure that at the end of this video everything will be clearer.

Basically, linear regression solves the least squares equation and then finds common points where the regression line should pass. Given two theoretical values, X and Y, we can calculate the regression line using the following formula. Where A will be the intercept, namely, the amount of Y when X is equal to 0, and B is called the "angular coefficient" or "regression coefficient", namely, it will be how much Y varies on average as X increases.

To calculate the values of A and B, we use the method of least squares that, as we said before, minimize the sum of squares between the data points and the regression line. The data points could be for example the last 10 market closes, and the regression line will be the one that will identify more points with less gap.

So, all we will have to do is solve this equation, which is the rewritten least squares equation. To do this we must calculate the first derivatives with respect to A and B and equal them to 0, and then place them inside a system that, when solved, gives us the values for A and B.

We see that B equals "n" multiplied by the covariance of X and Y divided by the deviance of X, while A equals the mean of Y minus B times the mean of X. 

Then the regression line, which is Y = A + B * X will calculate the intercept called "A" and then multiply the value of X by the coefficient called "B".

We therefore see that the value of B, also called, if you remember, the regression coefficient, decides the slope of the line. As a matter of fact, if B is greater than zero, we’ll have a line with a positive slope, as for example in this case. If B is less than zero, we’ll have a line with a negative slope. While if B is equal to zero, we’ll have a perfectly horizontal line. 

Further Clarification

So, in conclusion, we can see that the regression line identifies the common points of the variables with the smallest possible gap and has a slope in relation to the angular coefficient of the line, which can range from negative infinity to positive infinity. 

If you remember in the first slide, we saw the indicator plotted on MultiCharts. Here is the same linear regression indicator calculated over the last 9 periods with daily bars. But as you can see, this is a curve rather than a straight line. So why do we see a curve graphed on MultiCharts rather than a straight line? Are we doing something wrong? Well, no, we're not.

We see a curve plotting the linear regression indicator because in MultiCharts only the last value of the linear regression is graphed. In fact, by overlapping the linear regression indicator as a straight line, you’ll see that only the last point, the one which will be useful for trading, is common to both indicators. This very point right here.

The linear regression line updates at each bar, thus configuring different points that, if joined, will identify a curve resulting from the translation of all the lines that update from bar to bar.

We see in this slide an example of this mechanism, which is a bit more complex but helpful in understanding how linear regression moves point by point. You see, if we were to go and join all these various data points generated by the regression lines in the past, there would be a sort of continuous line, which is what we see today on MultiCharts.

Strategy Script

Now, after this theoretical analysis, which for some of you might have been a bit boring, but necessary, let's test the efficacy of this indicator on a portfolio of futures. As you can see, we're now in the Power Language Editor of MultiCharts, the software used to design and program our strategies and indicators, functions and so on.

In this case we created a signal, thus a strategy that will buy and sell on the market. We’ve inserted as usual some inputs, which you can see here. The first three (Price, Periods and Displace) are simply the inputs that this function needs to calculate the linear regression, so it will calculate the close of the last "N" periods. In this case we have left the default 9, with a Displace of zero. Simply, the Displace is used to translate the value of the indicator forward or backward by X bars. Now, we’ll leave it at zero because we want to see the "basic" linear regression.

And then two other inputs, stop loss and target, which will then be (we'll see it below) calculated on the ATR, which is the only variable I created, simply by calling this function of the Average True Range calculated at 5 periods. 

As said, here we’ll have the entry conditions. So, if we have a close above the linear regression point, we'll basically buy at the next bar to market, in continuation of trend. And vice versa, when the close will break down the level of the linear regression, then we’ll sell at the next bar.

As said the stop loss is calculated on the ATR because we’re going to test this type of strategy on a very varied futures portfolio which includes very different markets. Consequently, in order to standardize, in some way, the results we’ll use a stop loss and a profit target based on the ATR.

So now let’s move on to the MultiCharts Portfolio Trader. As you can see we have already uploaded a handful of futures of different types. This is why we’ll use a uniform time zone on all of them, thus the local time zone. Otherwise, you wouldn’t be able to mix instruments with different time zones using this software, the MultiCharts Portfolio Trader.

Let's now apply our strategy. I‘ve already optimized the main values, such as the periods which have been set to 40. We’re on daily bars and thus there are 40 trading days. Displace is zero. And the stop loss and take profit were set at the same level at twice the five-day daily ATR.

The Results

Let's now see how this strategy works on this portfolio of futures. Here are the results. Obviously, this is a strategy that we’ve already optimized, because, as previously said, we’ve chosen the best values. But we definitely see that on this portfolio the first results of the strategy are certainly interesting.

Let's go look at the resulting average trade. We are at about $100 in 7,879 trades. The short side loses while the long side gains. This may be because in this portfolio we included both equity indexes and bonds, which as you know have a strong bullish bias. Obviously, this is something that doesn't really help with trading the short side. However, though, overall, we can be satisfied.

Linear regression could be one of the many indicators we can use to build our strategies. We could also modify it by adding some bands calculated as the standard deviation of the linear regression and enter on a cleaner breakout, you know, as if they were Bollinger Bands.

So you see, really the possibilities are clearly endless. In our opinion, this is surely a good starting point. So why not give it a try?

We really hope that you have found the video helpful.

Let us know your thoughts in the comments below. Also, don't forget to leave us a Like, if of course you liked the video, subscribe to our channel and click on the notification bell to stay updated on the release of all of our new videos coming soon.

Below you’ll also find a link to a completely free webinar by the 4-time world trading champion Andrea Unger, who will explain step-by-step how to become successful in the financial markets.

Thanks so much for watching this video. Will see you soon! Bye-bye!

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

BOOK YOUR FREE STRATEGY SESSION NOW >>
Andrea Unger

Andrea Unger

Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.

I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems. 

In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.

Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.

Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.