Linear Regression |
Top Previous Next |
upperLinearReg( length, stdDev [, source | sym() | inv()] [, barIndex] ) middleLinearReg( length, stdDev [, source | sym() | inv()] [, barIndex] ) lowerLinearReg( length, stdDev [, source | sym() | inv()] [, barIndex] )
Linear regression is a statistical tool used to predict future values from past values. In the case of security prices, it is commonly used to determine when prices are overextended. A linear regression trendline uses the least squares method to plot a straight line through prices so as to minimize the distances between the prices and resulting trendline.
Parameters
To Create a Series
var myStudy1 = null; var myStudy2 = null; var myStudy3 = null; var bInit = false;
function main() { var myVar1; var myVar2; var myVar3;
if ( bInit == false ) {
myStudy1 = upperLinearReg( 20, 2 ); myStudy2 = middleLinearReg( 20, 2 ); myStudy3 = lowerLinearReg( 20, 2 );
bInit = true;
}
//retrieve the current values myVar1 = myStudy1.getValue(0); myVar2 = myStudy2.getValue(0); myVar3 = myStudy3.getValue(0);
return new Array( myVar1, myVar2, myVar3 );
}
To Retrieve a Single Value
function main() {
... ... myVar = middleLinearReg( 20, 2 );
//do something with the value in myVar
}
Calling Examples
//create a study that uses IBM as the symbol along with whatever bar //interval you are using in the chart where the script is loaded myStudy = middleLinearReg( 20, 2, sym( "IBM") );
//create a study that uses the 15-min bar interval, regardless of the bar //interval of the chart in which the script is loaded myStudy = upperLinearReg( 20, 2, inv(15) );
//create a study that uses the Daily bar interval, regardless of the bar //interval of the chart in which the script is loaded myStudy = lowerLinearReg( 10, 1.5, inv("D") );
//create a study that will be based on MSFT 30-min bars, regardless of //the symbol/bar interval of the chart in which the script is loaded myStudy = middleLinearReg( 20, 1, sym( "MSFT,30" ) );
//create a study that will be based on the high. myStudy = middleLinearReg( 10, 2, high() );
//create a study that will be based on the close, using 15-min bars, regardless of //the bar interval of the chart in which the script is loaded myStudy = upperLinearReg( 12, 2, close( inv(15) ) );
|