#property copyright "mrtools"
#property link      "mrtools"

#property indicator_separate_window
#property indicator_buffers    6
#property indicator_color1     clrLimeGreen
#property indicator_color2     clrMediumVioletRed
#property indicator_color3     clrMediumVioletRed
#property indicator_color4     clrDeepSkyBlue
#property indicator_color5     clrDeepSkyBlue
#property indicator_color6     clrGold
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width4     2
#property indicator_style6     STYLE_DASH

//
//
//
//
//
enum enTimeFrames
{
   tf_cu  = PERIOD_CURRENT, // Current time frame
   tf_m1  = PERIOD_M1,      // 1 minute
   tf_m5  = PERIOD_M5,      // 5 minutes
   tf_m15 = PERIOD_M15,     // 15 minutes
   tf_m30 = PERIOD_M30,     // 30 minutes
   tf_h1  = PERIOD_H1,      // 1 hour
   tf_h4  = PERIOD_H4,      // 4 hours
   tf_d1  = PERIOD_D1,      // Daily
   tf_w1  = PERIOD_W1,      // Weekly
   tf_mn1 = PERIOD_MN1,     // Monthly
   tf_n1  = -1,             // First higher time frame
   tf_n2  = -2,             // Second higher time frame
   tf_n3  = -3              // Third higher time frame
};
//
//
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 enTimeFrames    TimeFrame       = tf_cu;   // Time frame
extern double          PaFilter         = 1.0;
extern double          PaCycles         = 5.0;
extern double          mafilter         = 1.0;
extern double          macycles         = 0.25;
extern ENUM_MA_METHOD  MaMode           = MODE_LWMA;
extern enPrices        PaPrice          = pr_median;
extern int             SmoothLength     = 50;
extern double          SmoothPhase      = 0;
extern bool            SmoothDouble     = false;
extern bool            ShowHistogram    = true;
input bool             alertsOn         = true;              // Alerts on true/false?
input bool             alertsOnCurrent  = false;             // Alerts on open bar true/false?
input bool             alertsMessage    = true;              // Alerts popup message true/false?
input bool             alertsSound      = false;             // Alerts sound true/false?
input bool             alertsEmail      = false;             // Alerts email true/false?
input bool             alertsPush       = false;             // Alerts notification true/false?
input bool             arrowsVisible    = false;             // Arrows visible true/false?
input bool             arrowsOnNewest   = false;             // Arrows drawn on newest bar of higher time frame bar true/false?
input string           arrowsIdentifier = "wpr Arrows1";     // Unique ID for arrows
input double           arrowsUpperGap   = 1.0;               // Upper arrow gap
input double           arrowsLowerGap   = 1.0;               // Lower arrow gap
input color            arrowsUpColor    = clrBlue;           // Up arrow color
input color            arrowsDnColor    = clrCrimson;        // Down arrow color
input int              arrowsUpCode     = 221;               // Up arrow code
input int              arrowsDnCode     = 222;               // Down arrow code
input int              arrowsUpSize     = 2;                 // Up arrow size
input int              arrowsDnSize     = 2;                 // Down arrow size
extern bool            Interpolate      = true;



//
//
//
//
//

double wpr[];
double Upa[];
double Upb[];
double Dna[];
double Dnb[];
double wprma[];
double trend[];
double slope[];
string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
// 
//
//
//
//

int init()
{
    IndicatorBuffers(8);
    SetIndexBuffer(0,wpr);
    SetIndexBuffer(1,Dna);
    SetIndexBuffer(2,Dnb); 
    SetIndexBuffer(3,Upa);
    SetIndexBuffer(4,Upb);
    SetIndexBuffer(5,wprma);
    SetIndexBuffer(6,trend);
    SetIndexBuffer(7,slope);
   
    if (ShowHistogram)
    {
      SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1,"smooth %R");
      SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexLabel(2,"smooth %R");
      SetIndexStyle(3,DRAW_HISTOGRAM); SetIndexLabel(3,"smooth %R");
      SetIndexStyle(4,DRAW_HISTOGRAM); SetIndexLabel(4,"smooth %R");
    }
    else
    {
      SetIndexStyle(1,DRAW_NONE); SetIndexLabel(1,NULL);
      SetIndexStyle(2,DRAW_NONE); SetIndexLabel(2,NULL);
      SetIndexStyle(3,DRAW_NONE); SetIndexLabel(3,NULL);
      SetIndexStyle(4,DRAW_NONE); SetIndexLabel(4,NULL);
    }
    
    //
    //
    //
    //
    //
   
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = (enTimeFrames)timeFrameValue(TimeFrame);
      

    //
    //
    //
    //
    //
    
  
    IndicatorShortName(timeFrameToString(TimeFrame)+"  smooth pa %R ("+DoubleToStr(PaCycles,2)+","+DoubleToStr(PaFilter,2)+","+DoubleToStr(macycles,2)+","+DoubleToStr(mafilter,2)+")");
