A Faster Linear Regression Algorithm

Improved LinearRegFaster function code with the loop and summation replaced by an incremental update (highlighted)
Improved LinearRegFaster Function Code

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 latter 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 formula, as shown in the optimized code:

Improved LinearRegFaster function code with the loop and summation replaced by an incremental update (highlighted)
Fig. 2. Improved LinearRegFaster Function Code
Table demonstrating relative speeds of Linear Regression Codes
Table 1. Relative execution speeds of LinearRegFaster compared to LinearRegFC

Function LinearRegFaster produces output identical to TS function LinearRegFC under all conditions.

Downloads

Initially posted version: 11/03/12

Latest Update: 11/03/12

*.ELD files are compiled for TS 9.0

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

The code may be visualized here:

Related Posts

Was this helpful? Rate it: