Monday, 25 November 2019

quantopian lecture rolling mean, rolling std

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

start = '2018-01-01'
end = '2019-02-01'
data = get_pricing(['MSFT'], fields='price', start_date=start, end_date=end)

rm = data.rolling(window=30,center=False).mean()
std = data.rolling(window=30,center=False).std()

_, ax = plt.subplots()
ax.plot(data.index, data)
ax.plot(rm.index, rm)
ax.plot(rm + std)
ax.plot(rm - std)

plt.title("Mcrosoft Prices")
plt.ylabel("Price")
plt.xlabel("Date");
plt.legend(['Price', 'Moving Average', 'Moving Average +1 Std', 'Moving Average -1 Std'])


#rolling standard  deviation is not a constant
print std

2018-02-13 00:00:00+00:00             2.561080
...                                        ...
2018-12-19 00:00:00+00:00             2.896930
2018-12-20 00:00:00+00:00             3.068813
2018-12-21 00:00:00+00:00             3.353129
2018-12-24 00:00:00+00:00             3.949504
2018-12-26 00:00:00+00:00             4.022458
2018-12-27 00:00:00+00:00             4.104846
2018-12-28 00:00:00+00:00             4.207010
2018-12-31 00:00:00+00:00             4.260755
2019-01-02 00:00:00+00:00             4.307897
2019-01-03 00:00:00+00:00             4.482073
2019-01-04 00:00:00+00:00             4.510192
2019-01-07 00:00:00+00:00             4.502924
2019-01-08 00:00:00+00:00             4.506345
2019-01-09 00:00:00+00:00             4.497546
2019-01-10 00:00:00+00:00             4.487874
2019-01-11 00:00:00+00:00             4.470221
2019-01-14 00:00:00+00:00             4.303732
2019-01-15 00:00:00+00:00             4.151837
2019-01-16 00:00:00+00:00             3.947878
2019-01-17 00:00:00+00:00             3.652218
2019-01-18 00:00:00+00:00             3.616924
2019-01-22 00:00:00+00:00             3.484868
2019-01-23 00:00:00+00:00             3.531261
2019-01-24 00:00:00+00:00             3.485831
2019-01-25 00:00:00+00:00             3.421721

No comments:

Post a Comment