title: Sort ATR by hour
body: <p>Hello Im wondering if there is a way I can somehow view ATR by hour, so when it calculates results in has 24 different values from 00:00 to 23:00. I would like the average to be of the ranges for that hour only in the lookback period. (for example hour 02:00 compares it range to the other 02:00 ranges in the look back period)</p>
  <p>Original ATR Code</p>
  <pre><code>//|                                                          ATR.mq4 |
  //|                   Copyright 2005-2014, MetaQuotes Software Corp. |
  //|                                              http://www.mql4.com |
  //+------------------------------------------------------------------+
  #property copyright   &quot;2005-2014, MetaQuotes Software Corp.&quot;
  #property link        &quot;http://www.mql4.com&quot;
  #property description &quot;Average True Range&quot;
  #property strict
  
  //--- indicator settings
  #property indicator_separate_window
  #property indicator_buffers 1
  #property indicator_color1  DodgerBlue
  //--- input parameter
  input int InpAtrPeriod=14; // ATR Period
  //--- buffers
  double ExtATRBuffer[];
  double ExtTRBuffer[];
  //+------------------------------------------------------------------+
  //| Custom indicator initialization function                         |
  //+------------------------------------------------------------------+
  int OnInit(void)
    {
     string short_name;
  //--- 1 additional buffer used for counting.
     IndicatorBuffers(2);
     IndicatorDigits(Digits);
  //--- indicator line
     SetIndexStyle(0,DRAW_LINE);
     SetIndexBuffer(0,ExtATRBuffer);
     SetIndexBuffer(1,ExtTRBuffer);
  //--- name for DataWindow and indicator subwindow label
     short_name=&quot;ATR(&quot;+IntegerToString(InpAtrPeriod)+&quot;)&quot;;
     IndicatorShortName(short_name);
     SetIndexLabel(0,short_name);
  //--- check for input parameter
     if(InpAtrPeriod&lt;=0)
       {
        Print(&quot;Wrong input parameter ATR Period=&quot;,InpAtrPeriod);
        return(INIT_FAILED);
       }
  //---
     SetIndexDrawBegin(0,InpAtrPeriod);
  //---
     return(INIT_SUCCEEDED);
    }
  //+------------------------------------------------------------------+
  //| Average True Range                                               |
  //+------------------------------------------------------------------+
  int OnCalculate(const int rates_total,
                  const int prev_calculated,
                  const datetime &amp;time[],
                  const double &amp;open[],
                  const double &amp;high[],
                  const double &amp;low[],
                  const double &amp;close[],
                  const long &amp;tick_volume[],
                  const long &amp;volume[],
                  const int &amp;spread[])
    {
     int i,limit;
  //--- check for bars count and input parameter
     if(rates_total&lt;=InpAtrPeriod || InpAtrPeriod&lt;=0)
        return(0);
  //--- counting from 0 to rates_total
     ArraySetAsSeries(ExtATRBuffer,false);
     ArraySetAsSeries(ExtTRBuffer,false);
     ArraySetAsSeries(open,false);
     ArraySetAsSeries(high,false);
     ArraySetAsSeries(low,false);
     ArraySetAsSeries(close,false);
  //--- preliminary calculations
     if(prev_calculated==0)
       {
        ExtTRBuffer[0]=0.0;
        ExtATRBuffer[0]=0.0;
        //--- filling out the array of True Range values for each period
        for(i=1; i&lt;rates_total; i++)
           ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
        //--- first AtrPeriod values of the indicator are not calculated
        double firstValue=0.0;
        for(i=1; i&lt;=InpAtrPeriod; i++)
          {
           ExtATRBuffer[i]=0.0;
           firstValue+=ExtTRBuffer[i];
          }
        //--- calculating the first value of the indicator
        firstValue/=InpAtrPeriod;
        ExtATRBuffer[InpAtrPeriod]=firstValue;
        limit=InpAtrPeriod+1;
       }
     else
        limit=prev_calculated-1;
  //--- the main loop of calculations
     for(i=limit; i&lt;rates_total; i++)
       {
        ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
        ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-InpAtrPeriod])/InpAtrPeriod;
       }
  //--- return value of prev_calculated for next call
     return(rates_total);
    }
  //+------------------------------------------------------------------+````
  </code></pre>