python - Pandas and Rolling_Mean with Offset (Average Daily Volume Calculation) -
when pull stock data dataframe yahoo, want able calculate 5 day average of volume, excluding current date.
is there way use rolling mean offset? example, 5 day mean excludes current day , based on prior 5 days.
when run following code
r = datareader("bbry", "yahoo", '2015-01-01','2015-01-31') r['adv']=pd.rolling_mean(r['volume'], window=5)
it returns 5 day volume, inclusive of current date, when @ below, 1/8 has average volume 1/2,1/5,1/6,1/7, , 1/8. want 1/9 first date returns average volume , contain data 1/2,1/5,1/6,1/7, , 1/8.
date open high low close volume adj close symbol adv 1/2/2015 11.01 11.11 10.79 10.82 9733200 10.82 bbry 1/5/2015 10.6 10.77 10.37 10.76 12318100 10.76 bbry 1/6/2015 10.8 10.85 10.44 10.62 10176400 10.62 bbry 1/7/2015 10.65 10.8 10.48 10.67 10277400 10.67 bbry 1/8/2015 10.75 10.78 10.57 10.63 6868300 10.63 bbry 9,874,680.00 1/9/2015 10.59 10.65 10.28 10.38 7745600 10.38 bbry 9,477,160.00
thanks help
you can shift
rows achieve want:
in [44]: r['adv'] = pd.rolling_mean(r['volume'].shift(), window=5) r out[44]: open high low close volume adj close adv date 2015-01-02 11.01 11.11 10.79 10.82 9733200 10.82 nan 2015-01-05 10.60 10.77 10.37 10.76 12318100 10.76 nan 2015-01-06 10.80 10.85 10.44 10.62 10176400 10.62 nan 2015-01-07 10.65 10.80 10.48 10.67 10277400 10.67 nan 2015-01-08 10.75 10.78 10.57 10.63 6868300 10.63 nan 2015-01-09 10.59 10.65 10.28 10.38 7745600 10.38 9874680 2015-01-12 10.36 10.37 10.02 10.12 7739600 10.12 9477160 2015-01-13 10.05 10.23 9.68 9.71 15292900 9.71 8561460 2015-01-14 9.61 12.63 9.32 12.60 83543900 12.60 9584760 2015-01-15 10.36 10.71 10.01 10.11 52574600 10.11 24238060 2015-01-16 10.12 10.39 10.11 10.24 16068900 10.24 33379320 2015-01-20 10.28 10.37 9.82 10.03 15185900 10.03 35043980 2015-01-21 10.03 10.38 9.81 9.93 19614500 9.93 36533240 2015-01-22 10.44 11.11 10.24 10.51 44594300 10.51 37397560 2015-01-23 10.78 11.03 10.61 10.71 21079800 10.71 29607640 2015-01-26 10.67 10.71 10.40 10.52 6982000 10.52 23308680 2015-01-27 10.38 10.63 10.32 10.56 7057200 10.56 21491300 2015-01-28 10.65 10.67 10.10 10.12 9705000 10.12 19865560 2015-01-29 10.05 10.27 9.85 10.25 12304700 10.25 17883660 2015-01-30 10.15 10.26 10.00 10.15 9203400 10.15 11425740
Comments
Post a Comment