In [1]:
from __future__ import print_function
import os
import pandas as pd
%matplotlib inline
from matplotlib import pyplot as plt

In [14]:
#Load the dataset into a pandas.DataFrame
ibm_df = pd.read_csv('datasets/ibm-common-stock-closing-prices.csv')
ibm_df.index = ibm_df['Date']

In [15]:
#Let's find out the shape of the DataFrame
print('Shape of the dataframe:', ibm_df.shape)


Shape of the dataframe: (1009, 2)

In [16]:
#Let's see the top 10 rows
ibm_df.head(10)


Out[16]:
Date IBM common stock closing prices
Date
1962-01-02 1962-01-02 572.00
1962-01-03 1962-01-03 577.00
1962-01-04 1962-01-04 571.25
1962-01-05 1962-01-05 560.00
1962-01-08 1962-01-08 549.50
1962-01-09 1962-01-09 556.00
1962-01-10 1962-01-10 557.00
1962-01-11 1962-01-11 563.00
1962-01-12 1962-01-12 564.00
1962-01-15 1962-01-15 566.50

In [17]:
#Rename the second column
ibm_df.rename(columns={'IBM common stock closing prices': 'Close_Price'},
              inplace=True)
ibm_df.head()


Out[17]:
Date Close_Price
Date
1962-01-02 1962-01-02 572.00
1962-01-03 1962-01-03 577.00
1962-01-04 1962-01-04 571.25
1962-01-05 1962-01-05 560.00
1962-01-08 1962-01-08 549.50

In [18]:
#remove missing values
missing = (pd.isnull(ibm_df['Date'])) & (pd.isnull(ibm_df['Close_Price']))
print('No. of rows with missing values:', missing.sum())
ibm_df = ibm_df.loc[~missing, :]


No. of rows with missing values: 0

In [19]:
#To illustrate the idea of moving average we compute a weekly moving average taking
#a window of 5 days instead of 7 days because trading happens only during the weekdays.
ibm_df['5-Day Moving Avg'] = ibm_df['Close_Price'].rolling(5).mean()

In [22]:
fig = plt.figure(figsize=(5.5, 5.5))
ax = fig.add_subplot(2,1,1)
ibm_df['Close_Price'].plot(ax=ax, color='b')
ax.set_title('IBM Common Stock Close Prices during 1962-1965')
ax = fig.add_subplot(2,1,2)
ibm_df['5-Day Moving Avg'].plot(ax=ax, color='r')
ax.set_title('5-day Moving Average')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=2.0)
plt.savefig('plots/ch2/B07887_02_14.png', format='png', dpi=300)



In [10]:
#Calculate the moving averages using 'rolling' and 'mean' functions
MA2 = ibm_df['Close_Price'].rolling(window=2).mean()
TwoXMA2 = MA2.rolling(window=2).mean()

MA4 = ibm_df['Close_Price'].rolling(window=4).mean()
TwoXMA4 = MA4.rolling(window=2).mean()

MA3 = ibm_df['Close_Price'].rolling(window=3).mean()
ThreeXMA3 = MA3.rolling(window=3).mean()

In [11]:
#Let's remove NaN from the above variables
MA2 = MA2.ix[~pd.isnull(MA2)]
TwoXMA2 = TwoXMA2.ix[~pd.isnull(TwoXMA2)]

MA4 = MA4.ix[~pd.isnull(MA4)]
TwoXMA4 = TwoXMA4.ix[~pd.isnull(TwoXMA4)]

MA3 = MA3.ix[~pd.isnull(MA3)]
ThreeXMA3 = TwoXMA4.ix[~pd.isnull(ThreeXMA3)]

In [12]:
f, axarr = plt.subplots(3, sharex=True)
f.set_size_inches(5.5, 5.5)

ibm_df['Close_Price'].iloc[:45].plot(color='b', linestyle = '-', ax=axarr[0])
MA2.iloc[:45].plot(color='r', linestyle = '-', ax=axarr[0])
TwoXMA2.iloc[:45].plot(color='r', linestyle = '--', ax=axarr[0])
axarr[0].set_title('2 day MA & 2X2 day MA')

ibm_df['Close_Price'].iloc[:45].plot(color='b', linestyle = '-', ax=axarr[1])
MA4.iloc[:45].plot(color='g', linestyle = '-', ax=axarr[1])
TwoXMA4.iloc[:45].plot(color='g', linestyle = '--', ax=axarr[1])
axarr[1].set_title('4 day MA & 2X4 day MA')

ibm_df['Close_Price'].iloc[:45].plot(color='b', linestyle = '-', ax=axarr[2])
MA3.iloc[:45].plot(color='k', linestyle = '-', ax=axarr[2])
ThreeXMA3.iloc[:45].plot(color='k', linestyle = '--', ax=axarr[2])
axarr[2].set_title('3 day MA & 3X 3day MA')
plt.savefig('plots/ch2/B07887_02_15.png', format='png', dpi=300)