import pandas as pd import pandas.io.data as web import datetime import seaborn as sns %pylab inline # Helper function to get data from yahoo finance API # as google does not offer adjusted close prices oracle_data = web.get_data_yahoo('ORC', '2013-02-15','2014-02-15') sap_data = web.get_data_yahoo('SAP', '2013-02-15','2014-02-15') print pd.concat((sap_data.head(1),sap_data.tail(1))) print pd.concat((oracle_data.head(1),oracle_data.tail(1))) # Only need time-index and daily-close. oracle = oracle_data['Adj Close'] sap = sap_data['Adj Close'] # Create a dataframe from the two series for ploting df = pd.concat([oracle,sap],axis=1) df.columns =['Oracle','SAP'] plt.rcParams['figure.figsize'] = 11, 10 df.plot(subplots = True); plt.legend(loc='upper right') plt.title("One Year Daily Adjusted Close Price for Oracle and SAP") # Compute a day-to-day percentage change array (pd.series) with default settings oracle_pc = oracle.pct_change(periods=1, fill_method='pad', limit=None, freq=None) sap_pc = sap.pct_change(periods=1, fill_method='pad', limit=None, freq=None) plt.rcParams['figure.figsize'] = 10, 5 pd.rolling_corr(oracle_pc, sap_pc, window = 50).plot(title = "One Year 50-day Window Correlation of Oracle with SAP") ols_model = pd.ols(y = oracle_pc, x = {'sap_pc' : sap_pc}, window = 50) # Window attribute makes it dynamic ols_model.beta.head(2) ols_model.beta['sap_pc'].plot(title = "One Year OLS beta of Oracle with SAP") # Reusing our SAP adjusted close data to create a dataframe with a 20 and 50 day simple moving average SAPdf = pd.DataFrame(sap_data) SAPdf['SMA20'] = pd.stats.moments.rolling_mean(SAPdf['Adj Close'], 20) SAPdf['SMA50'] = pd.stats.moments.rolling_mean(SAPdf['Adj Close'], 50) SAPdf.head(2) plt.rcParams['figure.figsize'] = 11, 7 main = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4) main.plot(SAPdf.index, SAPdf['Adj Close'], label='Adj Close') main.plot(SAPdf.index, SAPdf['SMA20'], label='SMA20') main.plot(SAPdf.index, SAPdf['SMA50'], label='SMA50') main.axes.xaxis.set_ticklabels([]) plt.title('One Year SAP Daily Adj Close w/ 20 & 50 day SMA') plt.legend() vol = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4) vol.bar(SAPdf.index, SAPdf['Volume']) plt.title('SAP Daily Volume') # savefig('img.png', bbox_inches='tight', transparent=True)