Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Create 5 min bar with x period ATR . Help please

Who can help me with the ATR portion of this function? It always returns null values. Is it because talib requires minute or daily bars?

def createTimeBarRange(stock,interval,lookback):
# call by barPrices = createTimeBarRange(stock,5,10)
# get values, e.g. barPrices['high'][-1] for latest bar high
# or barPrices['high'][-2] for previous bar high etc.
# create a bar of that combines minutes into slices defined by interval

expanded= (interval  +  lookback )  
timeslice= str(interval)+ "min"  
#need to ignore the present value in our results so add 1 to lookback and then filter results  
open_price = history(expanded+1, '1m', 'open_price').dropna(axis=1)[:-1]  
open_price= open_price.resample(timeslice, how='first').dropna()


high = history(expanded+1, '1m', 'high').dropna(axis=1)[:-1]  
high= high.resample(timeslice, how='max').dropna()  


low = history(expanded+1, '1m', 'low').dropna(axis=1)[:-1]  
low= low.resample(timeslice, how='min').dropna()    


close_price = history(expanded+1, '1m', 'close_price').dropna(axis=1)[:-1]  
close_price= close_price.resample(timeslice, how='last').dropna()   




barAtr = talib.ATR( high [stock], low[stock], close_price[stock], timeperiod=lookback)  


dict = { "open_price" : open_price[stock], "high":high[stock] , "low": low[stock]  , "close_price" : close_price[stock], "barAtr": barAtr}  
return dict  
3 responses

I will respond to my own post, since no one else has :))

My testing has revealed that the talib library DOES NOT WORK with non daily (on minute) bar periods. So one would have to write one's own
function for doing so. In my case the ATR was not needed in this particular algo (I was just trying to write it into the module for use in future algos),
so I put it off for now.

If I ever do that I will post it here and on tristan's community function repository created on Github.

Serge, did you have any success with your ATR function that would work on 5 min bars?

Hey Max,

Been a while since I worked with this issue, but I think the following may work for you. Let me know how you fare:

def getATR(stock,bartype,barcount,lookback):
#usage myAtr=getATR(stock,'1m',30,14) returns series of atrs representing hi/low diff for each lookback period
# access last element by myAtr[-1]
# we must overshoot barcount because minute values returned contain offhour time slots with null values
#solution is to grab a lot more use atr timeperiod to cull them
if bartype not in ["1m","1d"]:
log.info("Incorrect bar type specified.(1m/1d) Check your parameters ong getBarATR")
return None

expanded= (30+14 ) * 10  
highs =  history(expanded+1,bartype, 'high').dropna(axis=1)[:-1]  
lows =   history(expanded+1,bartype,'low').dropna(axis=1)[:-1]  
closes = history(expanded+1,bartype, 'close_price').dropna(axis=1)[:-1]

stockHighs  = highs[stock][-expanded:]  
stockLows = lows[stock][-expanded:]  
stockCloses = closes[stock][-expanded:]  
atr = talib.ATR(stockHighs, stockLows, stockCloses, timeperiod=lookback)  
#now cull back to barcount size  
atr=atr[-barcount:]  
return atr[-1]