Notebook

Quantopian Research

The Quantopian research platform is a hosted research environment. Our aim is to make it simple to get started on your research without a lot of setup. Libraries are pre-installed and data is made readily accessible.

How do I use this?

The Quantopian research platform is build using IPython, an open source project that provides an interactive notebook with executable cells. Each cell can be executed individually, but they all share the same variables (more technically the same namespace).

To execute a cell, click on the cell and press shift + enter.

Resarching Investment Algorithms

We've added the following functions to optimize the platform for researching and developing investment algorithms.

  • get_pricing()
  • symbols()
  • local_csv()
  • get_backtest()

Query for Pricing Data

The get_pricing function provides access to 12 years of US Equity pricing data: the same data used by the Quantopian backtester.

get_pricing returns a pandas object. This could be a panel, dataframe or series depending on the input values.

In [1]:
get_pricing('AAPL') #returns a pandas dataframe when run for 1 security
Out[1]:
open_price high low close_price volume price
2013-01-03 00:00:00+00:00 78.270 78.526 77.287 77.484 82090158 77.484
2013-01-04 00:00:00+00:00 76.711 76.949 75.163 75.289 141288863 75.289
2013-01-07 00:00:00+00:00 74.573 75.616 73.601 74.854 114493553 74.854
2013-01-08 00:00:00+00:00 75.603 75.986 74.466 75.029 106374926 75.029
2013-01-09 00:00:00+00:00 74.644 75.002 73.714 73.873 93267415 73.873
2013-01-10 00:00:00+00:00 75.509 75.533 73.647 74.784 139591313 74.784
2013-01-11 00:00:00+00:00 74.430 75.047 74.147 74.304 78800349 74.304
2013-01-14 00:00:00+00:00 71.813 72.501 71.217 71.701 163209526 71.701
2013-01-15 00:00:00+00:00 71.187 71.286 69.056 69.403 208326893 69.403
2013-01-16 00:00:00+00:00 70.664 72.779 69.861 72.299 159155725 72.299
2013-01-17 00:00:00+00:00 72.903 72.966 71.720 71.816 105699461 71.816
2013-01-18 00:00:00+00:00 71.218 71.747 70.916 71.424 108352365 71.424
2013-01-22 00:00:00+00:00 72.081 72.556 70.949 72.103 103487778 72.103
2013-01-23 00:00:00+00:00 72.689 73.571 72.111 73.430 150399608 73.430
2013-01-24 00:00:00+00:00 65.716 66.534 64.323 64.358 339368906 64.358
2013-01-25 00:00:00+00:00 64.528 65.177 62.144 62.837 285973953 62.837
2013-01-28 00:00:00+00:00 62.548 64.746 62.267 64.248 181013747 64.248
2013-01-29 00:00:00+00:00 65.501 65.744 64.590 65.491 134268983 65.491
2013-01-30 00:00:00+00:00 65.287 66.087 64.930 65.274 97733858 65.274
2013-01-31 00:00:00+00:00 65.284 65.613 64.998 65.056 66514496 65.056
2013-02-01 00:00:00+00:00 65.588 65.641 64.051 64.824 125669970 64.824
2013-02-04 00:00:00+00:00 64.846 65.136 63.144 63.176 111873694 63.176
2013-02-05 00:00:00+00:00 63.437 65.678 63.176 65.404 136330974 65.404
2013-02-06 00:00:00+00:00 65.211 66.644 64.656 65.328 140355067 65.328
2013-02-07 00:00:00+00:00 66.180 67.144 64.876 66.894 160811101 66.894
2013-02-08 00:00:00+00:00 67.716 68.403 67.180 67.854 147639465 67.854
2013-02-11 00:00:00+00:00 68.073 69.279 67.608 68.566 118627131 68.566
2013-02-12 00:00:00+00:00 68.503 68.913 66.822 66.836 145834950 66.836
2013-02-13 00:00:00+00:00 66.746 67.664 66.176 66.716 111614622 66.716
2013-02-14 00:00:00+00:00 66.361 67.378 66.290 66.658 77903842 66.658
... ... ... ... ... ... ...
2013-11-20 00:00:00+00:00 74.177 74.347 73.477 73.574 45875325 73.574
2013-11-21 00:00:00+00:00 73.944 74.460 73.383 74.433 60442580 74.433
2013-11-22 00:00:00+00:00 74.219 74.596 74.077 74.243 47004108 74.243
2013-11-25 00:00:00+00:00 74.433 75.126 74.430 74.823 53836245 74.823
2013-11-26 00:00:00+00:00 74.876 76.593 74.859 76.230 86500678 76.230
2013-11-27 00:00:00+00:00 76.617 78.002 76.597 77.999 85159225 77.999
2013-11-29 00:00:00+00:00 78.499 79.763 78.260 79.440 75610142 79.440
2013-12-02 00:00:00+00:00 79.716 80.620 78.690 78.759 111696164 78.759
2013-12-03 00:00:00+00:00 79.759 80.913 79.670 80.913 102825346 80.913
2013-12-04 00:00:00+00:00 80.787 81.314 80.119 80.696 86112711 80.696
2013-12-05 00:00:00+00:00 81.809 82.466 80.917 81.159 97639241 81.159
2013-12-06 00:00:00+00:00 80.829 80.966 79.940 79.992 79291103 79.992
2013-12-09 00:00:00+00:00 80.223 81.370 80.130 80.944 74383060 80.944
2013-12-10 00:00:00+00:00 80.517 81.127 80.173 80.807 61224730 80.807
2013-12-11 00:00:00+00:00 80.939 81.570 79.957 80.173 81888933 80.173
2013-12-12 00:00:00+00:00 80.308 80.764 80.002 80.097 60993840 80.097
2013-12-13 00:00:00+00:00 80.370 80.413 79.096 79.214 75239577 79.214
2013-12-16 00:00:00+00:00 79.300 80.379 79.289 79.637 62266330 79.637
2013-12-17 00:00:00+00:00 79.409 79.922 79.054 79.284 51595695 79.284
2013-12-18 00:00:00+00:00 78.533 78.780 76.973 78.672 131173155 78.672
2013-12-19 00:00:00+00:00 78.502 78.577 77.677 77.780 73579994 77.780
2013-12-20 00:00:00+00:00 77.920 78.806 77.832 78.392 86285363 78.392
2013-12-23 00:00:00+00:00 81.144 81.533 80.396 81.463 115533067 81.463
2013-12-24 00:00:00+00:00 81.407 81.699 80.862 81.097 40526130 81.097
2013-12-26 00:00:00+00:00 81.129 81.359 80.480 80.559 46914818 80.559
2013-12-27 00:00:00+00:00 80.560 80.632 79.930 80.014 54221769 80.014
2013-12-30 00:00:00+00:00 79.707 79.729 78.905 79.216 59118822 79.216
2013-12-31 00:00:00+00:00 79.154 80.186 79.144 80.167 51011087 80.167
2014-01-02 00:00:00+00:00 79.430 79.584 78.862 79.034 54363271 79.034
2014-01-03 00:00:00+00:00 78.983 79.102 77.206 77.284 92325248 77.284

253 rows × 6 columns

Customize your Queries

By default, get_pricing returns OHLCV data at daily frequency for all of 2013. You can change the default behavior by passing the following parameters:

  • Specify start and end dates by passing values to start_date and end_date. Dates can be passed as python datetime objects, pandas Timestamps, or strings that can be parsed by pandas.
  • Narrow the set of returned fields by passing strings or lists of strings as fields. Valid fields are:
    • price
    • close_price (an alias of price)
    • open_price
    • high
    • low
    • volume
  • Access minutely or daily data by setting frequency to frequency = daily or frequency='minute'.
In [2]:
aapl_minute_closes = get_pricing(
    'AAPL', 
    fields='close_price', #modify to price, open_price, high, low or volume to change the field
    start_date='2014-01-01', #customize your pricing date range
    end_date = '2014-07-01', 
    frequency='minute', #change to daily for daily pricing
)

# matplotlib is installed for easy plotting
aapl_minute_closes.plot()
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f7e0d161950>

Convert Tickers to Symbols

By default symbols returns the security object for a ticker symbol. Specify a ticker symbol, or list of symbols, as a string and get a list of security objects back.

  • Use symbol_reference_date to identify which date you want the symbol back for a particular ticker symbol.
  • Specify how you would like missing results to be handeled with handle_missing
In [3]:
symbols('AAPL')
Out[3]:
Security(24, symbol=u'AAPL', security_name=u'APPLE INC', exchange=u'NASDAQ GLOBAL SELECT MARKET', start_date=u'Mon, 04 Jan 1993 00:00:00 GMT', end_date=u'Tue, 10 Mar 2015 00:00:00 GMT', first_traded=None)

