Notebook

Rolling mean with get_pricing example

In [1]:
# make a list of the securities one wishes to get pricing for
my_securities = symbols(['AAPL'])

# set the start and stop date-time.
# note that times/dates are specified in UTC 
start_date = '2016-12-10'
end_date = '2017-12-10'
In [2]:
# run the get_pricing method to pull in actual data
# the result is a Pandas dataframe
my_prices = get_pricing(symbols=my_securities, 
                        start_date=start_date, 
                        end_date=end_date, 
                        frequency='daily', 
                        fields=['close_price'])
In [3]:
# display the resulting dataframe
my_prices.close_price
Out[3]:
Equity(24 [AAPL])
2016-12-12 00:00:00+00:00 111.507
2016-12-13 00:00:00+00:00 113.367
2016-12-14 00:00:00+00:00 113.377
2016-12-15 00:00:00+00:00 113.987
2016-12-16 00:00:00+00:00 114.138
2016-12-19 00:00:00+00:00 114.784
2016-12-20 00:00:00+00:00 115.089
2016-12-21 00:00:00+00:00 115.207
2016-12-22 00:00:00+00:00 114.450
2016-12-23 00:00:00+00:00 114.676
2016-12-27 00:00:00+00:00 115.394
2016-12-28 00:00:00+00:00 114.893
2016-12-29 00:00:00+00:00 114.883
2016-12-30 00:00:00+00:00 114.007
2017-01-03 00:00:00+00:00 114.302
2017-01-04 00:00:00+00:00 114.184
2017-01-05 00:00:00+00:00 114.765
2017-01-06 00:00:00+00:00 116.044
2017-01-09 00:00:00+00:00 117.117
2017-01-10 00:00:00+00:00 117.225
2017-01-11 00:00:00+00:00 117.845
2017-01-12 00:00:00+00:00 117.363
2017-01-13 00:00:00+00:00 117.156
2017-01-17 00:00:00+00:00 118.091
2017-01-18 00:00:00+00:00 118.091
2017-01-19 00:00:00+00:00 117.875
2017-01-20 00:00:00+00:00 118.101
2017-01-23 00:00:00+00:00 118.189
2017-01-24 00:00:00+00:00 118.081
2017-01-25 00:00:00+00:00 119.971
... ...
2017-10-27 00:00:00+00:00 162.446
2017-10-30 00:00:00+00:00 166.123
2017-10-31 00:00:00+00:00 168.444
2017-11-01 00:00:00+00:00 166.252
2017-11-02 00:00:00+00:00 167.468
2017-11-03 00:00:00+00:00 171.862
2017-11-06 00:00:00+00:00 173.696
2017-11-07 00:00:00+00:00 174.164
2017-11-08 00:00:00+00:00 175.589
2017-11-09 00:00:00+00:00 175.240
2017-11-10 00:00:00+00:00 174.660
2017-11-13 00:00:00+00:00 173.980
2017-11-14 00:00:00+00:00 171.340
2017-11-15 00:00:00+00:00 169.090
2017-11-16 00:00:00+00:00 171.100
2017-11-17 00:00:00+00:00 170.110
2017-11-20 00:00:00+00:00 169.990
2017-11-21 00:00:00+00:00 173.130
2017-11-22 00:00:00+00:00 174.950
2017-11-24 00:00:00+00:00 174.960
2017-11-27 00:00:00+00:00 174.080
2017-11-28 00:00:00+00:00 173.000
2017-11-29 00:00:00+00:00 169.490
2017-11-30 00:00:00+00:00 171.750
2017-12-01 00:00:00+00:00 170.990
2017-12-04 00:00:00+00:00 169.820
2017-12-05 00:00:00+00:00 169.650
2017-12-06 00:00:00+00:00 169.050
2017-12-07 00:00:00+00:00 169.330
2017-12-08 00:00:00+00:00 169.360

251 rows × 1 columns

In [4]:
# Ue the 'rolling' method along with 'mean' to get a rolling average
# Set the window length to whatever is desired. In this case a window of 20 days
my_average_prices = my_prices.close_price.rolling(20).mean()
my_average_prices
Out[4]:
Equity(24 [AAPL])
2016-12-12 00:00:00+00:00 NaN
2016-12-13 00:00:00+00:00 NaN
2016-12-14 00:00:00+00:00 NaN
2016-12-15 00:00:00+00:00 NaN
2016-12-16 00:00:00+00:00 NaN
2016-12-19 00:00:00+00:00 NaN
2016-12-20 00:00:00+00:00 NaN
2016-12-21 00:00:00+00:00 NaN
2016-12-22 00:00:00+00:00 NaN
2016-12-23 00:00:00+00:00 NaN
2016-12-27 00:00:00+00:00 NaN
2016-12-28 00:00:00+00:00 NaN
2016-12-29 00:00:00+00:00 NaN
2016-12-30 00:00:00+00:00 NaN
2017-01-03 00:00:00+00:00 NaN
2017-01-04 00:00:00+00:00 NaN
2017-01-05 00:00:00+00:00 NaN
2017-01-06 00:00:00+00:00 NaN
2017-01-09 00:00:00+00:00 NaN
2017-01-10 00:00:00+00:00 114.66980
2017-01-11 00:00:00+00:00 114.98670
2017-01-12 00:00:00+00:00 115.18650
2017-01-13 00:00:00+00:00 115.37545
2017-01-17 00:00:00+00:00 115.58065
2017-01-18 00:00:00+00:00 115.77830
2017-01-19 00:00:00+00:00 115.93285
2017-01-20 00:00:00+00:00 116.08345
2017-01-23 00:00:00+00:00 116.23255
2017-01-24 00:00:00+00:00 116.41410
2017-01-25 00:00:00+00:00 116.67885
... ...
2017-10-27 00:00:00+00:00 156.24680
2017-10-30 00:00:00+00:00 156.89000
2017-10-31 00:00:00+00:00 157.61635
2017-11-01 00:00:00+00:00 158.28295
2017-11-02 00:00:00+00:00 158.91420
2017-11-03 00:00:00+00:00 159.77060
2017-11-06 00:00:00+00:00 160.69230
2017-11-07 00:00:00+00:00 161.63340
2017-11-08 00:00:00+00:00 162.61390
2017-11-09 00:00:00+00:00 163.60335
2017-11-10 00:00:00+00:00 164.51395
2017-11-13 00:00:00+00:00 165.24810
2017-11-14 00:00:00+00:00 165.82035
2017-11-15 00:00:00+00:00 166.31495
2017-11-16 00:00:00+00:00 167.09940
2017-11-17 00:00:00+00:00 167.82040
2017-11-20 00:00:00+00:00 168.53885
2017-11-21 00:00:00+00:00 169.36900
2017-11-22 00:00:00+00:00 170.32400
2017-11-24 00:00:00+00:00 171.22970
2017-11-27 00:00:00+00:00 171.81140
2017-11-28 00:00:00+00:00 172.15525
2017-11-29 00:00:00+00:00 172.20755
2017-11-30 00:00:00+00:00 172.48245
2017-12-01 00:00:00+00:00 172.65855
2017-12-04 00:00:00+00:00 172.55645
2017-12-05 00:00:00+00:00 172.35415
2017-12-06 00:00:00+00:00 172.09845
2017-12-07 00:00:00+00:00 171.78550
2017-12-08 00:00:00+00:00 171.49150

251 rows × 1 columns

In [5]:
# Now one can easily plot the rolling average along with the actual close prices
ax = my_prices.close_price.plot()
my_average_prices.plot(ax=ax)
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe2fa34be90>