ColorBearsCandle_v2_ZZ

Author: Copyright © 2018, Nikolay Kositsin
1 Views
0 Downloads
0 Favorites
ColorBearsCandle_v2_ZZ
ÿþ//+------------------------------------------------------------------+

//|                                       ColorBearsCandle_v2_ZZ.mq5 |

//|                               Copyright © 2018, Nikolay Kositsin | 

//|                              Khabarovsk,   farria@mail.redcom.ru | 

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

#property copyright "Copyright © 2018, Nikolay Kositsin"

#property link "farria@mail.redcom.ru"

#property description "ColorBearsCandle"

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

#property version   "1.00"

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

#property indicator_separate_window

//---- 4;O @0AG5B0 8 >B@8A>2:8 8=48:0B>@0 8A?>;L7>20=> A5<L 1CD5@>2

#property indicator_buffers 7

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

#property indicator_plots   2

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

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

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

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

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1   clrDodgerBlue,clrGray,clrMagenta

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

#property indicator_label1  "Open;High;Low;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"

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

#property indicator_color2 clrDarkGray

//---- ;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@5AG5B 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

  }; */

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

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

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

enum ColorMode

  {

   MODE_Candeles,        //:@0A:0 A25G ?> Open 8 Close

   MODE_ColorBearsOpen,  //:@0A:0 A25G ?> 3@0D8:C 8=48:0B>@0 ColorBears[Open]

   MODE_ColorBearsHigh,  //:@0A:0 A25G ?> 3@0D8:C 8=48:0B>@0 ColorBears[High]

   MODE_ColorBearsLow,   //:@0A:0 A25G ?> 3@0D8:C 8=48:0B>@0 ColorBears[Low]

   MODE_ColorBearsClose  //:@0A:0 A25G ?> 3@0D8:C 8=48:0B>@0 ColorBears[Close]

  };

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

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

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

input ColorMode CandelesColorMode=MODE_ColorBearsClose; //;3>@8B< >:@0A:8 A25G

input Smooth_Method MA_Method1=MODE_SMA_; //5B>4 CA@54=5=8O 8=48:0B>@0 

input uint Length1=12; //;C18=0 CA@54=5=8O 8=48:0B>@0                   

input int Phase1=15; //0@0<5B@ CA@54=5=8O 8=48:0B>@0,

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

input Smooth_Method MA_Method2=MODE_JJMA; //5B>4 A3;06820=8O 8B>3>2>3> 8=48:0B>@0

input uint Length2=5; //;C18=0 A3;06820=8O 8B>3>2>3> 8=48:0B>@0

input int Phase2=15;  //0@0<5B@ A3;06820=8O 8B>3>2>3> 8=48:0B>@0,

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

input int Shift=0;                                 //A4283 8=48:0B>@0 ?> 3>@87>=B0;8 2 10@0E

input uint Gap=50;                                     //@07<5@ =5CG8BK205<>3> 3M?0 2 ?C=:B0E

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

input uint ExtDepth=3;

input uint ExtDeviation=3;

input uint ExtBackstep=3;

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

//---- >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 ExtOpenBuffer[],ExtHighBuffer[],ExtLowBuffer[],ExtCloseBuffer[],ExtColorBuffer[];

double LowestBuffer[],HighestBuffer[];

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

int LASTlowpos,LASThighpos;

double LASTlow0,LASTlow1,LASThigh0,LASThigh1;

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

int min_rates_total;

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

int Ind_Handle[4];

double dGap;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---- 8=8F80;870F8O 3;>10;L=KE ?5@5<5==KE 

   min_rates_total=GetStartBars(MA_Method1,Length1,Phase1);

   min_rates_total+=GetStartBars(MA_Method2,Length2,Phase2);

   min_rates_total++;

   dGap=Gap*_Point;

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

   Ind_Handle[0]=iCustom(NULL,0,"ColorBears",MA_Method1,Length1,Phase1,MA_Method2,Length2,Phase2,PRICE_OPEN,0);

   if(Ind_Handle[0]==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 ColorBears["+string(0)+"]!");

      return(INIT_FAILED);

     }

//----

   Ind_Handle[1]=iCustom(NULL,0,"ColorBears",MA_Method1,Length1,Phase1,MA_Method2,Length2,Phase2,PRICE_HIGH,0);

   if(Ind_Handle[1]==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 ColorBears["+string(1)+"]!");

      return(INIT_FAILED);

     }

//----

   Ind_Handle[2]=iCustom(NULL,0,"ColorBears",MA_Method1,Length1,Phase1,MA_Method2,Length2,Phase2,PRICE_LOW,0);

   if(Ind_Handle[2]==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 ColorBears["+string(2)+"]!");

      return(INIT_FAILED);

     }

//----

   Ind_Handle[3]=iCustom(NULL,0,"ColorBears",MA_Method1,Length1,Phase1,MA_Method2,Length2,Phase2,PRICE_CLOSE,0);

   if(Ind_Handle[3]==INVALID_HANDLE)

     {

      Print(" 5 C40;>AL ?>;CG8BL E5=4; 8=48:0B>@0 ColorBears["+string(3)+"]!");

      return(INIT_FAILED);

     }

//---- ?@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);

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAG5B0 >B@8A>2:8 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

//---- >ACI5AB2;5=85 A42830 8=48:0B>@0 ?> 3>@87>=B0;8

   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);

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

   SetIndexBuffer(5,LowestBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,HighestBuffer,INDICATOR_DATA);

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

   ArraySetAsSeries(LowestBuffer,true);

   ArraySetAsSeries(HighestBuffer,true);

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

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

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

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

//---- 70?@5B =0 >B>1@065=85 7=0G5=89 8=48:0B>@0 2 ;52>< 25@E=5< C3;C >:=0 8=48:0B>@0

   PlotIndexSetInteger(1,PLOT_SHOW_DATA,false);

//---- >ACI5AB2;5=85 A42830 8=48:0B>@0 ?> 3>@87>=B0;8

   PlotIndexSetInteger(1,PLOT_SHIFT,Shift);

//---- CAB0=>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 <5B:0 4;O ?>4>:>= 

   string short_name="ColorBearsCandle_v2_ZZ";

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- 7025@H5=85 8=8F80;870F88

   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 @0AG5B0

   if(BarsCalculated(Ind_Handle[0])<rates_total

      || BarsCalculated(Ind_Handle[1])<rates_total

      || BarsCalculated(Ind_Handle[2])<rates_total

      || BarsCalculated(Ind_Handle[3])<rates_total

      || rates_total<min_rates_total)

      return(RESET);

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

   int to_copy,limit;

//---- @0AG5BK =5>1E>48<>3> :>;8G5AB20 :>?8@C5<KE 40==KE 8 AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AG5B0 10@>2

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

     {

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

     }

   else

     {

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

     }

//----

   to_copy=limit+1;

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

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

   if(CopyBuffer(Ind_Handle[2],0,0,to_copy,ExtHighBuffer)<=0) return(RESET);

   if(CopyBuffer(Ind_Handle[1],0,0,to_copy,ExtLowBuffer)<=0) return(RESET);

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

   switch(CandelesColorMode)

     {

      case  MODE_ColorBearsOpen:  if(CopyBuffer(Ind_Handle[0],1,0,to_copy,ExtColorBuffer)<=0) return(RESET); break;

      case  MODE_ColorBearsHigh:  if(CopyBuffer(Ind_Handle[1],1,0,to_copy,ExtColorBuffer)<=0) return(RESET); break;

      case  MODE_ColorBearsLow:   if(CopyBuffer(Ind_Handle[2],1,0,to_copy,ExtColorBuffer)<=0) return(RESET); break;

      case  MODE_ColorBearsClose: if(CopyBuffer(Ind_Handle[3],1,0,to_copy,ExtColorBuffer)<=0) return(RESET); break;

     }

     

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

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(close,true);



//---- >A=>2=>9 F8:; 8A?@02;5=8O 8 >:@0H820=8O A25G59

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

     {

      double Max=MathMax(ExtOpenBuffer[bar],ExtCloseBuffer[bar]);

      double Min=MathMin(ExtOpenBuffer[bar],ExtCloseBuffer[bar]);

      //----

      ExtHighBuffer[bar]=MathMax(Max,ExtHighBuffer[bar]);

      ExtLowBuffer[bar]=MathMin(Min,ExtLowBuffer[bar]);

      //---- C40;O5< =5ACI5AB2CNI85 35?K

      if(MathAbs(open[bar]-close[bar+1])<=dGap) ExtOpenBuffer[bar]=ExtCloseBuffer[bar+1];

      //----

      if(CandelesColorMode==MODE_Candeles)

        {

         if(ExtOpenBuffer[bar]<ExtCloseBuffer[bar]) ExtColorBuffer[bar]=0.0;

         else if(ExtOpenBuffer[bar]>ExtCloseBuffer[bar]) ExtColorBuffer[bar]=2.0;

         else ExtColorBuffer[bar]=1.0;

        }

     }

//----    

   int back,lasthighpos,lastlowpos;

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

//---- @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-int(ExtDepth)-1; // AB0@B>2K9 =><5@ 4;O @0AGQB0 2A5E 10@>2

      lastlow1=DBL_MIN;

      lasthigh1=DBL_MIN;

      lastlowpos=lasthighpos=limit+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;

     }

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

   for(int 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;

         dev=val-ExtHighBuffer[bar];

         if(dev>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=ExtHighBuffer[ArrayMaximum(ExtHighBuffer,bar,ExtDepth)];

      if(val==lasthigh0) val=EMPTY_VALUE;

      else

        {

         lasthigh0=val;

         dev=ExtLowBuffer[bar]-val;

         if(dev>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(int 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!=DBL_MIN)

           {

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

            else HighestBuffer[bar]=EMPTY_VALUE;

           }

         //---

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

           {

            lasthigh1=curhigh;

            lasthighpos=bar;

           }

         lastlow1=DBL_MIN;

        }

      //----

      if(curlow!=EMPTY_VALUE)

        {

         if(lastlow1!=DBL_MIN)

           {

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

            else LowestBuffer[bar]=EMPTY_VALUE;

           }

         //---

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

           {

            lastlow1=curlow;

            lastlowpos=bar;

           }

         lasthigh1=DBL_MIN;

        }

     }

//----

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