quantopian help
DESCRIPTION
Quantopian HelpTRANSCRIPT
(/home)
Quantopian OverviewQuantopian is a platform for developing and testing quantitative trading algorithms. Our goal is to handle all the
infrastructure, data, and setup tasks so that you can focus on developing and testing your investment ideas. Live
trading of your algorithms is in a open beta.
Important ConceptsWriting an algorithm in Quantopian uses two parts of our application: the IDE (interactive development
environment) and backtester (testing).
Algorithms are developed in Python in our IDE. With our built-in syntax checking, validation, and smoke tests, the IDE
tells you about issues before they crop up in backtesting.
Our backtester runs an event loop once per historical minute. Any calculations or orders your algorithm needs are
handled on each loop. At the end of a backtest, all the relevant performance, positions, transactions, and risk data
are available to you.
Data sourcesWe have minute bar historical data for US equities since 2002 up to the most recently completed trading day (data
uploaded nightly). SPY, an ETF tracking the S&P 500, is the benchmark used for algorithms simulated in the backtest.
It represents the total returns and reinvests the dividends to model the market performance. You can also set your
own benchmark in your algorithm.
A minute bar is a summary of the trading activity for a security for a one-minute period, and gives you the opening
price, closing price, high price, low price, and trading volume during that minute. Our US equity set is point-in-time,
which is important for backtest accuracy. Since our event-based system sends trading events to you serially, your
algorithm receives accurate historical data without any bias towards the present.
Our database includes all stocks that traded since 2002, even ones that are no longer traded. This is very important
because it helps you avoid survivorship bias in your algorithm. Databases that omit stocks that are no longer traded
ignore bankruptcies and other important events, and lead to false optimism about an algorithm. For example, LEH
(Lehman Brothers) is a security in which your algorithm can trade in 2008, even though the company no longer
exists in 2014; Lehman's bankruptcy was a major event that affected many algorithms at the time.
Our data uses adjusted close prices. That means that the effect of all stock splits and merger activity are applied to
price and volume data. As an example: A stock is trading at $100, and has a 2:1 split. The new price is $50, and all
past price data is retroactively updated 2:1 (volume data is correspondingly updated 1:2). In effect, that means you
can ignore stock splits unless you are storing prices in a live trading algorithm. In a live trading algorithm, the stored
variable needs to have the split applied to the price and volume. In a backtest, the split is already applied
Quantopian Help https://www.quantopian.com/help#api-doco
1 of 79 04/09/14 19:44
retroactively to all data.
There are many other financial data sources we'd like to incorporate, such as options, futures, and fundamental
data. What kind of data sources would you like us to have? Let us know. (mailto:[email protected])
Paper TradingPaper trading is also sometimes known as walk-forward testing. In paper trading, your algorithm gets live market
data (actually, 15-minute delay data) and 'trades' against the live data with a simulated portfolio. This is a good test
for any algorithm. If you inadvertently did overfitting during your algorithm development process, paper trading will
often reveal the problem. You can simulate trades for free, with no risks, against the current market on Quantopian.
Quantopian paper trading uses the same order-fulfillment logic as a regular backtest, including the slippage model.
Before you can paper trade your algorithm, you must run a minute backtest. Go to your algorithm, choose the
minute backtest option, and click the 'Full Backtest' button. Once that backtest is complete, you will see a 'Live Trade
Algorithm' button. Also, on the My Algorithms (/algorithms) list you will see a Start Trading button for any backtested
algorithm. When you start paper trading, you will be prompted to specify the amount of money used in the strategy.
Note that this cash amount is not enforced - it is used solely to calculate your algorithm's returns.
Live TradingWhen you click the Start Trading button, you will have the option to choose 'Broker'.
You will need to authenticate to your Interactive Brokers* account. (Note: Quantopian does not store your brokerage
password.) For more information about live trading and how to create an IB account, see our live trading FAQ
(https://www.quantopian.com/faq#live-trading).
We strongly recommend that you run your algorithm against IB's paper trading mode before running it against a
real money account. IB paper trading is a good test to help find lingering bugs. Once you're satisfied with the paper
trading performance, you should stop the live algorithm and re-launch it against your real money IB account.
While your algorithm is live trading, you can continue to make changes in the IDE to experiment with the code and
run additional backtests. This will not interfere with the live algorithm. These changes will not apply to your live algo
unless you stop the algorithm, run a full backtest with the new code, and redeploy.
If you manually stop a live trading algorithm, this will shutdown the algorithm and prevent it from placing additional
orders. This will not liquidate your portfolio and any open order may still be filled.
By default, orders placed through Quantopian use IB's Smart Router (https://www.interactivebrokers.com
/en/index.php?f=1685) algorithm for order execution. You can also specify to route your order to IEX.
Quantopian's live trading program is still in open beta, and the live trading platform has a number of limitations.
These limitations will be reduced and removed as we keep improving the live trading platform.
The live trading platform does not have the capability to do a mid-day recovery from an outage. If your algo goesdown mid-day (whether it's because of our bug, or your bug, or a third party issue) the algorithm won'tautomatically restart mid-day. If the bug was temporary, the algorithm will start the next day at market open. Onrestart, the data windows (batch_transform and simple transforms) will be filled with the "missing" data thatpassed during the previous day's outage.
There is no automatic liquidation on algorithm exit. If you need to liquidate your holdings, you need to do that
Quantopian Help https://www.quantopian.com/help#api-doco
2 of 79 04/09/14 19:44
programatically in your algorithm or in the Interactive Brokers interface.
If your IB login is terminated, you will need to log back in before trading continues. This could happenunexpectedly because of a server error, or it could be a planned code upgrade on our part. If a login is necessarywe will contact you.
All open orders are cancelled at end-of-day.
Orders made in the last minute of the trading day will be automatically cancelled.
If your algorithm uses fetch_csv(), it is important that all historical data in your fetched data be accessible andunchanged. We do not keep a copy of fetched data; it is reloaded at the start of every trading day. If historicaldata in the fetched file is altered or removed, the algorithm will not run properly. Providing additional, new datain the file for dates in the future is fine.
The use of random() isn't supported because of technical limitations with our start-of-day initialization.
IB only allows one login at a time per account. If you try to log into IB during market hours using the sameaccount as Quantopian, Quantopian will automatically try to force you out so that we can keep trading on yourbehalf. In general, we recommend that you maintain two logins, one for Quantopian and one for your use. If youneed to log in to IB using the Quantopian login during market hours, you should first stop the algorithm inQuantopian.
The open beta is limited to IB accounts with a portfolio value of less than $1,000,000.
The open beta is limited to accounts based in US dollars. The account can be funded by other currencies, but thebase must be US dollars.
We are working very hard at testing and improving our software; however, we certainly have software bugs. As
members of the open beta, in particular, you need to be alert to any bugs that might affect your trading. Bug reports
are greatly appreciated and we will try to fix them as quickly as possible.
* Interactive Brokers LLC is not affiliated with and does not endorse or recommend Quantopian, Inc. Interactive
Brokers provides execution and clearing services to customers who integrate their Interactive Brokers account with
their Quantopian account. For more information regarding Interactive Brokers LLC, please visit
www.interactivebrokers.com.
Privacy and SecurityWe take privacy and security extremely seriously. All trading algorithms and backtest results you generate inside
Quantopian are owned by you. Unless you choose to share your content, your content will remain private. We will
never access your data unless we are helping you troubleshoot issues.
Our databases live in Amazon's cloud infrastructure.
Specifically, our security plans include:
Never storing your password in plaintext in our database
SSL encryption for the Quantopian application
Secure websocket communication for the backtest data
Encrypting all trading algorithms before writing them to our database
We want to be very transparent about our security measures. Don't hesitate to email us
(mailto:[email protected]) with any questions or concerns.
Quantopian Help https://www.quantopian.com/help#api-doco
3 of 79 04/09/14 19:44
Two-Factor AuthenticationTwo-factor authentication (2FA) is a security mechanism to protect your account. When 2FA is enabled, your account
requires both a password and an authentication code you generate on your smart phone.
With 2FA enabled, the only way someone can sign into your account is if they know both your password and have
access to your phone. We strongly urge you to turn on 2FA to protect the safety of your account, especially for live
trading.
How does it work?
Once you enable 2FA, you will still enter your password as you normally do when logging into Quantopian. Then you
will be asked to enter an authentication code. This code can be either generated by the Google Authenticator app on
your smartphone, or sent as a text message (SMS).
If you lose your phone, you will need to use a backup code to log into your account. It's very important that you get
your backup code as soon as you enable 2FA. If you don't have your phone, and you don't have your backup code,
you won't be able to log in. Make sure to save your backup code in a safe place!
You can setup 2FA via SMS or Google Authenticator, or both. These methods require a smartphone and you can
switch between them at any time. To use the SMS option you need a US-based phone number, beginning with the
country code +1.
To configure 2FA via SMS:
Go to https://www.quantopian.com/account?page=twofactor' (https://www.quantopian.com/account?page=twofactor) and click 'Configure via SMS'.
1.
Enter your phone number. Note: Only US-based phone numbers are allowed for the SMS option. Standardmessaging rates apply.
2.
You'll receive a 5 or 6 digit security token on your mobile. Enter this code in the pop-on screen to verify yourdevice. Once verified, you'll need to provide the security token sent via SMS every time you login to Quantopian.
3.
Download your backup login code in case you lose your mobile device. Without your phone and backup code,you will not be able to access your Quantopian account.
4.
To configure 2FA via Google Authenticator:
Go to https://www.quantopian.com/account?page=twofactor' (https://www.quantopian.com/account?page=twofactor) and click 'Configure Google Authenticator'.
1.
If you don’t have Google Authenticator, go to your App Store and download the free app. Once installed, openthe app, click 'Add an Account', then 'Scan a barcode'. You might have to install a barcode scanning app.
2.
Hold the app up to your computer screen and scan the QR code. Enter the Quantopian security token from theGoogle Authenticator app.
3.
Download your backup login code in case you lose your mobile device. Without your phone and backup code,you will not be able to access your Quantopian account.
4.
Backup login code
If you lose your phone and can’t login via SMS or Google Authenticator, your last resort is to login using your backup
login code. Without your phone and this code, you will not be able to login to your Quantopian account.
To save your backup code:
Quantopian Help https://www.quantopian.com/help#api-doco
4 of 79 04/09/14 19:44
Go to https://www.quantopian.com/account?page=twofactor1.
Click 'View backup login code'.2.
Enter your security token received via SMS or on your Google Authenticator app.3.
Copy your backup code to a safe location.4.
Developing in the IDEQuantopian's Python IDE is where you develop your trading ideas. The standard features (autosave, fullscreen, font
size, color theme) help make your experience as smooth as possible.
Your work is automatically saved every 10 seconds, and you can click Save to manually save at any time. We'll also
warn you if you navigate away from the IDE page while there's unsaved content.
CollaborationYou can collaborate in real-time with other Quantopian members on your algorithm. In the IDE, press the
“Collaborate” button and enter your friend’s email address. Your collaborators will receive an email letting them
know they have been invited. They will also see your algorithm listed in their Algorithms Library
(https://www.quantopian.com/algorithms) with a collaboration icon. If your collaborator isn't yet a member of
Quantopian, they will have to register before they can see your algorithm.
The collab experience is fully coordinated:
The code is changed on all screens in real time.
When one collaborator "Builds" a backtest, all of the collaborators see the backtest results, logging, and/orerrors.
There is a chat tab for you to use while you collaborate.
Quantopian Help https://www.quantopian.com/help#api-doco
5 of 79 04/09/14 19:44
Technical Details
Only the owner can invite other collaborators, deploy the algorithm for live trading, or delete the algorithm.
There isn't a technical limit on number of collaborators, but there is a practical limit. The more collaborators youhave, the more likely that you'll notice a performance problem. We'll improve that in the future.
To connect with a member, search their profile in the forums and send them a private message. If they chooseto share their email address with you, then you can invite them to collaborate.
API OverviewWe have a simple but powerful API for you to use:
initialize(context) is the setup method for initializing state or other bookkeeping. This method is called
only once at the beginning of your algorithm. context is an augmented Python dictionary used for holding state
between methods. Properties can be accessed using dot notation ( context.some_property ).
handle_data(context, data) is called on every trading event. data is a dictionary containing your
universe at that time, keyed by security ids. For example, data[sid(26578)] gets you the latest data for Google.
Our backend will find all referenced securities in the algorithm and only send their trading events to this method.
Your algorithm can have other Python methods. For example, you might have handle_data call a utility method
to do some calculations.
For more information, jump to the API Documentation section below.
Referencing securitiesThere are three ways to add securities to your algorithm. Each method accepts up to 200 stocks.
Manual lookup using symbol or sid1.
Call set_universe to create a basket of stocks2.
Import your own list via Fetcher3.
Manual LookupIf you want to manually select a stock, you can use the symbol function to look up a security by its ticker or
company name. Using the symbol method brings up a search box that shows you the top results for your query.
To reference multiple securities in your algorithm, call the symbols function to create a list of stocks. Note the
Quantopian Help https://www.quantopian.com/help#api-doco
6 of 79 04/09/14 19:44
pluralization! The function can accept a list of up to 200 stocks and its parameters are not case sensitive.
Sometimes, tickers are reused over time as companies delist and new ones begin trading. For example, G used to
refer to Gillette but now refers to Genpact. If a ticker was reused by multiple companies, use
set_symbol_lookup_date('YYYY-MM-DD') to specify what date to use when resolving conflicts. This date
needs to be set before any calls to symbol.
Another option to manually enter stocks is to use the sid function. All securities have a unique security id (SID) in
our system. Since symbols may be reused among exchanges, this prevents any confusion and verifies you are calling
the desired security. You can use our sid method to look up a security by its id, symbol, or name.
In other words, sid(24) is equivalent to symbol('AAPL') or Apple.
Quantopian's backtester is intelligent and automatically detects the date range a stock traded. It will adjust the start
or end dates of the backtest during the period the security traded. For example, if you're trying to run a backtest
with Tesla in 2004, the backtest will suggest you begin on June 28, 2010 the first day the stock traded. This ability is
significantly decreased when using symbol instead of sid.
Set UniverseIf your algorithm manually picks a few specific stocks to study, you have some bias risk in your strategy. To avoid this
bias, we provide a tool called set_universe that selects a universe of stocks based on their traded dollar-
volume. This helps you avoid look-ahead bias and survivorship bias, because the criteria will be applied to the
companies as they existed at the point-in-time of your simulation, rather than current values.
A universe is a set of securities defined by certain criteria. Periodically, we re-apply the criteria to update the set of
qualifying securities. As a result, some securities might be added to the universe while others might be removed.
To set the universe, use the set_universe(YourUniverse) method in your algorithm's initializemethod.
We currently provide a single universe implementation, DollarVolumeUniverse , which uses a percentile range
for dollar volume of shares traded for securities. More explicitly, we take the trading volume of each stock and
multiply it by the price of each stock to calculate the dollar volume. We then rank each stock by dollar volume, from
#1 to (roughly) #8000 and convert that ranking into a percentile
To use the DollarVolumeUniverse , set the universe in the initialize method. You can set a universe
Quantopian Help https://www.quantopian.com/help#api-doco
7 of 79 04/09/14 19:44
interval up to 2% for backtests in minute mode or up to 10% in daily mode. Each percentile in the range will result in
~80 securities, so the following example consists of the ~160 securities with the highest dollar-volume volume, with
changes coming on the quarter end:
set_universe(universe.DollarVolumeUniverse(floor_percentile=98.0,ceiling_percentile=100.0))
The DollarVolumeUniverse updates quarterly on the last trading day of March, June, September, and
December. On these days, we use the dollar volume rank of the previous quarter to re-rank the securities. The
quarterly boundaries have some specific behaviors that should be noted:
If a stock is not present at the beginning of a quarter (due to an IPO for instance), it is excluded from theuniverse for that quarter. A stock that stops trading mid-quarter (bankruptcy, delisting, etc.) is kept in theuniverse with a forward-fill using the last recorded price.
At the end of a quarter if your algorithm has a position in a stock, that stock is held in the universe even if itwould have otherwise fallen out.
When a new stock appears in a universe with a batch transform, the historical data for that stock is loaded. Forexample, if you have a batch transform with a refresh window of 5 days, and a new stock is added at thequarterly refresh on March 31, then the batch transform loads the stock data back to March 26 and fills thebatch transform window.
When a new stock appears in a universe and mavg() , vwap() , returns() , and stddev() are called, the datais not backfilled. For example, if you have a 5-day moving average and a new stock is added at the quarterlyrefresh on March 31, then the moving average only has one day of data on April 1.
If you also refer to specific securities using the `sid` function, those sids will always be included in the universe.
All sids are accessible via data in handle_data . For example:
def initialize(context): set_universe(universe.DollarVolumeUniverse(floor_percentile=98.0, ceiling_percentile context.other_sids = [sid(105), sid(106)] # add some specific securities
def handle_data(context, data): for sid in data: log.info(str(sid)) # this will print out all the securities in the universe as well as
Fetcher - Load any CSV fileQuantopian provides historical data since 2002 for US equities in minute and daily bars. The US market data
provides a backbone for financial analysis, but some of the most promising areas of research are finding signals in
non-market data. Fetcher provides your algorithm with access to external time series data. Any time series that can
be retrieved as a CSV file via http or https can be incorporated into a Quantopian algorithm.
Fetcher lets you load CSV files over http. To use it, invoke fetch_csv(url) in your initialize method.
fetch_csv will retrieve the text of the CSV file using the (wonderful) requests module. The CSV is parsed into a
pandas dataframe using pandas.io.parsers.read_csv . You may then specify your own methods to modify
the entire dataframe prior to the start of the simulation. During simulation, the rows of the CSV/dataframe are
streamed to your algorithm's handle_data method as additional properties of the data parameter.
Best of all, your Fetcher data will play nicely with Quantopian's other data features:
Your data will be streamed to your algo without look-ahead bias.
Quantopian Help https://www.quantopian.com/help#api-doco
8 of 79 04/09/14 19:44
Use record to plot a time series of your fetcher data.
Create a trailing window of data using history to make statistical models using your fetcher data.
Fetcher supports two kinds of time series:
Security Information: data that is about individual securities, such as short interest for a stock
Signals: data that stands alone, such as the Consumer Price Index, or the spot price of palladium
For Security Info, your CSV file must have a column with header of symbol which represents the symbol of that
security on the date of that row. Internally, Fetcher maps the symbol to the Quantopian security id (sid). You can
have many symbols in a single CSV file. However, any symbol that is not also initialized (either using sid() or
set_universe() ) is discarded. To access your CSV data in handle_data:
## This algo imports sample short interest data from a CSV file for one security, ## NFLX, and plots the short interest:
def initialize(context): # fetch data from a CSV file somewhere on the web. # Note that one of the columns must be named 'symbol' for # the data to be matched to the stock symbol fetch_csv('https://dl.dropboxusercontent.com/u/169032081/fetcher_sample_file.csv' date_column = 'Settlement Date', date_format = '%m/%d/%y') context.stock = sid(23709) def handle_data(context, data): if 'Days To Cover' in data[context.stock]: record(Short_Interest = data[context.stock]['Days To Cover'])
Here is the sample CSV file. Note that for the Security Info type of import, one of the columns must be 'symbol'.
Settlement Date,symbol,Days To Cover9/30/13,NFLX,2.644849/13/13,NFLX,2.5508298/30/13,NFLX,2.5023318/15/13,NFLX,2.8118587/31/13,NFLX,1.690317
For Signals, your CSV file does not need a symbol column. Instead, you provide it via the symbol parameter:
def initialize(context): fetch_csv('http://yourserver.com/cpi.csv', symbol='cpi') context.stock = sid(24)
def handle_data(context, data): # get the cpi for this date current_cpi = data['cpi']['value']
# plot it record(cpi=current_cpi)
Importing Files from DropboxMany users find Dropbox to be a convenient way to access CSV files. To use Dropbox, you need to use the 'Public
Quantopian Help https://www.quantopian.com/help#api-doco
9 of 79 04/09/14 19:44
URL' provided by Dropbox. A common mistake is to use a URL of format
https://www.dropbox.com/s/abcdefg/filename.csv , which is a URL about the file, not the file itself.
Instead, you should use the Public URL which has a format similar to
https://dl.dropboxusercontent.com/u/1234567/filename.csv .
Importing Files from Google DriveYou can also import a CSV file directly from your Google Drive
Create a new folder in Drive and share it as "Public on the web".1.
Upload your CSV file to this folder.2.
Open the file and click Open in the bottom-right corner.3.
Click the Previewbutton in the toolbar.4.
Share the URL that looks like "www.googledrive.com/host/..." from the preview window.5.
Data Manipulation with FetcherIf you produce the CSV, it is relatively easy to put the data into a good format for Fetcher. First decide if your file
should be a signal or security info source, then build your columns accordingly.
However, you may not always have control over the CSV data file. It may be maintained by someone else, or you may
be using a service that dynamically generates the CSV. Quandl (http://www.quandl.com), for example, provides a
REST API to access many curated datasets as CSV. While you could download the CSV files and modify them before
using them in Fetcher, you would lose the benefit of the nightly data updates. In most cases it's better to request
fresh files directly from the source.
Fetcher provides two ways to alter the CSV file:
pre_func specifies the method you want to run on the pandas dataframe containing the CSV immediately afterit was fetched from the remote server. Your method can rename columns, reformat dates, slice or select data - itjust has to return a dataframe.
post_func is called after Fetcher has sorted the data based on your given date column. This method is intendedfor time series calculations you want to do on the entire dataset, such as timeshifting, calculating rollingstatistics, or adding derived columns to your dataframe. Again, your method should take a dataframe and returna dataframe.
Live Trading and FetcherWhen Fetcher is used in Live Trading or Paper Trading, the fetch_csv() command is invoked once per trading
day, when the algorithm warms up, before market open. It's important that the fetched data with dates in the past
be maintained so that warm up can be performed properly; Quantopian does not keep a copy of your fetched data,
and algorithm warmup will not work properly if past data is changed or removed. Data for 'today' and dates going
forward can be added and updated.
Working With Multiple Data FrequenciesWithout Fetcher data, all Quantopian simulations operate on datasources with identical frequencies. If you choose a
minutely simulation, your handle_data function is called exactly once per (simulation) minute. If you choose daily,
your function is similarly called once per day.
When pulling in external data, you need to be careful about the data frequency to prevent look-ahead bias. If you
are fetching daily data, but are running a minutely-based algorithm, the daily row will be fetched at the beginning of
the day, instead of the end of day. To guard against the bias, you need to use the post_func function. For more
information see post_func API documentation or take a look at this example algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
10 of 79 04/09/14 19:44
(https://www.quantopian.com/posts/ranking-and-trading-on-days-to-cover).
With Fetcher, you will need to have some guard code in your handle_data methods to avoid KeyError problems
accessing data properties. In general, you need to check that a sid is in data before you access it, and that a
property is available on a sid before you access it.:
def initialize(context): # a signal Fetcher fetch_csv('http://priceoftea.com/', symbol='tea') # a security info Fetcher fetch_csv('http://insiderselling.com')
def handle_data(context, data): # guard against being called before the first trade of a security if sid(123) in data: # guard against trades happening before the first insider selling event if 'insider' in data[sid(123)]: if data[sid(123)]['insider'] > 10.0: order(sid(123), -100)
# signal data will pass a blank place holder if the first event has not been sent yet. # So, you can just guard against missing properties if 'price' in data['tea']: record(price_of_tea=data['tea']['price'])
For more information about Fetcher, go to the API documentation or look at the sample algorithms.
Using Fetcher to create a custom universeYour algorithm's universe can be defined from your Fetcher data file by using the optional universe_funcparameter in Fetcher. universe_func allows you to create a universe of up to 200 securities (SIDs) without
manually entering each security in the initialize function.
This is useful if you have a large external fixed stock universe (such as a static list of index constituents) or if you
have a signal file with changing values used for ranking and screening a new list of stocks to trade every day.
To screen securities, use the universe_func to filter the securities down to a smaller set. This allows you to
manipulate stock sets and associated data attributes of any size (e.g. Russell 3000) while only passing the stocks you
want to trade on to handle_data where they will be accessible for minute by minute trading along with our
market data.
Because fetch_csv is smart about aligning external time series data with Quantopian's market data, your
universe will update on a day-to-day basis based on the dates in your file. The backtester will pull in only the data
that is available on the current bar, based on the 'date_column' specified in fetch_csv to prevent look-ahead
bias. This column will be overwritten with the algorithm datetime and be fed into the backtest once the data
becomes available each day.
Here's an example file with ranked stocks:
Quantopian Help https://www.quantopian.com/help#api-doco
11 of 79 04/09/14 19:44
symbol, start date, stock_scoreAA, 2/13/12, 11.7WFM, 2/13/12, 15.8FDX, 2/14/12, 12.1M, 2/16/12, 14.3
You can backtest the code below during the dates 2/13/2012 - 2/18/2012 in minute mode. When you use this sample
file and algorithm, this will be your custom universe on each day:
2/13/2012: AA, WFM
2/14/2012: FDX
2/15/2012: FDX (forward filled because no new data became available)
2/16/2012: M
Quantopian Help https://www.quantopian.com/help#api-doco
12 of 79 04/09/14 19:44
import pandas as pd
def initialize(context): # import the custom CSV data file fetch_csv("https://dl.dropboxusercontent.com/u/6865289/custom%20universe%20example.csv" pre_func=preview, post_func=preview, date_column='start date', universe_func=my_universe)
# my_universe returns a set of securities that define your universe.def my_universe(context, fetcher_data): # fetcher_data is the data resulting from the CSV file from fetcher.
# set my_stocks to be every sid in the fetcher_data my_stocks = set(fetcher_data['sid'])
# log the size of the universe for debugging context.count = len(my_stocks) print 'total universe size: {c}'.format(c=context.count)
# return the sids we identified earlier return my_stocks
# see a snapshot of your CSV for debuggingdef preview(df): log.info(' %s ' % df.head()) return df
def handle_data(context,data): # Convert to EST timezone exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern') # loop over the stocks in universe for stock in data: # At 10:30AM on the entry dates in the file, order target value of 500 shares of # all stocks in universe with scores above 14 if data[stock]['dt'].date() == exchange_time.date() and exchange_time.hour log.info('Entry date for : ' + str(stock.symbol))
if data[stock]['stock_score'] >= 14: order_target_value(stock, 500) log.info('ordered stock {s}'.format(s=stock.symbol))
Note that fetcher_data is bundled by day and passed into my_universe() when there is a new date in your Fetcher
file. If your Fetcher file does not contain data on a given trading day the last known universe is used.
In the example above, the universe is empty until 2/13/12, so there are no stocks found by the algorithm. On
2/13/12, the first date populated with fetched_data, two stocks enter the universe. On 2/14/12, the universe consists
of only one stock; the previous two-stock universe is replaced. On 2/15 there are no new rows in the file, so the
two-stock universe is carried forward. On 2/16/12 there is a new one stock universe, and the previous day's stock is
discarded.
Quantopian Help https://www.quantopian.com/help#api-doco
13 of 79 04/09/14 19:44
In addition to the universe defined by your Fetcher data, any security that is explicitly referenced in your algorithm
or held in your portfolio will be included in the universe. In the example above, an order is placed for WFM which
keeps it in the universe until the order is filled, while AA is dropped entirely.
This feature cannot be used in combination with set_universe(). If set_universe is also called, Fetcher's
universe_func() is ignored.
Note that when using this feature in live trading, it is important that all historical data in your fetched data be
accessible and unchanged. We do not keep a copy of fetched data; it is reloaded at the start of every trading day. If
historical data in the fetched file is altered or removed, the algorithm will not run properly. New data should always
be appended to (not overwritten over) your existing Fetcher .csv source file.
Setting a custom benchmarkThe default benchmark in your algorithm is SPY, an ETF that tracks the S&P 500. You can change it in the
initialize function by using set_benchmark and passing in another security. Only one security can be used
for the benchmark, and only one benchmark can be set per algorithm. If you set multiple benchmarks, the last one
will be used.
Here's an example:
def initialize(context): set_benchmark(sid(2673)) # Tesla
def handle_data(context, data): ...
Checking that trade data exists for a securityOur backtester protects you from running an algorithm against securities that don't exist yet. For instance, if you try
to backtest using Facebook, the backtest will fast-forward to the point where Facebook is traded. If you try to
backtest both Lehman and Facebook you will get an error since Facebook wasn't traded until May 2012, many years
after Lehman stopped trading.
Stocks do not necessarily trade every minute or even every day. This is especially true for thinly-traded securities, so
your algorithm needs to be robust to cover these edge cases.
To check if trade data exists for a security during a given trading period, just check if it exists in the data structure
that is passed to handle_data . For example, if sid in data: ... Your algorithm will check to see if the
stock has data in the bar. Otherwise, it will skip the stock and continue with your algorithm calculations without
receiving an error.
If your algorithm has multiple stocks, it's possible that in a trade bar, one stock has trade data while another does
not. By default, your algorithm is set to set_nodata_policy(NoDataPolicy.LOG_ONLY) , which logs the
error, skips over the stock with no data, and exits the handle_data call. In backtesting and live trading, if you try
to access trade information for a stock that had no data, the algorithm will log the information and continue
running.
If you prefer to stop the algorithm entirely when you try to access missing trade data, you can use
Quantopian Help https://www.quantopian.com/help#api-doco
14 of 79 04/09/14 19:44
set_nodata_policy(NoDataPolicy.EXCEPTION) in initialize . Trying to access missing trade data will
result in a NoTradeDataAvailableException, ending the backtest or live algorithm.
ValidationOur IDE has extensive syntax and validation checks. It makes sure your algorithm is valid Python, fulfills our API, and
has no obvious runtime exceptions (such as dividing by zero). You can run the validation checks by clicking on the
Build button (or pressing control-B), and we'll run them automatically right before starting a new backtest.
Errors and warnings are shown in the window on the right side of the IDE. Here's an example where the log line is
missing an end quote.
When all errors and warnings are resolved, the Build button kicks off a quick backtest. The daily data is less detailed
than the minute bar data, but it runs much faster. The quick backtest is a way to make sure that the algorithm
roughly does what you want it to, without any errors.
Once the algorithm is running roughly the way you'd like, click the 'Full Backtest' button to kick off a full backtest with
minute-bar data.
OrderingCall order(sid, amount) to place a simple market order. sid is the security you wish to trade, and the
amount is the number of shares you want to buy. Use a negative amount in order to sell. The method returns an
order id that can be used to track the order's status. The FAQ (/faq#backtester) has more detailed information about
how orders are handled and filled by the backtester.
Quantopian supports four different order types:
market order: order(context.sid, amount) will place a simple market order.
limit order: Use order(context.sid, amount, style=LimitOrder(price)) to place a limit order. A limit orderexecutes at the specified price or better, if the price is reached.
Note: order(context.sid, amount, limit_price=price) is old syntax and will be deprecated in the future.
stop order: Call order(context.sid, amount, style=StopOrder(price)) to place a stop order (also known as astop-loss order). When the specified price is hit, the order converts into a market order.
Note: order(context.sid, amount, stop_price=price) is old syntax and will be deprecated in the future.
stop-limit order: Call order(context.sid, amount, style=StopLimitOrder(limit_price, stop_price)) to placea stop-limit order. When the specified stop price is hit, the order converts into a limit order.
Note: order(context.sid, amount, limit_price=price1, stop_price=price2) is old syntax and will be deprecated inthe future.
All orders are good-til-cancelled, except for algorithms that are connected to a brokerage. Orders placed with a
brokerage (both paper trading and real-money trading) are cancelled at end-of-day. In the future we will most likely
change backtest order behavior to also cancel on end-of-day, to make the backtester more accurate.
Quantopian Help https://www.quantopian.com/help#api-doco
15 of 79 04/09/14 19:44
You can see the status of a specific order by calling get_order(order) . Here is a code example that places an
order, stores the order_id, and uses the id on subsequent calls to log the order amount and the amount filled:
# place a single order at market open. if exchange_time.hour ==9: and exchange_time.minute >=30: context.order_id = order_target(context.aapl, 1000000) # retrieve the order placed in the first bar aapl_order = get_order(context.order_id) if aapl_order: # log the order amount and the amount that is filled message = 'Order for {amount} has {filled} shares filled.' message = message.format(amount=aapl_order.amount, filled=aapl_order.filled log.info(message)
If your algorithm is using a stop order, you can check the stop status in the stop_reached attribute of the order
ID:
# Monitor open orders and check is stop order triggered for stock in context.secs: #check if we have any open orders queried by order id ID = context.secs[stock] order_info = get_order(ID)
# If we have orders, then check if stop price is reached if order_info: CheckStopPrice = order_info.stop_reached if CheckStopPrice: log.info(('Stop price triggered for stock %s') %
You can see a list of all open orders by calling get_open_orders() . This example logs all the open orders across
all securities:
# retrieve all the open orders and log the total open amount # for each order open_orders = get_open_orders() # open_orders is a dictionary keyed by sid, with values that are lists of orders. if open_orders: # iterate over the dictionary for security, orders in open_orders.iteritems(): # iterate over the orders for oo in orders: message = 'Open order for {amount} shares in {stock}' message = message.format(amount=oo.amount, stock=security) log.info(message)
If you want to see the open orders for a specific stock, you can specify the security like:
get_open_orders(sid=sid) . Here is an example that iterates over open orders in one stock:
Quantopian Help https://www.quantopian.com/help#api-doco
16 of 79 04/09/14 19:44
# retrieve all the open orders and log the total open amount # for each order open_aapl_orders = get_open_orders(context.aapl) # open_aapl_orders is a list of order objects. # iterate over the orders in aapl for oo in open_aapl_orders: message = 'Open order for {amount} shares in {stock}' message = message.format(amount=oo.amount, stock=security) log.info(message)
Orders can be cancelled by calling cancel_order(order) . Orders are cancelled asynchronously.
Exchange Routing
In live trading with Interactive Brokers (IB), you can specify if you want your order routed to exchanges via IB’s Smart
Routing (http://ibkb.interactivebrokers.com/article/728) or to be routed to IEX. By default, orders will use IB’s Smart
Routing method.
To route your order to IEX, you need to first import IBExchange from brokers.ib . Then, in your order
function, you can can pass exchange=IBExchange.IEX into the order style parameter. For example,
order(sid(24), 100, style=MarketOrder(exchange=IBExchange.IEX)) will send a market order to
IEX for 100 shares of Apple.
Order styles supported for routing to IEX:
MarketOrder
LimitOrder
StopOrder
StopLimitOrder
Below is a full code example:
from brokers.ib import IBExchange
def initialize(context): pass
def handle_data(context, data): # Explicitly setting exchange to IEX order(sid(24), 100, style=MarketOrder(exchange=IBExchange.IEX))
# Explicitly setting exchange to SMART. This is the default behavior if no exchange is pa order(sid(300), 100, style=LimitOrder(10, exchange=IBExchange.SMART))
LoggingYour algorithm can easily generate log output by using the log.error (and warn, info, debug) methods. Log
output appears in the right-hand panel of the IDE or in the backtest result page.
Logging is rate-limited (throttled) for performance reasons. The basic limit is two log messages per call of
initialize and handle_data . Each backtest has an additional buffer of 20 extra log messages. Once the
Quantopian Help https://www.quantopian.com/help#api-doco
17 of 79 04/09/14 19:44
limit is exceeded, messages are discarded until the buffer has been emptied. A message explaining that some
messages were discarded is shown.
Two examples:
Suppose in initialize you log 22 lines. Two lines are permitted, plus the 20 extra log messages, so this works.However, a 23rd log line would be discarded.
Suppose in handle_data , you log three lines. Each time handle_data is called, two lines are permitted, plus oneof the extra 20 is consumed. Thus, 20 calls of handle_data are logged entirely. On the 21st call two lines arelogged, and the last line is discarded. Subsequent log lines are also discarded until the buffer is emptied.
Additionally, there is a per-member overall log limit. When a backtest causes the overall limit to be reached, the logs
for the oldest backtest are discarded.
Event PropertiesThe data object in handle_data holds data for all the securities your algorithm references. Each security has
several trading event properties:
datetime is the Python datetime object representing the timestamp of last trade of this security. Trade data is
provided in UTC.
price is the price of this security at the end of this event. Most code within Quantopian operates on this property;
it is the property used by default to describe the price of a security.
volume is the volume of shares traded during this event.
open_price is the price of the security at start of this event.
close_price is the price of the security at end of this event. identical to price .
high is the highest price traded during this event.
low is the lowest price traded during this event.
( mavg , returns , and vwap are transforms, which are explained in the next section.)
Simple TransformsIn addition to event properties, we provide a number of transforms so that you don't have to calculate them
Quantopian Help https://www.quantopian.com/help#api-doco
18 of 79 04/09/14 19:44
yourself. They're listed in the dropdown in the event properties screenshot above, but additional contextual
information is shown when you use them:
mavg(days) calculates the moving average of the security's price over the given number of trading days.
returns() calculates the returns since the end of the previous trading day for the specified security.
stddev(days) calculates the standard deviation of the security's price over the given number of trailing trading
days. Standard deviation is calculated using Bessel's Correction (http://en.wikipedia.org/wiki/Bessel%27s_correction
Note that mathematically there is no standard deviation for a single data point, so this function returns 'None' until
the second data point.
vwap(days) calculates the volume-weighted average price over the given number of trading days.
Note that simple transforms don't have a warmup period in backtesting. As an example, if your algorithm uses
mavg(3), the window for the moving average won't be filled until after the third day. The first day mavg(3) will report
the first day's price, the second day will report the average of the first two days, etc. However, in paper trading and
live trading the simple transforms are warmed up.
You can use the history function with rolling transforms to achieve the same calculations with faster results and less
memory. These will warm up and speed up your time backtesting.
There are many other transforms we could add - which ones would you like? Let us know
(mailto:[email protected]).
Recording and plotting variablesWhen backtesting, you can create time series charts by using the record method and passing series names and
corresponding values using keyword arguments. Up to five series can be recorded and charted. Recording is done at
day-level granularity. Recorded time series are then displayed in a chart below the performance chart.
A simple, contrived example:
def initialize(context): pass
def handle_data(context, data): # track the 20-day moving average for MSFT and AAPL record(msft_mavg=data[sid(5061)].mavg(20), aapl_mavg=data[sid(24)].mavg(20
In the result for this backtest, you'll get something like this:
Quantopian Help https://www.quantopian.com/help#api-doco
19 of 79 04/09/14 19:44
You can also pass variables as the series name using positional arguments. The value of the variable can change
throughout the backtest, dynamically updating in the custom chart. The record function can accept a string or a
variable that has a string value.
def initialize(context): context.stocks = [sid(24), sid(300), sid(2)]
def handle_data(context, data): # You can pass a string variable into record(). # Here we record the price of all the stocks in our universe. for stock in data: price = data[stock].price record(stock, price)
# You can also pass in a variable with a string value. # This records the high and low values for Apple. fields = ['high', 'low'] for field in fields: record(field, data[sid(24)][field])
The code produces this custom chart. Click on a variable name to remove it from the chart, allowing you to zoom in
on the other time series. Click the variable name again to restore it on the chart.
Some notes:
For each series, the value at the end of each trading day becomes the recorded value for that day. This isimportant to remember when backtesting with minute-level data.
Until a series starts being recorded, it will have no value. If record(new_series=10) is done on day 10,new_series will have no value for days 1-9.
Each series will retain its last recorded value until a new value is used. If record(my_series=5) is done on day 5,every subsequent day will have the same value (5) for my_series until a new value is given.
To see more, check out the example record algorithm at the end of the help documentation.
DividendsThe Quantopian database holds over 150,000 dividend events dating from January 2002. While other corporate
events, such as splits and mergers, are handled by adjusting historical prices and volumes, dividends are more
complex. Dividends are treated as events and streamed through the performance tracking system that monitors
your algorithm during a backtest. Dividend events modify the security price and the portfolio's cash balance.
Quantopian Help https://www.quantopian.com/help#api-doco
20 of 79 04/09/14 19:44
Dividends specify four dates:
declared date is the date on which the company announced the dividend.
record date is the date on which a shareholder must be recorded as an owner to receive a dividend payment.Because settlement can take 3 days, a second date is used to calculate ownership on the record date.
ex date is 3 trading days prior to the record date. If a holder sells their stock before this date, they are not paidthe dividend. The ex date is when the price of the stock is typically most affected.
pay date is the date on which a shareholder receives the cash for a dividend.
Stock prices are marked down by the dividend amount on the open following the ex_date. The portfolio's cash
position is increased by the amount of the dividend on the pay date. Quantopian chose this method so that cash
positions are correctly maintained, which is particularly important when an algorithm is used for live trading. The
downside to this method is that this can create a noticeable discontinuity in price history and a short portfolio value.
In order for your algorithm to receive dividend cash payments, you must have a long position (positive amount) in
the stock as of the close of market on the trading day prior to the ex_date AND you must run the simulation through
the pay date, which is typically about 60 calendar days later.
If you are short the stock at market close on the trading day prior to the ex_date, your algorithm will be required to
pay the dividends due. As with long positions, the cash balance will be debited by the dividend payments on the pay
date. This is to reflect the short seller's obligation to pay dividends to the entity that loaned the stock.
Special dividends (where more than 25% of the value of the company is involved) are not yet tracked in Quantopian. There are several hundred of these over the last 11 years. We will add these dividends to our data in the future.
Dividends are not relayed to algorithms as events that can be accessed by the API; we will add that feature in the
future.
HistoryIn many strategies, it is useful to compare the most recent bar data to previous bars. The Quantopian platform
provides utilities to easily access and perform calculations on recent history.
This code queries the last 20 days of price history for a universe. Specifically, this returns the closing daily price for
the last 20 days, including the current price for the current day:
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=20, frequency='1d', field='price')
To use history, run your backtest in minute mode. You can choose history bars at daily or minutely frequency.
bar_countThe bar_count field specifies the number of days or minutes to include in the DataFrame returned by the history
function. This parameter accepts only integer values.
frequencyThe frequency field specifies how often the data is sampled: daily or minutely. Acceptable inputs are ‘1d’ or ‘1m’.
Other frequencies such as ‘30m’ or are not yet supported.
Quantopian Help https://www.quantopian.com/help#api-doco
21 of 79 04/09/14 19:44
Below are examples of code along with explanations of the data returned.
Day Units
"d" suffix to the frequency parameter.
The dataframe returned is always in daily bars. The bars never span more than one trading day.
Examples:history(1, "1d", "price") returns the current price.
history(1, "1d", "volume") returns the volume since the current day's open, even if it is partial.
history(2, "1d", "price") returns yesterday's close price and the current price.
history(6, "1d", "price") returns the prices for the previous 5 days and the current price.
Partial trading days are treated as a single day unit. Scheduled half day sessions, unplanned early closures forunusual circumstances, and other truncated sessions are each treated as a single trading day.
Minute Units
"m" suffix to the frequency parameter.
examples:history(1, "1m", "price") returns the current price.
history(2, "1m", "price") returns the previous minute's close price and the current price.
history(60, "1m", "volume") returns the volume for the previous 60 minutes.
FieldThe history functions return a pandas DataFrame populated with the values for the field specified in the third
parameter.
For example, history(bar_count=2, frequency='1d', field='price') returns a DataFrame
populated with the price information for each stock in the universe:
The available options for the field parameter are:
open_price
high
low
close_price
price
volume
Returned DataThe returned data for daily history, for each day, is:
close_price: the close of the last minute bar for that day. For the current day, the most recent close is returned.
price: same as close_price.
open_price: the open of the first minute bar of the given day.
volume: the sum of all minute volume s. For the current day, the sum of volume thus far.
high: the max of the minute high s for the given day.
low: the min of the minute bar low s for the given day.
If the data source had minute-bar closing prices for a stock XYZ as such:
Quantopian Help https://www.quantopian.com/help#api-doco
22 of 79 04/09/14 19:44
XYZ2013-09-05 20:59:00 17.02013-09-05 21:00:00 19.02013-09-06 14:31:00 20.02013-09-06 14:32:00 18.0
The following call to history would output the following results:
history(bar_count=2, frequency='1d', field='price')
At 2013-09-06 14:31 UTC:
XYZ2013-09-05 21:00:00 19.02013-09-06 14:31:00 20.0
One minute later, at 2013-09-06 14:32 UTC, notice that the timestamp and data for the previous day's data stays the
same, but the current day's value updates to the current algo time and value for price .
XYZ2013-09-05 21:00:00 19.02013-09-06 14:32:00 18.0
History and Backtest StartThe full history data panel is available on the first day of the backtest, so there is no need to 'warm up' the history
DataFrame. The data is backfilled so that calculations can be done starting with the first call to handle_data .
Obviously, we can't load history where the requested data extends farther into the past than our database. The
database limit is Jan 2, 2002 for backtesting and Mar 1, 2012 for paper trading and live trading.
Illiquidity and Forward FillingBy default, history methods will forward fill missing bars. If there is trade data missing for a security one day, the
function will use the previous known price until there is new trade data.
However, it can be useful to have visibility into gaps in the trading history. We provide an option for the historymethods, which disables forward filling. e.g.,
history(bar_count=15, frequency='1d', field='price', ffill=False) will return a DataFrame
that has nan value for price for days on which a given security did not trade.
Common Usage: Current and Previous BarA common use case is to compare yesterday's close price with the current price.
This example compares yesterday's close (labeled prevbar) with the current price (labeled currbar) and places an
order for 20 shares if the current price is above yesterday's closing price.
Quantopian Help https://www.quantopian.com/help#api-doco
23 of 79 04/09/14 19:44
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=2, frequency='1d', field='price') for s in data: prev_bar = price_history[s][-2] curr_bar = price_history[s][-1] if curr_bar > prev_bar: order(s, 20)
Common Usage: Looking Back X BarsIt can also be useful to look further back into history for a comparison. Computing the percent change over given
historical time frame requires the starting and ending price values only, and ignores intervening prices.
The following example operates over all stocks available in data , in pandas Series format to arrive at the percent
change over the past 10 trading days.
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): prices = history(bar_count=10, frequency='1d', field='price') pct_change = (prices.ix[-1] - prices.ix[0]) / prices.ix[0] log.info(pct_change)
Alternatively, leveraging the following iloc pandas DataFrame function, which returns the first and last values as
a pair:
price_history.iloc[[0, -1]]
The percent change example can be re-written as:
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=10, frequency="1d", field='price') pct_change = price_history.iloc[[0, -1]].pct_change() log.info(pct_change)
The difference code example in the Current and Previous Bar section can also be written as:
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=2, frequency="1d", field='price') diff = price_history.iloc[[0, -1]].diff() log.info(diff)
Common Usage: Rolling Transforms
Quantopian Help https://www.quantopian.com/help#api-doco
24 of 79 04/09/14 19:44
Rolling transform calculations such as mavg, stddev, etc. can be calculated via methods provided by pandas.
mavg -> DataFrame.mean (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.mean.html)
stddev -> DataFrame.std (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.std.html)
vwap -> DataFrame.sum (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.sum.html),for volume and price
Common Usage: Standard Deviation
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=5, frequency='1d', field='price') log.info(price_history.std())
Common Usage: Moving Average
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def handle_data(context, data): price_history = history(bar_count=5, frequency='1d', field='price') log.info(price_history.mean())
Common Usage: VWAP
def initialize(context): set_universe(universe.DollarVolumeUniverse(90.0, 90.1))
def vwap(prices, volumes): return (prices * volumes).sum() / volumes.sum()
def handle_data(context, data): prices_a = history(15, '1d', field='price') volumes_a = history(15, '1d', field='volume')
prices_b = history(30, '1d', field='price') volumes_b = history(30, '1d', field='volume')
vwap_a = vwap(prices_a, volumes_a) vwap_b = vwap(prices_b, volumes_b)
for s in data: if vwap_a[s] > vwap_b[s]: order(s, 50)
Common Usage: Using An External LibrarySince history returns a pandas DataFrame, the values can then be passed libraries that operate on numpy and
pandas data structures.
An example OLS strategy:
Quantopian Help https://www.quantopian.com/help#api-doco
25 of 79 04/09/14 19:44
import statsmodels.api as sm
def ols_transform(prices, sid1, sid2): """ Computes regression coefficient (slope and intercept) via Ordinary Least Squares between two SIDs. """ p0 = prices[sid1] p1 = sm.add_constant(prices[sid2], prepend=True) return sm.OLS(p0, p1).fit().params
def initialize(context): context.sid1 = sid(4283) # KO context.sid2 = sid(5885) # PEP
def handle_data(context, data): price_history = history(bar_count=30, frequency='1d', field='price') intercept, slope = ols_transform(price_history, context.sid1, context.sid2
Common Usage: Using TA-LibSince history returns a pandas DataFrame, a Series can be extracted and then passed to TA-Lib.
An example EMA calculation:
# Python TA-Lib wrapper# https://github.com/mrjbq7/ta-libimport talib
def initialize(context): context.my_sid = sid(24)
def handle_data(context, data): price_history = history(bar_count=30, frequency='1d', field='price') my_sid_series = price_history[context.my_sid] ema_result = talib.EMA(my_sid_series) record(ema=ema_result[-1])
Batch TransformsNote: This function is deprecated in favor of history and will stop working at some point in the future. Please start
using history instead.
Often, you will want to operate on a trailing window of minutely data. The work involved in creating a trailing window
of data is so common, Quantopian provides a helper facility called batch_transform .
batch_transform is available in the coding environment as a python function decorator. To use it, you define a
function that accepts a datapanel as its first argument, mark it with the batch_transform decorator, and then
invoke the function from within handle_data using data as the first parameter. The batch_transformdecorator transforms the data parameter sent to your function: data are accumulated into a pandas datapanel.
Here is a simple example that calculates the 10-day average price for all securities, running in a daily bar simulation:
Quantopian Help https://www.quantopian.com/help#api-doco
26 of 79 04/09/14 19:44
# Here we create a universe of securities based on the top 0.1% most liquid by# dollar volume traded. Approximately 7 individual securities will be used# by the algorithm.def initialize(context): set_universe(universe.DollarVolumeUniverse(99.9, 100.0))
# This method expects to receive a datapanel containing dataframes for price,# volume, open_price, close_price, high, low, and volume. Each dataframe will# have ten rows (1 per trading day), and about 7 columns (one for each# stock). window_length is always in trading days.@batch_transform(window_length=10)def get_averages(datapanel): # get the dataframe of prices prices = datapanel['price'] # return a dataframe with one row showing the averages for each stock. return prices.mean()
def handle_data(context, data): # here is the magic part. We invoke the get_averages method using # just the single data (a dictionary of daily bars indexed by security). # the decorator will convert this to a datapanel spanning the 10 day history # that we specified in with the window_length parameter in the decorator # above. averages = get_averages(data) # add a newline to the beginning of the log line so that the column header of the # is properly indented. log.info('\n%s' % averages)
Minute Data in bbaattcchh__ttrraannssffoorrmm , Rolling Updates, and rreeffrreesshh__ppeerriioodd
The same code above could also be run in minutely mode. In that case, the window of data would be 10 trading
days, but because updates are at minute granularity, the first and last day would be partial. For example a 10 day
window would stretch from 10:32am on 3/1/2013 to 10:31am on 3/14/2013. This is in contrast to daily mode, where
the trading days are always complete.
As you can see from this example, batch_transform is by default rolling - each new event passed to the
function will update the window of data. We chose rolling as the default behavior because it is the most commonly
needed, but sometimes it doesn't make sense to update the window on every bar: a batch transform may
depend on comparing whole trading days, or complete weeks and months.
For these cases, the batch_transform provides an optional parameter refresh_period . For example, if you
wanted your minutely simulation to only advance the window in whole day increments, so that the end of the
window would always be through yesterday's close, you could specify the batch_transform like this:
@batch_transform(window_length=10, refresh_period=1)def get_average(datapanel): ...
You may set refresh_period to any integer. Say for example, you are running a daily bar simulation and you
want to update the trailing window each week:
Quantopian Help https://www.quantopian.com/help#api-doco
27 of 79 04/09/14 19:44
@batch_transform(window_length=10, refresh_period=5)def get_average(datapanel): ...
By default refresh_period is 0, which means 'rolling.'
Missing Bars for Batch TransformsStocks do not necessarily trade every minute or even every day. There are cases where the trailing window may
include days that are pre-IPO or post-acquisition for a company. Stocks may be held from trading, or there simply
may not be any trading activity in a day or minute. For these missing periods, the batch_transform helper will
fill in missing values with the last prior value. As a result, it is still possible for the first period to have a missing value
(there is no prior) or for an entire column to be missing when a stock doesn't trade at all in the entire trailing
window. Missing values are represented by the pandas NA.
Pandas has excellent default handling in most aggregate functions so that NAs will not produce spurious results.
Optionally, you can have the batch_transform leave the NAs instead of filling them. batch_transform will
not fill the NAs if you set clean_nans to False (defaults to True). If NAs are not filled, your code needs to
defensively handle them, or runtime errors will often result.
@batch_transfrom(window_length=10, clean_nans=False)def get_averages(datapanel): prices = datapanel['price'] return prices.mean()
Accumulating Custom Fields for Batch Transformsbatch_transform creates a dataframe for each property in the union of all properties for all events in data. You
can extend the fields accumulated in the trailing window simply by adding those fields before sending data to the
decorated function. Suppose you want to track an event, such as the stock trading above its 30 day vwap. You can
set that new property on the events in data and then operate on the trailing history of the value in the
batch_transform :
def initialize(context): set_universe(universe.DollarVolumeUniverse(99.9, 100.0))
def handle_data(context, data): for stock in data: if data[stock].price > data[stock].vwap(30): data[stock]['over_vwap'] = 1 else: data[stock]['over_vwap'] = 0
print_signals(data)
@batch_transform(window_length=5)def print_signals(datapanel): signal = datapanel['over_vwap'] log.info('\n%s' % signal)
Supplemental Parameters for Batch Transforms
Quantopian Help https://www.quantopian.com/help#api-doco
28 of 79 04/09/14 19:44
Sometimes your batch transform needs additional parameters, outside the datapanel. For example, you might want
to pass in the current portfolio or a scalar value for the calculation.
The batch_transform decorator only modifies the first parameter by accumulating data into the datapanel. Any
other parameters are simply forwarded unmodified. Here is how you would pass the current portfolio to a
decorated method.
def initialize(context): set_universe(universe.DollarVolumeUniverse(99.9, 100.0))
def handle_data(context, data): # here is an example of passing an extra parameter to the function. averages = get_averages(data, context.portfolio) log.info('\n%s' % averages)
# Note that the decorator does not receive the supplemental parameters, # the function itself does.@batch_transform(window_length=10)def get_averages(datapanel, portfolio): # get the dataframe of prices prices = datapanel['price'] # return a dataframe with one row showing the averages for each stock. return prices.mean()
Repeated Invocations in Batch Transformsbatch_transform is idempotent, and will cache results where possible. However, for optimal speed, you should
try to use vector operations in the batch_transform - operate on the complete dataframe and return
dataframes. A rule of thumb is to strive to invoke a batch transform just once per handle_data invocation.
Partially Full Trailing Windows in Batch TransformsBy default, batch_transform will return None if the trailing window is not full. For example, if you set
window_length = 10 it will return None for the first 9 trading days and begin returning trailing data on the
10th trading day. This ensures that each window is equal length, and that comparisons between results are valid.
However, it is sometimes useful to perform a calculation before the trailing window is completely full. This is
supported by the optional parameter compute_only_full , which defaults to True. If set to False, the
batch_transform will trigger the function to recalculate values with whatever data is available. In the future,
batch_transform will be deprecated in support of the more robust history function, which does not require
this warm-up period
Slippage ModelsSlippage is where our backtester calculates the realistic impact of your orders on the execution price you receive.
When you place an order for a trade, your order affects the market. Your buy order drives prices up, and your sell
order drives prices down; this is generally referred to as the 'price impact' of your trade. The size of the price impact
is driven by how large your order is compared to the current trading volume. The slippage method also evaluates if
your order is simply too big: you can't trade more than market's volume, and generally you can't expect to trade
more than a quarter of the volume. All of these concepts are wrapped into the slippage method.
Slippage must be defined in the initialize method. It has no effect if defined in handle_data() . If you do
not specify a slippage method, slippage defaults to
Quantopian Help https://www.quantopian.com/help#api-doco
29 of 79 04/09/14 19:44
VolumeShareSlippage(volume_limit=0.25, price_impact=0.1) .
To set slippage, use the set_slippage method and pass in FixedSlippage , VolumeShareSlippage , or a
custom slippage model that you define.
By default, backtests use the VolumeShareSlippage model.
Fixed Slippage
When using the FixedSlippage model, the size of your order does not affect the price of your trade execution.
You specify a 'spread' that you think is a typical bid/ask spread to use. When you place a buy order, half of the
spread is added to the price; when you place a sell order, half of the spread is subtracted from the price.
Volume Share Slippage
In the VolumeShareSlippage model, the price you get is a function of your order size relative to the security's
actual traded volume. You provide a volume_limit cap (default 0.25), which limits the proportion of volume that your
order can take up per bar. For example: if the backtest is running in one-minute bars, and you place an order for 60
shares; then 100 shares trade in each of the next several minute; and the volume_limit is .25; then your trade order
will be split into three orders (25 shares, 25 shares, and 10 shares). Setting the volume_limit to 1.00 will permit the
backtester to use up to 100% of the bar towards filling your order. Using the same example, this will fill 60 shares in
the next minute bar.
The price impact constant (default 0.1) defines how large of an impact your order will have on the backtester's price
calculation. The slippage is calculated by multiplying the price impact constant by the square of the ratio of the order
to the total volume. In our previous example, for the 25-share orders, the price impact is .1 * (25/100) * (25/100), or
0.625%. For the 10-share order, the price impact is .1 * (10/100) * (10/100), or .1%.
Custom Slippage
You can build a custom slippage model that uses your own logic to convert a stream of orders into a stream of
transactions. In the initialize() function you must specify the slippage model to be used and any special
parameters that the slippage model will use. Example:
Quantopian Help https://www.quantopian.com/help#api-doco
30 of 79 04/09/14 19:44
def initialize(context): set_slippage(MyCustomSlippage(slippage-param))
Your custom model must be a class that inherits from slippage.SlippageModel and implements
process_order(self, trade_bar, order) .
Each order that your algorithm places, along with the current price information for that security (the trade bar), is
passed to your custom model's process_order . The logic inside that method decides whether to generate a
transaction, and if so, at what amount and price. To create a transaction, use the
slippage.create_transaction method
The order object has the following properties: amount (float), direction (1 for buy, -1 for sell), sid (int),
stop and limit (float), and stop_reached and limit_reached (boolean). The trade_bar object is
the same as data[sid] in handle_data and has open_price , close_price , high , low , volume , and
sid .
The slippage.create_transaction method takes the given trade_bar, the given order, the price and amount
calculated by your slippage model, and returns the newly built transaction.
Many slippage models' behavior depends on how much of the total volume traded is being captured by the
algorithm. You can use self.volume_for_bar to see how many shares of the current security have been
traded so far during this bar. If your algorithm has many different orders for the same stock in the same bar, this is
useful for making sure you don't take an unrealistically large fraction of the traded volume.
If your slippage model doesn't place a transaction for the full amount of the order, the order stays open with an
updated amount value, and will be passed to process_order on the next bar. Orders that have limits that
have not been reached will not be passed to process_order . Finally, if your transaction has 0 shares or more
shares than the original order amount, an exception will be thrown.
Please see the sample custom slippage model.
Commission ModelsTo set the cost of your trades, use the set_commission method and pass in PerShare or PerTrade . Like
the slippage model, set_commission must be used in the initialize method and has no effect if used in
handle_data . If you don't specify a commission, your backtest defaults to $0.03 per share.
You can define your trading cost in either dollars per share or dollars per trade.
Trading GuardsThere are several trading guards you can place in your algorithm to prevent unexpected behavior. All the guards are
enforced when orders are placed.
Quantopian Help https://www.quantopian.com/help#api-doco
31 of 79 04/09/14 19:44
Maximum Order Count
Sets a limit on the number of orders that can be placed by this algorithm in a single day. In the initializefunction you can enter the set_max_order_count , it will have no effect if placed in handle_data .
def initialize(context): # Algorithm will raise an exception if more than 50 orders are placed in a day set_max_order_count(50)
Maximum Order Size
Sets a limit on the size of any single order placed by this algorithm. This limit can be set in terms of number of
shares, dollar value, or both. The limit can optionally be set for a given security; if the security is not specified, it
applies to all securities. This must be run in the initialize function.
def initialize(context): # Algorithm will raise an exception if we attempt to order more than # 10 shares or 1000 dollars worth of sid(24) in a single order. set_max_order_size(sid(24), max_shares=10, max_notional=1000.0)
Maximum Position Size
Sets a limit on the absolute magnitude of any position held by the algorithm for a given security. This limit can be set
in terms of number of shares, dollar value, or both. A position can grow beyond this limit because of market
movement; the limit is only imposed at the time the order is placed. The limit can optionally be set for a given
security; if the security is not specified, it applies to all securities. This must be run in the initialize function.
def initialize(context): # Algorithm will raise an exception if we attempt to hold more than # 30 shares or 2000 dollars worth of sid(24). set_max_position_size(sid(24), max_shares=30, max_notional=2000.0)
Long Only
In the initialize function, specify long_only to prevent the algorithm from taking short positions. It does
not apply to existing open orders or positions in your portfolio.
def initialize(context): # Algorithm will raise an exception if it attempts to place an # order which would cause us to hold negative shares of any sid. set_long_only()
Viewing Portfolio StateYour current portfolio state is accessible from the context object in handle_data :
Quantopian Help https://www.quantopian.com/help#api-doco
32 of 79 04/09/14 19:44
To view an individual position's information, use the context.portfolio.positions dictionary:
Details on the portfolio and position properties can be found in the API documentation below.
Module ImportOnly specific, whitelisted Python modules can be imported. If you need a module that isn't on this list, please let us
know. (mailto:[email protected])
bisect
cmath
collections
datetime
functools
heapq
itertools
math
numpy
pandas
pytz
QSTK
Queue
random
re
scipy
statsmodels
sklearn
time
zipline
Quantopian Help https://www.quantopian.com/help#api-doco
33 of 79 04/09/14 19:44
Running BacktestsYou can set the start date, end date, and starting capital used by the backtest in the IDE.
To create a new backtest, click the 'Run Full Backtest' button from the IDE. That button appears once your algorithm
successfully validates. Press the 'Build' button to start the validation if the 'Run Full Backtest' button is not visible in
the upper-right of the IDE.
We also provide a Backtests Page that has a summary of all backtests run against an algorithm. To go to the
Backtests page, either click the Backtest button at the top right of the IDE, or, from the My Algorithms page click the
number of backtests that have been run. The backtests page lists all the backtests that have been run for this
algorithm, including any that are in progress. You can view an existing or in-progress backtest by clicking on it.
Closing the browser will not stop the backtest from running. Quantopian runs in the cloud and it will continue to
execute your backtest until it finishes running. If you want to stop the backtest, press the Cancel button.
Backtest resultsOnce a backtest starts, we load up all the trading events for the securities that your algorithm specified, and feed
them to your algorithm in time order. Results will start streaming in momentarily after the backtest starts.
Here is a snapshot of a backtest results page. Mouse over each section to learn more.
OVERALL RESULTS
CUMULATIVE PERFORMANCE AND BENCHMARK OVERLAY
BACKTEST SETTINGS AND STATUS
Quantopian Help https://www.quantopian.com/help#api-doco
34 of 79 04/09/14 19:44
Backtest settings and status: Shows the initial settings for the backtest, the progress bar when the backtest is in
progress, and the final state once the test is done. If the backtest is cancelled, exceeds its max daily loss, or have
runtime errors, that information will be displayed here.
Result details: Here's where you dive into the details of your backtest results. You can examine every transaction
that occurred during the backtest, see how your positions evolved over time, and look at detailed risk metrics. For
the risk metrics, we show you 1, 3, 6, and 12-month windows to provide more granular breakdowns
Overall results: This is the overall performance and risk measures of your backtest. These numbers will update
during the course of the backtest as new data comes in.
Cumulative performance and benchmark overlay: Shows your algorithm's performance over time (in blue) overlaid
with the benchmark (in red).
Daily and weekly P/L: Shows your P/L per day or week, depending on the date range selected.
Transactions chart: Shows all the cumulative dollar value of all the buys and sells your algorithm placed, per day or
week. Buys are shown as positive blue, and sells as negative reds.
API Documentation
Methods to implementYour algorithm has to implement two methods: initialize and handle_data .
iinniittiiaalliizzee((ccoonntteexxtt))Called once at the very beginning of a backtest. Your algorithm can use this method to set up any bookkeeping
that you'd like.
The context object will be passed to all the other methods in your algorithm.
Parameters
context: An initialized and empty Python dictionary. The dictionary has been augmented so that properties can
be accessed using dot notation as well as the traditional bracket notation.
Returns
DETAILSQuantopian Help https://www.quantopian.com/help#api-doco
35 of 79 04/09/14 19:44
None
Example
def initialize(context): context.notional_limit = 100000
hhaannddllee__ddaattaa((ccoonntteexxtt,, ddaattaa))Called whenever a market event occurs for any of your algorithm's specified securities.
Parameters
data: A dictionary containing your universe at that time, keyed by security id. It represents a snapshot of your
algorithm's universe as of when this method was called. Market information about each security and transforms
are all available in this object. Read more below.
context: Same context object in initialize , stores any state you've defined, and stores portfolio object.
Returns
None
Example
def handle_data(context, data): # all your algorithm logic here # ... order(sid(24), 100) # ...
Order MethodsWithin your algorithm, there are some order methods you can use:
oorrddeerr((ssiidd,, aammoouunntt,, ssttyyllee==OOrrddeerrTTyyppee))Places an order for the specified security of the specified number of shares. Order type is inferred from the
parameters used. If only sid and amount are used as parameters, the order is placed as a market order.
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
OrderType: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
Quantopian Help https://www.quantopian.com/help#api-doco
36 of 79 04/09/14 19:44
oorrddeerr__vvaalluuee((ssiidd,, aammoouunntt,, ssttyyllee==OOrrddeerrTTyyppee))Place an order by desired value rather than desired number of shares. Placing a negative order value will result in
selling the given value. Orders are always truncated to whole shares.
Example
Order AAPL worth up to $1000: order_value(sid(24), 1000) . If price of AAPL is $105 a share, this would
buy 9 shares, since the partial share would be truncated (discarding slippage and transaction cost).
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
OrderType: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
oorrddeerr__ppeerrcceenntt((ssiidd,, aammoouunntt,, ssttyyllee==OOrrddeerrTTyyppee))Places an order in the specified security corresponding to the given percent of the current portfolio value, which is
the sum of the positions value and ending cash balance. Placing a negative percent order will result in selling the
given percent of the current portfolio value. Orders are always truncated to whole shares. Percent must be
expressed as a decimal (0.50 means 50%).
Example
order_percent(sid(24), .5) will order AAPL shares worth 50% of current portfolio value. If AAPL is
$100/share and the portfolio value is $2000, this buys 10 shares (discarding slippage and transaction cost).
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
OrderType: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
oorrddeerr__ttaarrggeett((ssiidd,, aammoouunntt,, ssttyyllee==OOrrddeerrTTyyppee))Places an order to adjust a position to a target number of shares. If there is no existing position in the security, an
Quantopian Help https://www.quantopian.com/help#api-doco
37 of 79 04/09/14 19:44
order is placed for the full target number. If there is a position in the security, an order is placed for the difference
between the target number of shares and the current number of shares. Placing a negative target order will result
in a short position equal to the negative number specified.
Example
If the current portfolio has 5 shares of AAPL and the target is 20 shares, order_target(sid(24), 20)orders 15 more shares of AAPL.
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
OrderType: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
oorrddeerr__ttaarrggeett__vvaalluuee((ssiidd,, aammoouunntt,, ssttyyllee==OOrrddeerrTTyyppee))Places an order to adjust a position to a target value. If there is no existing position in the security, an order is
placed for the full target value. If there is a position in the security, an order is placed for the difference between
the target value and the current position value. Placing a negative target order will result in a short position equal
to the negative target value. Orders are always truncated to whole shares
Example
If the current portolio holds $500 worth of AAPL and the target is $2000,
order_target_value(sid(24), 2000) orders $1500 worth of AAPL (rounded down to the nearest share).
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
OrderType: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
oorrddeerr__ttaarrggeett__ppeerrcceenntt((ssiidd,, aammoouunntt,, ssttyyllee==ttyyppee))Place an order to adjust a position to a target percent of the current portfolio value. If there is no existing position
Quantopian Help https://www.quantopian.com/help#api-doco
38 of 79 04/09/14 19:44
in the security, an order is placed for the full target percentage. If there is a position in the security, an order is
placed for the difference between the target percent and the current percent. Placing a negative target percent
order will result in a short position equal to the negative target percent. Portfolio value is calculated as the sum of
the positions value and ending cash balance. Orders are always truncated to whole shares, and percentage must
be expressed as a decimal (0.50 means 50%).
Example
If the current portfolio value is 5% worth of AAPL and the target is to allocate 10% of the portfolio value to AAPL,
order_target_percent(sid(24), 0.1) will place an order for the difference, in this case ordering 5%
portfolio value worth of AAPL.
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
type: (optional) Specifies the order style and the default is a market order. The available order styles are:style=MarketOrder(exchange)
style=StopOrder(stop_price, exchange)
style=LimitOrder(limit_price, exchange)
style=StopLimitOrder(limit_price=price1, stop_price=price2, exchange)
Click here to view an example for exchange routing.
Returns
An order id.
ccaanncceell__oorrddeerr((oorrddeerr))Attempts to cancel the specified order. Cancel is attempted asynchronously.
Parameters
order: Can be the order_id as a string or the order object.
Returns
None
ggeett__ooppeenn__oorrddeerrss((ssiidd==ssiidd))If sid is None or not specified, returns all open orders. If sid is specified, returns open orders for that sid
Parameters
sid: (optional) A security object. Can be also be None.
Returns
If sid is unspecified or None, returns a dictionary keyed by security id. The dictionary contains a list of orders for
each sid, oldest first. If a sid is specified, returns a list of open orders for that sid, oldest first.
ggeett__oorrddeerr((oorrddeerr))Returns the specified order. The order object is discarded at the end of handle_data .
Parameters
order: Can be the order_id as a string or the order object.
Quantopian Help https://www.quantopian.com/help#api-doco
39 of 79 04/09/14 19:44
Returns
returns an order object that is read/writeable but is discarded at the end of handle_data .
Order Execution in IBWithin your algorithm, there are the special execution style for live trading:
Relative Order
oorrddeerr((ssiidd,, aammoouunntt,, ssttyyllee==RReellaattiivveeOOrrddeerr((ooffffsseett,, ppcctt__ooffffsseett,, lliimmiitt__pprriiccee,, eexxcchhaannggee))))RelativeOrder is an order type with Interactive Brokers (IB) that allows you to seek a more aggressive price than
the National Best Bid and Offer in live trading. By placing more aggressive bids and offers than the current best
bids and offers, you can increase your odds of filling the order. This can be placed as a fixed off-set amount (e.g.
two cents higher on a buy, two cents lower on a sell) and/or as a percentage offset. You can combine a fixed
amount-offset with a percentage offset. The quotes are automatically adjusted as the markets move, to remain
aggressive.
To use this order type, you need to have a subscription to IB's market data for Relative Orders. Otherwise, IB
will provide the free 15-minute delayed data for these values.
In a buy order, if the National Best Bid (NBB) increases before your order executes, your order automatically
adjusts upwards to maintain its fixed offset from the NBB. If the NBB moves down, there will be no adjustment
because your bid will become even more aggressive and execute.
For sell orders, your offer is pegged to the National Best Offer (NBO) by a more aggressive offset, and if the NBO
moves down, your offer will also adjust downward. If the NBO moves up, there will be no change because your
offer will become more aggressive and execute.
If you specify both a fixed off-set amount and percent offset, IB will use the more aggressive of the two possible
prices (i.e. higher on a buy, lower on a sell). In this order type, you can also specify a limit price that guarantees
your order will never adjust to a value greater than the specified price on a buy and will never adjust lower than
the specified price on a sell.
When backtesting this order type, or forward-testing a Quantopian-backed paper trading algorithm, the order is
modeled as a simple market order. It is only executed as a true RelativeOrder when run with an IB-backed
paper or real-money algorithm. In the future, the backtester will be updated to include this order type. Also, you
may choose to build a custom slippage model that models this type of order in backtesting.
Technical Details:Only IB real money accounts can execute algorithms with RelativeOrder .
IB doesn't support RelativeOrder for paper accounts. The order will appear as 'Cancelled' in the live algorithmdashboard.
In backtesting and Quantopian paper trading, the order is modeled as a simple market order.
You need a subscription to IB's market data for Relative Orders, otherwise IB provides 15 minute delayedprices.
To use this order style, you will need to import RelativeOrder from the library brokers.ib .
Quantopian Help https://www.quantopian.com/help#api-doco
40 of 79 04/09/14 19:44
Parameters
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
offset: Floating point value measured in dollars. Specifies a fixed offset from the current NBB or NBO.
pct_offset: Floating point value between 0.0 and 100.0 inclusive. Specifies a fixed % offset from the NBB or
NBO.
limit_price: Price specifying the maximum (on a buy) or minimum (on a sell) price at which order executes.
exchange: Not used. This order routes to SMART in IB Live Trading and is ignored in non-live trading. Routing to
IEX is not allowed for this order type.
Example
from brokers.ib import RelativeOrder
def initialize(context): pass
def handle_data(context, data): # Order to buy at a price two cents higher than the National Best Bid. order(sid(24), 100, style=RelativeOrder(offset=.02)) # Order to sell at a price 0.5% lower than the National Best Bid order(sid(300), -100, style=RelativeOrder(pct_offset=0.5)) # Order to buy at a price two cents higher than 0.5% higher than # the current National Best Bid. # But at a price no greater than $15.00 per share. order(sid(2), 100, style=RelativeOrder(limit_price=15, offset=0.02, pct_offset
VWAPBestEffort
oorrddeerr((ssiidd,, aammoouunntt,, ssttyyllee==VVWWAAPPBBeessttEEffffoorrtt((lliimmiitt__pprriiccee==pprriiccee11,, ssttaarrtt__ddaattee==ddaattee11,,eenndd__ddaattee==ddaattee22,, mmaaxx__ppcctt__vvooll==ppeerrcceenntt,,aavvooiidd__lliiqquuiiddiittyy==FFaallssee,, eexxcchhaannggee==EExxcchhaannggee))))
VWAPBestEffort (https://www.interactivebrokers.com/en/?f=%2Fen%2Ftrading%2Forders%2FvwapAlgo.php) is an
Interactive Brokers (IB) execution algorithm that breaks up your order into small portions and distributes them
over a specified period in attempt to get you the Volume Weighted Average Price (VWAP). It seeks to achieve the
VWAP calculated from the time you submit the order to the market close. This order type is exclusive to IB and
only available for live trading. IB supports BestEfforts VWAP for market orders and limit orders. It cannot be
applied to stop orders or stop-limit orders. This is compatible with any ordering function including order ,
order_target , order_value , and order_target_percent . Once your market or limit order is
submitted to IB, the broker will attempt to fill it according to their BestEfforts algorithm.
Using this method, your order is partially filled throughout the timperiod and is completely filled by the end date.
We strongly encourage you to check for open orders in your algorithm to drive your ordering logic.
You can specify a fixed window for your VWAP calculation or you can maintain a moving window throughout the
day, as specified by the start_date and end_date parameters. To place a VWAP order you will need to
import the VWAPBestEffort class from the brokers.ib module.
Parameters
Quantopian Help https://www.quantopian.com/help#api-doco
41 of 79 04/09/14 19:44
sid: A security object.
amount: The integer amount of shares. Positive means buy, negative means sell.
limit_price: (optional) Floating point value specifying the limit price for orders. If left None, orders are placed
as market orders.
start_date: (optional) Start of period to obtain VWAP. The default is the current algorithm time + 1 minute. The
BestEffort order will be submitted to IB in the next minute bar.
end_date: (optional) End of period to obtain VWAP. The default is at market close.
max_pct_volume: (optional) The maximum percentage allowed to trade of the daily volume. The available
range is 0.01 – 0.5, and if left unspecified will default to 0.25.
avoid_liquidity: (optional) Check to ensure the order will not hit the bid or lift the offer if possible. This may
help to avoid liquidity-taker fees, and could result in liquidity-adding rebates. However, it may also result in
greater deviations from the benchmark. It is a boolean variable that defaults to False.
exchange: (optional) Exchange where to route orders. Defaults to SMART in IB Live Trading, and is ignored in
non-live trading. Routing to IEX is not allowed for this order type.
Returns
An order id.
Example
Quantopian Help https://www.quantopian.com/help#api-doco
42 of 79 04/09/14 19:44
from brokers.ib import VWAPBestEffort, IBExchangefrom datetime import timedeltaimport pandas as pd
def initialize(context): pass
def handle_data(context, data): now = pd.Timestamp(get_datetime()).tz_convert('US/Eastern') # Do nothing if there are open orders: if has_orders(context,data): print('has open orders - doing nothing!') return
# For IB Paper and Live Trading, executes a VWAP Best Effort for in a moving 30 minute win # order using the specified parameters, with MKT as the underlying # order type. # Equivalent to order_target(sid(24), 100) during zipline backtesting. order_target(sid(24), 100, style=VWAPBestEffort(start_date=now + timedelta max_pct_vol=.05,avoid_liquidity=False,exchange=IBExchange. # For IB Paper and Live Trading, executes a VWAP Best Effort during the entire day # order using the specified parameters, with LMT as the underlying # order type. # Throws a validation warning and has an effect equivalent to # order_target(sid(23709), 100, style=LimitOrder(10)) during zipline backtesting. order_target(sid(23709), 100, style=VWAPBestEffort(limit_price=10, start_date max_pct_vol=.05, avoid_liquidity=False, exchange=IBExchange. def has_orders(context,data): # Return true if there are pending orders. has_orders = False for stock in data: orders = get_open_orders(stock) if orders: for oo in orders: message = 'Open order for {amount} shares in {stock}' message = message.format(amount=oo.amount, stock=stock) log.info(message) has_orders = True return has_orders
Other MethodsWithin your algorithm, there are some other methods you can use:
Loads the given CSV file (specified by url) to be used in a backtest.
ffeettcchh__ccssvv((uurrll,, pprree__ffuunncc==NNoonnee,, ppoosstt__ffuunncc==NNoonnee,, uunniivveerrssee__ffuunncc,, ddaattee__ccoolluummnn==''ddaattee'',, ddaattee__ffoorrmmaatt==''%%mm//%%dd//%%yy'',, ttiimmeezzoonnee==''UUTTCC'',, ssyymmbbooll==NNoonnee,, mmaasskk == FFaallssee,, ****kkwwaarrggss))
Quantopian Help https://www.quantopian.com/help#api-doco
43 of 79 04/09/14 19:44
Parameters
url: A well-formed http or https url pointing to a CSV file that has a header, a date column, and a symbol column (symbolcolumn required to match data to securities).
pre_func: (optional) A function that takes a pandas dataframe parameter (the result of pandas.io.parsers.read_csv) andreturns another pandas dataframe.
post_func: (optional) A function that takes a pandas dataframe parameter and returns a dataframe.
universe_func: (optional) A function that accepts two parameters (context and fetcher_data) and returns a set(https://docs.python.org/2/library/stdtypes.html#set) of Security objects
date_column: (optional) A string identifying the column in the CSV file's header row that holds the parseable dates.Data is only imported when the date is reached in the backtest to avoid look-ahead bias.
date_format: (optional) A string defining the format of the date/time information held in the date_column.
timezone: (optional) Either a pytz timezone object or a string conforming to the pytz timezone database.
symbol: (optional) If specified, the fetcher data will be treated as a signal source, and all of the data in each row will beadded to the data parameter of the handle_data method. You can access all the CSV data from this source asdata['symbol'].
mask: (optional) This is a boolean whose default is True. By default it will import information only for sids initialized inyour algo. If set to False, it will import information for all stocks in the CSV file.
**kwargs: (optional) Additional keyword arguments that are passed to the requests.get and pandasread_csv calls. Click here to see the valid arguments.
ggeett__ddaatteettiimmee(())Returns the current algorithm time.
Parameters
None
Returns
Returns a Python datetime object with the current time in the algorithm. For daily data, the hours, minutes, and
seconds are all 0. For minute data, it's the end of the minute bar.
hhiissttoorryy((bbaarr__ccoouunntt,, ffrreeqquueennccyy,, ffiieelldd,, ffffiillll==TTrruuee))Function to get a trailing window of daily data. For more complete documentation, see the M section.
Parameters
bar_count: The int number of bars returned by the history call. This includes the current bar.
frequency: The size of the bars returned by history. Available frequencies are '1d' and '1m' .
field: The data field selected from history. Currently, only the OHLCV data is supported. Options are:
'open_price' , 'close_price' , 'price' , 'high' , 'low' , 'volume' .
ffill: Whether or not to forward fill the history data. If ffill is False , the return frame will have
np.nan for pricing data, and 0 for volume. The default is True .
Returns
Returns a pandas
DataFrame (http://pandas.pydata.org/pandas-docs/dev/dsintro.html#dataframe) which
contains bar_count rows of data for the specified field . The columns of the DataFrame are the sids in
the current universe.
Quantopian Help https://www.quantopian.com/help#api-doco
44 of 79 04/09/14 19:44
lloogg..eerrrroorr((mmeessssaaggee)),, lloogg..iinnffoo((mmeessssaaggee)),, lloogg..wwaarrnn((mmeessssaaggee)),, lloogg..ddeebbuugg((mmeessssaaggee))Logs a message with the desired log level. Log messages are displayed in the backtest output screen, and we only
persist the last 512 of them.
Parameters
message: The message to log.
Returns
None
rreeccoorrdd((sseerriieess11__nnaammee==vvaalluuee11,, sseerriieess22__nnaammee==vvaalluuee22,, ......))Records the given series and values. Generates a chart for all recorded series.
Parameters
values: Keyword arguments (up to 5) specifying series and their values.
Returns
None
rreeccoorrdd((''sseerriieess11__nnaammee'',, vvaalluuee11,, ''sseerriieess22__nnaammee'',, vvaalluuee22,, ......))Records the given series and values. Generates a chart for all recorded series.
Parameters
values: Variables or securities (up to 5) specifying series and their values.
Returns
None
sseett__ssyymmbbooll__llooookkuupp__ddaattee((''YYYYYYYY--MMMM--DDDD''))Sets the date to use when disambiguating symbols that historically referred different securities. Needs to be set
before calling the symbol() or symbols() methods.
Parameters
date: The YYYY-MM-DD string format of a date.
Returns
None
ssiidd((iinntt))Convenience method to look up a security by its id. Within the IDE, an inline search box appears showing you
matches on security id, symbol, and name.
Parameters
int: The id of a security.
Returns
A security object.
Quantopian Help https://www.quantopian.com/help#api-doco
45 of 79 04/09/14 19:44
ssyymmbbooll((''ssyymmbbooll11''))Convenience method to look up a security by its symbol. Within the IDE, an inline search box appears showing you
matches on security id, symbol, and name.
Parameters
'symbol1': The string symbol of a security.
Returns
A security object.
ssyymmbboollss((''ssyymmbbooll11'',, ''ssyymmbbooll22'',, ......))Convenience method to initialize several securities by their symbol.
Parameters
'symbol1', 'symbol2', ...: Several string symbols.
Returns
A list of security objects.
Event properties and transformsQuantopian's backend parses all the referenced securities in your algorithm and sends you the trading events for
those securities. Each time any of the securities has a trading event (at most once per trading minute),
handle_data is called and the data object contains all the market data for your securities.
For example, to access the market event data for AAPL, use data[sid(24)] . The following properties are
supported:
ddaatteettiimmeeDateTime: The UTC timestamp of the market event.
pprriicceeFloat: The closing price of the security for the given bar.
ooppeenn__pprriicceeFloat: The opening price of the security for the given bar.
cclloossee__pprriicceeFloat: The closing price of the security for the given bar. Identical to price.
hhiigghhFloat: The highest price of the security within the given bar.
Quantopian Help https://www.quantopian.com/help#api-doco
46 of 79 04/09/14 19:44
lloowwFloat: The lowest price of the security within the given bar.
vvoolluummeeInteger: The whole number of shares traded in the most recent market event for this security.
We also provide some transforms:
mmaavvgg((ddaayyss))Moving average price for the given security for the given number of trailing days.
rreettuurrnnss(())The returns of this security since the end of the previous trading day.
ssttddddeevv((ddaayyss))Standard deviation of the price of the given security for the given number of trailing days, calculated using Bessel's
Correction.
vvwwaapp((ddaayyss))Volume-weighted average price for the given security for the given number of trailing days.
Order objectIf you have a reference to an order object, there are several properties that might be useful:
ccrreeaatteeddDatetime: The date and time the order was created, in UTC timezone.
ssttooppFloat: Optional stop price.
lliimmiittFloat: Optional limit price.
aammoouunnttInteger: Total shares ordered.
Quantopian Help https://www.quantopian.com/help#api-doco
47 of 79 04/09/14 19:44
ssiiddSecurity object: The security being ordered.
ffiilllleeddInteger: Total shares bought or sold for this order so far.
ssttoopp__rreeaacchheeddBoolean: Variable if stop price has been reached.
lliimmiitt__rreeaacchheeddBoolean: Variable if limit price has been reached.
CCoommmmiissssiioonnInteger: Commission for transaction.
Portfolio objectThe portfolio object is accessed using context.portfolio and has the following properties:
ccaappiittaall__uusseeddFloat: The net capital consumed (positive means spent) by buying and selling securities up to this point.
ccaasshhFloat: The current amount of cash in your portfolio.
ppnnllFloat: Dollar value profit and loss, for both realized and unrealized gains.
ppoossiittiioonnssDictionary: A dictionary of all the open positions, keyed by security ID. More information about each position
object can be found in the next section.
ppoorrttffoolliioo__vvaalluueeFloat: Sum value of all open positions and ending cash balance.
ppoossiittiioonnss__vvaalluuee
Quantopian Help https://www.quantopian.com/help#api-doco
48 of 79 04/09/14 19:44
Float: Sum value of all open positions.
rreettuurrnnssFloat: Cumulative percentage returns for the entire portfolio up to this point. Calculated as a fraction of the
starting value of the portfolio. The returns calculation includes cash and portfolio value. The number is not
formatted as a percentage, so a 10% return is formatted as 0.1.
ssttaarrttiinngg__ccaasshhFloat: Initial capital base for this backtest or live execution.
ssttaarrtt__ddaatteeDateTime: UTC datetime of the beginning of this backtest's period. For live trading, this marks the UTC datetime
that this algorithm started executing.
Position objectThe position object represents a current open position, and is contained inside the positions dictionary. For
example, if you had an open AAPL position, you'd access it using context.portfolio.positions[sid(24)]The position object has the following properties:
aammoouunnttInteger: Whole number of shares in this position.
ccoosstt__bbaassiissFloat: The volume-weighted average price paid (price and commission) per share in this position.
llaasstt__ssaallee__pprriicceeFloat: Price at last sale of this security. This is identical to close_price and price .
ssiiddInteger: The ID of the security.
Security objectIf you have a reference to a security object, there are several properties that might be useful:
ssiidd
Quantopian Help https://www.quantopian.com/help#api-doco
49 of 79 04/09/14 19:44
Integer: The id of this security.
ssyymmbboollString: The ticker symbol of this security.
sseeccuurriittyy__nnaammeeString: The full name of this security.
sseeccuurriittyy__ssttaarrtt__ddaatteeDatetime: The date when this security first started trading.
sseeccuurriittyy__eenndd__ddaatteeDatetime: The date when this security stopped trading (= today for securities that are trading normally).
TA-Lib methodsTA-Lib is an open-source library (http://www.ta-lib.org) to process financial data. The methods are available to use in
the Quantopian API and you can see here for the full list of available functions (http://ta-lib.org/function.html).
When using TA-Lib methods, import the library at the beginning of your algorithm.
You can look at the examples of commonly-used TA-Lib functions and at our sample TA-Lib algorithm.
Daily vs minute dataThe talib library respects the data frequency in your backtest or live algorithm. If you're passing daily data into the
backtest, then all the time periods are calculated in days. If you're backtesting or live trading with minute data, all the
time periods are in minutes.
Moving average typesSome of the TA-Lib methods have an integer matype parameter. Here's the list of moving average types:
Quantopian Help https://www.quantopian.com/help#api-doco
50 of 79 04/09/14 19:44
0: SMA (simple)1: EMA (exponential)2: WMA (weighted)3: DEMA (double exponential)4: TEMA (triple exponential)5: TRIMA (triangular) 6: KAMA (Kaufman adaptive)7: MAMA (Mesa adaptive)8: T3 (triple exponential T3)
Examples of Commonly Used Talib FunctionsATR
Bollinger Bands
MACD
RSI
STOCH
ATR
This algorithm uses ATR as a momentum strategy to identify price breakouts.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
51 of 79 04/09/14 19:44
# This strategy was taken from http://www.investopedia.com/articles/trading/08/atr.asp
# The idea is to use ATR to identify breakouts, if the price goes higher than# the previous close + ATR, a price breakout has occured. The position is closed when# the price goes 1 ATR below the previous close.
# This algorithm uses ATR as a momentum strategy, but the same signal can be used for # a reversion strategy, since ATR doesn't indicate the price direction.
# Because this algorithm uses the history function, it will only run in minute mode. # entry and exit points for trading the SPY.
import talibimport numpy as npimport pandas as pd
# Setup our variablesdef initialize(context): context.stock = symbol('SPY') # Create a variable to track the date change context.date = None # Algorithm will only take long positions. # It will stop if encounters a short position. set_long_only()
def handle_data(context, data): # We will constrain the trading to once per day at market open in this example. todays_date = get_datetime().date() # Do nothing unless the date has changed and it's a new day. if todays_date == context.date: return # Set the new date context.date = todays_date # Track our position current_position = context.portfolio.positions[context.stock].amount record(position_size=current_position) # Load historical data for the stocks high = history(30, '1d', 'high') low = history(30, '1d', 'low') close = history(30, '1d', 'close_price') # Calculate the ATR for the stock atr = talib.ATR(high[context.stock], low[context.stock],
Quantopian Help https://www.quantopian.com/help#api-doco
52 of 79 04/09/14 19:44
close[context.stock], timeperiod=14)[-1] price=data[context.stock].price # Use the close price from 2 days ago because we trade at market open prev_close = close.iloc[-3][context.stock] # An upside breakout occurs when the price goes 1 ATR above the previous close upside_signal = price - (prev_close + atr) # A downside breakout occurs when the previous close is 1 ATR above the price downside_signal = prev_close - (price + atr) # Enter position when an upside breakout occurs. Invest our entire portfolio to go long. if upside_signal > 0 and current_position <= 0: order_target_percent(context.stock, 1.0) # Exit position if a downside breakout occurs elif downside_signal > 0 and current_position >= 0: order_target_percent(context.stock, 0.0) record(upside_signal=upside_signal, downside_signal=downside_signal, ATR=atr)
Bollinger Bands
This example uses the talib Bollinger Bands function to determine entry points for long and short positions. When
the the price breaks out of the upper Bollinger band, a short position is opened. A long position is created when the
price dips below the lower band.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
53 of 79 04/09/14 19:44
# This algorithm uses the talib Bollinger Bands function to determine entry entry # points for long and short positions.
# When the the price breaks out of the upper Bollinger band, a short position# is opened. A long position is opened when the price dips below the lower band.
# Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example.
import talibimport numpy as npimport pandas as pd
# Setup our variablesdef initialize(context): context.stock = symbol('SPY') # Create a variable to track the date change context.date = None
def handle_data(context, data): todays_date = get_datetime().date() # Do nothing unless the date has changed if todays_date == context.date: return # Set the new date context.date = todays_date
current_position = context.portfolio.positions[context.stock].amount price=data[context.stock].price # Load historical data for the stocks prices = history(15, '1d', 'price') upper, middle, lower = talib.BBANDS( prices[context.stock], timeperiod=10, # number of non-biased standard deviations from the mean nbdevup=2, nbdevdn=2, # Moving average type: simple moving average here matype=0) # If price is below the recent lower band and we have # no long positions then invest the entire # portfolio value into SPY if price <= lower[-1] and current_position <= 0: order_target_percent(context.stock, 1.0) # If price is above the recent upper band and we have # no short positions then invest the entire # portfolio value to short SPY elif price >= upper[-1] and current_position >= 0:
Quantopian Help https://www.quantopian.com/help#api-doco
54 of 79 04/09/14 19:44
order_target_percent(context.stock, -1.0) record(upper=upper[-1], lower=lower[-1], mean=middle[-1], price=price, position_size=current_position)
MACD
In this example, when the MACD signal less than 0, the stock price is trending down and it's time to sell the security.
When the MACD signal greater than 0, the stock price is trending up it's time to buy.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
55 of 79 04/09/14 19:44
# This example algorithm uses the Moving Average Crossover Divergence (MACD) indicator as a buy/sell
# When the MACD signal less than 0, the stock price is trending down and it's time to sell.# When the MACD signal greater than 0, the stock price is trending up it's time to buy.
# Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example.
import talibimport numpy as npimport pandas as pd
# Setup our variablesdef initialize(context): context.stocks = symbols('MMM', 'SPY', 'GOOG_L', 'PG', 'DIA') context.pct_per_stock = 1.0 / len(context.stocks) # Create a variable to track the date change context.date = None
def handle_data(context, data): todays_date = get_datetime().date() # Do nothing unless the date has changed and its a new day. if todays_date == context.date: return # Set the new date context.date = todays_date # Load historical data for the stocks prices = history(40, '1d', 'price') # Create the MACD signal and pass in the three parameters: fast period, slow period, and the sig # This is a series that is indexed by sids. (??) macd = prices.apply(MACD, fastperiod=12, slowperiod=26, signalperiod=9) # Iterate over the list of stocks for stock in context.stocks: current_position = context.portfolio.positions[stock].amount # Close position for the stock when the MACD signal is negative and we own shares. if macd[stock] < 0 and current_position > 0: order_target(stock, 0) # Enter the position for the stock when the MACD signal is positive and # our portfolio shares are 0. elif macd[stock] > 0 and current_position == 0: order_target_percent(stock, context.pct_per_stock) record(goog=macd[symbol('GOOG_L')], spy=macd[symbol('SPY')], mmm=macd[symbol('MMM')])
Quantopian Help https://www.quantopian.com/help#api-doco
56 of 79 04/09/14 19:44
# Define the MACD function def MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9): ''' Function to return the difference between the most recent MACD value and MACD signal. Positive values are long position entry signals
optional args: fastperiod = 12 slowperiod = 26 signalperiod = 9
Returns: macd - signal ''' macd, signal, hist = talib.MACD(prices, fastperiod=fastperiod, slowperiod=slowperiod, signalperiod=signalperiod) return macd[-1] - signal[-1]
RSI
Use the RSI signal to drive algorithm buying and selling decisions. When the RSI is over 70, a stock can be seen as
overbought and it's time to sell. On the other hand, when the RSI is below 30, a stock can be seen as underbought
and it's time to buy.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
57 of 79 04/09/14 19:44
# This example algorithm uses the Relative Strength Index indicator as a buy/sell signal.# When the RSI is over 70, a stock can be seen as overbought and it's time to sell.# When the RSI is below 30, a stock can be seen as oversold and it's time to buy.
# Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example.
import talib
# Setup our variablesdef initialize(context): context.stocks = symbols('MMM', 'SPY', 'GE') context.max_cash_per_stock = 100000.0 / len(context.stocks) context.LOW_RSI = 30 context.HIGH_RSI = 70 # Create a variable to track the date change context.date = None
def handle_data(context, data): todays_date = get_datetime().date() # Do nothing unless the date has changed if todays_date == context.date: return # Set the new date context.date = todays_date
cash = context.portfolio.cash # Load historical data for the stocks prices = history(15, '1d', 'price') # Use pandas dataframe.apply to get the last RSI value # for for each stock in our basket rsi = prices.apply(talib.RSI, timeperiod=14).iloc[-1] # Loop through our list of stocks for stock in context.stocks: current_position = context.portfolio.positions[stock].amount # RSI is above 70 and we own shares, time to sell if rsi[stock] > context.HIGH_RSI and current_position > 0: order_target(stock, 0) log.info('{0}: RSI is at {1}, selling {2} shares'.format( stock.symbol, rsi[stock], current_position )) # RSI is below 30 and we don't have any shares, time to buy elif rsi[stock] < context.LOW_RSI and current_position == 0: # Use floor division to get a whole number of shares target_shares = cash // data[stock].price order_target(stock, target_shares)
Quantopian Help https://www.quantopian.com/help#api-doco
58 of 79 04/09/14 19:44
log.info('{0}: RSI is at {1}, buying {2} shares.'.format( stock.symbol, rsi[stock], target_shares ))
# record the current RSI values of each stock record(ge_rsi=rsi[symbol('GE')], spy_rsi=rsi[symbol('SPY')], mmm_rsi=rsi[symbol('MMM')])
STOCH
Below is an algorithm that uses the talib STOCH function. When the stochastic oscillator dips below 10, the stock is
determined to be oversold and a long position is opened. The position is exited when the indicator rises above 90
because the stock is considered to be overbought.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
59 of 79 04/09/14 19:44
# This algorithm uses talib's STOCH function to determine entry and exit points.
# When the stochastic oscillator dips below 10, the stock is determined to be oversold# and a long position is opened. The position is exited when the indicator rises above 90# because the stock is thought to be overbought.
# Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example.
import talibimport numpy as npimport pandas as pd
# Setup our variablesdef initialize(context): context.stocks = symbols('SPY', 'AAPL', 'GLD', 'AMZN') # Set the percent of the account to be invested per stock context.long_pct_per_stock = 1.0 / len(context.stocks) # Create a variable to track the date change context.date = None
def handle_data(context, data): todays_date = get_datetime().date() # Do nothing unless the date has changed if todays_date == context.date: return # Set the new date context.date = todays_date # Load historical data for the stocks high = history(30, '1d', 'high') low = history(30, '1d', 'low') close = history(30, '1d', 'close_price') # Iterate over our list of stocks for stock in context.stocks: current_position = context.portfolio.positions[stock].amount slowk, slowd = talib.STOCH(high[stock], low[stock], close[stock], fastk_period=5, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
# get the most recent value slowk = slowk[-1] slowd = slowd[-1] # If either the slowk or slowd are less than 10, the stock is
Quantopian Help https://www.quantopian.com/help#api-doco
60 of 79 04/09/14 19:44
Cause: Request took too long to execute. Possible messages include HandleDataTimeoutException,InitializeTimoutException, TimeoutException, ExecutionTimeout.
Solution: Optimize your code to use faster functions. For example, use history() instead ofbatch_transform(). And include Pandas rolling transformations(https://www.quantopian.com/posts/working-with-history-dataframes) instead of thesimple transformations.
Use a smaller universe of stocks. Every security you include in the algorithm increasesmemory and CPU requirements of the algorithm.
Use fewer securities.
Backtest over a small time period.
Cause: Incorrect use of a dictionary. Mapping key is not found in the set of existing keys.
Solution: Check for the existence of a key within the dictionary before accessing it. For example:
for sid in data: data[sid].price
Cause: Illegal matrix operation. For example, trying to take the determinant of a singular matrix.
Solution: Update matrix values to perform desired operation.
Cause: Backtest ran out of memory on server.
Solution: Avoid creating too many objects or unusally large objects in your algorithm. An object canbe a variable, function, or data structure.
# 'oversold,' a long position is opened if there are no shares # in the portfolio. if slowk < 10 or slowd < 10 and current_position <= 0: order_target_percent(stock, context.long_pct_per_stock) # If either the slowk or slowd are larger than 90, the stock is # 'overbought' and the position is closed. elif slowk > 90 or slowd > 90 and current_position >= 0: order_target(stock, 0)
Common Error MessagesBelow are examples and sample solutions to commonly-encountered errors while coding your strategy. If you need
assistance, contact us and we can help you debug the algorithm.
EExxeeccuuttiioonnTTiimmeeoouutt
KKeeyyEErrrroorr
LLiinnAAllggEErrrroorr
MMeemmoorryyEErrrroorr
Quantopian Help https://www.quantopian.com/help#api-doco
61 of 79 04/09/14 19:44
Use Pandas rolling transformations (https://www.quantopian.com/posts/working-with-history-dataframes) instead of the simple transformations.
Use a smaller universe of stocks. Every security you include in the algorithm increasesmemory and CPU requirements of the algorithm.
Avoid accumulating an increasing amount of data in each bar of the backtest.
Backtest over a shorter time period.
Cause: Unknown error.
Solution: Please contact feedback so we can help debug your code.
Cause: Invald Python or Quantopian syntax. May be due to missing colon, bracket, parenthesis, orquote.
Solution: Insert or remove extra notation in function.
Cause: Raised when encountering an object that is not of the expected type.
Solution: Insert or remove extra notation in function.
Cause: Raised when a function receives an argument that has the right type but an inappropriatevalue. For example, if you try to take the square root of a negative number.
Solution: Perform legal operations on expected values.
SSoommeetthhiinngg wweenntt wwrroonngg oonn oouurr eenndd.. SSoorrrryy ffoorr tthhee iinnccoonnvveenniieennccee..
SSyynnttaaxxEErrrroorr
TTyyppeeEErrrroorr
VVaalluueeEErrrroorr
IDE Tips and ShortcutsYou can customize the color and text size of the IDE for your algorithms. Simply click on the gear button in the top
right corner and choose your settings.
Quantopian Help https://www.quantopian.com/help#api-doco
62 of 79 04/09/14 19:44
Below are keyboard shortcuts you can use in the IDE.
Action Windows Keystroke Apple Keystroke
Build Algorithm Ctrl + B Cmd + B
Indent Ctrl + ] Cmd + ]
Outdent Ctrl + [ Cmd + [
Create/Remove Comment Ctrl + / Cmd + /
Search Code Ctrl + F Cmd + F
Undo Ctrl + Z Cmd + Z
Redo Ctrl + Y Cmd + Y
Need more space to see your code? Drag the bar all the way to the right to expand the code window.
If you are building a custom graph you can record up to five variables. To view only certain variables, click on the
variable name to remove it from the chart. Click it again to add it back to the graph. Want to zoom in on a certain
timeperiod? Use the bar underneath the graph to select a specific time window.
Below is an example of a custom graph with all the variables selected. The following graph displays only the
recorded cash value.
ZiplineZipline is our open-sourced (http://zipline.io) engine that powers the backtester in the IDE. You can see the code
Quantopian Help https://www.quantopian.com/help#api-doco
63 of 79 04/09/14 19:44
repository (https://github.com/quantopian/zipline) in Github and contribute pull requests to the project. There is a
Google group (https://groups.google.com/forum/#!forum/zipline) available for seeking help and facilitating
discussions. For other questions, please contact [email protected]
(mailto:[email protected]).
You can use Zipline to develop your strategy offline and then port to Quantopian for paper trading and live trading.
To easily convert between code in Zipline and Quantopian environments, use the get_environment method. To
run a section of code only in Zipline, use if get_environment() == 'zipline': . Alternatively, to run code
only in Quantopian, if get_environment() == 'zipline': . To execute these function, you need to import
get_environment from the zipline_api library.
Important: Python modules, packages, and related code that cannot be used in the Quantopian IDE must be
commented out. In the future, we hope to automatically do this on your behalf.
Here is a basic example:
from zipline_api import get_environment
def initialize(context): pass
def handle_data(context, data): if get_environment() == 'zipline': # Code that will only execute in Zipline environment print 'This section runs through Zipline'
elif get_environment() == 'quantopian': # Code that will only execute in Quantopian IDE print 'This section runs through Quantopian'
else: print 'This section runs elsewhere'
Sample AlgorithmsOur first example is very basic. If you want to get started quickly, you can just copy this code into your algorithm and
run a backtest. Other examples get into more advanced concepts.
Basic AlgorithmThis example demos all of the basic concepts you need to write a simple algorithm that tries to capitalize on a stock's
momentum. This shows you the security id (sid) function, logging, volume-weighted average price, price, and placing
orders. You can clone this algorithm from the community discussion of this post (https://www.quantopian.com
/posts/discuss-the-sample-algorithm-1).
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
64 of 79 04/09/14 19:44
# For this example, we're going to write a simple momentum script. # When the stock goes up quickly, we're going to buy; when it goes down we're going to sell. # Hopefully we'll ride the waves.
# To run an algorithm in Quantopian, you need two functions: initialize and handle_data.
def initialize(context): # The initialize function sets any data or variables that you'll use in your algorithm. # For instance, you'll want to define the security (or securities) you want to backtest. # You'll also want to define any parameters or values you're going to use later. # It's only called once at the beginning of your algorithm. # In our example, we're looking at Apple. If you re-type this line # yourself, you'll see the auto-complete that is available for security.
context.security = symbol('AAPL')
# The handle_data function is where the real work is done. # This function is run either every minute (in live trading and minutely backtesting mode) # or every day (in daily backtesting mode).def handle_data(context, data): # We've built a handful of useful data transforms for you to use. In this # line, we're computing the volume-weighted-average-price of the security # defined above, in the context.security variable. # To make market decisions, we will need to know the stock's average price for the last 5 days, # and the stock's current price. average_price = data[context.security].mavg(5) current_price = data[context.security].price # Another powerful built-in feature of the Quantopian backtester is the # portfolio object. The portfolio object tracks your positions, cash, # cost basis of specific holdings, and more. In this line, we calculate # the current amount of cash in our portfolio. cash = context.portfolio.cash # Here is the meat of our algorithm. # If the current price is 1% above the 5-day average price AND we have enough cash, then we will # If the current price is below the average price, then we want to close our position to 0 share if current_price > 1.01*average_price and cash > current_price: # Need to calculate how many shares we can buy number_of_shares = int(cash/current_price) # Place the buy order (positive means buy, negative means sell) order(context.security, +number_of_shares) log.info("Buying %s" % (context.security.symbol))
elif current_price < average_price:
Quantopian Help https://www.quantopian.com/help#api-doco
65 of 79 04/09/14 19:44
# Sell all of our shares by setting the target position to zero order_target(context.security, 0) log.info("Selling %s" % (context.security.symbol)) # You can use the record() method to track any custom signal. The record graph # will track up to five different variables. Here we record the Apple stock price. record(stock_price=data[context.security].price)
Multiple Security ExampleThis example shows how to initialize and trade with multiple securities. It imports the datetime and pytz libraries
and uses the log function to understand the behavior. You can clone this algorithm from the community discussion
of this example (https://www.quantopian.com/posts/updated-multi-sid-example-algorithm-1).
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
66 of 79 04/09/14 19:44
# This example runs the same momentum play as the first sample # (https://www.quantopian.com/help#sample-basic), but this time it uses more# securities during the backtest. # Important note: All securities in an algorithm must be traded for the # entire length of the backtest. For instance, if you try to backtest both# Google and Facebook against 2011 data you will get an error; Facebook# wasn't traded until 2012.
# First step is importing any needed libraries.
import datetimeimport pytz
def initialize(context): # Here we initialize each stock. Note that we're not storing integers; by # calling sid(24) we're storing the Security object. context.stocks = [sid(24), sid(2), sid(2673), sid(5061)] context.vwap = {} context.price = {} # Setting our maximum position size, like previous example context.max_notional = 1000000.1 context.min_notional = -1000000.0
# Initializing the time variables we use for logging # Convert timezone to US EST to avoid confusion est = pytz.timezone('EST') context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=est)
def handle_data(context, data): # Initializing the position as zero at the start of each frame notional=0 # This runs through each stock. It computes # our position at the start of each frame. for stock in context.stocks: price = data[stock].price notional = notional + context.portfolio.positions[stock].amount * price tradeday = data[stock].datetime # This runs through each stock again. It finds the price and calculates # the volume-weighted average price. If the price is moving quickly, and # we have not exceeded our position limits, it executes the order and # updates our position. for stock in context.stocks: vwap = data[stock].vwap(3) price = data[stock].price
if price < vwap * 0.995 and notional > context.min_notional: order(stock,-100) notional = notional - price*100 elif price > vwap * 1.005 and notional < context.max_notional:
Quantopian Help https://www.quantopian.com/help#api-doco
67 of 79 04/09/14 19:44
order(stock,+100) notional = notional + price*100
# If this is the first trade of the day, it logs the notional. if (context.d + datetime.timedelta(days=1)) < tradeday: log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y' context.d = tradeday
Set Universe ExampleThis example shows how to use the set_universe command. Here the stocks are chosen by selecting a segment of
the dollar volume universe.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
68 of 79 04/09/14 19:44
# This is the standard Quantopian function that initializes your data and # variables. In it, we define how large of a 'bet' we're making (in dollars) and what # stock we're working with.def initialize(context): # we want to try this on a range of highly liquid stocks set_universe(universe.DollarVolumeUniverse(98, 99)) context.bet_amount = 1000 context.count = 20
# This is the standard Quantopian event handling function. This function is # run once for each bar of data. In this example, it the min and max # prices for the trailing window. If the price exceeds the recent high, it # goes short; if the price dips below the recent low, it goes long. The algo# is a contrarian/mean reversion bet.def handle_data(context, data): # Until our batch transform's datapanel is full, it will return None. Once # the datapanel is full, then we have a max and min to work with. prices = history(bar_count=10, frequency='1d', field='price') ranking = sort_returns(prices)
if ranking is not None: column_name = ranking.columns[0] # bottom quantile to go long bottom = ranking[-1*context.count:] longs = bottom[bottom[column_name] < 0] # top quantile to go short top = ranking[:context.count] shorts = top[top[column_name] > 0] for stock in data.keys(): if stock in longs.index: amount = calculate_order_amount(context, stock, 1, data[stock].price elif stock in shorts.index: amount = calculate_order_amount(context, stock, -1, data[stock].price else: amount = calculate_order_amount(context, stock, 0, data[stock].price order(stock, amount) # This method is purely for order managment. It calculates and returns an # order amount to place binary bets.# If signal_val is -1, get to a short position of -1 * context.bet_size# If signal_val is 1, get to a long position of context.bet_sizedef calculate_order_amount(context, stock, signal_val, cur_price): current_amount = context.portfolio.positions[stock].amount abs_order_amount = int(context.bet_amount / cur_price) if signal_val == -1: return (-1 * abs_order_amount) - current_amount elif signal_val == 1: return abs_order_amount - current_amount
Quantopian Help https://www.quantopian.com/help#api-doco
69 of 79 04/09/14 19:44
elif signal_val == 0: return -1 * current_amount else: return 0
def sort_returns(prices): shifted_prices = prices.shift(19) returns = (prices - shifted_prices) / shifted_prices # use a slice operator to get the most recent returns last_returns = returns[-1:] last_date = last_returns.index[-1] sorted_returns = last_returns.T.sort(columns=last_date, ascending=0) return sorted_returns
Record Variables ExampleThis example compares Google's 20-day moving average with its 80-day moving average and trades when the two
averages cross. It records both moving averages as well as Google's share price.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
70 of 79 04/09/14 19:44
# This initialize function sets any data or variables that you'll use in# your algorithm.def initialize(context): context.stock = sid(698) # Boeing
# Now we get into the meat of the algorithm. def handle_data(context, data): # Create a variable for the price of the Google stock context.price = data[context.stock].price # Create variables to track the short and long moving averages. # The short moving average tracks over 20 days and the long moving average # tracks over 80 days. short = data[context.stock].mavg(20) long = data[context.stock].mavg(80)
# If the short moving average is higher than the long moving average, then # we want our portfolio to hold 500 stocks of Google if (short > long): order_target(context.stock, +500) # If the short moving average is lower than the long moving average, then # then we want to sell all of our Google stocks and own 0 shares # in the portfolio. elif (short < long): order_target_value(context.stock, 0)
# Record our variables to see the algo behavior. You can record up to # 5 custom variables. To see only a certain variable, deselect the # variable name in the custom graph in the backtest. record(short_mavg = short, long_mavg = long, goog_price = context.price)
Fetcher ExampleThis example loads and formats two price series from Quandl. It displays the price movements and decides to buy
and sell Tiffany stock based on the price of gold.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
71 of 79 04/09/14 19:44
import pandas
def rename_col(df): df = df.rename(columns={'New York 15:00': 'price'}) df = df.rename(columns={'Value': 'price'}) df = df.fillna(method='ffill') df = df[['price', 'sid']] # Correct look-ahead bias in mapping data to times df = df.tshift(1, freq='b') log.info(' \n %s ' % df.head()) return df def initialize(context): # import the external data fetch_csv('http://www.quandl.com/api/v1/datasets/JOHNMATT/PALL.csv?trim_start=2012-01-01' date_column='Date', symbol='palladium', post_func=rename_col, date_format='%Y-%m-%d')
fetch_csv('http://www.quandl.com/api/v1/datasets/BUNDESBANK/BBK01_WT5511.csv?trim_start=2012-01- date_column='Date', symbol='gold', post_func=rename_col, date_format='%Y-%m-%d') # Tiffany context.stock = sid(7447)
def handle_data(context, data): # Invest 10% of the portfolio in Tiffany stock when the price of gold is low. # Decrease the Tiffany position to 5% of portfolio when the price of gold is high.
if (data['gold'].price < 1600): order_target_percent(context.stock, 0.10) if (data['gold'].price < 1750): order_target_percent(context.stock, 0.05)
#record the variables if 'price' in data['palladium']: record(palladium=data['palladium'].price, gold=data['gold'].price)
Custom Slippage ExampleThis example implements the fixed slippage model, but with the additional flexibility to specify a different spread for
each stock.
! Clone Algorithm
Quantopian Help https://www.quantopian.com/help#api-doco
72 of 79 04/09/14 19:44
# Our custom slippage modelclass PerStockSpreadSlippage(slippage.SlippageModel):
# We specify the constructor so that we can pass state to this class, but this is optional. def __init__(self, spreads): # Store a dictionary of spreads, keyed by sid. self.spreads = spreads
def process_order(self, trade_bar, order): spread = self.spreads[order.sid] # In this model, the slippage is going to be half of the spread for # the particular stock slip_amount = spread / 2 # Compute the price impact of the transaction. Size of price impact is # proprotional to order size. # A buy will increase the price, a sell will decrease it. new_price = trade_bar.price + (slip_amount * order.direction)
log.info('executing order ' + str(trade_bar.sid) + ' stock bar price: ' + \ str(trade_bar.price) + ' and trade executes at: ' + str(new_price))
# Create the transaction using the new price we've calculated. return slippage.create_transaction( trade_bar, order, new_price, order.amount )
def initialize(context): # Provide the bid-ask spread for each of the securities in the universe. spreads = { sid(24): 0.05, sid(3766): 0.08 } # Initialize slippage settings given the parameters of our model set_slippage(PerStockSpreadSlippage(spreads))
def handle_data(context, data): # We want to own 100 shares of each stock in our universe for stock in data: order_target(stock, 100) log.info('placing market order for ' + str(stock.symbol) + ' at price ' + str(data[stock].price))
TA-Lib ExampleThis example uses TA-Lib's RSI method. For more examples of commonly-used TA-Lib functions, click here.
Quantopian Help https://www.quantopian.com/help#api-doco
73 of 79 04/09/14 19:44
! Clone Algorithm
# This example algorithm uses the Relative Strength Index indicator as a buy/sell signal.# When the RSI is over 70, a stock can be seen as overbought and it's time to sell.# When the RSI is below 30, a stock can be seen as oversold and it's time to buy.
# Because this algorithm uses the history function, it will only run in minute mode. # We will constrain the trading to once per day at market open in this example.
import talibimport numpy as npimport math
# Setup our variablesdef initialize(context): context.max_notional = 100000 context.intc = symbol('INTC') # Intel context.LOW_RSI = 30 context.HIGH_RSI = 70
def handle_data(context, data): #Get a trailing window of data prices = history(15, '1d', 'price') # Use pandas dataframe.apply to get the last RSI value # for for each stock in our basket rsi_data = prices.apply(talib.RSI, timeperiod=14).iloc[-1]
intc_rsi = rsi_data[context.intc]
# check how many shares of Intel we currently own current_intel_shares = context.portfolio.positions[context.intc].amount
# until 14 time periods have gone by, the rsi value will be numpy.nan # RSI is above 70 and we own GOOG, time to close the position. if intc_rsi > context.HIGH_RSI and current_intel_shares > 0: order_target(context.intc, 0) log.info('RSI is at ' + str(intc_rsi) + ', selling ' + str(current_intel_shares # RSI is below 30 and we don't have any Intel stock, time to buy. elif intc_rsi < context.LOW_RSI and current_intel_shares == 0: num_shares = math.floor(context.max_notional / data[context.intc].close_price order(context.intc, num_shares) log.info('RSI is at ' + str(intc_rsi) + ', buying ' + str(num_shares) + ' shares'
# record the current RSI value and the current price of INTC. record(intcRSI=intc_rsi, intcPRICE=data[context.intc].close_price)
Portfolio Allocation ExampleThis example uses order_target_percent order method.
Quantopian Help https://www.quantopian.com/help#api-doco
74 of 79 04/09/14 19:44
! Clone Algorithm
def initialize(context): context.stocks = [sid(1419),sid(12652)]
def handle_data(context, data): # This will order as many shares as needed to # achieve the desired portfolio allocation. # In our case, we end up with 20% allocation for # one stock and 80% allocation for the other stock. order_target_percent(context.stocks[0], .2) order_target_percent(context.stocks[1], .8)
# Plot portfolio allocations pv = float(context.portfolio.portfolio_value) portfolio_allocations = [] for stock in context.stocks: pos = context.portfolio.positions[stock] portfolio_allocations.append( pos.last_sale_price * pos.amount / pv * 100 )
record(perc_stock_0=portfolio_allocations[0], perc_stock_1=portfolio_allocations[1])
History ExampleThis example uses the history function.
! Clone Algorithm
# Standard Deviation Using History# Use history() to calculate the standard deviation of the days' closing# prices of the last 10 trading days, including price at the time of # calculation.def initialize(context): # this example works on Apple's data context.aapl = sid(24)
def handle_data(context, data): # use history to pull the last 10 days of price price_history = history(bar_count=10, frequency='1d', field='price') # calculate the standard deviation using std() std = price_history.std() # record the standard deviation as a custom signal record(std=std[context.aapl])
Batch Transform ExampleThis example shows how to create and use a batch transform. If the price hits the high in the trailing window, it sells;
if it hits the low in the trailing window, it buys.
Quantopian Help https://www.quantopian.com/help#api-doco
75 of 79 04/09/14 19:44
! Clone AlgorithmQuantopian Help https://www.quantopian.com/help#api-doco
76 of 79 04/09/14 19:44
# This is the standard Quantopian function that initializes your data and # variables. In it, we define how large of a 'bet' we're making (in dollars) and what # stock we're working with.def initialize(context): # we will be trading in AMZN and WMT shares context.stocks = [sid(16841), sid(8229)] context.bet_amount = 100000 context.long = 0
# This is the standard Quantopian event handling function. This function is # run once for each bar of data. In this example, it the min and max # prices for the trailing window. If the price exceeds the recent high, it # goes short; if the price dips below the recent low, it goes long. The algo# is a contrarian/mean reversion bet.def handle_data(context, data): # Until our batch transform's datapanel is full, it will return None. Once # the datapanel is full, then we have a max and min to work with. rval = minmax(data) if rval is None: return maximums, minimums = rval for stock in context.stocks: cur_max = maximums[stock] cur_min = minimums[stock] cur_price = data[stock].price cur_position = context.portfolio.positions[stock] order_direction = calculate_direction(stock, cur_min, cur_max, cur_price, cur_position order_amount = calculate_order_amount(context, stock, order_direction, cur_price # Optional: uncomment the log line below if you're looking for more detail about what's # going on. It will log all the information that is a 'moving part' of this # algorithm. Note: if you're doing a full backtest it's a lot of log lines! logmsg = '\n{s}: max {m} min {i} price {p} position amount {l}\nordering {n} shares' log.info(logmsg.format( s=stock, m=cur_max, i=cur_min, p=cur_price, l=cur_position.amount, n=order_amount )) order(stock, order_amount) # Here we do our test to see if we should buy or sell or do nothing. This is# the main part of the algorithm. Once we establish a position (long or short)# we use the context.long variable to remember which we took.def calculate_direction(stock, cur_min, cur_max, cur_price, cur_position): if cur_max is not None and cur_position.amount <= 0 and cur_price >= cur_max: return -1
Quantopian Help https://www.quantopian.com/help#api-doco
77 of 79 04/09/14 19:44
elif cur_min is not None and cur_position.amount >= 0 and cur_price <= cur_min: return 1 return 0 # This method is purely for order management. It calculates and returns an # order amount to place binary bets.# If signal_val is -1, get to a short position of -1 * context.bet_size# If signal_val is 1, get to a long position of context.bet_sizedef calculate_order_amount(context, stock, signal_val, cur_price): current_amount = context.portfolio.positions[stock].amount abs_order_amount = int(context.bet_amount / cur_price) if signal_val == -1: return (-1 * abs_order_amount) - current_amount elif signal_val == 1: return abs_order_amount - current_amount else: return 0 # This is our batch transform decorator/declaration. We set the # refresh_period and length of the window. In this case, once per day we're loading # the last 10 trading days and evaluating them.@batch_transform(refresh_period=1, window_length=10)def minmax(datapanel): # We are looking for the min and the max price to return. Just because it's interesting # we also are logging the current price. prices_df = datapanel['price'] min_price = prices_df.min() max_price = prices_df.max()
if min_price is not None and max_price is not None: return (max_price, min_price) else: return None
Quantopian Help https://www.quantopian.com/help#api-doco
78 of 79 04/09/14 19:44
COMPANY
About(/about)Careers(/jobs)
NOTICES
Terms of Use(/policies/terms)Privacy Policy(/policies/privacy)Trading Risk(/policies/risks)BrokerageTerms(/policies/live_trading)
SUPPORT
FAQs (/faq)Help & APIdocs (/help)Contact UsSite Status(http://status.quantopian.com)
FOLLOW US
Blog(http://blog.quantopian.com)Twitter(http://www.twitter.com/quantopian)Facebook(http://www.facebook.com/quantopian)LinkedIn(http://www.linkedin.com/company/quantopian)Blog RSS(http://feeds.feedburner.com/QuantopianBlog)
© 2014 Quantopian
(/home)Quantopian Help https://www.quantopian.com/help#api-doco
79 of 79 04/09/14 19:44