
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 clrYellow//clrAqua
#property indicator_color2 clrAqua
#property indicator_color3 clrRed
#property indicator_minimum    0
#property indicator_maximum    600

//--- input parameters
input int                 n_blue_line_lenght       = 7;
input int                 p_yellow_line_lenght     = 7;
input double              x_yellow_line_multiplier = 2.0;
extern bool               alertsOn                 = false;
extern bool               alertsOnCurrent          = false;
extern bool               alertsMessage            = false;
extern bool               alertsSend               = false;
extern bool               alertsEmail              = false;
extern bool               alertsSound              = false;
//--- indicator buffers
double         Label1Buffer[];
double         Label2Buffer[];
double         Label3Buffer[];
double a1;
double a2;
double NewA;
double r;
double PNew;
double d;
double m;
double w;
color bc;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   string short_name;
   short_name="extertion meter ("+string(n_blue_line_lenght)+","+string(p_yellow_line_lenght)+","+string(x_yellow_line_multiplier)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
   
   IndicatorDigits(Digits);
   SetIndexBuffer(0,Label1Buffer);
   SetIndexBuffer(1,Label2Buffer);
   SetIndexBuffer(2,Label3Buffer);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,SYMBOL_STOPSIGN);
   
   

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

    int counted_bars = IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars > 0)   counted_bars--;
   int limit = Bars - counted_bars;
   if(counted_bars==0) limit--;
   
   for(int i=limit;i>=0;i--)
   {
     int period_of_blue=(rates_total-i)<n_blue_line_lenght? rates_total-i :n_blue_line_lenght;
     int period_of_yellow=(rates_total-i< p_yellow_line_lenght)? rates_total-i :p_yellow_line_lenght;
     
     //yellow line
     a1 = high[i]>high[i+1]? high[i]-high[i+1]:0;
     a2 = low[i]<low[i+1]? low[i+1]-low[i]:0;
     NewA = a1+a2;
     r = iATR(NULL,0,period_of_yellow,1);
     PNew = 100*NewA/r;
     Label1Buffer[i]=PNew;

    //blue line
    d=iStdDevOnArray(Label1Buffer,0,period_of_blue,0,MODE_SMA,i);
    m=iMAOnArray(Label1Buffer,0,period_of_blue,0,MODE_SMA,i);
    w=m+x_yellow_line_multiplier*d;
    Label2Buffer[i]=w;

     if (PNew>w)        //? clrRed:CLR_NONE;
          Label3Buffer[i]=Label2Buffer[i];
     else
       Label3Buffer[i]=-100;
   
   }
    
    

   
   manageAlerts(); 
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; 
         if (Label3Buffer[whichBar] != -100) doAlert(whichBar,"point");
   }
}

void doAlert(int forBar, string doWhat)
{
   static datetime previousTime;
   string message;
   
   if (previousTime != Time[forBar]) {
   
       previousTime   = Time[forBar];
       message = "extertion-meter "+Symbol()+" "+Period()+" "+doWhat+" "+Hour()+":"+Minute();
       
          if (alertsMessage)      Alert(message);
          if (alertsSend)         SendNotification(message);
          if (alertsEmail)        SendMail("",message);
          if (alertsSound)        PlaySound("alert3.wav");
   }
}