#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 White
#property indicator_color2 Lime
#property indicator_color3 Lime
#property indicator_color4 Lime
#property indicator_color5 Red
#property indicator_color6 Red
#property indicator_color7 Red

extern string Mode_descr="0->MaxHodEMA, 1->Volatility";
extern int Mode=0;
//EMA method
extern string EMA_desc="===EMA method. Mode=0===";
extern int PeriodShiftEMA=0;
extern int CandleNumEMA=233;
//Volatility method
extern string Vol_desc="===Volatility method. Mode=1===";
extern int PeriodShiftVol=0;
extern int CandleNumVol=233;
extern string settings="===other settings===";
extern color label_color=Yellow;
extern int label_size=12;



string speriods[] = {"", "M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN1"};
int periods[] = {0, PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, 
                 PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1};
int period0;
string speriod0;

double ExtMapBuffer1[];

double ExtMapBufferR1[];
double ExtMapBufferR2[];
double ExtMapBufferR3[];

double ExtMapBufferS1[];
double ExtMapBufferS2[];
double ExtMapBufferS3[];

   double MAUpAv, MAUpMax, MADnAv, MADnMax; 
   string MAUpAv_str, MAUpMax_str, MADnAv_str, MADnMax_str; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
string mode_str;
if(Mode==1) {
period0 = periods[ArrayBsearch(periods, Period()) + PeriodShiftVol];
speriod0 = speriods[ArrayBsearch(periods, Period()) + PeriodShiftVol];
mode_str="Volatility";
}

if(Mode==0) {
period0 = periods[ArrayBsearch(periods, Period()) + PeriodShiftEMA];
speriod0 = speriods[ArrayBsearch(periods, Period()) + PeriodShiftEMA];
mode_str="EMA";
}


   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBufferR1);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBufferR2);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBufferR3);

   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBufferS1);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtMapBufferS2);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,ExtMapBufferS3);

ObjectCreate("label_object_mode", OBJ_LABEL, 0, 0, 0);
string text1 = "Mode=>";
text1=text1 + mode_str; 
ObjectSetText("label_object_mode", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_mode", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_mode", OBJPROP_YDISTANCE, 20);

ObjectCreate("label_object_period", OBJ_LABEL, 0, 0, 0);
text1 = "Counted period=>";
text1=text1 + speriod0; 
ObjectSetText("label_object_period", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_period", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_period", OBJPROP_YDISTANCE, 50);




   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   remove_ema_text();
   ObjectDelete("label_object_mode");
   ObjectDelete("label_object_period");
   ObjectDelete("label_object_vol");
   ObjectDelete("label_object_vol1");
   ObjectDelete("label_object_ema");
   ObjectDelete("label_object_cb");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

string text1;
if(Mode==1) {
   maxVol(CandleNumVol, period0);
   MAUpMax=MathMax(MathAbs(StrToDouble(MAUpMax_str)), MathAbs(StrToDouble(MADnMax_str)));
   MAUpAv=MathMax(MathAbs(StrToDouble(MAUpAv_str)), MathAbs(StrToDouble(MADnAv_str)));
   ema_draw(233, period0, MAUpAv, MAUpAv, MAUpMax-(MAUpAv+MAUpAv), MAUpAv, MAUpAv, MAUpMax-(MAUpAv+MAUpAv));

ObjectCreate("label_object_vol", OBJ_LABEL, 0, 0, 0);
text1=DoubleToStr(MAUpMax, 0); 
text1 = "Max Volatility=>" + text1;
ObjectSetText("label_object_vol", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_vol", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_vol", OBJPROP_YDISTANCE, 110);

ObjectCreate("label_object_vol1", OBJ_LABEL, 0, 0, 0);
text1=DoubleToStr(MAUpAv, 0); 
text1 = "Average Volatility=>" + text1;
ObjectSetText("label_object_vol1", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_vol1", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_vol1", OBJPROP_YDISTANCE, 140);


}
if(Mode==0) {
double maxHod=maxHodValEMA(233, CandleNumEMA);
ema_draw(233, period0, maxHod/3, maxHod/3, maxHod/3, maxHod/3, maxHod/3, maxHod/3);
ObjectCreate("label_object_ema", OBJ_LABEL, 0, 0, 0);
text1=DoubleToStr(maxHod, 0); 
text1 = "Max EMA hod=>" + text1;
ObjectSetText("label_object_ema", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_ema", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_ema", OBJPROP_YDISTANCE, 110);

}


   return(0);
  }
