In [ ]:
import pandas as pd
from pandas_datareader import data, wb
import datetime
# We will look at stock prices over the past year, starting at January 1, 2016
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
# Let's get Apple stock data; Apple's ticker symbol is AAPL
# First argument is the series we want, second is the source ("yahoo" for Yahoo! Finance), third is the start date, fourth is the end date
apple = data.get_data_yahoo("AAPL", start, end)
apple.head()
N values: df.head(N)df['column_name'] or df.column_name.df[['col1', 'col2']]df.query('Date>2017')df[df.Close>140]df.describe()df.groupby(pd.TimeGrouper(freq='60D')).mean()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
import pylab as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (15, 9) # Change the size of plots
## your plot code here
In [ ]:
microsoft = data.get_data_yahoo("MSFT", start, end)
google = data.get_data_yahoo("GOOG", start, end)
stocks = pd.DataFrame({"AAPL": apple["Adj Close"],
"MSFT": microsoft["Adj Close"],
"GOOG": google["Adj Close"]})
stocks.head()
In [ ]:
# plot here
Read http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html and apply a 20-day rolling averaging window to the apple adjusted close.
In [ ]:
# stocks['AAPL']...
We'd like to normalize the share price so that we can fit all of the stock trends on one plot. To do that let's normalize by the price on the start date for each stock. Check the http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html documentation to apply this scaling to each column.
Plot resulting profit time series.
In [ ]:
# use apply function
stock_return = stocks # change this line
stock_return.head()
In [ ]:
# plot returns
So far we manually specified a few ticker symbols. In order to do larger scale analysis, we need to pull data for more tickers. To do that, we need to find a data source with the ticker names. Fortunately, Wikipedia has all of the S&P tickers in a html table at https://en.wikipedia.org/wiki/List_of_S%26P_500_companies. Use pd.read_html to grab this ticker data and then loop through all ticker and pull down the stock data for each ticker.
In [ ]:
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
## use sp500 = pd.read_html...
In [ ]:
## Loop through tickers
In [ ]:
## Use apply again
# stock_return_sp500 = df_sp500 # change this line
In [ ]:
# plot hist
In [ ]:
# get high-performance stocks
In [ ]: