A very common process with time series is to create data based on a rolling mean over a given time period.
Often daily financial data can be a bit noisy.
We can use the rolling mean (often called Moving Average) to get more signal about the general trend of the data.
You will provide a window of a set time period and then use that to calculate your aggregate statistic (such as the mean).
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Best way to read in data with time series index!
df = pd.read_csv('time_data/walmart_stock.csv',index_col='Date',parse_dates=True)
df.head()
Plot the Open
df['Open'].plot(figsize=(16,6))
Use rolling().mean()
to add rolling mean. This rolling method provides row entries, where every entry is then representative of the window.
7 day rolling mean
Note the first 6 rows return NaN
because we don't have enough data to calculate. This is not an inplace operation because if we call df, the original data is still there
df.rolling(7).mean().head(20)
df['Open'].plot()
df.rolling(window=30).mean()['Close'].plot()
Easiest way to add a legend is to make this rolling value a new column, then pandas does it automatically!
df['Close: 30 Day Mean'] = df['Close'].rolling(window=30).mean()
df[['Close','Close: 30 Day Mean']].plot(figsize=(16,6))
Now what if you want to take into account everything from the start of the time series as a rolling value? For instance, not just take into account a period of 7 days, or monthly rolling average, but instead, take into everything since the beginning of the time series, continuously:
Use .expanding(min_period=1)
to calculate the running aggregate of a Series. The argument min_periods
specify a minimum number of periods
# Optional specify a minimum number of periods
df['Close'].expanding(min_periods=1).mean().plot(figsize=(16,6))
We will talk a lot more about financial analysis plots and technical indicators, but here is one worth mentioning!
More info : http://www.investopedia.com/terms/b/bollingerbands.asp
Developed by John Bollinger, Bollinger Bands® are volatility bands placed above and below a moving average. Volatility is based on the standard deviation, which changes as volatility increases and decreases. The bands automatically widen when volatility increases and narrow when volatility decreases. This dynamic nature of Bollinger Bands also means they can be used on different securities with the standard settings. For signals, Bollinger Bands can be used to identify Tops and Bottoms or to determine the strength of the trend.
Bollinger Bands reflect direction with the 20-period SMA and volatility with the upper/lower bands. As such, they can be used to determine if prices are relatively high or low. According to Bollinger, the bands should contain 88-89% of price action, which makes a move outside the bands significant. Technically, prices are relatively high when above the upper band and relatively low when below the lower band. However, relatively high should not be regarded as bearish or as a sell signal. Likewise, relatively low should not be considered bullish or as a buy signal. Prices are high or low for a reason. As with other indicators, Bollinger Bands are not meant to be used as a stand alone tool.
Get 20-Day Moving Average on Close
Use .rolling(20).mean()
to calculate the 20-day average on the Close
df['Close: 30 Day Mean'] = df['Close'].rolling(window=20).mean()
df['Upper'] = df['Close: 30 Day Mean'] + 2*df['Close'].rolling(window=20).std()
df['Lower'] = df['Close: 30 Day Mean'] - 2*df['Close'].rolling(window=20).std()
df[['Close','Close: 30 Day Mean','Upper','Lower']].plot(figsize=(16,6))
To get a closer look, you can use the tail()
method
Plot Last Year
df[['Close', 'Close: 30 Day Mean', 'Upper', 'Lower']].tail(250).plot(figsize=(16,6))
For expanding operations, it doesn't help very much to visualize this against the daily data, but instead its a good way to get an idea of the stability of a stock. This idea of stability and volatility is something we are going to be exploring heavily in the next project, so let's jump straight into it!