Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ATR looking back n days using talib

I am fairly new at this, and thought I had a handle on things until I started working with talib ATR. If I have history data for 40 days, when I get the SMA using length 20, what I have is the series of data that is the sma over the last 20 days. WIth na for the first 20 and then values for the last 20, makes perfect sense.

closes = history(40,'1d','close_price')[stock]  
sma = talib.SMA(closes,20)  
sma_two_days_ago = sma[-3]  

But when I try the same thing for ATR, the data seems completely off.

closes = history(40,'1d','close_price')[stock]  
highs = history(40,'1d','high')[stock]  
lows = history(40,'1d','low')[stock]  
atr = talib.ATR(highs, lows, closes,20)  
atr_two_days_ago = atr[-3] #this and all the numbers in the series are wrong for ATR  

The only way I have been able to get the right ATR for a lookback is by doing this(shifting the input series so I get exactly the one value I want, instead of rolling data):

atr = talib.ATR(highs[-23:-2],lows[-23:-2],closes[-23:-2],20)  
atr_two_days_ago = atr[-1]  

Why is the ATR calculation not rolling properly like the SMA and BBANDS and other indicators so nicely do? Shouldnt putting the time period of 20 in the ATR function take care of this? I think understanding this might give me a nice little breakthrough, this has been rattling my brain.

2 responses

You should provide a complete example to reproduce your issue, you should also explain what "numbers are wrong" means.

The actual code you pasted seems to work well enough, i.e. running:

from talib import ATR

def initialize(context):  
    context.stock = sid(24)

def handle_data(context, data):

    stock = context.stock  
    closes = history(40,'1d','close_price')[stock]  
    highs = history(40,'1d','high')[stock]  
    lows = history(40,'1d','low')[stock]  
    atr = ATR(highs, lows, closes,20)  
    atr_two_days_ago = atr[-3]  
    log.info("Serie: %r" % atr)  
    log.info("Two days ago: %f" % atr_two_days_ago)  

from 2011-01-02 to 2011-01-03 results in the following log:

2011-01-03 handle_data:14 INFO Serie: array([        nan,         nan,         nan,         nan,         nan,  
               nan,         nan,         nan,         nan,         nan,  
               nan,         nan,         nan,         nan,         nan,  
               nan,         nan,         nan,         nan,         nan,  
        0.8512    ,  0.85049   ,  0.8359155 ,  0.81891972,  0.79547374,  
        0.78755005,  0.77347255,  0.76204892,  0.74189648,  0.71435165,  
        0.71448407,  0.69545987,  0.67618687,  0.65722753,  0.65236615,  
        0.63374784,  0.61171045,  0.59867493,  0.58554118,  0.61211412])  
2011-01-03 handle_data:15 INFO Two days ago: 0.598675  

Here are some sample talib algos, including ATR and bollinger bands: https://www.quantopian.com/help#api-talib

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.