var fpArray = []; // Parameters in main to add bands var bUpDivergent = null; // Global boolean saves up divergent condition var bDnDivergent = null; // Global boolean saves down divergent condition var bPurple = null; // Global boolean to track purple shape...needed for streaming var bYellow = null; // Global boolean to track yellow shape...needed for streaming function preMain(){ setPriceStudy(false); setStudyTitle("Stochastic Divergence"); setCursorLabelName("%K_2",0); setPlotType(PLOTTYPE_LINE,0); setDefaultBarFgColor(Color.RGB(0,148,255), 0); setCursorLabelName("%D_2",1); setPlotType(PLOTTYPE_LINE,1); setDefaultBarFgColor(Color.RGB(255,106,0), 1); var x = 0; fpArray[x] = new FunctionParameter("UpperBand", FunctionParameter.INTEGER); with(fpArray[x++]){ setUpperLimit(100); setDefault(80); } fpArray[x] = new FunctionParameter("LowerBand", FunctionParameter.INTEGER); with(fpArray[x++]){ setLowerLimit(0); setDefault(20); } fpArray[x] = new FunctionParameter("AddBands", FunctionParameter.BOOLEAN); with(fpArray[x++]){ setDefault(false); } } function main(UpperBand, LowerBand, AddBands){ if(getBarState() == BARSTATE_ALLBARS){ if(AddBands){ addBand(UpperBand, PS_DASH, 1, Color.RGB(192,0,0)); addBand(LowerBand, PS_DASH, 1, Color.RGB(0,192,0)); } } if(getBarState() == BARSTATE_NEWBAR){ if(bUpDivergent) bYellow = true; else bYellow = false; if(bDnDivergent) bPurple = true; else bPurple = false; } if(bYellow && stochK(14,3,3,inv(2)) > stochD(14,3,3, inv(2))) bUpDivergent = true; if(bPurple && stochK(14,3,3,inv(2)) < stochD(14,3,3, inv(2))) bDnDivergent = true; if(!bUpDivergent) drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(70,100,160),Shape.CENTER|Shape.PRESET,"UP" + getCurrentBarCount()); if(!bDnDivergent) drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(70,100,160),Shape.CENTER|Shape.PRESET,"Down" + getCurrentBarCount()); if(stochK(14,3,3,inv(1), -1) > stochD(14,3,3, inv(1), -1) && stochK(14,3,3,inv(2), -1) > stochD(14,3,3, inv(2), -1) && stochK(14,3,3,inv(1)) > stochD(14,3,3, inv(1)) && stochK(14,3,3,inv(2)) < stochD(14,3,3, inv(2))){ bDnDivergent = true; drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(101,0,169),Shape.CENTER|Shape.PRESET,"DOWN" + getCurrentBarCount()); }else if(bDnDivergent && stochK(14,3,3,inv(2)) < stochD(14,3,3, inv(2))){ drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(101,0,169),Shape.CENTER|Shape.PRESET,"DOWN" + getCurrentBarCount()); }else if(stochK(14,3,3,inv(2)) > stochD(14,3,3, inv(2))) bDnDivergent = false; if(stochK(14,3,3,inv(1), -1) < stochD(14,3,3, inv(1), -1) && stochK(14,3,3,inv(2), -1) < stochD(14,3,3, inv(2), -1) && stochK(14,3,3,inv(1)) < stochD(14,3,3, inv(1)) && stochK(14,3,3,inv(2)) > stochD(14,3,3, inv(2))){ bUpDivergent = true; drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(255,255,0),Shape.CENTER|Shape.PRESET,"UP" + getCurrentBarCount()); }else if(bUpDivergent && stochK(14,3,3,inv(2)) > stochD(14,3,3, inv(2))){ drawShapeRelative(0,BottomRow1,Shape.SQUARE,null,Color.RGB(255,255,0),Shape.CENTER|Shape.PRESET,"UP" + getCurrentBarCount()); }else if(stochK(14,3,3,inv(2)) < stochD(14,3,3, inv(2))) bUpDivergent = false; return [stochK(14,3,3,inv(2)), stochD(14,3,3,inv(2))]; }