//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  LimeGreen
#property indicator_color2  Orange
#property indicator_color3  Orange
#property indicator_color4  DeepSkyBlue
#property indicator_color5  DeepSkyBlue
#property indicator_color6  PaleVioletRed
#property indicator_color7  PaleVioletRed
#property indicator_color8  Peru
#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             OmaPeriod               = 34;
extern double          OmaSpeed                = 3.0;
extern bool            OmaAdaptive             = true;
extern enPrices        Price                   = pr_close; // Price to use 
extern bool            ShowMiddleLine          = true;
extern int             DzLookBackBars          = 35;
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           = LimeGreen;
extern color           arrowsDnColor           = Red;
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[];

//
//
//
//
//

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);
   
      //
      //
      //
      //
      //
      
      OmaPeriod         = MathMax(OmaPeriod,1);
	   OmaSpeed          = MathMax(OmaSpeed,-1.5);
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period);
      
      //
      //
      //
      //
      //
               
   IndicatorShortName(timeFrameToString(TimeFrame)+" DZ of OMA");
   return(0);
}
int deinit()
{
   deleteArrows();

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int 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 (trend[limit]==-1) CleanPoint(limit,buffer2,buffer3);
      for(int i=limit; i>=0; i--)
      {
         buffer1[i] = iOma(getPrice(Price,Open,Close,High,Low,i),OmaPeriod,OmaSpeed,OmaAdaptive,i);  
         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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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,OmaPeriod,OmaSpeed,OmaAdaptive,Price,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 workOma[][7];
#define F01 0
#define F02 1
#define F03 2
#define F04 3
#define F05 4
#define F06 5
#define prc 6

//
//
//
//
//

double iOma(double price, double averagePeriod, double constant, bool adaptive, int r, int s=0)
{
   if (averagePeriod <=1) return(price);
   if (ArrayRange(workOma,0) != Bars) ArrayResize(workOma,Bars); r=Bars-r-1; s *=7;
   if (r<=1) 
   {
      for (int i=0; i<6; i++) workOma[r][i  +s] = 0;
                              workOma[r][prc+s] = price;
                              return(price);
   }      
   double f01=workOma[r-1][F01+s];  double f02=workOma[r-1][F02+s];
   double f03=workOma[r-1][F03+s];  double f04=workOma[r-1][F04+s];
   double f05=workOma[r-1][F05+s];  double f06=workOma[r-1][F06+s];

   //
   //
   //
   //
   //

      if (adaptive && (averagePeriod > 1))
      {
         double minPeriod = MathMin(averagePeriod,r)/2.0;
         double maxPeriod = MathMin(minPeriod*5.0,r);
         int    endPeriod = (int)MathCeil(maxPeriod);
         double signal    = MathAbs((price-workOma[r-endPeriod][prc+s]));
         double noise     = 0.00000000001;

            for(i=1; i<endPeriod; i++) noise=noise+MathAbs(price-workOma[r-i][prc+s]);

         averagePeriod = ((signal/noise)*(maxPeriod-minPeriod))+minPeriod;
      }
      
      //
      //
      //
      //
      //
      
      double Kg = (2.0+constant)/(1.0+constant+averagePeriod);
      double Hg = 1.0-Kg;

      f01 = Kg * price + Hg * f01; f02 = Kg * f01 + Hg * f02; double v01 = 1.5 * f01 - 0.5 * f02;
      f03 = Kg * v01   + Hg * f03; f04 = Kg * f03 + Hg * f04; double v02 = 1.5 * f03 - 0.5 * f04;
      f05 = Kg * v02   + Hg * f05; f06 = Kg * f05 + Hg * f06; double v03 = 1.5 * f05 - 0.5 * f06;

   //
   //
   //
   //
   //

   workOma[r][F01+s] = f01;  workOma[r][F02+s] = f02;
   workOma[r][F03+s] = f03;  workOma[r][F04+s] = f04;
   workOma[r][F05+s] = f05;  workOma[r][F06+s] = f06;
   workOma[r][prc+s] = price;
return(v03);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//
//

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 One more average ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Dynamic Zone One more average "),message);
          if (alertsSound)   PlaySound(soundFile);
   }
}