# QSTK Imports
import QSTK.qstkutil.qsdateutil as du
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkutil.DataAccess as da
# Third Party Imports
import datetime as dt
import pandas as pd
# Set golden ratio for image size
figsize(10*1.62, 10)
# List of symbols
ls_symbols = ["AAPL", "GLD", "GOOG", "$SPX", "XOM"]
# Start and End date of the charts
dt_start = dt.datetime(2006, 1, 1)
dt_end = dt.datetime(2010, 12, 31)
# We need closing prices so the timestamp should be hours=16.
dt_timeofday = dt.timedelta(hours=16)
# Get a list of trading days between the start and the end.
ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday)
# Creating an object of the dataaccess class with Yahoo as the source.
c_dataobj = da.DataAccess('Yahoo')
# Keys to be read from the data, it is good to read everything in one go.
ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
# Reading the data, now d_data is a dictionary with the keys above.
# Timestamps and symbols are the ones that were specified before.
ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))
# Getting the close prices in a dataframe.
closing_prices = d_data['close']
closing_prices.head()
AAPL | GLD | GOOG | $SPX | XOM | |
---|---|---|---|---|---|
2006-01-03 16:00:00 | 74.43 | 53.12 | 435.23 | 1268.80 | 50.47 |
2006-01-04 16:00:00 | 74.65 | 53.30 | 445.24 | 1273.46 | 50.56 |
2006-01-05 16:00:00 | 74.06 | 52.34 | 451.24 | 1273.48 | 50.31 |
2006-01-06 16:00:00 | 75.97 | 53.72 | 465.66 | 1285.45 | 51.30 |
2006-01-09 16:00:00 | 75.72 | 54.60 | 466.90 | 1290.15 | 51.28 |
closing_prices.plot() # closing_prices.plot(y:'Adjusted Close') doesn't work in 0.7.3
ylabel('Adjusted close');
normalized_prices = closing_prices / closing_prices.ix[0]
normalized_prices.plot()
ylabel('Normalized Close');
returns = normalized_prices.shift(-1) / normalized_prices - 1
# Plotting the plot of daily returns
returns[['$SPX','XOM']][0:50].plot()
axhline(y=0, color='r')
ylabel('Daily Returns');
scatter(returns['$SPX'], returns['XOM'], c='blue')
ylabel('XOM')
xlabel('$SPX');
# Plotting the scatter plot of daily returns between $SPX VS GLD
scatter(returns['$SPX'], returns['GLD'], c='blue') # $SPX v GLD
ylabel('GLD')
xlabel('$SPX');