Moving Average of Something Other than the Close Price

I am trying to calculate a simple average of something other than the close price. So, for example, the Low Price * .95. Or the High Price * 1.05. Does anyone have any suggestions? All I can find is the MAVG function and it seems to only work off of the close price.

6 responses

There's a section in the Help & API Docs titled History (https://www.quantopian.com/help#ide-history) that discusses how to get historical low/high etc prices for securities. You can define the number of days you want to include in your history look up by setting "bar_count" as a parameter to the history function. This will return a pandas DataFrame to which you can apply the standard pandas transforms. e.g. calculating a 5-day moving average (from the docs where price_history.mean() gives you a 5-day rolling moving average):

def initialize(context):
set_universe(universe.DollarVolumeUniverse(90.0, 90.1))

def handle_data(context, data):
price_history = history(bar_count=5, frequency='1d', field='price')
log.info(price_history.mean())


In your case, you can change the field parameter from 'price' (which gives you closing prices) to 'low' or 'high'.

Hi Sekuri-

Thanks so much for your reply. I really appreciate it. This looks like a way to calculate an average off of low or high price. How would I manipulate those prices first before running and Mean of them? Is there a way to look through them and modify their values?

Not sure I exactly understand what you're after, but do you mean something like this?

lows = 0.95*history(bar_count=5, frequency='1d', field='low')
lows_mavg = lows.mean()
highs = 1.05* history(bar_count=5, frequency='1d', field='high')
highs_mavg = highs.mean()


Thanks again for the quick reply. I'm anxious to get this working.

No, not quite. I want to manipulate each value individually. So, I would like to take the (High-Low/High) for each day, then calculate an average for the last 10 days from that number.

like this?

        lows = history(bar_count=11, frequency='1d', field='low') #bar_count =11 to get last 10 complete days (the last - 11th - bar is today's info before closing and will always be grabbed)
highs = history(bar_count=11, frequency='1d', field='high')
mavg = (highs[0:-1]-lows[0:-1]).divide(highs[0:-1]) # [0:-1] to account for last 10 complete days - ignore today's incomplete info
print mavg.mean()


Hi Sekuri,

Thank you very much. I was trying to do it in a loop, your method of using highs[0:-1] is much better. I really appreciate your help!