//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  clrLimeGreen
#property indicator_color2  clrOrange
#property indicator_color3  clrOrange
#property indicator_color4  clrDeepSkyBlue
#property indicator_color5  clrDeepSkyBlue
#property indicator_color6  clrPaleVioletRed
#property indicator_color7  clrPaleVioletRed
#property indicator_color8  clrPeru
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_style5  STYLE_DOT
#property indicator_style6  STYLE_DOT
#property indicator_style8  STYLE_DASH

//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
#import

//
//
//
//
//

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased   // Heiken ashi trend biased price
};

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame               = PERIOD_CURRENT;
extern int             RsxLength               = 35;
extern enPrices        RsxPrice                = pr_close; // Price to use 
extern int             MinMaxPeriod            = 49;
extern bool            ShowMiddleLine          = true;
extern int             DzLookBackBars          = 49;
extern double          DzStartBuyProbability1  = 0.10;
extern double          DzStartBuyProbability2  = 0.25;
extern double          DzStartSellProbability1 = 0.10;
extern double          DzStartSellProbability2 = 0.25;
extern bool            arrowsVisible           = true;
extern bool            arrowsOnFirst           = false;
extern bool            arrowsOnLevels          = false;
extern bool            arrowsOnSlope           = false;
extern string          arrowsIdentifier        = "dzoma arrows1";
extern double          arrowsUpperGap          = 0.5;
extern double          arrowsLowerGap          = 0.5;
extern color           arrowsUpColor           = clrLimeGreen;
extern color           arrowsDnColor           = clrRed;
extern int             arrowsUpCode            = 241;
extern int             arrowsDnCode            = 242;
extern bool            alertsOn                = true;
extern bool            alertsOnCurrent         = false;
extern bool            alertsOnSlope           = true;
extern bool            alertsOnLevels          = true;
extern bool            alertsMessage           = true;
extern bool            alertsSound             = false;
extern bool            alertsNotify            = false;
extern bool            alertsEmail             = false;
extern string          soundFile               = "alert2.wav";
extern bool            Interpolate             = true;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double bl1[];
double bl2[];
double sl1[];
double sl2[];
double zli[];
double trend[];
double trendbl1[];
double trendbl2[];
double trendsl1[];
double trendsl2[];
double trendzli[];
double wrkBuffer[][13];
string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(14);
   SetIndexBuffer(0,buffer1);
   SetIndexBuffer(1,buffer2);
   SetIndexBuffer(2,buffer3);
   SetIndexBuffer(3,bl1);
   SetIndexBuffer(4,bl2);
   SetIndexBuffer(5,sl2);
   SetIndexBuffer(6,sl1);
   SetIndexBuffer(7,zli);
   SetIndexBuffer(8,trend);
   SetIndexBuffer(9,trendbl1);
   SetIndexBuffer(10,trendbl2);
   SetIndexBuffer(11,trendsl2);
   SetIndexBuffer(12,trendsl1);
   SetIndexBuffer(13,trendzli);
   
      //
      //
      //
      //
      //
      
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period);
      
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(TimeFrame)+" DZ of OnChart Rsx nrp");
   return(0);
}
int deinit()
{
   deleteArrows();

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int i,r,counted_bars = IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars)  { buffer1[0] = limit+1; return(0); } 

   //
   //
   //
   //
   //

   if (TimeFrame == Period())
   {
      if (ArrayRange(wrkBuffer,0) != Bars) ArrayResize(wrkBuffer,Bars);
      if (trend[limit]==-1) CleanPoint(limit,buffer2,buffer3);
      double Kg = (3.0)/(2.0+RsxLength);
      double Hg = 1.0-Kg;
      for(i=limit, r=Bars-i-1; i>=0; i--, r++)
      {  
         double rsxValue;
         wrkBuffer[r][12] = getPrice(RsxPrice,Open,Close,High,Low,i);
         if (i==(Bars-1)) { for (int c=0; c<12; c++) wrkBuffer[r][c] = 0; continue; }  

         //
         //
         //
         //
         //
      
         double roc = wrkBuffer[r][12]-wrkBuffer[r-1][12];
         double roa = MathAbs(roc);
         for (int k=0; k<3; k++)
         {
            int kk = k*2;
               wrkBuffer[r][kk+0] = Kg*roc                + Hg*wrkBuffer[r-1][kk+0];
               wrkBuffer[r][kk+1] = Kg*wrkBuffer[r][kk+0] + Hg*wrkBuffer[r-1][kk+1]; roc = 1.5*wrkBuffer[r][kk+0] - 0.5 * wrkBuffer[r][kk+1];
               wrkBuffer[r][kk+6] = Kg*roa                + Hg*wrkBuffer[r-1][kk+6];
               wrkBuffer[r][kk+7] = Kg*wrkBuffer[r][kk+6] + Hg*wrkBuffer[r-1][kk+7]; roa = 1.5*wrkBuffer[r][kk+6] - 0.5 * wrkBuffer[r][kk+7];
         }
         if (roa != 0)
              rsxValue = MathMax(MathMin((roc/roa+1.0)*50.0,100.00),0.00);
         else rsxValue = 50.0;
         double max = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,MinMaxPeriod,i));
         double min = iLow (NULL,0,iLowest (NULL,0,MODE_LOW ,MinMaxPeriod,i));
         double rng = max-min;
         buffer1[i] = min+rng*rsxValue  /100; 
         if (DzStartBuyProbability1 >0) bl1[i] = dzBuyP (buffer1, DzStartBuyProbability1,  DzLookBackBars, Bars, i, 0.0001);
         if (DzStartBuyProbability2 >0) bl2[i] = dzBuyP (buffer1, DzStartBuyProbability2,  DzLookBackBars, Bars, i, 0.0001);
         if (DzStartSellProbability1>0) sl1[i] = dzSellP(buffer1, DzStartSellProbability1, DzLookBackBars, Bars, i, 0.0001);
         if (DzStartSellProbability2>0) sl2[i] = dzSellP(buffer1, DzStartSellProbability2, DzLookBackBars, Bars, i, 0.0001);
         if (ShowMiddleLine)            zli[i] = dzSellP(buffer1, 0.5                    , DzLookBackBars, Bars, i, 0.0001);
         
         //
         //
         //
         //
         //
         
         buffer2[i]  = EMPTY_VALUE;
         buffer3[i]  = EMPTY_VALUE;
         trend[i]    = trend[i+1];
         trendbl1[i] = trendbl1[i+1];
         trendbl1[i] = trendbl1[i+1];
         trendbl1[i] = trendbl1[i+1];
         trendbl1[i] = trendbl1[i+1];
         trendbl1[i] = trendbl1[i+1];
            if (buffer1[i]>buffer1[i+1]) trend[i]    = 1;
            if (buffer1[i]<buffer1[i+1]) trend[i]    =-1;
            if (buffer1[i]>bl1[i])       trendbl1[i] = 1;
            if (buffer1[i]<bl1[i])       trendbl1[i] =-1;
            if (buffer1[i]>bl2[i])       trendbl2[i] = 1;
            if (buffer1[i]<bl2[i])       trendbl2[i] =-1;
            if (buffer1[i]>sl1[i])       trendsl1[i] = 1;
            if (buffer1[i]<sl1[i])       trendsl1[i] =-1;
            if (buffer1[i]>sl2[i])       trendsl2[i] = 1;
            if (buffer1[i]<sl2[i])       trendsl2[i] =-1;
            if (buffer1[i]>zli[i])       trendzli[i] = 1;
            if (buffer1[i]<zli[i])       trendzli[i] =-1;
            if (trend[i]==-1) PlotPoint(i,buffer2,buffer3,buffer1);
            
            //
            //
            //
            //
            //
            
            if (arrowsVisible)
            {
              ObjectDelete(arrowsIdentifier+":1:"+Time[i]);
              ObjectDelete(arrowsIdentifier+":2:"+Time[i]);
              ObjectDelete(arrowsIdentifier+":3:"+Time[i]);
              ObjectDelete(arrowsIdentifier+":4:"+Time[i]);
              ObjectDelete(arrowsIdentifier+":5:"+Time[i]);
              ObjectDelete(arrowsIdentifier+":6:"+Time[i]);
              string lookFor = arrowsIdentifier+":"+Time[i]; ObjectDelete(lookFor);
              if (arrowsOnLevels && trendbl1[i] != trendbl1[i+1])
              {
                if (trendbl1[i] == 1) drawArrow("1",0.5,i,arrowsUpColor,arrowsUpCode,false);
                if (trendbl1[i] ==-1) drawArrow("1",0.5,i,arrowsDnColor,arrowsDnCode,true);
              }
              if (arrowsOnLevels && trendbl2[i] != trendbl2[i+1])
              {
                if (trendbl2[i] == 1) drawArrow("2",1,i,arrowsUpColor,arrowsUpCode,false);
                if (trendbl2[i] ==-1) drawArrow("2",1,i,arrowsDnColor,arrowsDnCode,true);
              }
              if (arrowsOnLevels && trendsl1[i] != trendsl1[i+1])
              {
                if (trendsl1[i] == 1) drawArrow("3",1.5,i,arrowsUpColor,arrowsUpCode,false);
                if (trendsl1[i] ==-1) drawArrow("3",1.5,i,arrowsDnColor,arrowsDnCode,true);
              }
              if (arrowsOnLevels && trendsl2[i] != trendsl2[i+1])
              {
                if (trendsl2[i] == 1) drawArrow("4",2,i,arrowsUpColor,arrowsUpCode,false);
                if (trendsl2[i] ==-1) drawArrow("4",2,i,arrowsDnColor,arrowsDnCode,true);
              }
              if (arrowsOnLevels && trendzli[i] != trendzli[i+1])
              {
                if (trendzli[i] == 1) drawArrow("5",2.5,i,arrowsUpColor,arrowsUpCode,false);
                if (trendzli[i] ==-1) drawArrow("5",2.5,i,arrowsDnColor,arrowsDnCode,true);
              }
              
              if (arrowsOnSlope && trend[i] != trend[i+1])
              {
                if (trend[i] == 1) drawArrow("6",3,i,arrowsUpColor,arrowsUpCode,false);
                if (trend[i] ==-1) drawArrow("6",3,i,arrowsDnColor,arrowsDnCode,true);
              }
          }
     } 
      
     //
     //
     //
     //
     //
         
     if (alertsOn)
     {
        if (alertsOnCurrent)
             int whichBar = 0;
        else     whichBar = 1;
        static datetime time1 = 0;
        static string   mess1 = "";
        if (alertsOnLevels && trendbl1[whichBar] != trendbl1[whichBar+1])
        {
           if (trendbl1[whichBar] ==  1 ) doAlert(time1,mess1,whichBar,"crossed outer lower level up");
           if (trendbl1[whichBar] == -1 ) doAlert(time1,mess1,whichBar,"crossed outer lower level down");
        }         
        static datetime time2 = 0;
        static string   mess2 = "";
        if (alertsOnLevels && trendbl2[whichBar] != trendbl2[whichBar+1])
        {
           if (trendbl2[whichBar] ==  1 ) doAlert(time2,mess2,whichBar,"crossed inner lower level up");
           if (trendbl2[whichBar] == -1 ) doAlert(time2,mess2,whichBar,"crossed inner lower level down");
        }         
        static datetime time3 = 0;
        static string   mess3 = "";
        if (alertsOnLevels && trendsl1[whichBar] != trendsl1[whichBar+1])
        {
           if (trendsl1[whichBar] ==  1 ) doAlert(time3,mess3,whichBar,"crossed outer upper level up");
           if (trendsl1[whichBar] == -1 ) doAlert(time3,mess3,whichBar,"crossed outer upper level down");
        } 
        static datetime time4 = 0;
        static string   mess4 = "";
        if (alertsOnLevels && trendsl2[whichBar] != trendsl2[whichBar+1])
        {
           if (trendsl2[whichBar] ==  1 ) doAlert(time4,mess4,whichBar,"crossed inner upper level up");
           if (trendsl2[whichBar] == -1 ) doAlert(time4,mess4,whichBar,"crossed inner upper level down");
        }
        static datetime time5 = 0;
        static string   mess5 = "";
        if (alertsOnLevels && trendzli[whichBar] != trendzli[whichBar+1])
        {
           if (trendzli[whichBar] ==  1 ) doAlert(time5,mess5,whichBar,"crossed middle level up");
           if (trendzli[whichBar] == -1 ) doAlert(time5,mess5,whichBar,"crossed middle level down");
        }                                 
        static datetime time6 = 0;
        static string   mess6 = "";
        if (alertsOnSlope && trend[whichBar] != trend[whichBar+1])
        {
           if (trend[whichBar] ==  1 ) doAlert(time6,mess6,whichBar,"slope changed to up");
           if (trend[whichBar] == -1 ) doAlert(time6,mess6,whichBar,"slope changed to down");
        }         
     }           
   return(0);
   }      

   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   if (trend[limit]==-1) CleanPoint(limit,buffer2,buffer3);
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         buffer1[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
         bl1[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,3,y);
         bl2[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,4,y);
         sl2[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,5,y);
         sl1[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,6,y);
         zli[i]     = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,7,y);
         trend[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RsxLength,RsxPrice,MinMaxPeriod,ShowMiddleLine,DzLookBackBars,DzStartBuyProbability1,DzStartBuyProbability2,DzStartSellProbability1,DzStartSellProbability2,arrowsVisible,arrowsOnFirst,arrowsOnLevels,arrowsOnSlope,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,alertsOn,alertsOnCurrent,alertsOnSlope,alertsOnLevels,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,8,y);
         buffer2[i] = EMPTY_VALUE;
         buffer3[i] = EMPTY_VALUE;

          //
          //
          //
          //
          //
            
          if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

          //
          //
          //
          //
          //

          datetime time = iTime(NULL,TimeFrame,y);
             for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
             for(int s = 1; s < n; s++) 
             {
  	             buffer1[i+s] = buffer1[i] + (buffer1[i+n] - buffer1[i]) * s/n;
  	             bl1[i+s]     = bl1[i]     + (bl1[i+n]     - bl1[i])     * s/n;
  	             bl2[i+s]     = bl2[i]     + (bl2[i+n]     - bl2[i])     * s/n;
  	             sl2[i+s]     = sl2[i]     + (sl2[i+n]     - sl2[i])     * s/n;
  	             sl1[i+s]     = sl1[i]     + (sl1[i+n]     - sl1[i])     * s/n;
  	             zli[i+s]     = zli[i]     + (zli[i+n]     - zli[i])     * s/n;
              }
   }
   for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,buffer2,buffer3,buffer1);
   return(0);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (price>=pr_haclose && price<=pr_hatbiased)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (price)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (price)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
   }
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}
void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
         if (first[i+2] == EMPTY_VALUE) 
              { first[i]  = from[i]; first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
         else { second[i] = from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else       { first[i]  = from[i];                          second[i] = EMPTY_VALUE; }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

void drawArrow(string nameAdd, double gapMul, int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+nameAdd+":"+Time[i];
   double gap  = iATR(NULL,0,20,i)*gapMul;   
   
      //
      //
      //
      //
      //
      
      int add = 0; if (!arrowsOnFirst) add = _Period*60-1;
      ObjectCreate(name,OBJ_ARROW,0,Time[i]+add,0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, int forBar, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Dynamic Zone OnChart Rsx ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Dynamic Zone OnChart Rsx "),message);
          if (alertsSound)   PlaySound(soundFile);
   }
}