#!/usr/bin/env python # coding: utf-8 # #Pandas # ##Apple Stock Exercise # 1. By default the data returned by Yahoo has data type string. Please, transform the data to reasonable data types. # 2. Use 'Date' column as index # 3. The initial public offering (IPO) of the Apple stock has been on December 12, 1980. # 1. 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. # 2. 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. # 4. What was/When was the biggest relative loss/gain of the Apple stock within a trading day (Intraday) # 5. What was/When was the biggest relative loss/gain of the Apple stock between two trading days (Overnight) **(Additional)**
**Hint:** Please, exclude stock splits that happenend on (June 16, 1987; June 21, 2000; February 28, 2005 and June 09, 2014). You can do that by masking those days in your DataFrame when doing the min/max calculation. # 6. # 1. Plot the closing values over the time # 2. Label important dates of Apple's initial product launches using matplotlibs annotate function **(Additional)** # * Initial launch of the iMac (July, 5 1998) # * Initial launch of the iPod (February, 28 2005) # * Initial launch of the iPhone (September, 1 2007) # * Initial launch of the iPad (January, 27 2010) # In[1]: get_ipython().run_line_magic('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. # In[2]: quotes = get_data_from_yahoo("AAPL") stock_data = pd.DataFrame(quotes['AAPL']) # ### 1. # By default the data returned by Yahoo as data type string. Transform data to use reasonable data types. # In[3]: stock_data = stock_data.convert_objects(convert_numeric=True) stock_data['Date'] = pd.to_datetime(stock_data.Date) stock_data.dtypes # ### 2. # Use 'Date' column as index. # In[4]: stock_data.set_index('Date', inplace=True) # ###3A # 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. # In[5]: 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 # ###3B # 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. # In[6]: 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) # ###4 # What was/When was the biggest relative loss/gain of the Apple stock within a trading day (Intraday) # In[7]: 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']) # ###5 # What was/When was the biggest relative loss/gain of the Apple stock between two trading days (Overnight) # In[8]: 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]) # ###6 # In[9]: 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='-|>')) # In[ ]: