This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. Apple - AAPL | |
| AAPL_p_01_01_2016 = 4000 | |
| number_of_shares_start = math.floor(AAPL_p_01_01_2016 / close_prices_2016.AAPL[0]) | |
| AAPL_p_12_31_2016 = number_of_shares_start * close_prices_2016.AAPL[-1] | |
| relative_return = (AAPL_p_12_31_2016 - AAPL_p_01_01_2016) / AAPL_p_01_01_2016 * 100 | |
| print "1. Relative return for Apple = %.2f%%" % relative_return | |
| # 2. Amazon - AMZN | |
| AMZN_p_01_01_2016 = 3000 | |
| number_of_shares_start = math.floor(AMZN_p_01_01_2016 / close_prices_2016.AMZN[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AAPL_p_01_01_2016 = 5000 | |
| number_of_shares_start = math.floor(AAPL_p_01_01_2016 / close_prices_2016.AAPL[0]) | |
| AAPL_p_12_31_2016 = number_of_shares_start * close_prices_2016.AAPL[-1] | |
| print "1. Relative return for Apple = %.2f%%" % ( (AAPL_p_12_31_2016 - AAPL_p_01_01_2016) / AAPL_p_01_01_2016 * 100 ) | |
| AMZN_p_01_01_2016 = 5000 | |
| number_of_shares_start = math.floor(AMZN_p_01_01_2016 / close_prices_2016.AMZN[0]) | |
| AMZN_p_12_31_2016 = number_of_shares_start * close_prices_2016.AMZN[-1] | |
| print "2. Relative return for Amazon = %.2f%%" % ( (AMZN_p_12_31_2016 - AMZN_p_01_01_2016) / AMZN_p_01_01_2016 * 100 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| box = sbn.boxplot(x='AMZN', data=AAPL_AMZN_log_returns) | |
| box.set_title('Box plot for Amazon') | |
| box.set_ylabel('Total relative returns (%)') | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| box = sbn.boxplot(x='AAPL', data=AAPL_AMZN_log_returns) | |
| box.set_title('Box plot for Apple') | |
| box.set_ylabel('Total relative returns (%)') | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| aapl_amzn_dict = { 'AAPL': log_returns.AAPL, 'AMZN': log_returns.AMZN } | |
| AAPL_AMZN_log_returns = pd.DataFrame.from_dict(aapl_amzn_dict) | |
| fig = plt.figure(figsize=[16,9]) | |
| ax = fig.add_subplot(2,1,2) | |
| for c in AAPL_AMZN_log_returns: | |
| ax.plot(AAPL_AMZN_log_returns.index, 100*(np.exp(AAPL_AMZN_log_returns[c].cumsum()) - 1), label=str(c)) | |
| ax.set_ylabel('Total relative returns (%)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| desc = close_prices_2016.describe() | |
| desc.loc['min-max diff'] = desc.loc['max'] - desc.loc['min'] | |
| desc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| log_returns = np.log(close_prices_2016).diff() | |
| log_returns.head() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| returns = close_prices_2016.pct_change(1) | |
| returns.head() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| tickers = ['AAPL', 'AMZN', 'GOOGL', 'IBM'] | |
| data_source = 'yahoo' | |
| date_2016 = (datetime.datetime(2016, 1, 1), datetime.datetime(2016, 12, 31)) | |
| all_weekdays_2016 = pd.date_range(start = date_2016[0], end = date_2016[1], freq='B') # here we get all business days from 1.JAN.2016 to 31.DEC.2016 | |
| data_2016 = data.DataReader(tickers, data_source, date_2016[0], date_2016[1]) | |
| close_prices_2016 = data_2016.ix['Adj Close'].reindex(all_weekdays_2016).dropna() # here we take Adjusted Close Price, take business days and drop NA values | |
| close_prices_2016.head() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| list_of_traded_corps = symbols.loc[:, ['NASDAQ Symbol', 'Security Name', 'Listing Exchange']] | |
| listing_exchange = { | |
| 'A': 'NYSE MKT', | |
| 'N': 'New York Stock Exchange (NYSE)', | |
| 'P': 'NYSE ARCA', | |
| 'Q': 'Q - NASDAQ?', # not sure about this one | |
| 'V': 'Investors\' Exchange, LLC (IEXG)', | |
| 'Z': 'BATS Global Markets (BATS)' | |
| } |