NOTE: THIS IS THIRD OF THE FOUR...COMBINE WITH HE NEXT THREAD... //Distances computed relative to selected bar i=0; _TRACE("Bull Reversal 00 - start of loop ------ "); do { i++; LastBar_PriorGroup = Sel_BarIndex-ValueWhen(Cross(Osc_Issue,Middle),BarIndex(),i); FirstBar_PriorGroup = Sel_BarIndex-ValueWhen(Cross(Middle,Osc_Issue),BarIndex(),i+1); Sel_LastBar_PG = IIf(TradeON==1,LastValue(LastBar_PriorGroup),SelectedValue(LastBar_PriorGroup)); Sel_FirstBar_PG = IIf(TradeON==1,LastValue(FirstBar_PriorGroup),SelectedValue(FirstBar_PriorGroup)); //Find Trough using scalars - This may interfere with Backtest operation, but needed Trough_PriorGroup = Ref(LLV(Osc_Issue,Sel_FirstBar_PG - Sel_LastBar_PG+1),-Sel_LastBar_PG+1); TroughBars_PriorGroup = Ref(LLVBars(Osc_Issue,Sel_FirstBar_PG - Sel_LastBar_PG+1),-Sel_LastBar_PG+1); Sel_LastBar_PriorGroup = IIf(TradeON==1,LastValue(TroughBars_PriorGroup),SelectedValue(TroughBars_PriorGroup)); Sel_TroughBars_PriorGroup = IIf(TradeON==1,LastValue(LastBar_PriorGroup),SelectedValue(LastBar_PriorGroup)); Sel_Trough_PriorGroup = IIf(TradeON==1,LastValue(Trough_PriorGroup),SelectedValue(Trough_PriorGroup)); Final_Bars = Sel_TroughBars_PriorGroup + Sel_LastBar_PriorGroup - 1; if (Diag_Trace) { _TRACE("Bull Reversal - loop 01 - i= " + WriteVal(i,1.0) + " FB_PrGr " + WriteVal(FirstBar_PriorGroup,1.0) + " / " + WriteVal(Sel_FirstBar_PG,1.0) + " LB_PrGr " + WriteVal(LastBar_PriorGroup,1.0) + " / " + WriteVal(Sel_LastBar_PG,1.0)); _TRACE("Bull Reversal - loop 02 - Sel_FB_PrGr " + WriteVal(Sel_FirstBar_PG,1.0) + " Sel_LB_PrGr " + WriteVal(Sel_LastBar_PG,1.0)); _TRACE("Bull Reversal - loop 03 - Tr_PG " + WriteVal(Trough_PriorGroup,1.2) + " Tr_PGBars " + WriteVal(TroughBars_PriorGroup,1.0) + " F Bars " + WriteVal(Final_Bars,1.0)); _TRACE("Bull Reversal - loop 04 - While1= " + WriteVal((Middle - SelectedValue(Trough_PriorGroup)),1.2) + " Compare= " + WriteVal(((Middle - SelectedValue(CurrTrough)) / 3),1.2)); } } while (((Middle - Sel_Trough_PriorGroup) < (Middle - Sel_CurrTrough) / 3) AND i<10); PriorTrough = Trough_PriorGroup; PriorTroughBars = Final_Bars; //Needed for display Sel_LR1 = IIf(TradeON==1,LastValue(LR1),SelectedValue(LR1)); Sel_LR2 = IIf(TradeON==1,LastValue(LR2),SelectedValue(LR2)); Sel_LR3 = IIf(TradeON==1,LastValue(LR3),SelectedValue(LR3)); Sel_LR4 = IIf(TradeON==1,LastValue(LR4),SelectedValue(LR4)); Sel_LR5 = IIf(TradeON==1,LastValue(LR5),SelectedValue(LR5)); //Determine Price Divergence if (Sel_LR1==1) { Bull_Diverge = 0; LDiv_Code = 11; Diverge = ""; Pattern = "Lower Low"; } if (Sel_LR2==1) { Curr_LowPrice = LLV(Low,5); Prior_LowPrice = Ref(LLV(Low,6),-(Sel_PriorTroughBars-3)); Sel_CLowPrice = IIf(TradeON==1,LastValue(Curr_LowPrice),SelectedValue(Curr_LowPrice)); Sel_PLowPrice = IIf(TradeON==1,LastValue(Prior_LowPrice),SelectedValue(Prior_LowPrice)); Sel_Bull_Diverge = Sel_CLowPrice <= Sel_PLowPrice; if (Sel_Bull_Diverge==1) { Bull_Diverge = 1; LDiv_Code = 13; Diverge = "Bullish"; Pattern = "Higher Low"; } else { Bull_Diverge = 0; LDiv_Code = 12; Diverge = ""; Pattern = "Higher Low"; } } if (Sel_LR3==1) { Curr_LowPrice = LLV(Low,5); CT_LowPrice = Ref(LLV(Low,6),-(Sel_CurrTroughBars-3)); Sel_CLowPrice = IIf(TradeON==1,LastValue(Curr_LowPrice),SelectedValue(Curr_LowPrice)); Sel_CTroughPrice = IIf(TradeON==1,LastValue(CT_LowPrice),SelectedValue(CT_LowPrice)); Sel_Bull_Diverge = Sel_CLowPrice <= Sel_CTroughPrice; } if (Sel_LR4==1) { Curr_LowPrice = LLV(Low,5); CT_LowPrice = Ref(LLV(Low,6),-(Sel_CurrTroughBars-3)); Sel_CLowPrice = IIf(TradeON==1,LastValue(Curr_LowPrice),SelectedValue(Curr_LowPrice)); Sel_CTroughPrice = IIf(TradeON==1,LastValue(CT_LowPrice),SelectedValue(CT_LowPrice)); Sel_Bull_Diverge = Sel_CLowPrice <= Sel_CTroughPrice; if (Sel_Bull_Diverge==1) { Bull_Diverge = 1; LDiv_Code = 17; Diverge = "Bullish"; Pattern = "Higher Low"; } else { Bull_Diverge = 0; LDiv_Code = 16; Diverge = ""; Pattern = "Higher Low"; } } if (Sel_LR5==1) { Bull_Diverge = 0; LDiv_Code = 18; Diverge = ""; } BT_LDiv_Code = IIf(LR1==1,11, IIf(LR2==1 AND Bull_Diverge==0,12,IIf(LR2==1 AND Bull_Diverge==1,13, IIf(LR3==1 AND Bull_Diverge==0,14,IIf(LR3==1 AND Bull_Diverge==1,15, IIf(LR4==1 AND Bull_Diverge==0,16,IIf(LR4==1 AND Bull_Diverge==1,17, IIf(LR5==1 AND LR4==0 AND LR3==0,18,0)))))))); if (Diag_trace) { _TRACE("Bull Reversal #00A - BT_Div_Code " + WriteVal(BT_LDiv_Code,1.0)); _TRACE("Bull Reversal #00B - Div_Code " + WriteVal(LDiv_Code,1.0) + " -- LR1 " + WriteVal(LR1,1.0) + " LR2 " + WriteVal(LR2,1.0) + " LR3 " + WriteVal(LR3,1.0) + " LR4 " + WriteVal(LR4,1.0)); _TRACE("Bull Reversal #00C - LastLow " + WriteVal(Sel_LastLow,1.2) + " CurrTrough " + WriteVal(Sel_CurrTrough,1.2) + " PriorTrough " + WriteVal(Sel_PriorTrough,1.2) ); } if (Backtest == 0) { //Assign Values to coordinates y10=Sel_CurrTrough; y11=Sel_LastLow; x10=BarCount - 1 - Sel_CurrTroughBars - (LastValue(BarIndex()) - Sel_BarIndex); x11=BarCount - 1 - Sel_LastLowBars - (LastValue(BarIndex()) - Sel_BarIndex); Line1 = LineArray( x10,y10,x11,y11, 0 ); y20=Sel_PriorTrough; y21=Sel_CurrTrough; x20=BarCount - 1 - SelectedValue(Final_Bars) - (LastValue(BarIndex())- Sel_BarIndex); x21=BarCount - 1 - Sel_CurrTroughBars - (LastValue(BarIndex())- Sel_BarIndex); Line2 = LineArray( x20, y20, x21, y21, 0 ); // //Find min value of oscillator in area of interest Area_MinValue = 0; Area_MaxValue = 0; if (Middle==50) { Area_MaxValue = 100; PlotOver2 = IIf(BarIndex() >= Sel_BarIndex - (x21-x20)-(x11-x10)-1 AND BarIndex() <= Sel_BarIndex - (x11-x10),Area_MaxValue,0); PlotOver1 = IIf(BarIndex() >= Sel_BarIndex - (x11-x10) -1 AND BarIndex() <= Sel_BarIndex,Area_MaxValue,0); if (SelectedValue(LDiv_Code)==15) { //Limit the blue bar for divergence to 2nd half of area for code 15 PlotDiverge = IIf(BarIndex() >= Sel_BarIndex - (x11-x10) -1 AND BarIndex() <= Sel_BarIndex AND Diverge=="Bullish",10,0); } if (SelectedValue(LDiv_Code)==13 OR SelectedValue(LDiv_Code)==17) { PlotDiverge = IIf(BarIndex() >= Sel_BarIndex - (x21-x20)-(x11-x10) -1 AND BarIndex() <= Sel_BarIndex AND Diverge=="Bullish",10,0); } } else { Area_MinValue = Min(y10,y20); PlotOver2 = IIf(BarIndex() >= Sel_BarIndex - (x21-x20)-(x11-x10) -1 AND BarIndex() <= Sel_BarIndex - (x11-x10) -2,Area_MinValue,0); PlotOver1 = IIf(BarIndex() >= Sel_BarIndex - (x11-x10) -1 AND BarIndex() <= Sel_BarIndex,Area_MinValue,0); } _TRACE("Plot - Bull #00C - PlotDiverge " + WriteVal(PlotDiverge,1.0)); } // end if backtest
NOTE: THIS IS THE FOURTH OF FOUR...COMBINE THIS CODE WITH THE PREVIOUS THREE... if (Diag_Trace==1) { _TRACE("Bull Reversal #0 - Backtest " + WriteVal(Backtest,1.0)); _TRACE("Bull Reversal #1 - DivCode: " + WriteVal(LDiv_Code,1.0) + " LB DivCode " + WriteVal(LDiv_Code[BarCount-1],1.0)); _TRACE("Bull Reversal #2 - Last Low " + WriteVal(LastLow,1.2)); _TRACE("Bull Reversal #3 - Area_MinValue " + WriteVal(Area_MinValue,1.2)); _TRACE("Bull Reversal #4 - CurrTrough " + WriteVal(Sel_CurrTrough,1.2) + " LastLow " + WriteVal(Sel_LastLow,1.2)); _TRACE("Bull Reversal #5 - FBar_CT " + WriteVal(SelectedValue(FirstBar_CurrGroup),1.0) + " BCurrTrough " + WriteVal(CurrTroughBars,1.0)); _TRACE("Bull Reversal #6 - CurrTrough " + WriteVal(CurrTrough,1.2) + " CTBars " + WriteVal(CurrTroughBars,1.0)); _TRACE("Bull Reversal #7 - LB_PrGroup " + WriteVal(LastBar_PriorGroup,1.0) + " FB_PrGroup " + WriteVal(FirstBar_PriorGroup,1.0)); _TRACE("Bull Reversal #8 - Tr_PrGroup " + WriteVal(Trough_PriorGroup,1.2) + " TrB_PrG " + WriteVal(TroughBars_PriorGroup,1.0)); _TRACE("Bull Reversal #9 - PrTrough " + WriteVal(PriorTrough,1.2) + " PrTrBars " + WriteVal(PriorTroughBars,1.0)); _TRACE("Bull Reversal #10 - xy1: x10=" + WriteVal(x10,1.0) + " y10=" + WriteVal(y10,1.2) + " x11=" + WriteVal(x11,1.0) + " y11=" + WriteVal(y11,1.2)); _TRACE("Bull Reversal #11 - xy2: x20=" + WriteVal(x20,1.0) + " y20=" + WriteVal(y20,1.2) + " x21=" + WriteVal(x21,1.0) + " y21=" + WriteVal(y21,1.2)); _TRACE("Bull Reversal - end =========================================== "); } // end Diag_Trace } // end Sel_TurnUp_OK else { LDiv_Code = 10; } return Sel_TurnUp_OK; } // end function BullishReversal function PlotTrendLines(PlotOver1,PlotOver2,BullBear) { //Plot Trendlines _TRACE("Plot - Enter Module "); Line1Color = IIf(BullBear=="Bullish",colorBlack,colorBrown); Line2Color = IIf(BullBear=="Bullish",colorGreen,colorYellow); Area1Color = IIf(BullBear=="Bullish",colorPaleGreen,colorLightYellow); Area2Color = IIf(BullBear=="Bullish",colorAqua,colorLightOrange); if (Trendlines ==1 OR Trendlines ==3) { Plot( Line1, "Trend line", Line1Color,styleDots|styleThick|styleNoLabel); Plot( Line2, "Trend line", Line2Color,styleDots|styleThick|styleNoLabel); } if (Trendlines>=2) { _TRACE("Plot - PlotDiverge " + WriteVal(PlotDiverge,1.0)); Plot(PlotDiverge,"",colorBlue,styleArea|styleNoLabel); Plot(Plotover1,"",Area1color,styleArea|styleNoLabel); Plot(Plotover2,"",Area2color,styleArea|styleNoLabel); } // end Trendlines _TRACE("Plot - End Module "); } // end PlotTrendlines // ******************************************************************************************* // //"Div - EndTime " + Now(2); // START OF CONTROL SECTION // Complete loop takes 7ms when no divergence is detected and 17 ms with divergence function DivergeControl(Osc_Issue,Middle) { Backtest = Status("action")==5; //Declare Variable and set up parameters ParameterSetup(Osc_Issue); //Process Bearish Reversal and Continuation RunStatus = Status("action"); StartTime = Now(4); _TRACE("Timer Start - " + WriteVal(StartTime,1.0)); Sel_TurnDn_OK = DetectBearishReversal(Osc_Issue,Middle); _TRACE("Timer End - " + WriteVal(Now(4),1.0) + " Diff " + WriteVal((Now(4) - StartTime),1.4)); //_TRACE("Control Section - DivCode " + WriteVal(SDiv_Code,1.0) + " LB DivCode " + WriteVal(SDiv_Code[BarCount-1],1.0)); Sel_TurnUp_OK = DetectBullishReversal(Osc_Issue,Middle); if (Backtest == 0 AND (Sel_TurnDn_OK==1 OR Sel_TurnUp_OK==1)) { Bull_Bear = WriteIf(Sel_TurnDn_OK==1,"Bearish",WriteIf(Sel_TurnUp_OK==1,"Bullish","")); PlotTrendlines(PlotOver1,PlotOver2,Bull_Bear); } _TRACE("Control Section - end =============================================================="); _TRACE("Control Section - end =============================================================="); if (Status("action")==1 AND Alerts==1) { _TRACE("Alert section - Bull Code " + WriteVal(LDiv_Code,1.0) + " Bear Code " + WriteVal(SDiv_Code,1.0)); AlertIf(LDiv_Code==11,"Sound D:\\Amibroker\\Wav Files\\Lower Low.wav","",6,12,0 ); AlertIf((LDiv_Code==12 OR LDiv_Code==13 OR LDiv_Code==14) AND Bull_Diverge==0,"Sound D:\\Amibroker\\Wav Files\\Higher Low.wav","",5,12,0 ); AlertIf((LDiv_Code==12 OR LDiv_Code==13 OR LDiv_Code==14) AND Bull_Diverge==1,"Sound D:\\Amibroker\\Wav Files\\Bull Diverge.wav","",6,12,0 ); AlertIf(SDiv_Code==11,"Sound D:\\Amibroker\\Wav Files\\Higher High.wav","",6,12,0 ); AlertIf((SDiv_Code==12 OR SDiv_Code==13 OR SDiv_Code==14) AND Bear_Diverge==0,"Sound D:\\Amibroker\\Wav Files\\Lower High.wav","",7,12,0 ); AlertIf((SDiv_Code==12 OR SDiv_Code==13 OR SDiv_Code==14) AND Bear_Diverge==1,"Sound D:\\Amibroker\\Wav Files\\Bear Diverge.wav","",8,12,0 ); } return Pattern + " - " + Diverge; } // end DivergeControl Plot(CCI(20),"CCI(20)",colorRed,styleLine); divergeControl(Issue,Middle);
//ADX CODE ADXcolor= IIf(ADX(14)>=30,colorLime,colorRed); Plot(ADX(14),"ADX(14)",ADXcolor,styleLine,styleNoTitle); Plot(30,"",colorBlue,styleLine); Title="ADX(14)";
My apologies to all who are having problems getting the divergence indicator to work....here is a direct link to Amibrokers web page and you can down load it in its entirety from there... -Dennis http://www.amibroker.com/library/detail.php?id=586
here is another divergence indicator I use. I add it to the same window as the previous divergence indicator, when the two give the same signal, then this is added confirmation... just change this line of code... trendMom = RSI(period); to this... trendMom = CCI(20); http://www.amibroker.com/library/detail.php?id=374
Dennis: Can you post a chart of your divergence indicator for some of us non-amibroker followers. Thanks, Shermdog