Hi how would i compute the cumulative moving average of the closing price in a using get_pricing? i have tried np.ma.average() however this only gives me the final value i would like each value, every day so it can be plotted. thanks in advance
Hi how would i compute the cumulative moving average of the closing price in a using get_pricing? i have tried np.ma.average() however this only gives me the final value i would like each value, every day so it can be plotted. thanks in advance
Am I correct in assuming you would like a rolling moving average and NOT really the cumulative average. For instance, every day calculate the the average of the previous n days (that would be a rolling average). The cumulative average would be to every day calculate the average from the beginning to the current day.
For a rolling average use the 'rolling' dataframe method (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html) . 'get_pricing' returns a dataframe so something like this (if you wanted a 20 day moving average).
my_prices = get_pricing(symbols=my_securities,
start_date=start_date,
end_date=end_date,
frequency='daily',
fields=['close_price'])
my_average_prices = my_prices.close_price.rolling(20).mean()
See the attached notebook with this in action.
If you really wanted the cumulative average that can be done too.
thank you for your response! ideally haven't really been that specific in my question. I would like a cumulative average so that we take into consideration the entirety of the prices up until the current data. e.g. (1,2,3,4) - cumulative average = 2.5 / (1,2,3,4,5) - cumulative average = 1.5. i would like to do this in the algorithms ide. i was able to use '.expanding' to do this in the notebook environment could i just use the same in the algorithms section when actually building my algo
Yup, the 'expanding' method is what you want. Just like the above but replace 'rolling' with 'expanding'
my_average_prices = my_prices.close_price.expanding().mean()
You can do this in the IDE too. To get the data however you will need to use the 'data.history' method ('get_pricing' only works in notebooks).
Threw me off when you said you wanted to plot the output.
thank you for your help. the reason why i need this value is i am trying to experiment with a mean reversion trade using the cumulative average. i would like to compute a z score on a daily basis which takes the (current price - cumulative average) / cumulative std. im getting a value error when attempting the following:
def compute_z_score(context, data):
#for conveniece
costco = context.cost
#historical data
prices = data.history(context.cost,"price",1,"1d")
#create our cumulative moving average
cumulative_moving_average = prices.expanding(min_periods=1).mean()[-1:]
#compute z score std
std = prices.expanding(min_periods=1).std()[-1:]
compute z score
if std > 0:
z_score = (prices - cumulative_moving_average) / std
On the if statement i get a value error. i would be so grateful if you could see why?
'std' is a numpy array. The if statement doesn't know what you mean by
if std > 0:
I believe you want the last scaler value of the array. Use the 'item' method to get a single value. Maybe something like this
std = prices.expanding(min_periods=1).std().tail(1).item()
compute z score
if std > 0:
z_score = (prices - cumulative_moving_average) / std
Himansu,
In addition to what was recommended to you by Dan Whitnable, one of The Best Quantopian Forum Helpers, you may try and compare this:
# cumulative_moving_average ZScore
import numpy as np
def initialize(context):
schedule_function(cumulative_moving_average_ZScore, date_rules.every_day(), time_rules.market_close(minutes = 1))
context.prices = np.array([])
def cumulative_moving_average_ZScore(context, data):
stock = symbol('SPY')
P = data.current(stock,'price')
context.prices = np.append(context.prices, P)
CMA = cumulative_moving_average = context.prices.mean()
STD = context.prices.std()
Z = (P - CMA) / STD if STD > 0 else 0
print Z
record( price = P, CMA = cumulative_moving_average, ZScore = Z)