It can be useful during strategy design/backtest to include rollover dates (or any other dates come to that) for specific symbols when using continuous contracts ...
1) Create a text file SymbolRollOverDates.txt in \FormulaOutput with each line starting with a unique symbol name followed by the sequence year,month,day,hour,minute
e.g. GB 1!-DT,2005,8,31,17,30,2005,11,30,17,30 ... etc
2) Execute the following function once say on each BARSTATE_ALLBARS to load the data corresponding to the current chart symbol:
3) Execute the following function say on each BARSTATE_NEWBAR and if true, close all open positions. Note that this only works with Daily & Intraday charts and hour & minute are only tested with Intraday charts:
1) Create a text file SymbolRollOverDates.txt in \FormulaOutput with each line starting with a unique symbol name followed by the sequence year,month,day,hour,minute
e.g. GB 1!-DT,2005,8,31,17,30,2005,11,30,17,30 ... etc
2) Execute the following function once say on each BARSTATE_ALLBARS to load the data corresponding to the current chart symbol:
PHP Code:
function readRollOverDates() {
var fstr = "SymbolRollOverDates.txt";
var fptr = new File(fstr);
var str = "";
var sets = 0;
var setlen = 5;
if(fptr.exists() ){
fptr.open("rt");
while(!fptr.eof()) {
str = fptr.readln();
var strarray = str.split(",");
if (strarray[0] == getSymbol()) {
sets = Math.floor((strarray.length - 1)/setlen);
break;
}
str = "";
}
fptr.close();
}
if (str != "" && sets > 0) {
RollOverTime = new Array(sets);
for (i=0; i<RollOverTime.length; ++i) {
RollOverTime[i] = new Array(setlen);
}
var strarray = str.split(",");
var k = 1;
for (var i=0; i<sets; ++i) {
for (j=0; j<setlen; ++j) RollOverTime[i][j] = strarray[k+j];
k += setlen;
}
}
else {
RollOverTime = new Array(1);
RollOverTime[0] = new Array(setlen);
}
return;
}
PHP Code:
function isRollOver() {
for (i=0; i<RollOverTime.length; ++i) {
if (getYear() == RollOverTime[i][0] && getMonth() == RollOverTime[i][1] && getDay() == RollOverTime[i][2]) {
if (isDaily()) return true;
else if (isIntraday() && getHour() >= RollOverTime[i][3] && getMinute() >= RollOverTime[i][4]) return true;
}
}
return false;
}
Comment