Exercises: Plotting

By Christopher van Hoecke, Max Margenot, and Delaney Mackenzie

Lecture Link:

https://www.quantopian.com/lectures/plotting-data

IMPORTANT NOTE:

This lecture corresponds to the Plotting Data lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.

Part of the Quantopian Lecture Series:


In [1]:
# Useful Functions
import numpy as np
import matplotlib.pyplot as plt

Exercise 1: Histograms

a. Returns

Find the daily returns for SPY over a 7-year window.

In [4]:
data = get_pricing(['SPY'], fields='price', start_date='2010-01-01', end_date='2017-01-01')
R = data.pct_change()[1:]

b. Graphing

Using the techniques laid out in lecture, plot a histogram of the returns

In [7]:
plt.hist(R, bins=30)
plt.xlabel('Return')
plt.ylabel('Number of Days Observed')
plt.title('Frequency Distribution of SPY Returns, 2010-2017');

c. Cumulative distribution

Plot the cumulative distribution histogram for your returns

In [13]:
plt.hist(R, bins=30, cumulative=True)
plt.xlabel('Return')
plt.ylabel('Number of Days Observed')
plt.title('Cumulative Distribution of SPY Returns, 2010-2017');

Exercise 2 : Scatter Plots

a. Data

Start by collecting the close prices of McDonalds Corp. (MCD) and Starbucks (SBUX) for the last 5 years with daily frequency.

In [41]:
data = get_pricing(['SPY', 'SBUX'], fields='close_price', start_date='2013-01-01', end_date='2018-01-01', frequency='daily')
data.columns = [e.symbol for e in data.columns]
data.tail()
Out[41]:
SPY SBUX
2017-12-22 00:00:00+00:00 267.49 57.295
2017-12-26 00:00:00+00:00 267.16 57.140
2017-12-27 00:00:00+00:00 267.34 57.280
2017-12-28 00:00:00+00:00 267.86 57.820
2017-12-29 00:00:00+00:00 266.89 57.440

b. Plotting

Graph a scatter plot of SPY and Starbucks.

In [42]:
plt.scatter(data['SPY'], data['SBUX'])
plt.xlabel('SPY')
plt.ylabel('SBUX')
plt.title('Daily Prices 2013-2018');

c. Plotting Returns

Graph a scatter plot of the returns of SPY and Starbucks.

In [44]:
plt.scatter(data['SPY'].pct_change()[1:], data['SBUX'].pct_change()[1:])
plt.xlabel('SPY')
plt.ylabel('SBUX')
plt.title('Daily Returns 2013-2018');

Remember a scatter plot must have the same number of values for each parameter. If SPY and SBUX did not have the same number of data points, your graph will return an error


Exercise 3 : Linear Plots

a. Getting Data

Use the techniques laid out in lecture to find the open price over a 2-year period for Starbucks (SBUX) and Dunkin Brands Group (DNKN). Print them out in a table.

In [57]:
data = get_pricing(['SBUX', 'DNKN'], fields='open_price', start_date='2015-01-01', end_date='2017-01-01', frequency='daily')
data.tail()
Out[57]:
Equity(6683 [SBUX]) Equity(41759 [DNKN])
2016-12-23 00:00:00+00:00 57.29 53.85
2016-12-27 00:00:00+00:00 56.99 53.49
2016-12-28 00:00:00+00:00 56.80 53.51
2016-12-29 00:00:00+00:00 56.35 53.27
2016-12-30 00:00:00+00:00 56.28 53.24

b. Data Structure

The data is returned to us as a pandas dataframe object. Index your data to convert them into simple strings.

In [58]:
data.columns = [e.symbol for e in data.columns]
data.tail()
Out[58]:
SBUX DNKN
2016-12-23 00:00:00+00:00 57.29 53.85
2016-12-27 00:00:00+00:00 56.99 53.49
2016-12-28 00:00:00+00:00 56.80 53.51
2016-12-29 00:00:00+00:00 56.35 53.27
2016-12-30 00:00:00+00:00 56.28 53.24

c. Plotting

Plot the data for SBUX stock price as a function of time. Remember to label your axis and title the graph.

In [59]:
plt.plot(data['SBUX'])
plt.ylabel('Price')
plt.xlabel('Time')
plt.legend(['SBUX']);

Exercise 4 : Best fits plots

Here we have a scatter plot of two data sets. Vary the a and b parameter in the code to try to draw a line that 'fits' our data nicely. The line should seem as if it is describing a pattern in the data. While quantitative methods exist to do this automatically, we would like you to try to get an intuition for what this feels like.

In [60]:
data1 = get_pricing('SBUX', fields='open_price', start_date='2013-01-01', end_date='2014-01-01')
data2 = get_pricing('SPY', fields='open_price', start_date = '2013-01-01', end_date='2014-01-01')

rdata1= data1.pct_change()[1:]
rdata2= data2.pct_change()[1:]
plt.scatter(rdata2, rdata1);
In [75]:
plt.scatter(rdata2, rdata1)

a = 0.000001
b = 1.14

x = np.arange(-0.02, 0.03, 0.01)
y = a + (b*x)
plt.plot(x,y, color='r');

Congratulations on completing the Plotting exercises!

As you learn more about writing trading algorithms and the Quantopian platform, be sure to check out the daily Quantopian Contest, in which you can compete for a cash prize every day.

Start by going through the Writing a Contest Algorithm tutorial.

This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. ("Quantopian"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.