title: Python pandas calculate ATR on higher timeframe without resampling
body: <p>I'm having a dataframe of 1min stock data. I want to calculate AverageTruerange indicator of a higher time frame without respampling the data. I came up with the below solution, however it is slow. </p>
  
  <p>tf - higher time frame, n = lookback for ATR, OHLC gives higher timeframe candles.</p>
  
  <pre><code>def ATR(df, tf, n):
    ha_o = 'open_' + str(tf)
    ha_h = 'high_' + str(tf)
    ha_l = 'low_' + str(tf)
    ha_c = 'close_' + str(tf)
    df = OHLC(df, tf, n)
    df['p_close'] = df[ha_c].shift(tf)
    TR = df[ha_h] - df[ha_l]
    df['TR'] = TR
    df['TR_avg'] = np.nan
    atr_label = 'ATR_' + str(tf) + '_' + str(n)
    df[atr_label] = np.nan
    i = tf*n
    while i &lt;= df.index[-1]:
        TR_sum = 0
        j = 0
        while j &lt; n:
            TR_sum += TR.at[i-j*tf]
            j += 1
        TR_sum /= n
        df.at[(i, 'TR_avg')] = TR_sum
        df.at[(i, atr_label)] = (TR.at[i] + (n-1)*df.at[(i-tf, 'TR_avg')])/n
        i = i + 1
    return df
  </code></pre>
  
  <p>I have seen below pandas ATR function which works well on 1 minute. However I want to calculate on 10/15 minute.</p>
  
  <pre><code>def ATR(df, tf, n):
    i = 0
    TR_l = [0]
    while i &lt; df.index[-1]:
        TR = max(df.get_value(i + 1, 'high'), df.get_value(i, 'close')) - min(df.get_value(i + 1, 'low'), df.get_value(i, 'close'))
        TR_l.append(TR)
        i = i + 1
    df['ATR_' + str(tf) + '_' + str(n)] = Series(TR_l).ewm(ignore_na=False,span=1,min_periods=tf*n,adjust=False).mean()
    return df
  </code></pre>
  
  <p>Please help me in optimizing the function.</p>