By Christopher van Hoecke, Max Margenot, and Delaney Mackenzie
https://www.quantopian.com/lectures/plotting-data
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:
# Useful Functions
import numpy as np
import matplotlib.pyplot as plt
data = get_pricing('SPY', fields='price', start_date='2010-01-01', end_date='2017-01-01')
returns = data.pct_change()[1:]
Using the techniques laid out in lecture, plot a histogram of the returns
plt.hist(returns, bins = 30);
plt.xlabel('Random Numbers');
plt.ylabel('Number of Times Observed');
plt.title('Frequency Distribution of randomly generated number');
Plot the cumulative distribution histogram for your returns
plt.hist(returns, bins = 30, cumulative='true');
SPY = get_pricing('SPY', fields='close_price', start_date='2013-06-19', end_date='2018-06-19', frequency='daily')
SBUX = get_pricing('SBUX', fields='close_price', start_date='2013-06-19', end_date='2018-06-19', frequency='daily')
Graph a scatter plot of SPY and Starbucks.
plt.scatter(SPY, SBUX);
plt.title('Scatter plot of spy and sbux');
plt.xlabel('SPY Price');
plt.ylabel('SBUX Price');
Graph a scatter plot of the returns of SPY and Starbucks.
SPY_R = SPY.pct_change()[1:]
SBUX_R = SBUX.pct_change()[1:]
plt.scatter(SPY_R, SBUX_R);
plt.title('Scatter plot of spy and starbucks returns');
plt.xlabel('SPY Return');
plt.ylabel('SBUX Return');
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
data = get_pricing(['SBUX', 'DNKN'], fields='open_price', start_date = '2015-01-01', end_date='2017-01-01') ## Your code goes here.
data.head()
The data is returned to us as a pandas dataframe object. Index your data to convert them into simple strings.
data.columns = [e.symbol for e in data.columns]
data['SBUX'].head()
Plot the data for SBUX stock price as a function of time. Remember to label your axis and title the graph.
plt.plot(data['SBUX']);
plt.xlabel('Time');
plt.ylabel('Price');
plt.title('Price vs Time');
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.
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);
plt.scatter(rdata2, rdata1)
# Answer
a = 0.0004
b = 1.14
#Answer
x = np.arange(-0.02, 0.03, 0.01)
y = a + (b*x)
plt.plot(x,y, color='r');
Congratulations on completing the answer key to 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.