I am trying to make use of linear regression values in a trading strategy. The original formula study was copied from an efs posted by Alexis.
The problem I am having is that when the efs is run on realtime data, I get one set of values. But when I run it on historical data (or when I refresh the data) the values change. This is despite using the SetComputeOnClose() statement. What am I missing?
I would like the values in realtime to be the same as when the efs is applied on historical data.
Here is the efs file in question followed by examples of different values under the two scenarios. Any help from anyone would be greatly appreciated.
Here are the values:
Values on live recording
Mkt Time LR LR1 LR2 LR3
AB H7-5 2007-02-01 11:15 807.6406 807.6406 808.0557 808.4735
AB H7-5 2007-02-01 11:20 807.2062 807.2062 807.6406 808.0557
AB H7-5 2007-02-01 11:25 806.6378 806.6378 807.2062 807.6406
AB H7-5 2007-02-01 11:30 806.1338 806.1338 806.6378 807.2062
AB H7-5 2007-02-01 11:35 805.7154 805.7154 806.1338 806.6378
Values after data refresh
Mkt Time LR LR1 LR2 LR3
AB H7-5 2007-02-01 11:15 807.6538 807.6538 808.0698 808.4886
AB H7-5 2007-02-01 11:20 807.2185 807.2185 807.6538 808.0698
AB H7-5 2007-02-01 11:25 806.6492 806.6492 807.2185 807.6538
AB H7-5 2007-02-01 11:30 806.1443 806.1443 806.6492 807.2185
AB H7-5 2007-02-01 11:35 805.7249 805.7249 806.1443 806.6492
Thanks in advance,
Hamid.
The problem I am having is that when the efs is run on realtime data, I get one set of values. But when I run it on historical data (or when I refresh the data) the values change. This is despite using the SetComputeOnClose() statement. What am I missing?
I would like the values in realtime to be the same as when the efs is applied on historical data.
Here is the efs file in question followed by examples of different values under the two scenarios. Any help from anyone would be greatly appreciated.
PHP Code:
/*
Test of Linear regression values
Formula from an example study by Alexis Montenegro
*/
var sum = null;
var LR = null;
var LR1 = null;
var LR2 = null;
var LR3 = null;
var Length = 25;
var bInitialized = false;
var aFPArray = new Array();
var Time = null;
var sMkt = null;
function preMain() {
setPriceStudy(true);
setStudyTitle("Linear Regression");
setCursorLabelName("LR");
setDefaultBarThickness(3);
setPlotType(PLOTTYPE_INSTANTCOLORLINE);
setComputeOnClose();
x=0;
aFPArray[x] = new FunctionParameter( "bTest", FunctionParameter.BOOLEAN);
with( aFPArray[x] ) {
setName( "Start Test?" );
addOption( true );
addOption( false );
setDefault( true );
}
}
function main(bTest) {
if ( getBarState() == BARSTATE_ALLBARS ) {
return null;
}
if ( bInitialized == false ) {
//WriteFileHeaders
myFile = new File("LRtestFile.csv");
sMkt = getSymbol() + "-" + getInterval() ;
if (bTest == true) {
myFile.open("at+");
if(!myFile.isOpen()) {
debugPrintln("Could not open file!");
} else {
myFile.writeln("Mkt,Time,LR,LR1,LR2,LR3" );
}
myFile.close();
}
bInitialized = true;
}
if ( getBarState() == BARSTATE_NEWBAR ) {
sum = 0;
LR3 = LR2;
LR2 = LR1;
LR1 = LR;
Time = getYear(0) + "-" + getMonth(0) + "-" + getDay(0) + " " + getHour(0) + ":" + getMinute(0) ;
if (bTest == true) WriteFile();
for(var i = Length; i > 0; i--) {
sum += (i - (Length + 1) / 3) * close(i - Length);
}
LR = 6 / (Length * (Length + 1)) * sum;
return LR;
}
}
function WriteFile() {
myFile.open("at+");
if(!myFile.isOpen()) {
debugPrintln("Could not open file!");
} else {
myFile.writeln(sMkt + "," + Time + "," + LR + "," + LR1 + "," + LR2 + "," + LR3 );
}
myFile.close();
}
Values on live recording
Mkt Time LR LR1 LR2 LR3
AB H7-5 2007-02-01 11:15 807.6406 807.6406 808.0557 808.4735
AB H7-5 2007-02-01 11:20 807.2062 807.2062 807.6406 808.0557
AB H7-5 2007-02-01 11:25 806.6378 806.6378 807.2062 807.6406
AB H7-5 2007-02-01 11:30 806.1338 806.1338 806.6378 807.2062
AB H7-5 2007-02-01 11:35 805.7154 805.7154 806.1338 806.6378
Values after data refresh
Mkt Time LR LR1 LR2 LR3
AB H7-5 2007-02-01 11:15 807.6538 807.6538 808.0698 808.4886
AB H7-5 2007-02-01 11:20 807.2185 807.2185 807.6538 808.0698
AB H7-5 2007-02-01 11:25 806.6492 806.6492 807.2185 807.6538
AB H7-5 2007-02-01 11:30 806.1443 806.1443 806.6492 807.2185
AB H7-5 2007-02-01 11:35 805.7249 805.7249 806.1443 806.6492
Thanks in advance,
Hamid.
Comment