//+------------------------------------------------------------------+

int search(int timeframe, int EMAPeriod, int CandleNum)
{
 //определеям количество баров на определенном временном графике 
 int bars=iBars( Symbol(), timeframe);
int counted_bars=IndicatorCounted(), limit;
 double CurEMA;
  double HodVal;
  double MaxHodVal=0;
  string text1;
  
  //---- last counted bar will be recounted
   if(counted_bars>0) 
   {
      counted_bars--;
    }
      
   if(CandleNum>0) {
   limit=CandleNum;
   }
   else
   {
   limit=bars-counted_bars;
   }

ObjectCreate("label_object_cb", OBJ_LABEL, 0, 0, 0);
text1=DoubleToStr(limit, 0); 
text1 = "Counted bars=>" + text1;
ObjectSetText("label_object_cb", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_cb", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_cb", OBJPROP_YDISTANCE, 80);
   
 //начинаем обсчет всех баров и получаем их значения
 for(int i=0; i<=limit; i++)
 {
   CurEMA=NormalizeDouble(iMA(NULL,timeframe,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i),Digits);
   double high=iHigh(Symbol(), timeframe, i);
   double low=iLow(Symbol(), timeframe, i);
   HodVal = MathMax(MathAbs(iHigh(Symbol(), timeframe, i) - CurEMA), MathAbs(iLow(Symbol(), timeframe, i)  - CurEMA) );
      if(HodVal>MaxHodVal)
      {
         MaxHodVal=HodVal;
         
      }
 
 }

 int    lotsize=MarketInfo(Symbol(),MODE_LOTSIZE);
  
return(MaxHodVal/Point);
}


//Процедура определяющее максимальный ход валюты от EMA233 и строящая кривые
int maxHodValEMA(int EMAPeriod, int CandleNum)
{

   int maxHod=search(period0, EMAPeriod, CandleNum);
   return(maxHod);
}


//Процедура определения волатильности (метод PUPO)
int maxVol(int CountBarsForAverage, int timeframe)
{
   int limit;
   int counted_bars=IndicatorCounted(), i, y, CountAverage;
   double   Spread=Ask-Bid, open;
   double  CandleSum=0, CandleMax=0, CandleAverage=0 ;
//   double MAUpAv, MAUpMax, MADnAv, MADnMax; 
   double AvgRatio = 1.2;             // Коэффициент запаса для отступа стопов по средней
   double MaxRatio = 1.2;             // Коэффициент запаса для отступа стопов по максимальной
string text1;
//---- check for possible errors
   if(counted_bars<0) 
      return(-1);
      
//---- last counted bar will be recounted
   if(counted_bars>0) 
      counted_bars--;
      
   limit=iBars(Symbol(), timeframe)-counted_bars;

ObjectCreate("label_object_cb", OBJ_LABEL, 0, 0, 0);
text1=DoubleToStr(CountBarsForAverage, 0); 
text1 = "Counted bars=>" + text1;
ObjectSetText("label_object_cb", text1, label_size, "Times New Roman", label_color);
ObjectSet("label_object_cb", OBJPROP_XDISTANCE, 0);
ObjectSet("label_object_cb", OBJPROP_YDISTANCE, 80);


//---- main loop
   for( i=limit; i>=0; i--)
    {
     CandleSum=0;
     CandleMax=0;
     
     for (CountAverage=CountBarsForAverage; CountAverage>0; CountAverage--) 
         {
          CandleSum+=MathAbs(iHigh(Symbol(), timeframe, CountAverage+i)-iLow(Symbol(), timeframe, CountAverage+i));
          if (CandleMax < MathAbs(iHigh(Symbol(), timeframe, CountAverage+i)-iLow(Symbol(), timeframe, CountAverage+i)) )
             CandleMax = MathAbs(iHigh(Symbol(), timeframe, CountAverage+i)-iLow(Symbol(), timeframe, CountAverage+i));  
         }
             
     CandleAverage=CandleSum/CountBarsForAverage;
     open=iOpen(Symbol(), timeframe, i);
     MAUpAv=open+AvgRatio*CandleAverage+Spread;
     MAUpAv_str=DoubleToStr((MAUpAv-open)*MathPow(10,Digits),0);
     MADnAv=open-AvgRatio*CandleAverage;
     MADnAv_str=DoubleToStr((MADnAv-open)*MathPow(10,Digits),0);
     
     MAUpMax=open+MaxRatio*CandleMax+Spread;
     MAUpMax_str=DoubleToStr((MAUpMax-open)*MathPow(10,Digits),0);

     MADnMax=open-MaxRatio*CandleMax;
     MADnMax_str=DoubleToStr((MADnMax-open)*MathPow(10,Digits),0);

    }

//---- done
   return(0);

}

