Author: Copyright © 2016, MetaQuotes Software Corp.
Indicators Used
Larry William percent range indicator
1 Views
0 Downloads
0 Favorites
ZigZag_WPR
ÿþ//+------------------------------------------------------------------+

//|                                                   ZigZag_WPR.mq5 |

//|                      Copyright © 2016, MetaQuotes Software Corp. |

//|                                       http://www.metaquotes.net/ |

//+------------------------------------------------------------------+ 

//---- 02B>@AB2> 8=48:0B>@0

#property copyright "Copyright © 2016, MetaQuotes Software Corp."

//---- AAK;:0 =0 A09B 02B>@0

#property link      "http://www.metaquotes.net/"

//---- =><5@ 25@A88 8=48:0B>@0

#property version   "1.00"

#property description "ZigZag" 

//---- >B@8A>2:0 8=48:0B>@0 2 >B45;L=>< >:=5

#property indicator_separate_window 

//---- 4;O @0AGQB0 8 >B@8A>2:8 8=48:0B>@0 8A?>;L7>20=> 3 1CD5@0

#property indicator_buffers 3

//---- 8A?>;L7>20=> 2A53> 2 3@0D8G5A:8E ?>AB@>5=8O

#property indicator_plots   2

//+----------------------------------------------+

//|  0@0<5B@K >B@8A>2:8 >AF8;;OB>@0             |

//+----------------------------------------------+

//---- >B@8A>2:0 8=48:0B>@0 1 2 2845 ;8=88

#property indicator_type1   DRAW_LINE

//---- 2 :0G5AB25 F25B0 ;8=88 8=48:0B>@0 8A?>;L7>20= Teal F25B

#property indicator_color1  clrTeal

//---- ;8=8O 8=48:0B>@0 1 - =5?@5@K2=0O :@820O

#property indicator_style1  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 1 @02=0 1

#property indicator_width1  1

//---- >B>1@065=85 1KG59 <5B:8 8=48:0B>@0

#property indicator_label1  "WPR Oscillator"

//+----------------------------------------------+ 

//|  0@0<5B@K >B@8A>2:8 7837030                 |

//+----------------------------------------------+

//---- 2 :0G5AB25 8=48:0B>@0 8A?>;L7>20= ZIGZAG

#property indicator_type2   DRAW_ZIGZAG

//---- >B>1@065=85 <5B:8 8=48:0B>@0

#property indicator_label2  "ZigZag_WPR"

//---- 2 :0G5AB25 F25B0 ;8=88 8=48:0B>@0 8A?>;L7>20= Gold F25B

#property indicator_color2 clrGold

//---- ;8=8O 8=48:0B>@0 - =5?@5@K2=0O :@820O

#property indicator_style2  STYLE_SOLID

//---- B>;I8=0 ;8=88 8=48:0B>@0 @02=0 2

#property indicator_width2  2

//+----------------------------------------------+

//|  >1JO2;5=85 :>=AB0=B                         |

//+----------------------------------------------+

#define RESET  0 // >=AB0=B0 4;O 2>72@0B0 B5@<8=0;C :><0=4K =0 ?5@5AGQB 8=48:0B>@0

//+----------------------------------------------+ 

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                 |

//+----------------------------------------------+ 

//---- 2E>4=K5 ?0@0<5B@K >AF8;;OB>@0 WPR

input uint WPR_period=13;                                   // ?5@8>4 WPR  

//---- 2E>4=K5 ?0@0<5B@K 7837030

input uint ExtDepth=3;

input uint ExtDeviation=3;

input uint ExtBackstep=3;

input int  HighLevel=-30;                                  // C@>25=L ?5@570:C?0

input int  LowLevel=-70;                                   // C@>25=L ?5@5?@>40==>AB8

//+----------------------------------------------+

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2 

// 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double LowestBuffer[],HighestBuffer[],WPRBuffer[];



//---- 1JO2;5=85 ?5@5<5==KE ?0<OB8 4;O ?5@5AGQB0 8=48:0B>@0 B>;L:> =0 =5?>AG8B0==KE 10@0E

