python - Volume Weighted Moving Averages -
i have dataframe looks like:
last price volume volume_ratio date 2005-01-03 1202.08 1.332210e+09 1.23 2005-01-04 1188.05 1.552646e+09 1.55 2005-01-05 1183.74 1.428365e+09 1.65 2005-01-06 1187.89 1.331004e+09 1.23 2005-01-07 1186.19 1.273960e+09 0.83 2005-01-10 1190.25 1.213694e+09 1.06 with period = 5, want compute moving average vama = cumsum/cumdiv cumsum = (df['volume_ratio']*df['last price']).cumsum() , cumdiv = df['volume_ratio'].cumsum(), condition cumdiv <= period.
initially thought using expanding.sum() , df.apply work struggling it; like:
cum_div = df['volume_ratio'].expanding(min_periods = 1).sum() cum_summ = (df['last price']*df['volume_ratio']).expanding(min_periods =1).sum() df['cum_sum'] = df.apply(lambda x: cum_summ if cum_div <= 13, axis = 1) this doesn't work, interested in best way attack this.
thanks in advance
just using rolling http://pandas.pydata.org/pandas-docs/stable/generated/pandas.dataframe.rolling.html
df['myrollingmean'] = df.vama.rolling(window = 5).mean()
Comments
Post a Comment