ZigZag_XMACandles

Author: Copyright © 2016, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
ZigZag_XMACandles
ÿþ//+------------------------------------------------------------------+

//|                                            ZigZag_XMACandles.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_XMACandles" 

//---- >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=> 7 1CD5@>2

#property indicator_buffers 7

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

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

//---- 2 :0G5AB25 8=48:0B>@0 8A?>;L7>20=K F25B=K5 A25G8

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1   clrMediumOrchid,clrSlateBlue,clrGreen

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

#property indicator_label1  "XMACandles Open;XMACandles High;XMACandles Low;XMACandles Close"

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

//|  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_XMACandles"

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

#property indicator_color2 clrPurple

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

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

//|  ?8A0=85 :;0AA0 CXMA                        |

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

#include <SmoothAlgorithms.mqh>

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

//|  >1JO2;5=85 ?5@5G8A;5=89                     |

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

/*enum Smooth_Method - ?5@5G8A;5=85 >1JO2;5=> 2 D09;5 SmoothAlgorithms.mqh

  {

   MODE_SMA_,  // SMA

   MODE_EMA_,  // EMA

   MODE_SMMA_, // SMMA

   MODE_LWMA_, // LWMA

   MODE_JJMA,  // JJMA

   MODE_JurX,  // JurX

   MODE_ParMA, // ParMA

   MODE_T3,    // T3

   MODE_VIDYA, // VIDYA

   MODE_AMA,   // AMA

  }; */

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

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

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

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

input Smooth_Method MA_Method1=MODE_T3;     // 5B>4 CA@54=5=8O ?5@2>3> A3;06820=8O 

input int Length1=12;                       // ;C18=0 ?5@2>3> A3;06820=8O                    

input int Phase1=15;                        // 0@0<5B@ ?5@2>3> A3;06820=8O, 

//--- 4;O JJMA 87<5=ONI89AO 2 ?@545;0E -100 ... +100, 2;8O5B =0 :0G5AB2> ?5@5E>4=>3> ?@>F5AA0;

//--- ;O VIDIA MB> ?5@8>4 CMO, 4;O AMA MB> ?5@8>4 <54;5==>9 A:>;L7OI59

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

input uint ExtDepth=3;

input double ExtDeviation=3.0;

input uint ExtBackstep=10;

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

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

double ExtOpenBuffer[];

double ExtHighBuffer[];

double ExtLowBuffer[];

double ExtCloseBuffer[];

double ExtColorBuffer[];

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

//---- 1JO2;5=85 F5;KE ?5@5<5==KE 4;O E5=4;>2 8=48:0B>@>2

int Ind_Handle;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

   Ind_Handle=iCustom(NULL,0,"XMACandles",MA_Method1,Length1,Phase1,0);

   if(Ind_Handle==INVALID_HANDLE)

     {

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

      return(INIT_FAILED);

     }

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

   min_rates_1=int(GetStartBars(MA_Method1, Length1, Phase1));

   min_rates_total=min_rates_1+int(ExtDepth+ExtBackstep);



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

   SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 F25B>2>9, 8=45:A=K9 1CD5@   

   SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX);

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

   ArraySetAsSeries(ExtOpenBuffer,true);

   ArraySetAsSeries(ExtHighBuffer,true);

   ArraySetAsSeries(ExtLowBuffer,true);

   ArraySetAsSeries(ExtCloseBuffer,true);

   ArraySetAsSeries(ExtColorBuffer,true);

   

   SetIndexBuffer(5,LowestBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,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);

//---- 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,_Digits);

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

   IndicatorSetString(INDICATOR_SHORTNAME,"ZigZag_MACD");

//---   

   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(Ind_Handle)<rates_total || rates_total<min_rates_total) return(RESET);



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

   int to_copy,limit,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

      //---- 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 <0AA82K

   if(CopyBuffer(Ind_Handle,0,0,to_copy,ExtOpenBuffer)<=0) return(RESET);

   if(CopyBuffer(Ind_Handle,1,0,to_copy,ExtHighBuffer)<=0) return(RESET);

   if(CopyBuffer(Ind_Handle,2,0,to_copy,ExtLowBuffer)<=0) return(RESET);

   if(CopyBuffer(Ind_Handle,3,0,to_copy,ExtCloseBuffer)<=0) return(RESET);

   if(CopyBuffer(Ind_Handle,4,0,to_copy,ExtColorBuffer)<=0) return(RESET);



//---- 5@2K9 1>;LH>9 F8:; @0AGQB0 7837030

   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=ExtLowBuffer[ArrayMinimum(ExtLowBuffer,bar,ExtDepth)];

      if(val==lastlow0) val=EMPTY_VALUE;

      else

        {

         lastlow0=val;

         if(ExtLowBuffer[bar]-val>ExtDeviation*_Point) 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=ExtHighBuffer[ArrayMaximum(ExtHighBuffer,bar,ExtDepth)];

      if(val==lasthigh0) val=EMPTY_VALUE;

      else

        {

         lasthigh0=val;

         if(val-ExtHighBuffer[bar]>ExtDeviation*_Point) 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 7837030

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