//+------------------------------------------------------------------+
//|                                                      Atr_3v1.mq4 |
//|                          Copyright © 2006, HexSys Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, HexSys Software Corp."
#property link      ""

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 5
#property indicator_level1 0
#property indicator_level2 0
#property indicator_color1 LightSteelBlue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Yellow
#property indicator_color5 Red
//---- input parameters
extern int       AtrMAX=4000;
extern int       Percent=90;
extern int       mBars=200;
extern int       Koeff=3;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY, 1);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY, 2);
   //SetIndexArrow(1,241);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexStyle(2,DRAW_HISTOGRAM,EMPTY, 2);
   //SetIndexArrow(2,242);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   //*
   IndicatorDigits(2);
   SetLevelStyle(STYLE_DOT,1,Yellow);
   SetLevelValue(0,Percent);
   SetLevelValue(1,Percent*Koeff);


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   double aATR[25],aVol[25];
//----
   //for(int i=0;i<counted_bars;i++)
   double s=0; int j=0;   
   int sh=0,H=0,kol=0;
   
   for(j=1;j<=AtrMAX;j++)
   {
      H=TimeHour(Time[j]);
      
      if (H==0) kol++;
      
      if (aATR[H]==0) aATR[H]=iATR(NULL,0,1,j);
         else aATR[H]=(aATR[H]+iATR(NULL,0,1,j))/2;
      
      if (aVol[H]==0) aVol[H]=Volume[j];
         else aVol[H]=(aVol[H]+Volume[j])/2;
         
   }
   
   Comment(kol);
   
   //for zero bar
      H=TimeHour(Time[0]);
      ExtMapBuffer1[0]=(iATR(NULL,0,1,0)*100) / aATR[H];
      ExtMapBuffer4[0]=(aVol[0]*100) / aVol[H];
      
   //for All bars...
   for(int i=1;i<mBars;i++)
   {
      H=TimeHour(Time[i]);
      //ExtMapBuffer2[i]=iATR(NULL,0,AtrMAX,i);
      ExtMapBuffer1[i]=(iATR(NULL,0,1,i)*100) / aATR[H];
      
      s=aVol[H]; if (s==0) s=1;
      ExtMapBuffer4[i]=(Volume[i]*100) / s;
      
      if(ExtMapBuffer4[i]>Percent && ExtMapBuffer1[i]>Percent && 
         ExtMapBuffer4[i]<Percent*Koeff && ExtMapBuffer1[i]<Percent*Koeff &&
         ExtMapBuffer1[i]>ExtMapBuffer4[i])
      {
         if (Open[i]<Close[i]) ExtMapBuffer2[i]=ExtMapBuffer1[i];
         if (Open[i]>Close[i]) ExtMapBuffer3[i]=ExtMapBuffer1[i];
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+