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(true); setStudyTitle("Stochastic Divergence PS"); 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); } function main(){ 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; }