I have large number of charts with Linear Regression (LR) Indicators. But that was chewing up lots of CPU cycles... CPU usage stayed above 50% duing NY sessions. Therefore I have tried a different algorithm to avoid loops in the LR value computation, to my surprise this technique gave amazingly closer value that any standard LR algorithm gives and is 7 times faster..
Following is the EFS code... Let me know if you any critical thoughts or suggestions...
/*********************************
Linear Regression Indicator
By bizken.com
Email [email protected]
Version : 1.0
Date(ddmmyyyy) : 12/02/2009
Parameters: Default:
Period 64
Price Close
UpColor Cyan
DnColor Magenta
Plot Type Solid
Plot ThickNess 1
Remarks:
Indicator value will be computed during
initialization and first tick of the bar
License : EFS code is free for end-users
**********************************/
var SampleIndex= 0;
var BarIndex = 1;
var TotalX = 0;
var TotalY = 0;
var TotalXX = 0;
var TotalXY = 0;
var Alpha = 0;
var Beta = 0;
var LinearRegressionValue=0;
var SamplesY = new Array();
var SamplesX = new Array();
var SamplesXX = new Array();
var SamplesXY = new Array();
var FPArray = new Array();
var SamplesLR = new Array();
var Init = false;
var Src = null;
var nSrc = 0;
var BarStat = null;
var SampleIndex1 = 0;
var IdxFPArray = 0;
function preMain() {
setPriceStudy(true);
setStudyTitle("LRI");
setCursorLabelName("LRI", 0);
setDefaultBarFgColor(Color.cyan, 0);
setDefaultBarThickness(1, 0);
//initialize formula parameters
IdxFPArray=0;
FPArray[IdxFPArray] = new FunctionParameter( "Period", FunctionParameter.NUMBER);
with( FPArray[IdxFPArray] ) {
setName( "Period" );
setLowerLimit( 1 );
setUpperLimit( 512 );
setDefault( 64 );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Price", FunctionParameter.STRING);
with( FPArray[IdxFPArray] ) {
setName( "LRI Price Source" );
addOption( "close" );
addOption( "open" );
addOption( "high" );
addOption( "low" );
addOption( "hl2" );
addOption( "hlc3" );
addOption( "ohlc4" );
setDefault( "close" );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "UpColor", FunctionParameter.COLOR);
with( FPArray[IdxFPArray] ) {
setName( "Up Color" );
setDefault( Color.cyan );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "DnColor", FunctionParameter.COLOR);
with( FPArray[IdxFPArray] ) {
setName( "Dn Color" );
setDefault( Color.magenta );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Type", FunctionParameter.STRING);
with( FPArray[IdxFPArray] ) {
setName( "Plot Type" );
addOption( "Solid" );
addOption( "Dot" );
addOption( "Dash" );
addOption( "DashDot" );
addOption( "DashDotDot" );
setDefault( "Solid" );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Thickness", FunctionParameter.NUMBER);
with( FPArray[IdxFPArray] ) {
setName( "Plot Thickness" );
setLowerLimit( 1 );
setUpperLimit( 10 );
setDefault( 1 );
}
}
function main(Period, Price, UpColor, DnColor, Type, Thickness) {
BarStat = getBarState();
if ( Init == false )
{
setDefaultBarThickness( Math.round( Thickness ), 0 );
setDefaultBarStyle( eval( "PS_"+Type.toUpperCase() ), 0 );
Src = eval( Price )();
Init = true;
}
if (BarStat == BARSTATE_NEWBAR || BarStat == BARSTATE_ALLBARS ) {
nSrc = Src.getValue(0);
if (BarIndex <= Period)
{
TotalY += nSrc;
TotalX += BarIndex;
TotalXX += BarIndex * BarIndex;
TotalXY += nSrc * BarIndex;
SamplesY[SampleIndex] = nSrc;
SamplesX[SampleIndex] = BarIndex;
SamplesXX[SampleIndex] = BarIndex * BarIndex;
SamplesXY[SampleIndex] = nSrc * BarIndex;
Alpha = (Period * TotalXY - TotalX * TotalY)/(Period * TotalXX - TotalX * TotalX);
Beta = (TotalY - Alpha * TotalX)/Period;
LinearRegressionValue = Alpha * BarIndex + Beta ;
SamplesLR[SampleIndex] = LinearRegressionValue;
SampleIndex++;
BarIndex++;
return;
}
else
{
if (SampleIndex == Period)
{
SampleIndex = 0;
}
TotalY += nSrc - SamplesY[SampleIndex];
TotalX += BarIndex - SamplesX[SampleIndex];
TotalXX += BarIndex * BarIndex - SamplesXX[SampleIndex];
TotalXY += nSrc * BarIndex - SamplesXY[SampleIndex];
SamplesY[SampleIndex] = nSrc;
SamplesX[SampleIndex] = BarIndex;
SamplesXX[SampleIndex] = BarIndex * BarIndex;
SamplesXY[SampleIndex] = nSrc * BarIndex;
Alpha = (Period * TotalXY - TotalX * TotalY) / (Period * TotalXX - TotalX * TotalX);
Beta = (TotalY - Alpha * TotalX) / Period;
LinearRegressionValue = Alpha * BarIndex + Beta ;
SamplesLR[SampleIndex] = LinearRegressionValue;
if (SampleIndex == 0)
{
SampleIndex1 = Period-1;
}
else{
SampleIndex1 = SampleIndex - 1;
}
if ( SamplesLR[SampleIndex] > SamplesLR[SampleIndex1] )
{
setBarFgColor( UpColor, 0 );
}
else
{
setBarFgColor( DnColor, 0 );
}
SampleIndex++;
BarIndex++;
}
}
return LinearRegressionValue;
}
Following is the EFS code... Let me know if you any critical thoughts or suggestions...
/*********************************
Linear Regression Indicator
By bizken.com
Email [email protected]
Version : 1.0
Date(ddmmyyyy) : 12/02/2009
Parameters: Default:
Period 64
Price Close
UpColor Cyan
DnColor Magenta
Plot Type Solid
Plot ThickNess 1
Remarks:
Indicator value will be computed during
initialization and first tick of the bar
License : EFS code is free for end-users
**********************************/
var SampleIndex= 0;
var BarIndex = 1;
var TotalX = 0;
var TotalY = 0;
var TotalXX = 0;
var TotalXY = 0;
var Alpha = 0;
var Beta = 0;
var LinearRegressionValue=0;
var SamplesY = new Array();
var SamplesX = new Array();
var SamplesXX = new Array();
var SamplesXY = new Array();
var FPArray = new Array();
var SamplesLR = new Array();
var Init = false;
var Src = null;
var nSrc = 0;
var BarStat = null;
var SampleIndex1 = 0;
var IdxFPArray = 0;
function preMain() {
setPriceStudy(true);
setStudyTitle("LRI");
setCursorLabelName("LRI", 0);
setDefaultBarFgColor(Color.cyan, 0);
setDefaultBarThickness(1, 0);
//initialize formula parameters
IdxFPArray=0;
FPArray[IdxFPArray] = new FunctionParameter( "Period", FunctionParameter.NUMBER);
with( FPArray[IdxFPArray] ) {
setName( "Period" );
setLowerLimit( 1 );
setUpperLimit( 512 );
setDefault( 64 );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Price", FunctionParameter.STRING);
with( FPArray[IdxFPArray] ) {
setName( "LRI Price Source" );
addOption( "close" );
addOption( "open" );
addOption( "high" );
addOption( "low" );
addOption( "hl2" );
addOption( "hlc3" );
addOption( "ohlc4" );
setDefault( "close" );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "UpColor", FunctionParameter.COLOR);
with( FPArray[IdxFPArray] ) {
setName( "Up Color" );
setDefault( Color.cyan );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "DnColor", FunctionParameter.COLOR);
with( FPArray[IdxFPArray] ) {
setName( "Dn Color" );
setDefault( Color.magenta );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Type", FunctionParameter.STRING);
with( FPArray[IdxFPArray] ) {
setName( "Plot Type" );
addOption( "Solid" );
addOption( "Dot" );
addOption( "Dash" );
addOption( "DashDot" );
addOption( "DashDotDot" );
setDefault( "Solid" );
}
IdxFPArray++;
FPArray[IdxFPArray] = new FunctionParameter( "Thickness", FunctionParameter.NUMBER);
with( FPArray[IdxFPArray] ) {
setName( "Plot Thickness" );
setLowerLimit( 1 );
setUpperLimit( 10 );
setDefault( 1 );
}
}
function main(Period, Price, UpColor, DnColor, Type, Thickness) {
BarStat = getBarState();
if ( Init == false )
{
setDefaultBarThickness( Math.round( Thickness ), 0 );
setDefaultBarStyle( eval( "PS_"+Type.toUpperCase() ), 0 );
Src = eval( Price )();
Init = true;
}
if (BarStat == BARSTATE_NEWBAR || BarStat == BARSTATE_ALLBARS ) {
nSrc = Src.getValue(0);
if (BarIndex <= Period)
{
TotalY += nSrc;
TotalX += BarIndex;
TotalXX += BarIndex * BarIndex;
TotalXY += nSrc * BarIndex;
SamplesY[SampleIndex] = nSrc;
SamplesX[SampleIndex] = BarIndex;
SamplesXX[SampleIndex] = BarIndex * BarIndex;
SamplesXY[SampleIndex] = nSrc * BarIndex;
Alpha = (Period * TotalXY - TotalX * TotalY)/(Period * TotalXX - TotalX * TotalX);
Beta = (TotalY - Alpha * TotalX)/Period;
LinearRegressionValue = Alpha * BarIndex + Beta ;
SamplesLR[SampleIndex] = LinearRegressionValue;
SampleIndex++;
BarIndex++;
return;
}
else
{
if (SampleIndex == Period)
{
SampleIndex = 0;
}
TotalY += nSrc - SamplesY[SampleIndex];
TotalX += BarIndex - SamplesX[SampleIndex];
TotalXX += BarIndex * BarIndex - SamplesXX[SampleIndex];
TotalXY += nSrc * BarIndex - SamplesXY[SampleIndex];
SamplesY[SampleIndex] = nSrc;
SamplesX[SampleIndex] = BarIndex;
SamplesXX[SampleIndex] = BarIndex * BarIndex;
SamplesXY[SampleIndex] = nSrc * BarIndex;
Alpha = (Period * TotalXY - TotalX * TotalY) / (Period * TotalXX - TotalX * TotalX);
Beta = (TotalY - Alpha * TotalX) / Period;
LinearRegressionValue = Alpha * BarIndex + Beta ;
SamplesLR[SampleIndex] = LinearRegressionValue;
if (SampleIndex == 0)
{
SampleIndex1 = Period-1;
}
else{
SampleIndex1 = SampleIndex - 1;
}
if ( SamplesLR[SampleIndex] > SamplesLR[SampleIndex1] )
{
setBarFgColor( UpColor, 0 );
}
else
{
setBarFgColor( DnColor, 0 );
}
SampleIndex++;
BarIndex++;
}
}
return LinearRegressionValue;
}
Comment