%matplotlib inline
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
import contextlib
import csv
import urllib2
def get_data_from_yahoo(index):
"""Helper function to download quotes from Yahoo:
returns a dictionary {'index_name': [{'{'Volume': ...,
'Adj Close': ...,
'High': ...,
'Low': ...,
'Date': ...,
'Close': ...,
'Open': ...}, ...]}
"""
url = 'http://ichart.yahoo.com/table.csv?s=%s&a=0&b=1&c=1980&d=11&e=31&f=2015&g=d&ignore=.csv' % (index)
with contextlib.closing(urllib2.urlopen(url)) as f:
fieldnames = f.readline().strip().split(',')
my_csv_reader = csv.DictReader(f, fieldnames=fieldnames, delimiter=',')
quotes = {index:[row for row in my_csv_reader]}
return quotes
Get data from Yahoo using the little helper function above and transform it into a data frame.
quotes = get_data_from_yahoo("AAPL")
stock_data = pd.DataFrame(quotes['AAPL'])
By default the data returned by Yahoo as data type string. Transform data to use reasonable data types.
stock_data = stock_data.convert_objects(convert_numeric=True)
stock_data['Date'] = pd.to_datetime(stock_data.Date)
stock_data.dtypes
Adj Close float64 Close float64 Date datetime64[ns] High float64 Low float64 Open float64 Volume int64 dtype: object
Use 'Date' column as index.
stock_data.set_index('Date', inplace=True)
Estimate how much money would you have, if you had invested 10000 US$ in Apple stocks at this time? Please, take into account devidends as well as stock splits happened over the time.
Hint: Make yourself familiar with the term adjusted closing price.
total_shares = int(10000./stock_data.iloc[-1]['Adj Close'])
value_yesterday = total_shares * stock_data.iloc[0]['Adj Close']
print "I would have %.2f$ today!" % value_yesterday
I would have 2576834.35$ today!
When did Apple stocks reach its highest price ever? Estimate how much money would you have, if you had sold the stocks at the highest price ever? Please, take into account devidends as well as stock splits happened over the time.
Hint: Make yourself familiar with the term adjusted closing price.
date_max = stock_data['Adj Close'].idxmax()
value_max = total_shares * stock_data.ix[date_max]['Adj Close']
max_date = date_max.strftime('%a %b %d, %Y')
print "I would have sold my stocks on %s and I would have %.2f$!" % (max_date, value_max)
I would have sold my stocks on Fri May 22, 2015 and I would have 3010933.81$!
What was/When was the biggest relative loss/gain of the Apple stock within a trading day (Intraday)
stock_data['rel_intraday_loss'] = (stock_data['Close']-stock_data['Open'])/stock_data['Open']
date_min = stock_data['rel_intraday_loss'].idxmin()
date_max = stock_data['rel_intraday_loss'].idxmax()
#https://en.wikipedia.org/wiki/Black_Monday_(1987)
print "The biggest relative loss has been on %s and it has been %.2f%%" % (date_min.strftime('%a %b %d, %Y'), 100.*stock_data.ix[date_min]['rel_intraday_loss'])
print "The biggest relative gain has been on %s and it has been %.2f%%" % (date_max.strftime('%a %b %d, %Y'), 100.*stock_data.ix[date_max]['rel_intraday_loss'])
The biggest relative loss has been on Mon Oct 19, 1987 and it has been -24.35% The biggest relative gain has been on Fri Jan 02, 1998 and it has been 19.27%
What was/When was the biggest relative loss/gain of the Apple stock between two trading days (Overnight)
stock_splits = (dt.datetime(2014, 6, 9),
dt.datetime(2005, 2, 28),
dt.datetime(2000, 6, 21),
dt.datetime(1987, 6, 16))
yesterdays_close = stock_data['Close'].shift(-1)
stock_data['rel_overnight_loss'] = (stock_data['Open']-yesterdays_close)/yesterdays_close
masked_stock_splits = np.logical_not(stock_data.index.isin(stock_splits))
date_min = stock_data['rel_overnight_loss'][masked_stock_splits].idxmin()
date_max = stock_data['rel_overnight_loss'][masked_stock_splits].idxmax()
print "The biggest relative loss has been on %s and it has been %.2f%%" % (date_min.strftime('%a %b %d, %Y'), 100.*stock_data.ix[date_min]['rel_overnight_loss'])
print "The biggest relative gain has been on %s and it has been %.2f%%" % (date_max.strftime('%a %b %d, %Y'), 100.*stock_data.ix[date_max]['rel_overnight_loss'])
#alternatively
print 100*"="
tmp = stock_data.select(lambda x: x not in stock_splits)['rel_overnight_loss']
date_min = tmp.idxmin()
date_max = tmp.idxmax()
print "The biggest relative loss has been on %s and it has been %.2f%%" % (date_min.strftime('%a %b %d, %Y'), 100.*tmp.ix[date_min])
print "The biggest relative gain has been on %s and it has been %.2f%%" % (date_max.strftime('%a %b %d, %Y'), 100.*tmp.ix[date_max])
The biggest relative loss has been on Fri Sep 29, 2000 and it has been -47.31% The biggest relative gain has been on Wed Aug 06, 1997 and it has been 27.85% ==================================================================================================== The biggest relative loss has been on Fri Sep 29, 2000 and it has been -47.31% The biggest relative gain has been on Wed Aug 06, 1997 and it has been 27.85%
important_dates = {'iMac': {'date': dt.datetime(1998, 5, 7),
'close': stock_data.ix[dt.datetime(1998, 5, 7)]['Close']
},
'iPod': {'date': dt.datetime(2001, 10, 28),
'close': stock_data.ix[dt.datetime(2001, 10, 23)]['Close']
},
'iPhone': {'date': dt.datetime(2007,1,9),
'close': stock_data.ix[dt.datetime(2007, 1, 9)]['Close']
},
'iPad': {'date': dt.datetime(2010, 1, 27),
'close': stock_data.ix[dt.datetime(2010, 1, 27)]['Close']
}
}
ax = stock_data['Close'].plot(legend=False)
ax.set_ylabel('US$')
for key in important_dates.iterkeys():
ax.annotate(key,
(mdates.date2num(important_dates[key]['date']), important_dates[key]['close']),
xytext=(-len(key)*2.5, 100),
textcoords='offset points',
arrowprops=dict(arrowstyle='-|>'))