Zigzag2_R_Color_Price

Author: Copyright © 2005, MetaQuotes Software Corp.
1 Views
0 Downloads
0 Favorites
Zigzag2_R_Color_Price
ÿþ//+------------------------------------------------------------------+

//|                                        Zigzag2_R_Color_Price.mq5 |

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

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

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

/*  AB0=40@B=>< 8=48:0B>@5 http://codebase.mql4.com/ru/238 4;O      |

@8A>20=8O 8A?>;L7>20;AO AB8;L DRAW_SECTION. -B>B AB8;L ?>72>;O5B     |

@8A>20BL >B@57:0<8 B>;L:> <564C B>G:0<8, =0E>4OI8<8<AO =0 @07=KE     |

10@0E. !B8;L >B@8A>2:8 DRAW_ZIGZAG ?>72>;O5B A=OBL MB> >3@0=8G5=85,  |

4;O MB>3> 8A?>;L7CNBAO 420 1CD5@0 2<5AB> >4=>3>. ;O 8;;NAB@0F88     |

MB>3> AB8;O 8 1K; =0?8A0= Zigzag2_R_.mq5.                            |

 :>4 4>102;5=0 >1@01>B:0 2=5H=53>  10@0 (outside bar), :>340  High  |

B5:CI53> 10@0 2KH5 ?@54K4CI8E, 0 Low B5:CI53> 10@0 =865 ?@54K4CI8E.  |

;>: >1@01>B:8 2=5H=53> 10@0 40= :0: ?@8<5@. K <>65B5 8A?>;L7>20BL  |

A2>9 0;3>@8B< 4;O B0:>9 A8BC0F88.                                    |

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

*/

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

#property copyright "Copyright © 2005, 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 3;02=>< >:=5

#property indicator_chart_window 

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

#property indicator_buffers 5

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

#property indicator_plots   1

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

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

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

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

#property indicator_type1   DRAW_COLOR_ZIGZAG

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

#property indicator_label1  "Zigzag2_R_Color_Price"

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

#property indicator_color1 clrMediumVioletRed,clrMediumSeaGreen

//---- ;8=8O 8=48:0B>@0 - A?;>H=0O

#property indicator_style1  STYLE_SOLID

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

#property indicator_width1  3

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

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

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

input string  SirName="Zigzag2_R_Color_Price";//5@20O G0ABL 8<5=8 3@0D8G5A:8E >1J5:B>2

input int ExtDepth=12;

input int ExtDeviation=5;

input int ExtBackstep=3;

input bool ShowPrice=true;

input uint nSize=1;

input color UpColor=clrBlue;

input color DnColor=clrMagenta;

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

//---- >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 ZigzagPeakBuffer[],ZigzagLawnBuffer[];

double HighMapBuffer[],LowMapBuffer[];

double ColorBuffer[];

int level=3; // recounting's depth 

bool downloadhistory=false;

double dExtDeviation;

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

int min_rates_total;

string UpLable_,DnLable_;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

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

   min_rates_total=ExtDepth+ExtBackstep;

   dExtDeviation=ExtDeviation*_Point;

   UpLable_=SirName+"_Up_";

   DnLable_=SirName+"_Dn_";



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

   SetIndexBuffer(0,ZigzagLawnBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ZigzagPeakBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ColorBuffer,INDICATOR_COLOR_INDEX);

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

   SetIndexBuffer(3,HighMapBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,LowMapBuffer,INDICATOR_CALCULATIONS);

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

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,NULL);

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

   ArraySetAsSeries(ZigzagLawnBuffer,true);

   ArraySetAsSeries(ZigzagPeakBuffer,true);

   ArraySetAsSeries(ColorBuffer,true);

   ArraySetAsSeries(HighMapBuffer,true);

   ArraySetAsSeries(LowMapBuffer,true);

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

   PlotIndexSetInteger(0,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,_Digits);

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

   string shortname;

   StringConcatenate(shortname,"Zigzag2_R_Color_Price(ExtDepth=",

                     ExtDepth,"ExtDeviation = ",ExtDeviation,"ExtBackstep = ",ExtBackstep,")");

   IndicatorSetString(INDICATOR_SHORTNAME,shortname);

//----   

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//----

   ObjectsDeleteAll(0,SirName,0,OBJ_ARROW_RIGHT_PRICE);

//----

   ChartRedraw(0);

  }

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

//| 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(rates_total<min_rates_total) return(0);



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

   bool Max,Min;

   int limit,climit,bar;

   int iii=0,counterZ=0,whatlookfor=0;

   int back,lasthighpos,lastlowpos;

   double val,res;

   double curlow=9999999999.0,curhigh=0.0,lasthigh=0.0,lastlow=999999999.0;

   string stime;



//---- @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

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

      ArrayInitialize(ZigzagPeakBuffer,NULL);

      ArrayInitialize(ZigzagLawnBuffer,NULL);

      ArrayInitialize(HighMapBuffer,NULL);

      ArrayInitialize(LowMapBuffer,NULL);

     }

   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



      //----

      while(counterZ<level && iii<100)

        {

         res=(ZigzagPeakBuffer[iii]+ZigzagLawnBuffer[iii]);

         //----

         if(res) counterZ++;

         iii++;

        }

      iii--;

      limit=iii;

      //----

      if(LowMapBuffer[iii])

        {

         curlow=LowMapBuffer[iii];

         whatlookfor=1;

        }

      else

        {

         curhigh=HighMapBuffer[iii];

         whatlookfor=-1;

        }

      //----

      for(iii=limit-1; iii>=0; iii--)

        {

         ZigzagPeakBuffer[iii] = NULL;

         ZigzagLawnBuffer[iii] = NULL;

         LowMapBuffer[iii]=NULL;

         HighMapBuffer[iii]=NULL;

        }

     }



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

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(time,true);



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

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

     {

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

      //----

      if(val==lastlow)

         val=NULL;

      else

        {

         lastlow=val;

         //----

         if(low[bar]-val>dExtDeviation)

            val=NULL;

         else

           {

            //----

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

              {

               res=LowMapBuffer[bar+back];

               //----

               if(res && res>val) LowMapBuffer[bar+back]=NULL;

              }

           }

        }

      //----

      if(low[bar]==val)

         LowMapBuffer[bar]=val;

      else

         LowMapBuffer[bar]=NULL;

      //--- high

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

      //----

      if(val==lasthigh)

         val=NULL;

      else

        {

         lasthigh=val;

         //----

         if(val-high[bar]>dExtDeviation)

            val=NULL;

         else

           {

            //----

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

              {

               res=HighMapBuffer[bar+back];

               //----

               if(res && res<val) HighMapBuffer[bar+back]=NULL;

              }

           }

        }

      //----

      if(high[bar]==val)

         HighMapBuffer[bar]=val;

      else

         HighMapBuffer[bar]=NULL;

     }



