# Tutorial 50 | Creating a linear regression function

Welcome to tutorial 50 in this series of tutorials designed to help you learn TradeStation EasyLanguage. This tutorial was created using TradeStation 8.7 and builds upon the work done in tutorial 48 and tutorial 49. In this tutorial we create a function that calculates and draws a linear regression line starting a specified number of bars ago. It also calculates and returns the value of the slope.

The image on the right applies the function to the chart using the following syntax:

If LastBarOnChart then Value1 = _MarkplexLinReg( 5, 30, C );

Using LastBarOnChart means the creation of a new linear regression line every time a new bar is formed on the chart. Practically speaking, this is not very useful, but it demonstrates how to call the function. The three inputs are:

1. The number of bars ago that the end point of the linear regression line should be calculated and drawn,
2. The length of the linear regression line and
3. The value that the linear regression is being calculated, in this case the Close of the bar.

The function returns the slope of the line. In the above example, this value is stored in Value1. If you were to add the following statement to the calling program:

Print( D, T, ” Slope “, Value1:7:7 );

You would get something like the following in the EasyLanguage Output Bar;

1100824.001356.00 Slope -0.0000258
1100824.001356.00 Slope -0.0000258
1100824.001356.00 Slope -0.0000258
1100824.001356.00 Slope -0.0000258
1100824.001356.00 Slope -0.0000258
1100824.001356.00 Slope -0.0000258

As explained in tutorial 48, the formulae we use to calculate the linear regression are as follows:

Slope(m) = (NΣXY – (ΣX)(ΣY)) / (NΣX2 – (ΣX)2)

Intercept(c) = (ΣY – m(ΣX)) / N

Equation(y) = mx + c

Where the ‘Σ‘ symbol means ‘sum.’

In the following video I explain how the function was created in more detail.

Please join our email mailing list if you have not already done so and we will let you know when we release new tutorials or programs.

TO THE BEST OF MARKPLEX CORPORATION’S KNOWLEDGE, ALL OF THE INFORMATION ON THIS PAGE IS CORRECT, AND IT IS PROVIDED IN THE HOPE THAT IT WILL BE USEFUL. HOWEVER, MARKPLEX CORPORATION ASSUMES NO LIABILITY FOR ANY DAMAGES, DIRECT OR OTHERWISE, RESULTING FROM THE USE OF THIS INFORMATION AND/OR PROGRAM(S) DESCRIBED, AND NO WARRANTY IS MADE REGARDING ITS ACCURACY OR COMPLETENESS. USE OF THIS INFORMATION AND/OR PROGRAMS DESCRIBED IS AT YOUR OWN RISK.

ANY EASYLANGUAGE OR POWERLANGUAGE TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (AND PARTS THEREOF) AND ASSOCIATED TECHNIQUES REFERRED TO, INCLUDED IN OR ATTACHED TO THIS TUTORIAL OR PROGRAM DESCRIPTION ARE EXAMPLES ONLY, AND HAVE BEEN INCLUDED SOLELY FOR EDUCATIONAL PURPOSES. MARKPLEX CORPORATION. DOES NOT RECOMMEND THAT YOU USE ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS (OR ANY PARTS THEREOF) OR TECHNIQUES. THE USE OF ANY SUCH TRADING STRATEGIES, SIGNALS, STUDIES, INDICATORS, SHOWME STUDIES, PAINTBAR STUDIES, PROBABILITYMAP STUDIES, ACTIVITYBAR STUDIES, FUNCTIONS AND TECHNIQUES DOES NOT GUARANTEE THAT YOU WILL MAKE PROFITS, INCREASE PROFITS, OR MINIMIZE LOSSES.