Turning point

Author: Copyright 2021, MetaQuotes Software Corp.
Indicators Used
Moving average indicator
1 Views
0 Downloads
0 Favorites
Turning point
ÿþ//+------------------------------------------------------------------+

//|                                                Turning point.mq5 |

//|                        Copyright 2021, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2021, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.001"

//---

#include <MovingAverages.mqh>

//---

#property indicator_chart_window

#property indicator_buffers 34

#property indicator_plots   6

#property indicator_label1  "Sell"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrViolet

#property indicator_width1  2

#property indicator_label2  "Buy"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrAquamarine

#property indicator_width2  2

#property indicator_label3  "Middle"

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_color3  clrAqua,clrDeepPink,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

#property indicator_label4  "Upper"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrDarkGray

#property indicator_width4  1

#property indicator_style4  STYLE_DOT

#property indicator_label5  "Lower"

#property indicator_type5   DRAW_LINE

#property indicator_color5  clrDarkGray

#property indicator_width5  1

#property indicator_style5  STYLE_DOT

#property indicator_label6  "Coral"

#property indicator_type6   DRAW_COLOR_LINE

#property indicator_color6  clrGreen,clrRed,clrDarkGray

#property indicator_style6  STYLE_SOLID

#property indicator_width6  2

input group  "---- Pivot Time ----"

input int    DayStartHour                = 0;           // Hour of day start

input int    DayStartMinute              = 0;           // Minute of day start

input bool   AttachSundToMond            = true;        // Attach the sunday bars to monday

input group  "---- parameters ----"

input double InpScaleFactor              = 2.25;        // Scale factor

input int    InpMaPeriod                 = 3;           // Smooth Period

input int    InpVolatilityPeriod         = 70;          // Volatility Period

input double InpCoeffcop                 = 0.06349206;  // Coral Coefficient

input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price Coral

input group  "---- Signal ----"

input bool   Inpyna                      = true;        // 1 Volatility Signal

input bool   Inpdoy                      = false;       // 2 Coral Signal

input bool   Inptrei                     = false;       // 3 "Volatility < Coral" Signal

input bool   Inppatry                    = false;       // 4 "Coral < Volatility" Signal

input bool   Inpcinc                     = false;       // 5 Coral Signal Price

input bool   Inpshase                    = false;       // 6 Volatility Signal Price

input bool   Inpshapt                    = false;       // 7 Pivot Signal Price

input group  "---- Filter ----"

input bool   InpFiltryC                  = false;       // Filter Coral

input bool   InpFiltryM                  = false;       // Filter Volatility

input bool   InpFiltryP                  = false;       // Filter Pivot

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

int InpFastPeriod=int(InpVolatilityPeriod/7); //  Fast Period

int InpMode=2; //  1:Simple Mode 2:hybrid Mode

ENUM_MA_METHOD InpMaMethod=MODE_SMMA; // Ma Method

//---- will be used as indicator buffers

double UpArrowiCustom[];

double DnArrowiCustom[];

double BufferC[];

double BufferColors[];

double BufferCcop[];

double BufferColorscop[];

double BufferB1cop[];

double BufferB2cop[];

double BufferB3cop[];

double BufferB4cop[];

double BufferB5cop[];

double BufferB6cop[];

double BufferMAcop[];

//---

double UpperBuffer[];

double MiddleBuffer[];

double LowerBuffer[];

//---

double UpperMaBuffer[];

double MiddleMaBuffer[];

double LowerMaBuffer[];

//---

double BaseBuffer[];

//---

double HighBuffer[];

double LowBuffer[];

double CloseBuffer[];

double StdDevBuffer[];

double StdDevCalcBuffer[];

//---- declaration of global variables

double coeff1cop;

double coeff2cop;

int    handle_iCustomcop,min_rates_total,z,Z_,OldTrend;

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

//--- indicator buffers

double PivotBuffer[];

double S1Buffer[];

double R1Buffer[];

double S2Buffer[];

double R2Buffer[];

double S0Buffer[];

double R0Buffer[];

double BufHigh[];

double BufLow[];

//---

double P;

double R1;

double S1;

double R2;

double S2;

double R0;

double S0;

//---

string m_r2_name = "R2";

string m_r1_name = "R1";

string m_r0_name = "R0";

string m_s0_name = "S0";

string m_s1_name = "S1";

