Hi, I'm doing a simple calculation which doesn't work with back-testing. If I replace vUpper and vLower with v (in the if ... codes) the code works. The code is as following;
function preMain() {
setPriceStudy(true);
setStudyTitle("Bollinger Bands");
setCursorLabelName("UpperBB", 0 );
setCursorLabelName("MiddleBB", 1 );
setCursorLabelName("LowerBB", 2 );
setDefaultBarFgColor(Color.red, 0); // upper
setDefaultBarFgColor(Color.blue, 1); // middle
setDefaultBarFgColor(Color.green, 2); // lower
setColorPriceBars(true);
setDefaultPriceBarColor(Color.black);
}
var study = new BollingerStudy(10, "Close", 2.0);
var study = new MAStudy(40, 0, "Close", MAStudy.SIMPLE);
function main() {
var v = study.getValue(MAStudy.MA);
if(v == null) return;
var vUpper = study.getValue(BollingerStudy.UPPER);
var vMiddle = study.getValue(BollingerStudy.BASIS);
var vLower = study.getValue(BollingerStudy.LOWER);
// return new Array(vUpper, vMiddle, vLower);
if(close() <= vLower && !Strategy.isLong())
Strategy.doLong("Going Up", Strategy.MARKET, Strategy.THISBAR);
if(close() >= vUpper && !Strategy.isShort())
Strategy.doShort("Going Down", Strategy.MARKET, Strategy.THISBAR);
// if(Strategy.isLong() && high() >= vUpper)
// Strategy.doSell("Sell Up", Strategy.MARKET, Strategy.THISBAR);
// else if(Strategy.isShort() && low() <= vLower && !Stragegy.doSell)
// Strategy.doSell("Sell Down", Strategy.MARKET, Strategy.THISBAR);
if(Strategy.isLong()) setPriceBarColor(Color.lime);
else if(Strategy.isShort()) setPriceBarColor(Color.red);
// return new Array(vUpper, vMiddle, vLower);
return v;
}
function preMain() {
setPriceStudy(true);
setStudyTitle("Bollinger Bands");
setCursorLabelName("UpperBB", 0 );
setCursorLabelName("MiddleBB", 1 );
setCursorLabelName("LowerBB", 2 );
setDefaultBarFgColor(Color.red, 0); // upper
setDefaultBarFgColor(Color.blue, 1); // middle
setDefaultBarFgColor(Color.green, 2); // lower
setColorPriceBars(true);
setDefaultPriceBarColor(Color.black);
}
var study = new BollingerStudy(10, "Close", 2.0);
var study = new MAStudy(40, 0, "Close", MAStudy.SIMPLE);
function main() {
var v = study.getValue(MAStudy.MA);
if(v == null) return;
var vUpper = study.getValue(BollingerStudy.UPPER);
var vMiddle = study.getValue(BollingerStudy.BASIS);
var vLower = study.getValue(BollingerStudy.LOWER);
// return new Array(vUpper, vMiddle, vLower);
if(close() <= vLower && !Strategy.isLong())
Strategy.doLong("Going Up", Strategy.MARKET, Strategy.THISBAR);
if(close() >= vUpper && !Strategy.isShort())
Strategy.doShort("Going Down", Strategy.MARKET, Strategy.THISBAR);
// if(Strategy.isLong() && high() >= vUpper)
// Strategy.doSell("Sell Up", Strategy.MARKET, Strategy.THISBAR);
// else if(Strategy.isShort() && low() <= vLower && !Stragegy.doSell)
// Strategy.doSell("Sell Down", Strategy.MARKET, Strategy.THISBAR);
if(Strategy.isLong()) setPriceBarColor(Color.lime);
else if(Strategy.isShort()) setPriceBarColor(Color.red);
// return new Array(vUpper, vMiddle, vLower);
return v;
}
Comment