Tradestation provides built-in linear regression functions LinearReg and LinearRegFC, which both use identical calculation algorithms except for the inclusion of the ExecOffset feature in the later function.

The FC suffix after a function name normally implies a fast calculation algorithm is being used. However, in the case of LinearReg and LinearRegFC, both are using the same algorithm, which has not been optimized for execution speed.

LinearRegFC contains the following code:

Original TradeStation LinearRegFC Code
Fig 1. Original LinearRegFC Code
The area highlighted in yellow contains a loop and a summation that is performed with every new bar processed.

It is possible to optimize this section, replacing the loop and summation code with a simple addition/subtraction forumula, as shown in the optimized code:

Improved LinRegFast Function Code

Fig. 2.  Improved LinRegFast Function Code.

Table demonstrating relative speeds of Linear Regression Codes
Table 1. Relative execution speeds of LinearRegFaster compared to LinearRegFC
Function LinearRegFast produces output identical to TS function LinearRegFC under all conditions.


Initially posted version:  11/03/12

Latest Update:  11/03/12

*.ELD files are compiled for TS 9.0

All ELD and code text files packaged here:

Users of earlier versions fo TradeStation may compile the code
from the text files included in the above *.zip file.

The code may be visualized here:

Function LinearRegFaster

Method LinearRegFaster

Original TS Function LinearRefFC