string m_s2_name = "S2";

string m_p_name  = "P";

//---

string   m_Coral_name  = "Coral";

string   m_Middle_name = "Middle";

string   m_Upper_name  = "Upper";

string   m_Lower_name  = "Lower";

datetime TimeShift;

datetime m_right_time = 0,m_left_Ctime = 0;

double   m_Coral_price = 0.0,m_Middle_price = 0.0,m_Upper_price = 0.0,m_Lower_price = 0.0;

string   m_name[]  = {"R2","R1","R0","S0","S1","S2","P","Coral","Middle","Upper","Lower"};

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create a timer with a 1 second period

   TrendCreate(0,m_r2_name,clrDeepSkyBlue,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_r1_name,clrDeepSkyBlue,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_r0_name,clrDeepSkyBlue,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_s0_name,clrRed,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_s1_name,clrRed,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_s2_name,clrRed,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_p_name,clrOrange,STYLE_DOT,1);

//---

   TrendCreate(0,m_Coral_name,clrSienna,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_Middle_name,clrDeepPink,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_Upper_name,clrDarkGray,STYLE_DASHDOTDOT,1);

   TrendCreate(0,m_Lower_name,clrDarkGray,STYLE_DASHDOTDOT,1);

//---

   TimeShift=DayStartHour*3600+DayStartMinute*60;

   coeff1cop=(InpCoeffcop<=0.0086 ? 0.0086 : InpCoeffcop>1 ? 1: InpCoeffcop);

   coeff2cop=1.0-coeff1cop;

//---- Initialization of variables of data calculation starting point

   min_rates_total=1+InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod+1;

//--- indicator buffers mapping

   SetIndexBuffer(0,UpArrowiCustom,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_ARROW,159);

   ArraySetAsSeries(UpArrowiCustom,true);

   SetIndexBuffer(1,DnArrowiCustom,INDICATOR_DATA);

   PlotIndexSetInteger(1,PLOT_ARROW,159);

   ArraySetAsSeries(DnArrowiCustom,true);