int LASTlowpos,LASThighpos,LASTColor;

double LASTlow0,LASTlow1,LASThigh0,LASThigh1;



//---- 1JO2;5=85 F5;KE ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

int min_rates_1,min_rates_total,WPR_Handle;

//+------------------------------------------------------------------+ 

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+ 

int OnInit()

  {

//--- ?>;CG5=85 E5=4;0 8=48:0B>@0 iWPR

   WPR_Handle=iWPR(NULL,0,WPR_period);

   if(WPR_Handle==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 iWPR");

      return(INIT_FAILED);

     }

//---- =8F80;870F8O ?5@5<5==KE =0G0;0 >BAGQB0 40==KE

   min_rates_1=int(WPR_period);

   min_rates_total=int(min_rates_1+ExtDepth+ExtBackstep);



//---- ?@52@0I5=85 48=0<8G5A:8E <0AA82>2 2 8=48:0B>@=K5 1CD5@K

   SetIndexBuffer(0,WPRBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowestBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,HighestBuffer,INDICATOR_DATA);

//---- 70?@5B =0 >B@8A>2:C 8=48:0B>@>< ?CABKE 7=0G5=89

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- 8=45:A0F8O M;5<5=B>2 2 1CD5@0E :0: 2 B09<A5@8OE   

   ArraySetAsSeries(LowestBuffer,true);

   ArraySetAsSeries(HighestBuffer,true);

   ArraySetAsSeries(WPRBuffer,true);

//---- CAB0=>2:0 ?>78F88, A :>B>@>9 =0G8=05BAO >B@8A>2:0 C@>2=59 >;;8=465@0

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_1);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

//---- #AB0=>2:0 D>@<0B0 B>G=>AB8 >B>1@065=8O 8=48:0B>@0

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//---- 8<O 4;O >:>= 40==KE 8 ;M910 4;O AC1J>:>= 

   IndicatorSetString(INDICATOR_SHORTNAME,"ZigZag_WPR");

//---- :>;8G5AB2>  3>@87>=B0;L=KE C@>2=59 8=48:0B>@0 3   

   IndicatorSetInteger(INDICATOR_LEVELS,3);

//---- 7=0G5=8O 3>@87>=B0;L=KE C@>2=59 8=48:0B>@0   

   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,HighLevel);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,-50);

   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,LowLevel);

//---- 2 :0G5AB25 F25B>2 ;8=89 3>@87>=B0;L=KE C@>2=59 8A?>;L7>20=K

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrBlue);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrGray);

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrMagenta);

//---- 2 ;8=88 3>@87>=B0;L=>3> C@>2=O 8A?>;L7>20= :>@>B:89 HB@8E-?C=:B8@  

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASHDOTDOT);

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,2,STYLE_DASHDOTDOT);

//---   

   return(INIT_SUCCEEDED); 

  }

//+------------------------------------------------------------------+ 

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+ 

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AGQB0

   if(BarsCalculated(WPR_Handle)<rates_total || rates_total<min_rates_total) return(RESET);



//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE 

   int to_copy,limit,climit,bar,back,lasthighpos,lastlowpos;

   double curlow,curhigh,lasthigh0=EMPTY_VALUE,lastlow0=EMPTY_VALUE,lasthigh1,lastlow1,val,res;