return(0);
}
void OnDeinit(const int reason)  {  ObjectsDeleteAll(0,arrowsIdentifier+":"); }

//
//
//
//
//

int start()
{
   int i,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) { wpr[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (TimeFrame == Period())
   {
      for (i=limit; i>=0; i--)
      {
          double hi  = High[iHighest(NULL,0,MODE_HIGH,iHilbertPhase(getPrice(PaPrice,Open,Close,High,Low,i),PaFilter,PaCycles,i,0),i)];
          double lo  = Low[iLowest(NULL,  0,MODE_LOW, iHilbertPhase(getPrice(PaPrice,Open,Close,High,Low,i),PaFilter,PaCycles,i,1),i)];         
          if (hi!=lo)
               wpr[i] = iDSmooth(50+(-100)*(hi - Close[i]) /(hi - lo),SmoothLength,SmoothPhase,SmoothDouble,i);
          else wpr[i] = 0;                     
      }                  
      for (i=limit; i>=0; i--)
      {
         wprma[i] = iMAOnArray(wpr,0,iHilbertPhase(getPrice(PaPrice,Open,Close,High,Low,i),mafilter,macycles,i,2),0,MaMode,i);
         Dna[i]   = EMPTY_VALUE;
         Dnb[i]   = EMPTY_VALUE;
         Upa[i]   = EMPTY_VALUE;
         Upb[i]   = EMPTY_VALUE;
         trend[i] = trend[i+1];
         slope[i] = slope[i+1];
         if (wpr[i] > 0)        trend[i] =  1;
         if (wpr[i] < 0)        trend[i] = -1;
         if (wpr[i] > wpr[i+1]) slope[i] =  1;
         if (wpr[i] < wpr[i+1]) slope[i] = -1;
 
         if (trend[i]== 1 && slope[i] == 1) Upa[i] = wpr[i];
         if (trend[i]== 1 && slope[i] ==-1) Upb[i] = wpr[i];
         if (trend[i]==-1 && slope[i] ==-1) Dna[i] = wpr[i];
         if (trend[i]==-1 && slope[i] == 1) Dnb[i] = wpr[i];  
         
         if (arrowsVisible)
         {
           string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);            
           if (i<(Bars-1) && trend[i] != trend[i+1])
           {
             if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
             if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize, true);
           }
         }   
     }
     if (alertsOn)
     {
       int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("crossing zero up");
       else  doAlert("crossing zero down");       
     }  
     return(0);
     }

     //
     //
     //
     //
     //

     limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
     for (i=limit; i>=0; i--)
     {
        int y = iBarShift(NULL,TimeFrame,Time[i]);
           wpr[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,0,y);
           Dna[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,1,y);
           Dnb[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,2,y);
           Upa[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,3,y);
           Upb[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,4,y);
           wprma[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,PaFilter,PaCycles,mafilter,macycles,MaMode,PaPrice,SmoothLength,SmoothPhase,SmoothDouble,ShowHistogram,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,alertsPush,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,5,y);
           
           //
           //
           //
           //
           //
           
           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 k = 1; k < n; k++)
              {
                 wpr[i+k]   = wpr[i]   +(wpr[i+n]   - wpr[i])   * k/n;
                 wprma[i+k] = wprma[i] +(wprma[i+n] - wprma[i]) * k/n;
                 if (Dna[i]!= EMPTY_VALUE) Dna[i+k] = wpr[i+k];
                 if (Dnb[i]!= EMPTY_VALUE) Dnb[i+k] = wpr[i+k];
                 if (Upa[i]!= EMPTY_VALUE) Upa[i+k] = wpr[i+k];
                 if (Upb[i]!= EMPTY_VALUE) Upb[i+k] = wpr[i+k];
              }
     }
