I've started to learn to write backtest code. Been using the great stuff from Brad, and looking at some other examples.
I have written one simple backtest efs. It is a stop and reverse sma crossover. No stops, no targets. The problem is that it does not work. It flips back and forth on every bar. Testing on a 15 min chart FWIW.
Any ideas why?
----------------------
var vmafast = null;
var vmaslow = null;
function preMain() {
setPriceStudy(true);
}
function main() {
if (vmafast == null) vmafast = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
if (vmaslow == null) vmaslow = new MAStudy(20, 0, "Close", MAStudy.SIMPLE);
var vf = vmafast.getValue(MAStudy.MA);
var vs = vmaslow.getValue(MAStudy.MA);
if(vf == null || vs == null)
return;
if ((!Strategy.isInTrade())|| (Strategy.isShort())) { // not in a trade currently or currently short
if(vf > vs ) { // test for long entry
Strategy.doLong("Enter Long", Strategy.MARKET, Strategy.NEXTBAR);
}
}
else{
if ((!Strategy.isInTrade())|| (Strategy.isLong())) { // not in trade or currently long
if(vs < vf ){ // test for short entry
Strategy.doShort("Enter Short", Strategy.MARKET, Strategy.NEXTBAR);
}
} // end if strategy not in a trade
}
return;
}
I have written one simple backtest efs. It is a stop and reverse sma crossover. No stops, no targets. The problem is that it does not work. It flips back and forth on every bar. Testing on a 15 min chart FWIW.
Any ideas why?
----------------------
var vmafast = null;
var vmaslow = null;
function preMain() {
setPriceStudy(true);
}
function main() {
if (vmafast == null) vmafast = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
if (vmaslow == null) vmaslow = new MAStudy(20, 0, "Close", MAStudy.SIMPLE);
var vf = vmafast.getValue(MAStudy.MA);
var vs = vmaslow.getValue(MAStudy.MA);
if(vf == null || vs == null)
return;
if ((!Strategy.isInTrade())|| (Strategy.isShort())) { // not in a trade currently or currently short
if(vf > vs ) { // test for long entry
Strategy.doLong("Enter Long", Strategy.MARKET, Strategy.NEXTBAR);
}
}
else{
if ((!Strategy.isInTrade())|| (Strategy.isLong())) { // not in trade or currently long
if(vs < vf ){ // test for short entry
Strategy.doShort("Enter Short", Strategy.MARKET, Strategy.NEXTBAR);
}
} // end if strategy not in a trade
}
return;
}
Comment