Upload your own CSVs

If you want to work with your own data in our environment, you can upload a csv to your data directory and load the file with local_csv. By default, local_csv returns a DataFrame with the data from the loaded file.

You can customize the resulting DataFrame by passing the following parameters:

  • Automatically convert strings to security objects by indicating a symbol_column
  • Automatically parse dates as Datetime object by indicating a date_column
  • Specify what timezone to use with timezone
  • Set the date column of your choosing to be the index with use_date_column_as_index
In [4]:
# Apple_Rev is a CSV from Quandl showing the quarterly revenue for Apple. 
# It is pre-loaded in your data folder. 

AAPLRev = local_csv('AAPL_Rev.csv', date_column = 'Date', use_date_column_as_index = True)
AAPLRev
Out[4]:
Revenue
Date
2014-06-28 37432000000
2014-03-29 45646000000
2013-12-28 57594000000
2013-09-28 37472000000
2013-06-29 35323000000
2013-03-30 43603000000
2012-12-29 54512000000
2012-09-29 35966000000
2012-06-30 35023000000
2012-03-31 39186000000
2011-12-31 46333000000
2011-09-24 28270000000
2011-06-25 28571000000
2011-03-26 24667000000
2010-12-25 26741000000
2010-09-25 20343000000
2010-06-26 15700000000
2010-03-27 13499000000
2009-12-26 15683000000
2009-06-27 8337000000
In [5]:
# Plot with matplotlib

AAPLRev.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f7e0d161d50>

Access Backtest Results (Alpha)

The get_backtest function provides programmatic access to the results of backtests run on the Quantopian platform. It takes a single parameter, the ID of a backtest for which results are desired.

You can find the ID of a backtest in the URL of its full results page, which will be of the form:

https://www.quantopian.com/algorithms/&lt;algorithm_id&gt;/&lt;backtest_id&gt;.

In [6]:
result = get_backtest('536a6d181a4f090716c383b7')
100% Time: 0:00:08|###########################################################|

The BacktestResult Object

The BacktestResult object returned by get_backtest contains all the information stored by Quantopian about the performance of a given backtest run, as well as some additional metadata about the backtest.

Get_backtest returns a backtest results object that includes 11 different data sets. If you type results.&lt;tab&gt; you will see a list of all the things that are available.

In [ ]:
result.

Because there are so many fields on BacktestResult, the class provides helper attributes for easy reference.

  • scalars contains a list of all the scalar attributes of BacktestResult.
  • frames contains a list of all the DataFrame attributes of BacktestResult.
  • attrs contains a list off all the public attributes of BacktestResult.
In [7]:
print "Scalars:"
print result.scalars
print ""
print "Frames:"
print result.frames
print ""
print "All Attributes:"
print result.attrs
Scalars:
['benchmark_security', 'capital_base', 'end_date', 'start_date']

Frames:
['cumulative_performance', 'daily_performance', 'orders', 'positions', 'recorded_vars', 'risk', 'transactions']

All Attributes:
['cumulative_performance', 'daily_performance', 'orders', 'positions', 'recorded_vars', 'risk', 'transactions', 'benchmark_security', 'capital_base', 'end_date', 'start_date']

Preview Contents

For easy inspection of the contents of frame contents, BacktestResult provides a preview method that returns a transposed version of the first few columns of a frame.

In [8]:
result.preview('risk')
Out[8]:
2004-01-07 00:00:00+00:00 2004-01-08 00:00:00+00:00 2004-01-09 00:00:00+00:00
alpha -0.0427 -0.0427 -0.0411
benchmark_period_return 0.004448794 0.008274758 0.001067711
benchmark_volatility 0 0.007181476 0.1034783
beta 0 0 0
excess_return -0.0427 -0.0427 -0.0411
information NaN NaN NaN
max_drawdown 0 0 0
period_label 2004-01 2004-01 2004-01
period_return 0 0 0
sharpe NaN NaN NaN
sortino 0 0 0
treasury_period_return 0.0427 0.0427 0.0411
volatility 0 0 0

Get Interactive Documentation

View documentation for any callable python object with the ? operator.

In [ ]:
get_pricing?
In [ ]:
import pandas as pd
pd.DataFrame?