Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can I produce an average of the prices from my resampled data?

Hey everybody, I'm new to python and having some issues here... I am looking to calculate the moving average price using 30min bars. I've managed to get the data resampled but I have two problem. The data returned includes the equity information so I can't just get the average as I normally would. Also, the output is including a few moving average prices and then NAN (I'm thinking because it is returning data from a time when the market was closed? If so, I'm not sure how to avoid that)

I originally started playing around last week with an algo titled "Robin Hood Extreme Vetting" which I found on these forums. It was suggest to me by somebody else as a starting point to wrap my head around how this all works. Below you will see a snippet of the code where I'm trying to calculate the averages. I've commented out a few lines as I realized they were causing errors but I'm sure you'll get the idea. Thanks for your time and help, looking forward to your replies!

Output:
2016-12-01 09:31 PRINT Equity(2078 [DAIO])
2016-11-30 20:00:00+00:00 4.225
2016-11-30 20:30:00+00:00 4.270
2016-11-30 21:00:00+00:00 4.160
2016-11-30 21:30:00+00:00 4.160
2016-11-30 22:00:00+00:00 NaN
2016-11-30 22:30:00+00:00 NaN
2016-11-30 23:00:00+00:00 NaN
2016-11-30 23:30:00+00:00 NaN
2016-12-01 00:00:00+00:00 NaN
2016-12-01 00:30:00+00:00 NaN
2016-12-01 01:00:00+00:00 NaN
2016-12-01 01:30:00+00:00 NaN
2016-12-01 02:00:00+00:00 NaN
2016-12-01 02:30:00+00:00 NaN
2016-12-01 03:00:00+00:00 NaN
2016-12-01 03:30:00+00:00 NaN
2016-12-01 04:00:00+00:00 NaN
2016-12-01 04:30:00+00:00 NaN
2016-12-01 05:00:00+00:00 NaN
2016-12-01 05:30:00+00:00 NaN

Code Sample:

Cycle through all stocks in myCandidates

for x in range(maxBuyOrders):  

    ## !! Insert check that stock is not in pilot program  

    stock = context.myCandidates.next()  


    ## Pull all the 1m closes needed to resample down to <candlePeriod> sized periods  
    priceHistoryShort = data.history([stock], 'close', context.shortSMAPeriod * context.candlePeriod, '1m')  
    priceHistoryLong = data.history([stock], 'close', context.longSMAPeriod * context.candlePeriod, '1m')  

    ## Resample and get average  
    shortSMA = priceHistoryShort.resample(str(context.candlePeriod) + 'T', label='right').mean()  
    print shortSMA  
    ##shortSMA = sum(shortSMA)/len(shortSMA)  
    longSMA = priceHistoryLong.resample(str(context.candlePeriod) + 'T', label='right').mean()  
    print longSMA  
    ##longSMA = sum(longSMA)/len(longSMA)  
1 response

I've found the answers I need I believe. I can't test them at the moment as I am on my phone but I think the problem getting the average might just need some help from float(). Something like x = float(shortSMA.mean) ?? And as for the NAN values in the resampled (and original data I'd assume), here is a link for a function used to remove NAN values.
https://www.quantopian.com/posts/how-to-get-rsi-in-30-minutes-time-frame

Figured I'd post what I've found incase somebody else stumbled across this in the future.