//+------------------------------------------------------------------+
//|                                         i_Alex_Activity_v02c.mq4 |
//+------------------------------------------------------------------+
#property copyright "fion"
#property link      ""
//#property strict

//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  LightSlateGray
#property  indicator_color2  Blue
#property  indicator_color3  Red
#property  indicator_width1  2
#property  indicator_width2  2
#property  indicator_width3  2
#property  indicator_levelcolor LightSlateGray
#property  indicator_levelstyle STYLE_DOT                      

//--- indicator parameters
extern int BARS_Period       = 3;                                    // Период расчёта активности 
extern int Level             = 100;                                  // Пороговый уровень активности

//--- indicator buffers
double     Buffer0[], Buffer1[], Buffer2[]; 

int         mp; 

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {

  IndicatorDigits(1);
  
  if(BARS_Period<1) BARS_Period=1;
  IndicatorShortName("i_Alex_Activity("+BARS_Period+")_v02с");
  
  SetIndexStyle(0,DRAW_HISTOGRAM);
  SetIndexStyle(1,DRAW_HISTOGRAM);
  SetIndexStyle(2,DRAW_HISTOGRAM);
    
  SetIndexBuffer(0,Buffer0);  
  SetIndexBuffer(1,Buffer1);  
  SetIndexBuffer(2,Buffer2);   
  
  SetIndexLabel(1,"Upper");
  SetIndexLabel(2,"Lower");
  
  SetLevelValue(1,Level);
  SetLevelValue(2,-Level);
  
  mp=1; 
  if(Digits==3 || Digits==5) mp=10;
  
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {

int i, j, limit, counted_bars; 

  counted_bars=IndicatorCounted();
  if(counted_bars>0) counted_bars--;
  limit=Bars-counted_bars;
  
  if(BARS_Period<1) BARS_Period=1;
  
  for(i=0;i<limit;i++) {
    double Summa=0.0;
    for(j=i;j<i+BARS_Period;j++) {
      if(Close[j]-Open[j]>=0) Summa+=(Close[j]-Open[j])/Point/mp;
      else Summa+=-(Open[j]-Close[j])/Point/mp;
    }  
    if(Summa>Level) Buffer1[i]=Summa;
    else if(Summa<-Level) Buffer2[i]=Summa;
         else Buffer0[i]=Summa;
  }

  return(0);
}