//---- @0AGQB AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AGQB0 10@>2 8 AB0@B>20O 8=8F80;870F8O ?5@5<5==KE

   if(prev_calculated>rates_total || prev_calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AGQB0 8=48:0B>@0

     {

      limit=rates_total-min_rates_total; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2

      lastlow1=-1;

      lasthigh1=-1;

      lastlowpos=-1;

      lasthighpos=-1;

     }

   else

     {

      limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AGQB0 =>2KE 10@>2

      climit=limit+min_rates_total; // AB0@B>2K9 =><5@ 4;O @0A:@0A:8 8=48:0B>@0



      //---- 2>AAB0=02;8205< 7=0G5=8O ?5@5<5==KE

      lastlow0=LASTlow0;

      lasthigh0=LASThigh0;



      lastlow1=LASTlow1;

      lasthigh1=LASThigh1;



      lastlowpos=LASTlowpos+limit;

      lasthighpos=LASThighpos+limit;

     }

    to_copy=limit+1;



//--- :>?8@C5< 2=>2L ?>O282H85AO 40==K5 2 <0AA82

   if(CopyBuffer(WPR_Handle,0,0,to_copy,WPRBuffer)<=0) return(RESET);



//---- 5@2K9 1>;LH>9 F8:; @0AGQB0 8=48:0B>@0

   for(bar=limit; bar>=0 && !IsStopped(); bar--)

     {

      //---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5

      if(rates_total!=prev_calculated && bar==0)

        {

         LASTlow0=lastlow0;

         LASThigh0=lasthigh0;

        }

      LowestBuffer[bar]=EMPTY_VALUE;

      HighestBuffer[bar]=EMPTY_VALUE;

      //--- low

      val=WPRBuffer[ArrayMinimum(WPRBuffer,bar,ExtDepth)];

      if(val==lastlow0) val=EMPTY_VALUE;

      else

        {

         lastlow0=val;

         if(WPRBuffer[bar]-val>ExtDeviation) val=EMPTY_VALUE;

         else

           {

            for(back=1; back<=int(ExtBackstep); back++)

              {

               res=LowestBuffer[bar+back];

               if(res!=EMPTY_VALUE && res>val) LowestBuffer[bar+back]=EMPTY_VALUE;

              }

           }

        }

      LowestBuffer[bar]=val;



      //--- high

      val=WPRBuffer[ArrayMaximum(WPRBuffer,bar,ExtDepth)];

      if(val==lasthigh0) val=EMPTY_VALUE;

      else

        {

         lasthigh0=val;

         if(val-WPRBuffer[bar]>ExtDeviation) val=EMPTY_VALUE;

         else

           {

            for(back=1; back<=int(ExtBackstep); back++)

              {

               res=HighestBuffer[bar+back];

               if(res!=EMPTY_VALUE && res<val) HighestBuffer[bar+back]=EMPTY_VALUE;

              }

           }

        }

      HighestBuffer[bar]=val;

     }



//---- B>@>9 1>;LH>9 F8:; @0AGQB0 8=48:0B>@0

   for(bar=limit; bar>=0 && !IsStopped(); bar--)

     {

      //---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5

      if(rates_total!=prev_calculated && bar==0)

        {

         LASTlow1=lastlow1;

         LASThigh1=lasthigh1;

         //----

         LASTlowpos=lastlowpos;

         LASThighpos=lasthighpos;

        }



      curlow=LowestBuffer[bar];

      curhigh=HighestBuffer[bar];

      //---

      if(curlow==EMPTY_VALUE && curhigh==EMPTY_VALUE) continue;

      //---

      if(curhigh!=EMPTY_VALUE)

        {

         if(lasthigh1>0)

           {

            if(lasthigh1<curhigh) HighestBuffer[lasthighpos]=EMPTY_VALUE;

            else HighestBuffer[bar]=EMPTY_VALUE;

           }

         //---

         if(lasthigh1<curhigh || lasthigh1<0 || curhigh==EMPTY_VALUE || lasthigh1==EMPTY_VALUE)

           {

            lasthigh1=curhigh;

            lasthighpos=bar;

           }

         lastlow1=-1;

        }

      //----

      if(curlow!=EMPTY_VALUE)

        {

         if(lastlow1>0)

           {

            if(lastlow1>curlow) LowestBuffer[lastlowpos]=EMPTY_VALUE;

            else LowestBuffer[bar]=EMPTY_VALUE;

           }

         //---

         if(curlow<lastlow1 || lastlow1<0 || curlow==EMPTY_VALUE || lastlow1==EMPTY_VALUE)

           {

            lastlow1=curlow;

            lastlowpos=bar;

           }

         lasthigh1=-1;

        }

     }

//----     

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---