Hello all,
I'm trying to backtest a stochastic trading strategy. A simple crossover of stochD and stochK when it's above or below 20/80.
I can't seem to get the code quite right though.
thanks for any help!
So far what I have is
/************************************************** ***************
Crossover Stochastic
************************************************** ****************/
function preMain() {
setPriceStudy(false);
setStudyTitle("Stochastic");
setCursorLabelName("%K",0);
setCursorLabelName("%D",1);
setDefaultBarFgColor(Color.RGB(0x00,0x94,0xFF), 0);
setDefaultBarFgColor(Color.RGB(0xFE,0x69,0x00), 1);
setPlotType(PLOTTYPE_LINE,0);
setPlotType(PLOTTYPE_LINE,1);
setDefaultBarThickness(1,0);
setDefaultBarThickness(1,1);
addBand( 80, PS_DASH, 1, Color.grey,"Upper");
addBand( 20, PS_DASH, 1, Color.grey,"Lower");
}
function main() {
if (stochD<20 && stochK.getValue(-1) > stochD.getValue(-1) && !Strategy.isLong()) {
Strategy.doLong("Long", Strategy.MARKET , Strategy.THISBAR);
}
if (stochD>80 && stochK.getValue(-1)<stochD.getValue(-1) && !Strategy.isShort()) {
Strategy.doShort("Short", Strategy.MARKET , Strategy.THISBAR);
}
if(Strategy.isLong())
setPriceBarColor(Color.lime);
else if(Strategy.isShort())
setPriceBarColor(Color.red);
return new Array (stochK(14,3,3),stochD(14,3,3));
}
I'm trying to backtest a stochastic trading strategy. A simple crossover of stochD and stochK when it's above or below 20/80.
I can't seem to get the code quite right though.
thanks for any help!
So far what I have is
/************************************************** ***************
Crossover Stochastic
************************************************** ****************/
function preMain() {
setPriceStudy(false);
setStudyTitle("Stochastic");
setCursorLabelName("%K",0);
setCursorLabelName("%D",1);
setDefaultBarFgColor(Color.RGB(0x00,0x94,0xFF), 0);
setDefaultBarFgColor(Color.RGB(0xFE,0x69,0x00), 1);
setPlotType(PLOTTYPE_LINE,0);
setPlotType(PLOTTYPE_LINE,1);
setDefaultBarThickness(1,0);
setDefaultBarThickness(1,1);
addBand( 80, PS_DASH, 1, Color.grey,"Upper");
addBand( 20, PS_DASH, 1, Color.grey,"Lower");
}
function main() {
if (stochD<20 && stochK.getValue(-1) > stochD.getValue(-1) && !Strategy.isLong()) {
Strategy.doLong("Long", Strategy.MARKET , Strategy.THISBAR);
}
if (stochD>80 && stochK.getValue(-1)<stochD.getValue(-1) && !Strategy.isShort()) {
Strategy.doShort("Short", Strategy.MARKET , Strategy.THISBAR);
}
if(Strategy.isLong())
setPriceBarColor(Color.lime);
else if(Strategy.isShort())
setPriceBarColor(Color.red);
return new Array (stochK(14,3,3),stochD(14,3,3));
}