int ema_draw(int EMAPeriod, int timeframe, double R1, double R2, double R3, double S1, double S2, double S3)
{
   int    counted_bars=IndicatorCounted(), limit;
   int bars=iBars( Symbol(), timeframe);

  //---- last counted bar will be recounted
   if(counted_bars>0) 
      counted_bars--;
      
   limit=bars-counted_bars;
   int CountBarsForShift=12;
   datetime  time=Time[0]+CountBarsForShift*60*Period();
   remove_ema_text();
Print("Timeframe=>", timeframe);
   for(int i=0; i<=limit; i++)
   {

      ExtMapBuffer1[i]=iMA(NULL, timeframe,EMAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      ObjectCreate("ema", OBJ_TEXT, 0, time, ExtMapBuffer1[i]);
      ExtMapBufferR1[i]=ExtMapBuffer1[i]+NormalizeDouble(R1*Point,Digits) ;
      ObjectCreate("R1", OBJ_TEXT, 0, time , ExtMapBufferR1[i]);
      ExtMapBufferR2[i]=ExtMapBufferR1[i]+NormalizeDouble(R2*Point,Digits) ;
      ObjectCreate("R2", OBJ_TEXT, 0, time , ExtMapBufferR2[i]);
      ExtMapBufferR3[i]=ExtMapBufferR2[i]+NormalizeDouble(R3*Point,Digits) ;
      ObjectCreate("R3", OBJ_TEXT, 0, time, ExtMapBufferR3[i]);

      ExtMapBufferS1[i]=ExtMapBuffer1[i]-NormalizeDouble(S1*Point,Digits) ;
      ObjectCreate("SS1", OBJ_TEXT, 0, time, ExtMapBufferS1[i]);
      ExtMapBufferS2[i]=ExtMapBufferS1[i]-NormalizeDouble(S2*Point,Digits) ;
      ObjectCreate("SS2", OBJ_TEXT, 0, time, ExtMapBufferS2[i]);
      ExtMapBufferS3[i]=ExtMapBufferS2[i]-NormalizeDouble(S3*Point,Digits) ;
      ObjectCreate("SS3", OBJ_TEXT, 0, time, ExtMapBufferS3[i]);
   }   

   string text0=DoubleToStr(0,0);
   string text1=DoubleToStr(R1,0);
   string text2=DoubleToStr(R2,0);
   string text3=DoubleToStr(R3,0);
   
   ObjectSetText("ema", text0, 15, "Terminal", indicator_color1);
   ObjectSetText("R1", "R1( "+text1+")", 13, "Terminal", indicator_color2);
   ObjectSetText("R2", "R2("+text2+")", 13, "Terminal", indicator_color3);
   ObjectSetText("R3", "R3("+text3+")", 13, "Terminal", indicator_color4);

   ObjectSetText("SS1", "S1("+text1+")", 13, "Terminal", indicator_color5);
   ObjectSetText("SS2", "S2("+text2+")", 13, "Terminal", indicator_color6);
   ObjectSetText("SS3", "S3("+text3+")", 13, "Terminal", indicator_color7);
}


//remove EAM text labels for rewritting with new values
int remove_ema_text()
{
 ObjectDelete("ema");
 ObjectDelete("R1");
 ObjectDelete("R2");
 ObjectDelete("R3");
 ObjectDelete("SS1");
 ObjectDelete("SS2");
 ObjectDelete("SS3");
 return(0);
}