Skip to content

Instantly share code, notes, and snippets.

View kanzitelli's full-sized avatar
💭
I may be slow to respond.

Batyr kanzitelli

💭
I may be slow to respond.
View GitHub Profile
from pandas_datareader.nasdaq_trader import get_nasdaq_symbols
from pandas_datareader import data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sbn
import datetime
import math
symbols = get_nasdaq_symbols()
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)'
}
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()
returns = close_prices_2016.pct_change(1)
returns.head()
log_returns = np.log(close_prices_2016).diff()
log_returns.head()
desc = close_prices_2016.describe()
desc.loc['min-max diff'] = desc.loc['max'] - desc.loc['min']
desc
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 (%)')
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()
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()
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 )