//---

   SetIndexBuffer(2,MiddleMaBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(4,UpperMaBuffer,INDICATOR_DATA);

   SetIndexBuffer(5,LowerMaBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,BufferCcop,INDICATOR_DATA);

   SetIndexBuffer(7,BufferColorscop,INDICATOR_COLOR_INDEX);

//---

   SetIndexBuffer(8,UpperBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BaseBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,MiddleBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,LowerBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(12,HighBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(13,LowBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(14,CloseBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(15,StdDevBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(16,StdDevCalcBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(17,BufferC,INDICATOR_CALCULATIONS);

   SetIndexBuffer(18,BufferB1cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(19,BufferB2cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(20,BufferB3cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(21,BufferB4cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(22,BufferB5cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(23,BufferB6cop,INDICATOR_CALCULATIONS);

   SetIndexBuffer(24,BufferMAcop,INDICATOR_CALCULATIONS);

//--- indicator buffers mapping

   SetIndexBuffer(25,PivotBuffer,INDICATOR_DATA);

   SetIndexBuffer(26,S1Buffer,INDICATOR_DATA);

   SetIndexBuffer(27,R1Buffer,INDICATOR_DATA);

   SetIndexBuffer(28,S2Buffer,INDICATOR_DATA);

   SetIndexBuffer(29,R2Buffer,INDICATOR_DATA);

   SetIndexBuffer(30,S0Buffer,INDICATOR_DATA);

   SetIndexBuffer(31,R0Buffer,INDICATOR_DATA);

   SetIndexBuffer(32,BufHigh,INDICATOR_CALCULATIONS);

   SetIndexBuffer(33,BufLow,INDICATOR_CALCULATIONS);

//---

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(9,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(10,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(11,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(12,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(13,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(14,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(15,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(16,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(17,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(18,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(19,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(20,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(21,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(22,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(23,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(24,PLOT_EMPTY_VALUE,0);

//---

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(6,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(7,PLOT_DRAW_BEGIN,min_rates_total);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

   string short_name="Turning point";

//---

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- create iCustom handlecop

   ResetLastError();

   handle_iCustomcop=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice);

   if(handle_iCustomcop==INVALID_HANDLE)

     {

      Print("The iCustom object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   for(int i=0; i<ArraySize(m_name); i++)

     {

      ObjectDelete(0,m_name[i]);

     }

//--- removes all objects of the specified type using prefix in object names

  }

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

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

  {

//---

   int i,j,first,limit;

   bool CoralUp,CoralDn;

//---

   static bool error=true;

   int startX;

   if(prev_calculated==0)

     {

      error=true;

     }

   if(error)

     {

      startX=1;

      error=false;

     }

   else

     {

      startX=prev_calculated-1;

     }

   for(i=startX; i<rates_total; i++)

     {

      BufHigh[i]=BufHigh[i-1];

      BufLow[i]=BufLow[i-1];

      PivotBuffer[i]=PivotBuffer[i-1];

      S1Buffer[i]=S1Buffer[i-1];

      R1Buffer[i]=R1Buffer[i-1];

      S2Buffer[i]=S2Buffer[i-1];

      R2Buffer[i]=R2Buffer[i-1];

      S0Buffer[i]=S0Buffer[i-1];

      R0Buffer[i]=R0Buffer[i-1];

      datetime NowTime=time[i]-TimeShift;

      datetime PreTime=time[i-1]-TimeShift;

      if(NewDay(NowTime,PreTime))

        {

         if(BufHigh[i]!=0)

           {

            P=(BufHigh[i]+BufLow[i]+close[i-1])/3;

            R1=(2.0*P)-BufLow[i];

            S1=(2.0*P)-BufHigh[i];

            R2=P+(BufHigh[i]-BufLow[i]);

            S2=P-(BufHigh[i]-BufLow[i]);

            R0=P+(BufHigh[i]-BufLow[i])/4.0;

            S0=P-(BufHigh[i]-BufLow[i])/4.0;

            PivotBuffer[i]=P;

            S1Buffer[i]=S1;

            R1Buffer[i]=R1;

            S2Buffer[i]=S2;

            R2Buffer[i]=R2;

            S0Buffer[i]=S0;

            R0Buffer[i]=R0;

           }

         BufHigh[i]=high[i];

         BufLow[i]=low[i];

        }

      else

        {

         BufHigh[i]=MathMax(BufHigh[i],high[i]);

         BufLow[i]=MathMin(BufLow[i],low[i]);

        }

     }

//--- check for bars count

   if(rates_total<=min_rates_total)

      return(0);

//---

   MathSrand(int(TimeLocal()));

//--- indicator buffers

   ArraySetAsSeries(UpperMaBuffer,false);

   ArraySetAsSeries(LowerMaBuffer,false);

   ArraySetAsSeries(MiddleMaBuffer,false);

   ArraySetAsSeries(UpperBuffer,false);

   ArraySetAsSeries(LowerBuffer,false);

   ArraySetAsSeries(MiddleBuffer,false);

   ArraySetAsSeries(HighBuffer,false);

   ArraySetAsSeries(LowBuffer,false);

   ArraySetAsSeries(CloseBuffer,false);

   ArraySetAsSeries(StdDevBuffer,false);

   ArraySetAsSeries(BaseBuffer,false);

//--- rate data

   ArraySetAsSeries(high,false);

   ArraySetAsSeries(low,false);

   ArraySetAsSeries(close,false);

   ArraySetAsSeries(time,false);

//---

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

//| Set Median Buffeer                                 |

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

   first=InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod;

   if(first+1<prev_calculated)

      first=prev_calculated-2;

   else

     {

      for(i=0; i<first; i++)

        {

         MiddleBuffer[i]=close[i];

         HighBuffer[i]=high[i];

         LowBuffer[i]=low[i];

        }

     }

//---

   for(i=first; i<rates_total && !IsStopped(); i++)

     {

      StdDevBuffer[i]=calcStdDev(close,InpFastPeriod,i);

      //---

      double h,l,c,hsum=0.0,lsum=0.0,csum=0.0;

      //---

      for(j=0; j<InpMaPeriod; j++)

        {

         hsum += high[i-j];

         lsum += low[i-j];

         csum += close[i-j];

        }

      //---

      h=hsum/InpMaPeriod;

      l=lsum/InpMaPeriod;

      c=csum/InpMaPeriod;

      //--- Base Volatility

      double sd=0.0;

      for(j=(i-InpVolatilityPeriod+1); j<=i; j++)

         sd+=StdDevBuffer[j];

      //--- Ma Buffer

      double v=sd/InpVolatilityPeriod;

      BaseBuffer[i]=v;

      double base=v*InpScaleFactor;

      //--- Hybrid Mode

      if((h-base)>HighBuffer[i-1])

         HighBuffer[i]=h;

      else

         if(h+base<HighBuffer[i-1])

            HighBuffer[i]=h+base;

         else

            HighBuffer[i]=HighBuffer[i-1];

      //---

      if(l+base<LowBuffer[i-1])

         LowBuffer[i]=l;

      else

         if((l-base)>LowBuffer[i-1])

            LowBuffer[i]=l-base;

         else

            LowBuffer[i]=LowBuffer[i-1];

      //---

      if((c-base/2)>CloseBuffer[i-1])

         CloseBuffer[i]=c-base/2;

      else

         if(c+base/2<CloseBuffer[i-1])

            CloseBuffer[i]=c+base/2;

         else

            CloseBuffer[i]=CloseBuffer[i-1];

      //---

      MiddleBuffer[i]=(HighBuffer[i]+LowBuffer[i]+CloseBuffer[i]*2)/4;

      UpperBuffer[i]=HighBuffer[i] + base/2;

      LowerBuffer[i]=LowBuffer[i]  - base/2;

      //---

      hsum=0.0;

      lsum=0.0;

      csum=0.0;

      //---

      for(j=0; j<InpMaPeriod; j++)

        {

         hsum += UpperBuffer[i-j];

         lsum += LowerBuffer[i-j];

         csum += MiddleBuffer[i-j];

        }

      //---

      UpperMaBuffer[i]=hsum/InpMaPeriod;

      LowerMaBuffer[i]=lsum/InpMaPeriod;

      MiddleMaBuffer[i]=csum/InpMaPeriod;

      m_Middle_price = MiddleMaBuffer[i];

      m_Upper_price  = UpperMaBuffer[i];

      m_Lower_price  = LowerMaBuffer[i];

     }

//---

   limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,2);

     }

   ArraySetAsSeries(BufferC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(UpperMaBuffer,true);

   ArraySetAsSeries(LowerMaBuffer,true);

   ArraySetAsSeries(MiddleMaBuffer,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

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

     {

      if(i==rates_total-2)

         BufferC[i]=MiddleMaBuffer[i];

      else

        {

         BufferC[i]=MiddleMaBuffer[i];

         BufferColors[i]=(BufferC[i]>BufferC[i+1] ? 0 : BufferC[i]<BufferC[i+1] ? 1 : 2);

        }

     }

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferCcop,EMPTY_VALUE);

      ArrayInitialize(BufferColorscop,2);

      ArrayInitialize(BufferB1cop,0);

      ArrayInitialize(BufferB2cop,0);

      ArrayInitialize(BufferB3cop,0);

      ArrayInitialize(BufferB4cop,0);

      ArrayInitialize(BufferB5cop,0);

      ArrayInitialize(BufferB6cop,0);

      ArrayInitialize(BufferMAcop,0);

     }

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferCcop,true);

   ArraySetAsSeries(BufferColorscop,true);

   ArraySetAsSeries(BufferB1cop,true);

   ArraySetAsSeries(BufferB2cop,true);

   ArraySetAsSeries(BufferB3cop,true);

   ArraySetAsSeries(BufferB4cop,true);

   ArraySetAsSeries(BufferB5cop,true);

   ArraySetAsSeries(BufferB6cop,true);

   ArraySetAsSeries(BufferMAcop,true);

//---

//--- >43>B>2:0 40==KE

   int count=(limit>1 ? rates_total : 1);

   int copied=CopyBuffer(handle_iCustomcop,0,0,count,BufferMAcop);

   if(copied!=count)

      return 0;

//---  0AGQB 8=48:0B>@0

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

     {

      if(i==rates_total-2)

         BufferB1cop[i]=BufferB2cop[i]=BufferB3cop[i]=BufferB4cop[i]=BufferB5cop[i]=BufferB6cop[i]=BufferMAcop[i];

      else

        {

         BufferB1cop[i]=coeff1cop*BufferMAcop[i]+coeff2cop*BufferB1cop[i+1];

         BufferB2cop[i]=coeff1cop*BufferB1cop[i]+coeff2cop*BufferB2cop[i+1];

         BufferB3cop[i]=coeff1cop*BufferB2cop[i]+coeff2cop*BufferB3cop[i+1];

         BufferB4cop[i]=coeff1cop*BufferB3cop[i]+coeff2cop*BufferB4cop[i+1];

         BufferB5cop[i]=coeff1cop*BufferB4cop[i]+coeff2cop*BufferB5cop[i+1];

         BufferB6cop[i]=coeff1cop*BufferB5cop[i]+coeff2cop*BufferB6cop[i+1];

         BufferCcop[i]=(-0.064)*BufferB6cop[i]+0.672*BufferB5cop[i]-2.352*BufferB4cop[i]+2.744*BufferB3cop[i];

         BufferColorscop[i]=(BufferCcop[i]>BufferCcop[i+1] && BufferMAcop[i]>BufferCcop[i] ? 0 :

                             BufferCcop[i]<BufferCcop[i+1] && BufferMAcop[i]<BufferCcop[i] ? 1 : 2);

         m_Coral_price  = BufferCcop[i];

        }

     }

//---

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

//---

   z=Z_;

   for(i=limit; i>=0; i--)

     {

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

         Z_=z;

      //---

      DnArrowiCustom[i]=0.0;

      UpArrowiCustom[i]=0.0;

      CoralDn=((Inpyna && BufferC[i]<BufferC[i+1]) || (Inpdoy && BufferCcop[i]<BufferCcop[i+1]) ||

               (Inptrei && BufferC[i]<BufferCcop[i]) || (Inppatry && BufferCcop[i]<BufferC[i]) ||

               (Inpcinc && close[i]<m_Coral_price) || (Inpshase && close[i]<BufferC[i]) || (Inpshapt && close[i]<P));

      CoralUp=((Inpyna && BufferC[i]>BufferC[i+1]) || (Inpdoy && BufferCcop[i]>BufferCcop[i+1]) ||

               (Inptrei && BufferC[i]>BufferCcop[i]) || (Inppatry && BufferCcop[i]>BufferC[i]) ||

               (Inpcinc && close[i]>m_Coral_price) || (Inpshase && close[i]>BufferC[i]) || (Inpshapt && close[i]>P));

      if(CoralDn)

         z = 1;

      if(CoralUp)

         z = 2;

      if(CoralDn && (z==1 || z==0))

        {

         if(((InpFiltryP && close[i]<P) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)) ||

            ((InpFiltryC && close[i]<m_Coral_price) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)) ||

            ((InpFiltryM && close[i]<m_Coral_price) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)))

            if(OldTrend>0)

               UpArrowiCustom[i]=high[i];

         if(i!=0)

            OldTrend=-1;

        }

      if(CoralUp && (z==2 || z==0))

        {

         if(((InpFiltryP && close[i]>P) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)) ||

            ((InpFiltryC && close[i]>m_Coral_price) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)) ||

            ((InpFiltryM && close[i]>m_Coral_price) || (!InpFiltryP && !InpFiltryM && !InpFiltryC)))

            if(OldTrend<0)

               DnArrowiCustom[i]=low[i];

         if(i!=0)

            OldTrend=+1;

        }

     }

   m_left_Ctime=time[rates_total-2];

   m_right_time=TimeCurrent();

//---

   TrendPointChange(0,m_r2_name,0,m_left_Ctime,R2);

   TrendPointChange(0,m_r2_name,1,m_right_time,R2);

//---

   TrendPointChange(0,m_r1_name,0,m_left_Ctime,R1);

   TrendPointChange(0,m_r1_name,1,m_right_time,R1);

//---

   TrendPointChange(0,m_r0_name,0,m_left_Ctime,R0);

   TrendPointChange(0,m_r0_name,1,m_right_time,R0);

//---

   TrendPointChange(0,m_s0_name,0,m_left_Ctime,S0);

   TrendPointChange(0,m_s0_name,1,m_right_time,S0);

//---

   TrendPointChange(0,m_s1_name,0,m_left_Ctime,S1);

   TrendPointChange(0,m_s1_name,1,m_right_time,S1);

//---

   TrendPointChange(0,m_s2_name,0,m_left_Ctime,S2);

   TrendPointChange(0,m_s2_name,1,m_right_time,S2);

//---

   TrendPointChange(0,m_p_name,0,m_left_Ctime,P);

   TrendPointChange(0,m_p_name,1,m_right_time,P);

//---

   TrendPointChange(0,m_Coral_name,0,m_left_Ctime,m_Coral_price);

   TrendPointChange(0,m_Coral_name,1,m_right_time,m_Coral_price);

//---

   TrendPointChange(0,m_Middle_name,0,m_left_Ctime,m_Middle_price);

   TrendPointChange(0,m_Middle_name,1,m_right_time,m_Middle_price);

//---

   TrendPointChange(0,m_Upper_name,0,m_left_Ctime,m_Upper_price);

   TrendPointChange(0,m_Upper_name,1,m_right_time,m_Upper_price);

//---

   TrendPointChange(0,m_Lower_name,0,m_left_Ctime,m_Lower_price);

   TrendPointChange(0,m_Lower_name,1,m_right_time,m_Lower_price);

   ChartRedraw(0);

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//|                                                                  |

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

double calcStdDev(const double  &close[],int span,int i)

  {

   if(i<span-1)

      return 0.0;

//---

   if(i==span-1)

      StdDevCalcBuffer[i]=SimpleMA(i,span,close);

   else

      StdDevCalcBuffer[i]=SmoothedMA(i,span,StdDevCalcBuffer[i-1],close);

//---

   double sum=0.0;

   for(int j=0; j<span; j++)

      sum+=MathPow(close[i-j]-StdDevCalcBuffer[i],2);

   return MathSqrt(sum/span);

  }

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

//|                                                                  |

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

bool NewDay(datetime aNowTime,datetime aPreTime)

  {

   long NowDay=aNowTime/86400;

   long PreDay=aPreTime/86400;

   if(AttachSundToMond)

     {

      if(NowDay!=PreDay)

        {

         MqlDateTime nts,pts;

         TimeToStruct(aNowTime,nts);

         TimeToStruct(aPreTime,pts);

         if(nts.day_of_week==1 && pts.day_of_week==0)

           {

            return(false);

           }

         return(true);

        }

     }

   else

     {

      return(NowDay!=PreDay);

     }

   return(false);

  }

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

//| Create a trend line by the given coordinates                     |

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

bool TrendCreate(const long            chart_ID=0,        // chart's ID

                 const string          name="TrendLine",  // line name

                 const color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 const int             sub_window=0,      // subwindow index

                 datetime              time1=0,           // first point time

                 double                price1=0,          // first point price

                 datetime              time2=0,           // second point time

                 double                price2=0,          // second point price

                 const bool            back=false,        // in the background

                 const bool            selection=false,   // highlight to move

                 const bool            ray_left=false,    // line's continuation to the left

                 const bool            ray_right=true,    // line's continuation to the right

                 const bool            hidden=true,       // hidden in the object list

                 const long            z_order=0)         // priority for mouse click

  {

//--- set anchor points' coordinates if they are not set

   ChangeTrendEmptyPoints(time1,price1,time2,price2);

//--- reset the error value

   ResetLastError();

//--- create a trend line by the given coordinates

   if(!ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a trend line! Error code = ",GetLastError());

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the line by mouse

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- enable (true) or disable (false) the mode of continuation of the line's display to the left

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_LEFT,ray_left);

//--- enable (true) or disable (false) the mode of continuation of the line's display to the right

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move trend line anchor point                                     |

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

bool TrendPointChange(const long   chart_ID=0,       // chart's ID

                      const string name="TrendLine", // line name

                      const int    point_index=0,    // anchor point index

                      datetime     time=0,           // anchor point time coordinate

                      double       price=0)          // anchor point price coordinate

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move trend line's anchor point

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check the values of trend line's anchor points and set default   |

//| values for empty ones                                            |

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

void ChangeTrendEmptyPoints(datetime &time1,double &price1,

                            datetime &time2,double &price2)

  {

//--- if the first point's time is not set, it will be on the current bar

   if(!time1)

      time1=TimeCurrent();

//--- if the first point's price is not set, it will have Bid value

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- if the second point's time is not set, it is located 9 bars left from the second one

   if(!time2)

     {

      //--- array for receiving the open time of the last 10 bars

      datetime temp[10];

      CopyTime(Symbol(),Period(),time1,10,temp);

      //--- set the second point 9 bars left from the first one

      time2=temp[0];

     }

//--- if the second point's price is not set, it is equal to the first point's one

   if(!price2)

      price2=price1;

  }

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

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