
#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;
extern bool               ShowChartLine            = false;
input int                 limitOfLineBars          = 1000;
input int                 limitline                = 3;
extern bool               ShowChartArrows          = false;
input int                 SniperArrowsY            = 3;
extern bool               RallyMode                = false;
extern bool               ChartArrowsLow           = 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;
double arsh;
int limitOfLineBars_plus;




//+------------------------------------------------------------------+
//| 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);
   limitOfLineBars_plus=limitOfLineBars+500;
   for(int i=1000; i>0; i--){arsh=arsh+(High[i]-Low[i]);} arsh=arsh/1000/SniperArrowsY;
   
   

   
//---
   return(INIT_SUCCEEDED);
  }
int deinit(){ ObjectsDeleteAll(ChartID(),"extertion-meter",0); return(0); }
//+------------------------------------------------------------------+
//| 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-1;
   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]=EMPTY_VALUE;
   
   }
   

   if(alertsOn==true){manageAlerts();}
   if(ShowChartLine==true){manageLine(limit);}
   if(ShowChartArrows==true){manageArrows(limit);} 
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void manageAlerts()
{
   if (alertsOnCurrent) int whichBar = 0;
   else                     whichBar = 1;
   if (Label3Buffer[whichBar] != EMPTY_VALUE) doAlert(whichBar,"signal");
}

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");
   }
}


void manageLine(int limit)
{
   if(limit>limitOfLineBars){limit=limitOfLineBars;}

   int li_li=0;

   for(int i=0;i<=limit;i++){  string ar_up="extertion-meter_line_up"+Time[i];   string ar_dw="extertion-meter_line_dw"+Time[i];

     if(Label3Buffer[i]!=EMPTY_VALUE && ((Open[i]>Close[i] && RallyMode==false) || (Open[i]<Close[i] && RallyMode==true))){  li_li++;
        ObjectCreate(ChartID(),ar_up,OBJ_VLINE,0,Time[i],0); ObjectSet(ar_up,OBJPROP_COLOR,DarkGreen);  ObjectSet(ar_up,OBJPROP_STYLE,0);  }
     else {ObjectDelete(ar_up);}

     if(Label3Buffer[i]!=EMPTY_VALUE && ((Open[i]<Close[i] && RallyMode==false) || (Open[i]>Close[i] && RallyMode==true))){  li_li++;
        ObjectCreate(ChartID(),ar_dw,OBJ_VLINE,0,Time[i],0); ObjectSet(ar_dw,OBJPROP_COLOR,Maroon);     ObjectSet(ar_up,OBJPROP_STYLE,0);  }
     else {ObjectDelete(ar_dw);}

     if(li_li>=limitline){break;}  
     }
     
   static datetime previousTime, endTime=Time[limitOfLineBars_plus];
   int ln=0;
   
   for(i=0;i<limitOfLineBars_plus;i++){ if (previousTime==Time[0] || limitOfLineBars==limitline || endTime>Time[i]){break;}
   
    if(Label3Buffer[i]!=EMPTY_VALUE){ln++;}
    
    if(Label3Buffer[i]!=EMPTY_VALUE && ln>limitline){ar_up="extertion-meter_line_up"+Time[i]; ObjectDelete(ar_up); ar_dw="extertion-meter_line_dw"+Time[i]; ObjectDelete(ar_dw); endTime=Time[i]; break;}  }

   if (previousTime!=Time[0]) {previousTime=Time[0];}
}

void manageArrows(int limit)
{
   for(int i=limit;i>=0;i--){  string ar_up="extertion-meter_arrow_up"+Time[i];   string ar_dw="extertion-meter_arrow_dw"+Time[i];

     if((Label3Buffer[i]!=EMPTY_VALUE && Open[i]>Close[i] && RallyMode==false)  || (Label3Buffer[i]!=EMPTY_VALUE && Open[i]<Close[i] && RallyMode==true)){
        ObjectCreate(ChartID(),ar_up,OBJ_ARROW,0,Time[i],Low[i]); ObjectSet(ar_up,OBJPROP_COLOR,DodgerBlue); ObjectSet(ar_up,OBJPROP_ARROWCODE,233); ObjectSet(ar_up,OBJPROP_WIDTH,1);  }
     else {ObjectDelete(ar_up);}

     if((Label3Buffer[i]!=EMPTY_VALUE && Open[i]<Close[i] && RallyMode==false) || (Label3Buffer[i]!=EMPTY_VALUE && Open[i]>Close[i] && RallyMode==true)){ double hh; if(ChartArrowsLow==false){hh=High[i]+arsh;} else{hh=Low[i];}
        ObjectCreate(ChartID(),ar_dw,OBJ_ARROW,0,Time[i],hh);     ObjectSet(ar_dw,OBJPROP_COLOR,Red);        ObjectSet(ar_dw,OBJPROP_ARROWCODE,234); ObjectSet(ar_dw,OBJPROP_WIDTH,1);  }
     else {ObjectDelete(ar_dw);}
   }                                                                   
}