return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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("");
}
int timeFrameValue(int _tf)
{
   int add  = (_tf>=0) ? 0 : MathAbs(_tf);
   if (add != 0) _tf = _Period;
   int size = ArraySize(iTfTable); 
      int i =0; for (;i<size; i++) if (iTfTable[i]==_tf) break;
                                   if (i==size) return(_Period);
                                                return(iTfTable[(int)MathMin(i+add,size-1)]);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double wrk[][20];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iDSmooth(double price, double length, double phase, bool isDouble, int i, int s=0)
{
   if (isDouble)
         return (iSmooth(iSmooth(price,MathSqrt(length),phase,i,s),MathSqrt(length),phase,i,s+10));
   else  return (iSmooth(price,length,phase,i,s));
}

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double workHil[][27];
#define _price      0
#define _smooth     1
#define _detrender  2
#define _period     3
#define _instPeriod 4
#define _phase      5
#define _deltaPhase 6
#define _Q1         7
#define _I1         8

#define Pi 3.14159265358979323846264338327950288

//
//
//
//
//

double iHilbertPhase(double price, double filter, double cyclesToReach, int i, int s=0)
{
   if (ArrayRange(workHil,0)!=Bars) ArrayResize(workHil,Bars);
   int r = Bars-i-1; s = s*9;
      
   //
   //
   //
   //
   //
      
      workHil[r][s+_price]      = price;
      workHil[r][s+_smooth]     = (4.0*workHil[r][s+_price]+3.0*workHil[r-1][s+_price]+2.0*workHil[r-2][s+_price]+workHil[r-3][s+_price])/10.0;
      workHil[r][s+_detrender]  = calcComp(r,_smooth,s);
      workHil[r][s+_Q1]         = 0.15*calcComp(r,_detrender,s)  +0.85*workHil[r-1][s+_Q1];
      workHil[r][s+_I1]         = 0.15*workHil[r-3][s+_detrender]+0.85*workHil[r-1][s+_I1];
      workHil[r][s+_phase]      = workHil[r-1][s+_phase];
      workHil[r][s+_instPeriod] = workHil[r-1][s+_instPeriod];

      //
      //
      //
      //
      //
           
         if (MathAbs(workHil[r][s+_I1])>0)
                     workHil[r][s+_phase] = 180.0/Pi*MathArctan(MathAbs(workHil[r][s+_Q1]/workHil[r][s+_I1]));
           
         if (workHil[r][s+_I1]<0 && workHil[r][s+_Q1]>0) workHil[r][s+_phase] = 180-workHil[r][s+_phase];
         if (workHil[r][s+_I1]<0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 180+workHil[r][s+_phase];
         if (workHil[r][s+_I1]>0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 360-workHil[r][s+_phase];

      //
      //
      //
      //
      //
                        
      workHil[r][s+_deltaPhase] = workHil[r-1][s+_phase]-workHil[r][s+_phase];

         if (workHil[r-1][s+_phase]<90 && workHil[r][s+_phase]>270)
             workHil[r][s+_deltaPhase] = 360+workHil[r-1][s+_phase]-workHil[r][s+_phase];
             workHil[r][s+_deltaPhase] = MathMax(MathMin(workHil[r][s+_deltaPhase],60),7);
      
            //
            //
            //
            //
            //
                  
            double alpha    = 2.0/(1.0+MathMax(filter,1));
            double phaseSum = 0; for (int k=0; phaseSum<cyclesToReach*360 && (r-k)>0; k++) phaseSum += workHil[r-k][s+_deltaPhase];
         
            if (k>0) workHil[r][s+_instPeriod]= k;
                    workHil[r][s+_period] = workHil[r-1][s+_period]+alpha*(workHil[r][s+_instPeriod]-workHil[r-1][s+_period]);
            return (workHil[r][s+_period]);
}

//
//
//
//
//

double calcComp(int r, int from, int s)
{
   return((0.0962*workHil[r  ][s+from] + 
           0.5769*workHil[r-2][s+from] - 
           0.5769*workHil[r-4][s+from] - 
           0.0962*workHil[r-6][s+from]) * (0.075*workHil[r-1][s+_period] + 0.54));
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      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 (tprice)
         {
            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 (tprice)
   {
      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 doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" pa wprsmooth "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsPush)    SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" pa wprsmooth ",message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void drawArrow(int i,color theColor,int theCode, int theSize, bool up)
{
   string name = arrowsIdentifier+":"+(string)Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //

      datetime atime = Time[i]; if (arrowsOnNewest)  atime += PeriodSeconds(_Period)-1;       
      ObjectCreate(name,OBJ_ARROW,0,atime,0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         ObjectSet(name,OBJPROP_WIDTH,theSize);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}