//---- final cutting 

   if(!whatlookfor)

     {

      lastlow=NULL;

      lasthigh=NULL;

     }

   else

     {

      lastlow=curlow;

      lasthigh=curhigh;

     }



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

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

     {

      res=NULL;

      switch(whatlookfor)

        {

         // look for peak or lawn 

         case 0: if(!lastlow && !lasthigh)

           {

            if(HighMapBuffer[bar])

              {

               lasthigh=high[bar];

               lasthighpos = bar;

               whatlookfor = -1;

               ZigzagPeakBuffer[bar]=lasthigh;

               stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

               CreateArrowRightPrice(0,UpLable_+stime,0,time[bar],lasthigh,UpColor,nSize);

               res=1;

              }

            if(LowMapBuffer[bar])

              {

               lastlow=low[bar];

               lastlowpos=bar;

               whatlookfor=1;

               ZigzagLawnBuffer[bar]=lastlow;

               stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

               CreateArrowRightPrice(0,DnLable_+stime,0,time[bar],lastlow,DnColor,nSize);

               res=1;

              }

           }

         break;

         // look for peak

         case 1: if(LowMapBuffer[bar] && LowMapBuffer[bar]<lastlow && !HighMapBuffer[bar])

           {

            ZigzagLawnBuffer[lastlowpos]=NULL;

            stime=TimeToString(time[lastlowpos],TIME_DATE|TIME_MINUTES);

            ObjectDelete(0,DnLable_+stime);

            lastlowpos=bar;

            lastlow=LowMapBuffer[bar];

            ZigzagLawnBuffer[bar]=lastlow;

            stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

            CreateArrowRightPrice(0,DnLable_+stime,0,time[bar],lastlow,DnColor,nSize);

            res=1;

           }

         if(HighMapBuffer[bar] && !LowMapBuffer[bar])

           {

            lasthigh=HighMapBuffer[bar];

            lasthighpos=bar;

            ZigzagPeakBuffer[bar]=lasthigh;

            stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

            CreateArrowRightPrice(0,UpLable_+stime,0,time[bar],lasthigh,UpColor,nSize);

            whatlookfor=-1;

            res=1;

           }

         break;

         // look for lawn

         case -1:  if(HighMapBuffer[bar] && HighMapBuffer[bar]>lasthigh && !LowMapBuffer[bar])

           {

            ZigzagPeakBuffer[lasthighpos]=NULL;

            stime=TimeToString(time[lasthighpos],TIME_DATE|TIME_MINUTES);

            ObjectDelete(0,UpLable_+stime);

            lasthighpos=bar;

            lasthigh=HighMapBuffer[bar];

            ZigzagPeakBuffer[bar]=lasthigh;

            stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

            CreateArrowRightPrice(0,UpLable_+stime,0,time[bar],lasthigh,UpColor,nSize);

           }

         if(LowMapBuffer[bar] && !HighMapBuffer[bar])

           {

            lastlow=LowMapBuffer[bar];

            lastlowpos=bar;

            ZigzagLawnBuffer[bar]=lastlow;

            stime=TimeToString(time[bar],TIME_DATE|TIME_MINUTES);

            CreateArrowRightPrice(0,DnLable_+stime,0,time[bar],lastlow,DnColor,nSize);

            whatlookfor=1;

           }

         break;

         default: return(rates_total);

        }

     }

//---- "@5B89 1>;LH>9 F8:; @0A:@0A:8 8=48:0B>@0

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

     {

      Max=ZigzagPeakBuffer[bar];

      Min=ZigzagLawnBuffer[bar];



      if(!Max && !Min) ColorBuffer[bar]=ColorBuffer[bar+1];

      if(Max && Min)

        {

         if(ColorBuffer[bar+1]==0) ColorBuffer[bar]=1;

         else                      ColorBuffer[bar]=0;

        }



      if( Max && !Min) ColorBuffer[bar]=1;

      if(!Max &&  Min) ColorBuffer[bar]=0;

     }

//---- 

   ChartRedraw(0);

   return(rates_total);

  }

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

//|  !>740=85 B5:AB>2>9 ;591K                                        |

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

void CreateArrowRightPrice

(

 long   chart_id,         // 845=B8D8:0B>@ 3@0D8:0

 string name,             // 8<O >1J5:B0

 int    nwin,             // 8=45:A >:=0

 datetime Time,           // ?>;>65=85 F5=>2>9 <5B:8 ?> 2@5<5=8

 double Price,            // ?>;>65=85 F5=>2>9 <5B:8 ?> 25@B8:0;8

 color  Color,            // F25B B5:AB0

 uint    Size             // @07<5@ H@8DB0

 )

//---- 

  {

//----

   ObjectCreate(chart_id,name,OBJ_ARROW_RIGHT_PRICE,nwin,Time,Price);

   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);

   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,Size);

//ObjectSetInteger(chart_id,name,OBJPROP_BACK,true);

//----

  }

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

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 ---