In [ ]:
import matplotlib.pyplot as plt # Import matplotlib
# This line is necessary for the plot to appear in a Jupyter notebook
%matplotlib inline
# Control the default size of figures in this Jupyter notebook
%pylab inline
pylab.rcParams['figure.figsize'] = (15, 9) # Change the size of plots
In [ ]:
# https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1/
import pandas as pd
In [ ]:
# load stock info from local
# more info see ./save-stock-info.py
apple = pd.read_csv("./data/AAPL.csv", index_col=0)
ms = pd.read_csv("./data/MSFT.csv", index_col=0)
In [ ]:
type(apple) # pandas.core.frame.DataFrame
In [ ]:
apple.head()
In [ ]:
apple["2018-01-01":"2020-01-01"]["Adj Close"].plot()
In [ ]:
apple["Adj Close"].plot(grid = True) # Plot the adjusted closing price of AAPL
# ms_price = ms[["Adj. Close"]]
# ms_price.rename(columns={"Adj. Close":"MSFT"}, inplace=True)
In [ ]:
# apple_price = apple[["Adj. Close"]]
# apple_price.rename(columns={"Adj. Close":"AAPL"}, inplace=True)
# Below I create a DataFrame consisting of the adjusted closing price of these stocks, first by making a list of these objects and using the join method
stocks = pd.DataFrame({"AAPL": apple["Adj Close"],
"MSFT": ms["Adj Close"]})
In [ ]:
# both_stocks = ms_price.join(apple_price, how="inner")
# both_stocks.plot()
stocks.head()
In [ ]:
stocks.plot(grid = True)
In [ ]:
stocks.plot(secondary_y = ["AAPL"], grid = True)
In [79]:
# df.apply(arg) will apply the function arg to each column in df, and return a DataFrame with the result
# Recall that lambda x is an anonymous function accepting parameter x; in this case, x will be a pandas Series object
stock_return = stocks.apply(lambda x: x / x[0])
In [80]:
stock_return.head()
Out[80]:
In [81]:
stock_return.plot(grid = True).axhline(y = 1, color = "black", lw = 2)
Out[81]:
In [ ]: