Files
catalyst/beginner-tutorial.html
T
Victor Grau Serrat fd3a297cff DOC: fixed missing link
2017-12-12 09:14:03 -07:00

2750 lines
125 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catalyst Beginner Tutorial &mdash; Catalyst 0.3 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="top" title="Catalyst 0.3 documentation" href="index.html"/>
<link rel="next" title="Live Trading" href="live-trading.html"/>
<link rel="prev" title="Install" href="install.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Catalyst
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="install.html">Install</a><ul>
<li class="toctree-l2"><a class="reference internal" href="install.html#installing-with-conda">Installing with <code class="docutils literal"><span class="pre">conda</span></code></a><ul>
<li class="toctree-l3"><a class="reference internal" href="install.html#troubleshooting-conda-install">Troubleshooting <code class="docutils literal"><span class="pre">conda</span></code> Install</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="install.html#installing-with-pip">Installing with <code class="docutils literal"><span class="pre">pip</span></code></a><ul>
<li class="toctree-l3"><a class="reference internal" href="install.html#troubleshooting-pip-install">Troubleshooting <code class="docutils literal"><span class="pre">pip</span></code> Install</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="install.html#gnu-linux-requirements">GNU/Linux Requirements</a><ul>
<li class="toctree-l3"><a class="reference internal" href="install.html#amazon-linux-ami-notes">Amazon Linux AMI Notes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="install.html#macos-requirements">MacOS Requirements</a><ul>
<li class="toctree-l3"><a class="reference internal" href="install.html#macos-virtualenv-matplotlib">MacOS + virtualenv + matplotlib</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="install.html#windows-requirements">Windows Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="install.html#getting-help">Getting Help</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Catalyst Beginner Tutorial</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#basics">Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#my-first-algorithm">My first algorithm</a></li>
<li class="toctree-l2"><a class="reference internal" href="#ingesting-data">Ingesting data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#running-the-algorithm">Running the algorithm</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#command-line-interface">Command line interface</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#access-to-previous-prices-using-history">Access to previous prices using <code class="docutils literal"><span class="pre">history</span></code></a><ul>
<li class="toctree-l3"><a class="reference internal" href="#working-example-dual-moving-average-cross-over">Working example: Dual Moving Average Cross-Over</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#jupyter-notebook">Jupyter Notebook</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#install">Install</a></li>
<li class="toctree-l3"><a class="reference internal" href="#running-algorithms">Running Algorithms</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#next-steps">Next steps</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="live-trading.html">Live Trading</a><ul>
<li class="toctree-l2"><a class="reference internal" href="live-trading.html#supported-exchanges">Supported Exchanges</a></li>
<li class="toctree-l2"><a class="reference internal" href="live-trading.html#authentication">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="live-trading.html#currency-symbols">Currency Symbols</a></li>
<li class="toctree-l2"><a class="reference internal" href="live-trading.html#trading-an-algorithm">Trading an Algorithm</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="features.html">Features</a><ul>
<li class="toctree-l2"><a class="reference internal" href="features.html#current-functionality">Current Functionality</a></li>
<li class="toctree-l2"><a class="reference internal" href="features.html#upcoming-features">Upcoming features</a></li>
<li class="toctree-l2"><a class="reference internal" href="features.html#naming-convention">Naming Convention</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="example-algos.html">Example Algorithms</a><ul>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#overview">Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#buy-btc-simple-algorithm">Buy BTC Simple Algorithm</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#buy-and-hodl-algorithm">Buy and Hodl Algorithm</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#dual-moving-average-crossover">Dual Moving Average Crossover</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#mean-reversion-algorithm">Mean Reversion Algorithm</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#simple-universe">Simple Universe</a></li>
<li class="toctree-l2"><a class="reference internal" href="example-algos.html#portfolio-optimization">Portfolio Optimization</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="utilities.html">Utilities</a><ul>
<li class="toctree-l2"><a class="reference internal" href="utilities.html#output-to-csv-file">Output to CSV file</a></li>
<li class="toctree-l2"><a class="reference internal" href="utilities.html#extracting-market-data">Extracting market data</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="videos.html">Videos</a><ul>
<li class="toctree-l2"><a class="reference internal" href="videos.html#installation-macos">Installation: MacOS</a></li>
<li class="toctree-l2"><a class="reference internal" href="videos.html#installation-windows">Installation: Windows</a></li>
<li class="toctree-l2"><a class="reference internal" href="videos.html#backtesting-a-strategy">Backtesting a Strategy</a></li>
<li class="toctree-l2"><a class="reference internal" href="videos.html#live-trading-a-strategy">Live Trading a Strategy</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="resources.html">Resources</a><ul>
<li class="toctree-l2"><a class="reference internal" href="resources.html#related-3rd-party-apis">Related 3rd Party APIs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="development-guidelines.html">Development Guidelines</a><ul>
<li class="toctree-l2"><a class="reference internal" href="development-guidelines.html#creating-a-development-environment">Creating a Development Environment</a></li>
<li class="toctree-l2"><a class="reference internal" href="development-guidelines.html#git-branching-structure">Git Branching Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="development-guidelines.html#contributing-to-the-docs">Contributing to the Docs</a></li>
<li class="toctree-l2"><a class="reference internal" href="development-guidelines.html#commit-messages">Commit messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="development-guidelines.html#formatting-docstrings">Formatting Docstrings</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="releases.html">Release Notes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-10">Version 0.3.10</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#bug-fixes">Bug Fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-9">Version 0.3.9</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id1">Bug Fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#build">Build</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-8">Version 0.3.8</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id2">Bug Fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-7">Version 0.3.7</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id3">Bug Fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id4">Build</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-6">Version 0.3.6</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id5">Bug Fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-5">Version 0.3.5</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id6">Bug Fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-4">Version 0.3.4</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id7">Bug Fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id8">Build</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#documentation">Documentation</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-3">Version 0.3.3</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id10">Bug Fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id11">Build</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-2">Version 0.3.2</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id12">Bug Fixes</a></li>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id13">Build</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3-1">Version 0.3.1</a><ul>
<li class="toctree-l3"><a class="reference internal" href="releases.html#id14">Bug Fixes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-3">Version 0.3</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-2-dev5">Version 0.2.dev5</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-2-dev4">Version 0.2.dev4</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-2-dev3">Version 0.2.dev3</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-2-dev2">Version 0.2.dev2</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-2-dev1">Version 0.2.dev1</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-1-dev9">Version 0.1.dev9</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-1-dev8">Version 0.1.dev8</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-1-dev7">Version 0.1.dev7</a></li>
<li class="toctree-l2"><a class="reference internal" href="releases.html#version-0-1-dev6">Version 0.1.dev6</a></li>
</ul>
</li>
</ul>
</div>
&nbsp;
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Catalyst</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li>Catalyst Beginner Tutorial</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/beginner-tutorial.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document">
<div class="section" id="catalyst-beginner-tutorial">
<h1>Catalyst Beginner Tutorial<a class="headerlink" href="#catalyst-beginner-tutorial" title="Permalink to this headline"></a></h1>
<div class="section" id="basics">
<h2>Basics<a class="headerlink" href="#basics" title="Permalink to this headline"></a></h2>
<p>Catalyst is an open-source algorithmic trading simulator for crypto
assets written in Python. The source code can be found at:
<a class="reference external" href="https://github.com/enigmampc/catalyst">https://github.com/enigmampc/catalyst</a></p>
<p>Some benefits include:</p>
<ul class="simple">
<li>Support for several of the top crypto-exchanges by trading volume.</li>
<li>Realistic: slippage, transaction costs, order delays.</li>
<li>Stream-based: Process each event individually, avoids look-ahead
bias.</li>
<li>Batteries included: Common transforms (moving average) as well as
common risk calculations (Sharpe).</li>
<li>Developed and continuously updated by
<a class="reference external" href="https://www.enigma.co">Enigma MPC</a> which is building the Enigma
data marketplace protocol as well as Catalyst, the first application
that will run on our protocol. Powered by our financial data
marketplace, Catalyst empowers users to share and curate data and
build profitable, data-driven investment strategies.</li>
</ul>
<p>This tutorial assumes that you have Catalyst correctly installed, see the
<a class="reference internal" href="install.html"><em>Install</em></a> section if you haven&#8217;t set up Catalyst yet.</p>
<p>Every <code class="docutils literal"><span class="pre">catalyst</span></code> algorithm consists of at least two functions you have to
define:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">initialize(context)</span></code></li>
<li><code class="docutils literal"><span class="pre">handle_data(context,</span> <span class="pre">data)</span></code></li>
</ul>
<p>Before the start of the algorithm, <code class="docutils literal"><span class="pre">catalyst</span></code> calls the
<code class="docutils literal"><span class="pre">initialize()</span></code> function and passes in a <code class="docutils literal"><span class="pre">context</span></code> variable.
<code class="docutils literal"><span class="pre">context</span></code> is a persistent namespace for you to store variables you
need to access from one algorithm iteration to the next.</p>
<p>After the algorithm has been initialized, <code class="docutils literal"><span class="pre">catalyst</span></code> calls the
<code class="docutils literal"><span class="pre">handle_data()</span></code> function on each iteration, that&#8217;s one per day (daily) or
once every minute (minute), depending on the frequency we choose to run our
simulation. On every iteration, <code class="docutils literal"><span class="pre">handle_data()</span></code> passes the same <code class="docutils literal"><span class="pre">context</span></code>
variable and an event-frame called <code class="docutils literal"><span class="pre">data</span></code> containing the current trading bar
with open, high, low, and close (OHLC) prices as well as volume for each
crypto asset in your universe.</p>
</div>
<div class="section" id="my-first-algorithm">
<h2>My first algorithm<a class="headerlink" href="#my-first-algorithm" title="Permalink to this headline"></a></h2>
<p>Lets take a look at a very simple algorithm from the <code class="docutils literal"><span class="pre">examples</span></code> directory:
<a class="reference external" href="https://github.com/enigmampc/catalyst/blob/master/catalyst/examples/buy_btc_simple.py">buy_btc_simple.py</a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">catalyst.api</span> <span class="kn">import</span> <span class="n">order</span><span class="p">,</span> <span class="n">record</span><span class="p">,</span> <span class="n">symbol</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
<span class="n">context</span><span class="o">.</span><span class="n">asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s">&#39;btc_usd&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">order</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">record</span><span class="p">(</span><span class="n">btc</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">current</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>As you can see, we first have to import some functions we would like to
use. All functions commonly used in your algorithm can be found in
<code class="docutils literal"><span class="pre">catalyst.api</span></code>. Here we are using <code class="xref py py-func docutils literal"><span class="pre">order()</span></code> which takes
twoarguments: a cryptoasset object, and a number specifying how many assets you
wouldlike to order (if negative, <code class="xref py py-func docutils literal"><span class="pre">order()</span></code> will sell/short
assets). In this case we want to order 1 bitcoin at each iteration.</p>
<p>Finally, the <code class="xref py py-func docutils literal"><span class="pre">record()</span></code> function allows you to save the value
of a variable at each iteration. You provide it with a name for the variable
together with the variable itself: <code class="docutils literal"><span class="pre">varname=var</span></code>. After the algorithm
finished running you will have access to each variable value you tracked
with <code class="xref py py-func docutils literal"><span class="pre">record()</span></code> under the name you provided (we will see this
further below). You also see how we can access the current price data of
a bitcoin in the <code class="docutils literal"><span class="pre">data</span></code> event frame.</p>
</div>
<div class="section" id="ingesting-data">
<h2>Ingesting data<a class="headerlink" href="#ingesting-data" title="Permalink to this headline"></a></h2>
<p>Before you can backtest your algorithm, you first need to load the historical
pricing data that Catalyst needs to run your simulation through a process called
<code class="docutils literal"><span class="pre">ingestion</span></code>. When you ingest data, Catalyst downloads that data in compressed
form from the Enigma servers (which eventually will migrate to the Enigma Data
Marketplace), and stores it locally to make it available at runtime.</p>
<p>In order to ingest data, you need to run a command like the following:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst ingest-exchange -x bitfinex -i btc_usd
</pre></div>
</div>
<p>This instructs Catalyst to download pricing data from the <code class="docutils literal"><span class="pre">Bitfinex</span></code> exchange
for the <code class="docutils literal"><span class="pre">btc_usd</span></code> currency pair (this follows from the simple algorithm
presented above where we want to trade <code class="docutils literal"><span class="pre">btc_usd</span></code>), and we&#8217;re choosing to test
our algorithm using historical pricing data from the Bitfinex exchange. By
default, Catalyst assumes that you want data with <code class="docutils literal"><span class="pre">daily</span></code> frequency (one candle
bar per day). If you want instead <code class="docutils literal"><span class="pre">minute</span></code> frequency (one candle bar for every
minute), you would need to specify it as follows:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst ingest-exchange -x bitfinex -i btc_usd -f minute
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Ingesting exchange bundle bitfinex...
[====================================] Ingesting daily price data on bitfinex: 100%
</pre></div>
</div>
<p>We believe it is important for you to have a high-level understanding of how
data is managed, hence the following overview:</p>
<ul class="simple">
<li>Pricing data is split and packaged into <code class="docutils literal"><span class="pre">bundles</span></code>: chunks of data organized
as time series that are kept up to date daily on Enigma&#8217;s servers. Catalyst
downloads the requested bundles and reconstructs the full dataset in your
hard drive.</li>
<li>Pricing data is provided in <code class="docutils literal"><span class="pre">daily</span></code> and <code class="docutils literal"><span class="pre">minute</span></code> resolution. Those are
different bundle datasets, and are managed separately.</li>
<li>Bundles are exchange-specific, as the pricing data is specific to the trades
that happen in each exchange. As a result, you must specify which
exchange you want pricing data from when ingesting data.</li>
<li>Catalyst keeps track of all the downloaded bundles, so that it only has to
download them once, and will do incremental updates as needed.</li>
<li>When running in <code class="docutils literal"><span class="pre">live</span> <span class="pre">trading</span></code> mode, Catalyst will first look for
historical pricing data in the locally stored bundles. If there is anything
missing, Catalyst will hit the exchange for the most recent data, and merge
it with the local bundle to optimize the number of requests it needs to make
to the exchange.</li>
</ul>
<p>The <code class="docutils literal"><span class="pre">ingest-exchange</span></code> command in catalyst offers additional parameters to
further tweak the data ingestion process. You can learn more by running the
following from the command line:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst ingest-exchange --help
</pre></div>
</div>
</div>
<div class="section" id="running-the-algorithm">
<h2>Running the algorithm<a class="headerlink" href="#running-the-algorithm" title="Permalink to this headline"></a></h2>
<p>You can now test your algorithm using cryptoassets&#8217; historical pricing data,
<code class="docutils literal"><span class="pre">catalyst</span></code> provides three interfaces:</p>
<ul class="simple">
<li>A command-line interface (CLI),</li>
<li>a <code class="xref py py-func docutils literal"><span class="pre">run_algorithm()</span></code> that you can call from other
Python scripts,</li>
<li>and the <code class="docutils literal"><span class="pre">Jupyter</span> <span class="pre">Notebook</span></code> magic.</li>
</ul>
<p>We&#8217;ll start with the CLI, and introduce the <code class="docutils literal"><span class="pre">run_algorithm()</span></code> in the last
example of this tutorial. Some of the <a class="reference internal" href="example-algos.html"><em>example algorithms</em></a>
provide instructions on how to run them both from the CLI, and using the
<code class="xref py py-func docutils literal"><span class="pre">run_algorithm()</span></code> function. For the third method, refer to the
corresponding section on <code class="xref doc docutils literal"><span class="pre">Catalyst</span> <span class="pre">&amp;</span> <span class="pre">Jupyter</span> <span class="pre">Notebook</span></code> after you
have assimilated the contents of this tutorial.</p>
<div class="section" id="command-line-interface">
<h3>Command line interface<a class="headerlink" href="#command-line-interface" title="Permalink to this headline"></a></h3>
<p>After you installed Catalyst, you should be able to execute the following
from your command line (e.g. <code class="docutils literal"><span class="pre">cmd.exe</span></code> or the <code class="docutils literal"><span class="pre">Anaconda</span> <span class="pre">Prompt</span></code> on Windows,
or the Terminal application on MacOS).</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>catalyst --help
</pre></div>
</div>
<p>This is the resulting output, simplified for eductional purposes:</p>
<div class="highlight-python"><div class="highlight"><pre>Usage: catalyst [OPTIONS] COMMAND [ARGS]...
Top level catalyst entry point.
Options:
--version Show the version and exit.
--help Show this message and exit.
Commands:
ingest-exchange Ingest data for the given exchange.
live Trade live with the given algorithm.
run Run a backtest for the given algorithm.
</pre></div>
</div>
<p>There are three main modes you can run on Catalyst. The first being
<code class="docutils literal"><span class="pre">ingest-exchange</span></code> for data ingestion, which we have covered in the previous
section. The second is <code class="docutils literal"><span class="pre">live</span></code> to use your algorithm to trade live against a
given exchange, and the third mode <code class="docutils literal"><span class="pre">run</span></code> is to backtest your algorithm before
trading live with it.</p>
<p>Let&#8217;s start with backtesting, so run this other command to learn more about
the available options:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>catalyst run --help
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Usage: catalyst run [OPTIONS]
Run a backtest for the given algorithm.
Options:
-f, --algofile FILENAME The file that contains the algorithm to run.
-t, --algotext TEXT The algorithm script to run.
-D, --define TEXT Define a name to be bound in the namespace
before executing the algotext. For example
&#39;-Dname=value&#39;. The value may be any python
expression. These are evaluated in order so
they may refer to previously defined names.
--data-frequency [daily|minute]
The data frequency of the simulation.
[default: daily]
--capital-base FLOAT The starting capital for the simulation.
[default: 10000000.0]
-b, --bundle BUNDLE-NAME The data bundle to use for the simulation.
[default: poloniex]
--bundle-timestamp TIMESTAMP The date to lookup data on or before.
[default: &lt;current-time&gt;]
-s, --start DATE The start date of the simulation.
-e, --end DATE The end date of the simulation.
-o, --output FILENAME The location to write the perf data. If this
is &#39;-&#39; the perf will be written to stdout.
[default: -]
--print-algo / --no-print-algo Print the algorithm to stdout.
-x, --exchange-name [poloniex|bitfinex|bittrex]
The name of the targeted exchange
(supported: bitfinex, bittrex, poloniex).
-n, --algo-namespace TEXT A label assigned to the algorithm for data
storage purposes.
-c, --base-currency TEXT The base currency used to calculate
statistics (e.g. usd, btc, eth).
--help Show this message and exit.
</pre></div>
</div>
<p>As you can see there are a couple of flags that specify where to find your
algorithm (<code class="docutils literal"><span class="pre">-f</span></code>) as well as a the <code class="docutils literal"><span class="pre">-x</span></code> flag to specify which exchange to
use. There are also arguments for the date range to run the algorithm over
(<code class="docutils literal"><span class="pre">--start</span></code> and <code class="docutils literal"><span class="pre">--end</span></code>). You also need to set the base currency for your
algorithm through the <code class="docutils literal"><span class="pre">-c</span></code> flag, and the <code class="docutils literal"><span class="pre">--capital_base</span></code>. All the
aforementioned parameters are required. Optionally, you will want to save the
performance metrics of your algorithm so that you can analyze how it performed.
This is done via the <code class="docutils literal"><span class="pre">--output</span></code> flag and will cause it to write the
performance <code class="docutils literal"><span class="pre">DataFrame</span></code> in the pickle Python file format. Note that you can
also define a configuration file with these parameters that you can then
conveniently pass to the <code class="docutils literal"><span class="pre">-c</span></code> option so that you don&#8217;t have to supply the
command line args all the time.</p>
<p>Thus, to execute our algorithm from above and save the results to
<code class="docutils literal"><span class="pre">buy_btc_simple_out.pickle</span></code> we would call <code class="docutils literal"><span class="pre">catalyst</span> <span class="pre">run</span></code> as follows:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst run -f buy_btc_simple.py -x bitfinex --start 2016-1-1 --end 2017-9-30 -c usd --capital-base <span class="m">100000</span> -o buy_btc_simple_out.pickle
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>INFO: run_algo: running algo in backtest mode
INFO: exchange_algorithm: initialized trading algorithm in backtest mode
INFO: Performance: Simulated 639 trading days out of 639.
INFO: Performance: first open: 2016-01-01 00:00:00+00:00
INFO: Performance: last close: 2017-09-30 23:59:00+00:00
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">run</span></code> first calls the <code class="docutils literal"><span class="pre">initialize()</span></code> function, and then
streams the historical asset price day-by-day through <code class="docutils literal"><span class="pre">handle_data()</span></code>.
After each call to <code class="docutils literal"><span class="pre">handle_data()</span></code> we instruct <code class="docutils literal"><span class="pre">catalyst</span></code> to order 1
bitcoin. After the call of the <code class="docutils literal"><span class="pre">order()</span></code> function, <code class="docutils literal"><span class="pre">catalyst</span></code>
enters the ordered stock and amount in the order book. After the
<code class="docutils literal"><span class="pre">handle_data()</span></code> function has finished, <code class="docutils literal"><span class="pre">catalyst</span></code> looks for any open
orders and tries to fill them. If the trading volume is high enough for
this asset, the order is executed after adding the commission and
applying the slippage model which models the influence of your order on
the stock price, so your algorithm will be charged more than just the
asset price. (Note, that you can also change the commission and
slippage model that <code class="docutils literal"><span class="pre">catalyst</span></code> uses).</p>
<p>Let&#8217;s take a quick look at the performance <code class="docutils literal"><span class="pre">DataFrame</span></code>. For this, we write
different Python script&#8211;let&#8217;s call it <code class="docutils literal"><span class="pre">print_results.py</span></code>&#8211;and we make use of
the fantastic <code class="docutils literal"><span class="pre">pandas</span></code> library to print the first ten rows. Note that
<code class="docutils literal"><span class="pre">catalyst</span></code> makes heavy usage of <a class="reference external" href="http://pandas.pydata.org/">pandas</a>,
especially for data analysis and outputting so it&#8217;s worth spending some time to
learn it.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="n">perf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_pickle</span><span class="p">(</span><span class="s">&#39;buy_btc_simple_out.pickle&#39;</span><span class="p">)</span> <span class="c"># read in perf DataFrame</span>
<span class="k">print</span><span class="p">(</span><span class="n">perf</span><span class="o">.</span><span class="n">head</span><span class="p">())</span>
</pre></div>
</div>
<p>Which we execute by running:</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>python print_results.py
</pre></div>
</div>
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>algo_volatility</th>
<th>algorithm_period_return</th>
<th>alpha</th>
<th>benchmark_period_return</th>
<th>benchmark_volatility</th>
<th>beta</th>
<th>btc</th>
<th>capital_used</th>
<th>ending_cash</th>
<th>ending_exposure</th>
<th>...</th>
<th>short_exposure</th>
<th>short_value</th>
<th>shorts_count</th>
<th>sortino</th>
<th>starting_cash</th>
<th>starting_exposure</th>
<th>starting_value</th>
<th>trading_days</th>
<th>transactions</th>
<th>treasury_period_return</th>
</tr>
</thead>
<tbody>
<tr>
<th>2016-01-01 23:59:00+00:00</th>
<td>NaN</td>
<td>0.000000e+00</td>
<td>NaN</td>
<td>-0.010937</td>
<td>NaN</td>
<td>NaN</td>
<td>433.979999</td>
<td>0.000000</td>
<td>1.000000e+07</td>
<td>0.00</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>NaN</td>
<td>1.000000e+07</td>
<td>0.00</td>
<td>0.00</td>
<td>1</td>
<td>[]</td>
<td>0.0227</td>
</tr>
<tr>
<th>2016-01-02 23:59:00+00:00</th>
<td>0.000011</td>
<td>-9.536708e-07</td>
<td>-0.000170</td>
<td>-0.006480</td>
<td>0.173338</td>
<td>-0.000062</td>
<td>432.700000</td>
<td>-442.236708</td>
<td>9.999558e+06</td>
<td>432.70</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>-11.224972</td>
<td>1.000000e+07</td>
<td>0.00</td>
<td>0.00</td>
<td>2</td>
<td>[{u'order_id': u'7869f7828fa140328eb40477bb7de...</td>
<td>0.0227</td>
</tr>
<tr>
<th>2016-01-03 23:59:00+00:00</th>
<td>0.000011</td>
<td>-2.328842e-06</td>
<td>-0.000176</td>
<td>-0.026512</td>
<td>0.197857</td>
<td>0.000009</td>
<td>428.390000</td>
<td>-437.831716</td>
<td>9.999120e+06</td>
<td>856.78</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>-12.754262</td>
<td>9.999558e+06</td>
<td>432.70</td>
<td>432.70</td>
<td>3</td>
<td>[{u'order_id': u'be62ff77760c4599abaac43be9cc9...</td>
<td>0.0227</td>
</tr>
<tr>
<th>2016-01-04 23:59:00+00:00</th>
<td>0.000011</td>
<td>-2.380954e-06</td>
<td>-0.000139</td>
<td>-0.008640</td>
<td>0.269790</td>
<td>0.000020</td>
<td>432.900000</td>
<td>-442.441116</td>
<td>9.998677e+06</td>
<td>1298.70</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>-11.287205</td>
<td>9.999120e+06</td>
<td>856.78</td>
<td>856.78</td>
<td>4</td>
<td>[{u'order_id': u'd6dca79513214346a646079213526...</td>
<td>0.0224</td>
</tr>
<tr>
<th>2016-01-05 23:59:00+00:00</th>
<td>0.000011</td>
<td>-3.650729e-06</td>
<td>-0.000158</td>
<td>-0.021426</td>
<td>0.245989</td>
<td>0.000024</td>
<td>431.840000</td>
<td>-441.357754</td>
<td>9.998236e+06</td>
<td>1727.36</td>
<td>...</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>-12.333847</td>
<td>9.998677e+06</td>
<td>1298.70</td>
<td>1298.70</td>
<td>5</td>
<td>[{u'order_id': u'505275d6646a41f3856b22b16678d...</td>
<td>0.0225</td>
</tr>
</tbody>
</table>
</div><div class="line-block">
<div class="line"><br /></div>
</div>
<p>There is a row for each trading day, starting on the first day of our
simulation Jan 1st, 2016. In the columns you can find various
information about the state of your algorithm. The column
<code class="docutils literal"><span class="pre">btc</span></code> was placed there by the <code class="docutils literal"><span class="pre">record()</span></code> function mentioned earlier
and allows us to plot the price of bitcoin. For example, we could easily
examine now how our portfolio value changed over time compared to the
bitcoin price.</p>
<p>Now we will run the simulation again, but this time we extend our original
algorithm with the addition of the <code class="docutils literal"><span class="pre">analyze()</span></code> function. Somewhat analogously
as how <code class="docutils literal"><span class="pre">initialize()</span></code> gets called once before the start of the algorith,
<code class="docutils literal"><span class="pre">analyze()</span></code> gets called once at the end of the algorithm, and receives two
variables: <code class="docutils literal"><span class="pre">context</span></code>, which we discussed at the very beginning, and <code class="docutils literal"><span class="pre">perf</span></code>,
which is the pandas dataframe containing the performance data for our algorithm
that we reviewed above. Inside the <code class="docutils literal"><span class="pre">analyze()</span></code> function is where we can
analyze and visualize the results of our strategy. Here&#8217;s the revised simple
algorithm (note the addition of Line 1, and Lines 11-18)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">catalyst.api</span> <span class="kn">import</span> <span class="n">order</span><span class="p">,</span> <span class="n">record</span><span class="p">,</span> <span class="n">symbol</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
<span class="n">context</span><span class="o">.</span><span class="n">asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s">&#39;btc_usd&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="n">order</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">record</span><span class="p">(</span><span class="n">btc</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">current</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">analyze</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">perf</span><span class="p">):</span>
<span class="n">ax1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">portfolio_value</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;portfolio value&#39;</span><span class="p">)</span>
<span class="n">ax2</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">,</span> <span class="n">sharex</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">btc</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax2</span><span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;bitcoin price&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<p>Here we make use of the external visualization library called
<a class="reference external" href="https://matplotlib.org/">matplotlib</a>, which you might recall we installed
alongside enigma-catalyst (with the exception of the <code class="docutils literal"><span class="pre">Conda</span></code> install, where it
was included by default inside the conda environment we created). If for any
reason you don&#8217;t have it installed, you can add it by running:</p>
<div class="highlight-python"><div class="highlight"><pre>(catalyst)$ pip install matplotlib
</pre></div>
</div>
<p>If everything works well, you&#8217;ll see the following chart:</p>
<img alt="https://s3.amazonaws.com/enigmaco-docs/github.io/buy_btc_simple_graph.png" src="https://s3.amazonaws.com/enigmaco-docs/github.io/buy_btc_simple_graph.png" />
<p>Our algorithm performance as assessed by the <code class="docutils literal"><span class="pre">portfolio_value</span></code> closely
matches that of the bitcoin price. This is not surprising as our algorithm
only bought bitcoin every chance it got.</p>
<blockquote>
<div><p>If you get an error when invoking matplotlib to visualize the performance
results refer to <a class="reference external" href="install.html#macos-virtualenv-matplotlib">MacOS + Matplotlib</a>.
Alternatively, some users have reported the following error when running an algo
in a Linux environment:</p>
<div class="highlight-python"><div class="highlight"><pre>ImportError: No module named _tkinter, please install the python-tk package
</pre></div>
</div>
<p>Which can easily solved by running (in Ubuntu/Debian-based systems):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">sudo</span> <span class="n">apt</span> <span class="n">install</span> <span class="n">python</span><span class="o">-</span><span class="n">tk</span>
</pre></div>
</div>
</div></blockquote>
</div>
</div>
<div class="section" id="access-to-previous-prices-using-history">
<span id="history"></span><h2>Access to previous prices using <code class="docutils literal"><span class="pre">history</span></code><a class="headerlink" href="#access-to-previous-prices-using-history" title="Permalink to this headline"></a></h2>
<div class="section" id="working-example-dual-moving-average-cross-over">
<h3>Working example: Dual Moving Average Cross-Over<a class="headerlink" href="#working-example-dual-moving-average-cross-over" title="Permalink to this headline"></a></h3>
<p>The Dual Moving Average (DMA) is a classic momentum strategy. It&#8217;s
probably not used by any serious trader anymore but is still very
instructive. The basic idea is that we compute two rolling or moving
averages (mavg) &#8211; one with a longer window that is supposed to capture
long-term trends and one shorter window that is supposed to capture
short-term trends. Once the short-mavg crosses the long-mavg from below
we assume that the stock price has upwards momentum and long the stock.
If the short-mavg crosses from above we exit the positions as we assume
the stock to go down further.</p>
<p>As we need to have access to previous prices to implement this strategy
we need a new concept: History. <code class="docutils literal"><span class="pre">data.history()</span></code> is a convenience function
that keeps a rolling window of data for you. The first argument is the number
of bars you want to collect, the second argument is the unit (either <code class="docutils literal"><span class="pre">'1d'</span></code>
for daily or <code class="docutils literal"><span class="pre">'1m'</span></code> for minute frequency, but note that you need to have
minute-level data when using <code class="docutils literal"><span class="pre">1m</span></code>). This is a function we use in the
<code class="docutils literal"><span class="pre">handle_data()</span></code> section.</p>
<p>You will note that the code below is substantially longer than the previous
examples. Don&#8217;t get overwhelmed by it as the logic is fairly simple and easy to
follow. Most of the added some complexity has been added to beautify the output,
which you can skim through for now. A copy of this algorithm is available in
the <code class="docutils literal"><span class="pre">examples</span></code> directory:
<a class="reference external" href="https://github.com/enigmampc/catalyst/blob/master/catalyst/examples/dual_moving_average.py">dual_moving_average.py</a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">logbook</span> <span class="kn">import</span> <span class="n">Logger</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">from</span> <span class="nn">catalyst</span> <span class="kn">import</span> <span class="n">run_algorithm</span>
<span class="kn">from</span> <span class="nn">catalyst.api</span> <span class="kn">import</span> <span class="p">(</span><span class="n">order</span><span class="p">,</span> <span class="n">record</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">order_target_percent</span><span class="p">,</span>
<span class="n">get_open_orders</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">catalyst.exchange.stats_utils</span> <span class="kn">import</span> <span class="n">extract_transactions</span>
<span class="n">NAMESPACE</span> <span class="o">=</span> <span class="s">&#39;dual_moving_average&#39;</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">Logger</span><span class="p">(</span><span class="n">NAMESPACE</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
<span class="n">context</span><span class="o">.</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">context</span><span class="o">.</span><span class="n">asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s">&#39;ltc_usd&#39;</span><span class="p">)</span>
<span class="n">context</span><span class="o">.</span><span class="n">base_price</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="c"># define the windows for the moving averages</span>
<span class="n">short_window</span> <span class="o">=</span> <span class="mi">50</span>
<span class="n">long_window</span> <span class="o">=</span> <span class="mi">200</span>
<span class="c"># Skip as many bars as long_window to properly compute the average</span>
<span class="n">context</span><span class="o">.</span><span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">i</span> <span class="o">&lt;</span> <span class="n">long_window</span><span class="p">:</span>
<span class="k">return</span>
<span class="c"># Compute moving averages calling data.history() for each</span>
<span class="c"># moving average with the appropriate parameters. We choose to use</span>
<span class="c"># minute bars for this simulation -&gt; freq=&quot;1m&quot;</span>
<span class="c"># Returns a pandas dataframe.</span>
<span class="n">short_mavg</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">history</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">,</span>
<span class="n">bar_count</span><span class="o">=</span><span class="n">short_window</span><span class="p">,</span> <span class="n">frequency</span><span class="o">=</span><span class="s">&quot;1m&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="n">long_mavg</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">history</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">,</span>
<span class="n">bar_count</span><span class="o">=</span><span class="n">long_window</span><span class="p">,</span> <span class="n">frequency</span><span class="o">=</span><span class="s">&quot;1m&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="c"># Let&#39;s keep the price of our asset in a more handy variable</span>
<span class="n">price</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">current</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">)</span>
<span class="c"># If base_price is not set, we use the current value. This is the</span>
<span class="c"># price at the first bar which we reference to calculate price_change.</span>
<span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">base_price</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">context</span><span class="o">.</span><span class="n">base_price</span> <span class="o">=</span> <span class="n">price</span>
<span class="n">price_change</span> <span class="o">=</span> <span class="p">(</span><span class="n">price</span> <span class="o">-</span> <span class="n">context</span><span class="o">.</span><span class="n">base_price</span><span class="p">)</span> <span class="o">/</span> <span class="n">context</span><span class="o">.</span><span class="n">base_price</span>
<span class="c"># Save values for later inspection</span>
<span class="n">record</span><span class="p">(</span><span class="n">price</span><span class="o">=</span><span class="n">price</span><span class="p">,</span>
<span class="n">cash</span><span class="o">=</span><span class="n">context</span><span class="o">.</span><span class="n">portfolio</span><span class="o">.</span><span class="n">cash</span><span class="p">,</span>
<span class="n">price_change</span><span class="o">=</span><span class="n">price_change</span><span class="p">,</span>
<span class="n">short_mavg</span><span class="o">=</span><span class="n">short_mavg</span><span class="p">,</span>
<span class="n">long_mavg</span><span class="o">=</span><span class="n">long_mavg</span><span class="p">)</span>
<span class="c"># Since we are using limit orders, some orders may not execute immediately</span>
<span class="c"># we wait until all orders are executed before considering more trades.</span>
<span class="n">orders</span> <span class="o">=</span> <span class="n">get_open_orders</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">orders</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span>
<span class="c"># Exit if we cannot trade</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">data</span><span class="o">.</span><span class="n">can_trade</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">):</span>
<span class="k">return</span>
<span class="c"># We check what&#39;s our position on our portfolio and trade accordingly</span>
<span class="n">pos_amount</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">portfolio</span><span class="o">.</span><span class="n">positions</span><span class="p">[</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">]</span><span class="o">.</span><span class="n">amount</span>
<span class="c"># Trading logic</span>
<span class="k">if</span> <span class="n">short_mavg</span> <span class="o">&gt;</span> <span class="n">long_mavg</span> <span class="ow">and</span> <span class="n">pos_amount</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="c"># we buy 100% of our portfolio for this asset</span>
<span class="n">order_target_percent</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">short_mavg</span> <span class="o">&lt;</span> <span class="n">long_mavg</span> <span class="ow">and</span> <span class="n">pos_amount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="c"># we sell all our positions for this asset</span>
<span class="n">order_target_percent</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">analyze</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">perf</span><span class="p">):</span>
<span class="c"># Get the base_currency that was passed as a parameter to the simulation</span>
<span class="n">base_currency</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">exchanges</span><span class="o">.</span><span class="n">values</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">base_currency</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="c"># First chart: Plot portfolio value using base_currency</span>
<span class="n">ax1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">411</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s">&#39;portfolio_value&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">legend_</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;Portfolio Value</span><span class="se">\n</span><span class="s">({})&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">base_currency</span><span class="p">))</span>
<span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">ax1</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()</span>
<span class="n">ax1</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_ticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="p">(</span><span class="n">end</span><span class="o">-</span><span class="n">start</span><span class="p">)</span><span class="o">/</span><span class="mi">5</span><span class="p">))</span>
<span class="c"># Second chart: Plot asset price, moving averages and buys/sells</span>
<span class="n">ax2</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">412</span><span class="p">,</span> <span class="n">sharex</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s">&#39;price&#39;</span><span class="p">,</span><span class="s">&#39;short_mavg&#39;</span><span class="p">,</span><span class="s">&#39;long_mavg&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax2</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s">&#39;Price&#39;</span><span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">legend_</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;{asset}</span><span class="se">\n</span><span class="s">({base})&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="n">asset</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">asset</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span>
<span class="n">base</span> <span class="o">=</span> <span class="n">base_currency</span>
<span class="p">))</span>
<span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">ax2</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_ticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="p">(</span><span class="n">end</span><span class="o">-</span><span class="n">start</span><span class="p">)</span><span class="o">/</span><span class="mi">5</span><span class="p">))</span>
<span class="n">transaction_df</span> <span class="o">=</span> <span class="n">extract_transactions</span><span class="p">(</span><span class="n">perf</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">transaction_df</span><span class="o">.</span><span class="n">empty</span><span class="p">:</span>
<span class="n">buy_df</span> <span class="o">=</span> <span class="n">transaction_df</span><span class="p">[</span><span class="n">transaction_df</span><span class="p">[</span><span class="s">&#39;amount&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">sell_df</span> <span class="o">=</span> <span class="n">transaction_df</span><span class="p">[</span><span class="n">transaction_df</span><span class="p">[</span><span class="s">&#39;amount&#39;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">buy_df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">to_pydatetime</span><span class="p">(),</span>
<span class="n">perf</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">buy_df</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">],</span>
<span class="n">marker</span><span class="o">=</span><span class="s">&#39;^&#39;</span><span class="p">,</span>
<span class="n">s</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">c</span><span class="o">=</span><span class="s">&#39;green&#39;</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="s">&#39;&#39;</span>
<span class="p">)</span>
<span class="n">ax2</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">sell_df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">to_pydatetime</span><span class="p">(),</span>
<span class="n">perf</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">sell_df</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="s">&#39;price&#39;</span><span class="p">],</span>
<span class="n">marker</span><span class="o">=</span><span class="s">&#39;v&#39;</span><span class="p">,</span>
<span class="n">s</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">c</span><span class="o">=</span><span class="s">&#39;red&#39;</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="s">&#39;&#39;</span>
<span class="p">)</span>
<span class="c"># Third chart: Compare percentage change between our portfolio</span>
<span class="c"># and the price of the asset</span>
<span class="n">ax3</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">413</span><span class="p">,</span> <span class="n">sharex</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s">&#39;algorithm_period_return&#39;</span><span class="p">,</span> <span class="s">&#39;price_change&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax3</span><span class="p">)</span>
<span class="n">ax3</span><span class="o">.</span><span class="n">legend_</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
<span class="n">ax3</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;Percent Change&#39;</span><span class="p">)</span>
<span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">ax3</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()</span>
<span class="n">ax3</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_ticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="p">(</span><span class="n">end</span><span class="o">-</span><span class="n">start</span><span class="p">)</span><span class="o">/</span><span class="mi">5</span><span class="p">))</span>
<span class="c"># Fourth chart: Plot our cash</span>
<span class="n">ax4</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">414</span><span class="p">,</span> <span class="n">sharex</span><span class="o">=</span><span class="n">ax1</span><span class="p">)</span>
<span class="n">perf</span><span class="o">.</span><span class="n">cash</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ax</span><span class="o">=</span><span class="n">ax4</span><span class="p">)</span>
<span class="n">ax4</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">&#39;Cash</span><span class="se">\n</span><span class="s">({})&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">base_currency</span><span class="p">))</span>
<span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">ax4</span><span class="o">.</span><span class="n">get_ylim</span><span class="p">()</span>
<span class="n">ax4</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_ticks</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">end</span><span class="o">/</span><span class="mi">5</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
<span class="n">run_algorithm</span><span class="p">(</span>
<span class="n">capital_base</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
<span class="n">data_frequency</span><span class="o">=</span><span class="s">&#39;minute&#39;</span><span class="p">,</span>
<span class="n">initialize</span><span class="o">=</span><span class="n">initialize</span><span class="p">,</span>
<span class="n">handle_data</span><span class="o">=</span><span class="n">handle_data</span><span class="p">,</span>
<span class="n">analyze</span><span class="o">=</span><span class="n">analyze</span><span class="p">,</span>
<span class="n">exchange_name</span><span class="o">=</span><span class="s">&#39;bitfinex&#39;</span><span class="p">,</span>
<span class="n">algo_namespace</span><span class="o">=</span><span class="n">NAMESPACE</span><span class="p">,</span>
<span class="n">base_currency</span><span class="o">=</span><span class="s">&#39;usd&#39;</span><span class="p">,</span>
<span class="n">start</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="s">&#39;2017-9-22&#39;</span><span class="p">,</span> <span class="n">utc</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
<span class="n">end</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="s">&#39;2017-9-23&#39;</span><span class="p">,</span> <span class="n">utc</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In order to run the code above, you have to ingest the needed data first:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst ingest-exchange -x bitfinex -f minute -i ltc_usd
</pre></div>
</div>
<p>And then run the code above with the following command:</p>
<div class="highlight-bash"><div class="highlight"><pre>catalyst run -f dual_moving_average.py -x bitfinex -s 2017-9-22 -e 2017-9-23 --capital-base <span class="m">1000</span> --base-currency usd --data-frequency minute -o out.pickle
</pre></div>
</div>
<p>Alternatively, we can make use of the <code class="docutils literal"><span class="pre">run_algorithm()</span></code> function included at
the end of the file, where we can specify all the simulation parameters, and
execute this file as a Python script:</p>
<div class="highlight-bash"><div class="highlight"><pre>python dual_moving_average.py
</pre></div>
</div>
<p>Either way, we obtain the following charts:</p>
<img alt="https://s3.amazonaws.com/enigmaco-docs/github.io/tutorial_dual_moving_average.png" src="https://s3.amazonaws.com/enigmaco-docs/github.io/tutorial_dual_moving_average.png" />
<p>A few comments on the code above:</p>
<blockquote>
<div><p>At the beginning of our code, we import a number of Python libraries that we
will be using in different parts of our script. It&#8217;s good practice to keep all
imports at the beginning of the file, as they are available globally
throughout our script. All the libraries imported in this example are already
present in your environment since they are prerequisites for the Catalyst
installation.</p>
<p>Focus on the code that is inside <code class="docutils literal"><span class="pre">handle_data()</span></code> that is where all the
trading logic occurs. You can safely dismiss most of the code in the
<code class="docutils literal"><span class="pre">analyze()</span></code> section, which is mostly to customize the visualization of the
performance of our algorithm using the matplotlib library. You can copy and
paste this whole section into other algorithms to obtain a similar display.</p>
<p>Inside the <code class="docutils literal"><span class="pre">handle_data()</span></code>, we also used the <code class="docutils literal"><span class="pre">order_target_percent()</span></code>
function above. This and other functions like it can make order management
and portfolio rebalancing much easier.</p>
<p>The <code class="docutils literal"><span class="pre">ltc_usd</span></code> asset was arbitrarily chosen. The values of 50 and 200 for the
<code class="docutils literal"><span class="pre">short_window</span></code> and <code class="docutils literal"><span class="pre">long_window</span></code> parameters are fairly common for a dual
moving average crossover strategy from the world of traditional stocks (but
bear in mind that they are usually used with daily bars instead of minute
bars). The <code class="docutils literal"><span class="pre">start</span></code> and <code class="docutils literal"><span class="pre">end</span></code> dates have been chosen so as to demonstrate
how our strategy can both perform better (blue line above green line on the
<code class="docutils literal"><span class="pre">Percent</span> <span class="pre">Change</span></code> chart) and worse (green line above blue line towards the end) than the
price of the asset we are trading.</p>
<p>You can change any of these parameters: <code class="docutils literal"><span class="pre">asset</span></code>, <code class="docutils literal"><span class="pre">short_window</span></code>,
<code class="docutils literal"><span class="pre">long_window</span></code>, <code class="docutils literal"><span class="pre">start_date</span></code> and <code class="docutils literal"><span class="pre">end_date</span></code> and compare the results, and
you will see that in most cases, the performance is either worse than the
price of the asset, or you are overfitting to one specific case. As we said
at the beginning of this section, this strategy is probably not used by any
serious trader anymore, but its educational purpose.</p>
</div></blockquote>
<p>Although it might not be directly apparent, the power of <code class="docutils literal"><span class="pre">history()</span></code>
(pun intended) can not be under-estimated as most algorithms make use of
prior market developments in one form or another. You could easily
devise a strategy that trains a classifier with
<a class="reference external" href="http://scikit-learn.org/stable/">scikit-learn</a> which tries to
predict future market movements based on past prices (note, that most of
the <code class="docutils literal"><span class="pre">scikit-learn</span></code> functions require <code class="docutils literal"><span class="pre">numpy.ndarray</span></code>s rather than
<code class="docutils literal"><span class="pre">pandas.DataFrame</span></code>s, so you can simply pass the underlying
<code class="docutils literal"><span class="pre">ndarray</span></code> of a <code class="docutils literal"><span class="pre">DataFrame</span></code> via <code class="docutils literal"><span class="pre">.values</span></code>).</p>
</div>
</div>
<div class="section" id="jupyter-notebook">
<h2>Jupyter Notebook<a class="headerlink" href="#jupyter-notebook" title="Permalink to this headline"></a></h2>
<p>(<a class="reference external" href="https://github.com/enigmampc/catalyst/blob/master/catalyst/examples/running_catalyst_in_jupyter_notebook.ipynb">This is actual Notebook</a> referenced in the text below)</p>
<p>The <a class="reference external" href="https://jupyter.org/">Jupyter Notebook</a> is a very powerful
browser-based interface to a Python interpreter. As it is already the
de-facto interface for most quantitative researchers, <code class="docutils literal"><span class="pre">catalyst</span></code>
provides an easy way to run your algorithm inside the Notebook without
requiring you to use the CLI. We include this section here as an alternative to
running algorithms through the command line.</p>
<div class="section" id="install">
<h3>Install<a class="headerlink" href="#install" title="Permalink to this headline"></a></h3>
<p>In order to use Jupyter Notebook, you first have to install it inside your
environment. It&#8217;s available as <code class="docutils literal"><span class="pre">pip</span></code> package, so regardless of how you
installed Catalyst, go inside your catalyst environemnt and run:</p>
<div class="code bash highlight-python"><div class="highlight"><pre>(catalyst)$ pip install jupyter
</pre></div>
</div>
<p>Once you have Jupyter Notebook installed, every time you want to use it run:</p>
<div class="code bash highlight-python"><div class="highlight"><pre>(catalyst)$ jupyter notebook
</pre></div>
</div>
<p>A local server will launch, and will open a new window on your browser. That&#8217;s
the interface through which you will interact with Jupyter Notebook.</p>
</div>
<div class="section" id="running-algorithms">
<h3>Running Algorithms<a class="headerlink" href="#running-algorithms" title="Permalink to this headline"></a></h3>
<p>Before running your algorithms inside the Jupyter Notebook, remember to ingest
the data from the command line interface (CLI). In the example below, you would
need to run first:</p>
<div class="code bash highlight-python"><div class="highlight"><pre>catalyst ingest-exchange -x bitfinex -i btc_usd
</pre></div>
</div>
<p>To use Catalyst inside a Jupyter Noebook, you have to write your algorithm in a
cell and let the Jupyter know that it is supposed to execute this algorithm with
Catalyst. This is done via the <code class="docutils literal"><span class="pre">%%catalyst</span></code> IPython magic command that is
available after you import <code class="docutils literal"><span class="pre">catalyst</span></code> from within the Notebook. This magic
takes the same arguments as the command line interface. Thus to run the
algorithm just supply the same parameters as the CLI but without the -f
and -o arguments. We just have to execute the following cell after
importing <code class="docutils literal"><span class="pre">catalyst</span></code> to register the magic.</p>
<div class="code python highlight-python"><div class="highlight"><pre># Register the catalyst magic
%load_ext catalyst
</pre></div>
</div>
<div class="code python highlight-python"><div class="highlight"><pre># Setup matplotlib to display graphs inline in this Notebook
%matplotlib inline
</pre></div>
</div>
<p>Note below that we do not have to specify an input file (-f) since the
magic will use the contents of the cell and look for your algorithm
functions.</p>
<div class="code python highlight-python"><div class="highlight"><pre>%%catalyst --start 2015-3-2 --end 2017-6-28 --capital-base 100000 -x bitfinex -c usd
from catalyst.finance.slippage import VolumeShareSlippage
from catalyst.api import (
order_target_value,
symbol,
record,
cancel_order,
get_open_orders,
)
def initialize(context):
context.ASSET_NAME = &#39;btc_usd&#39;
context.TARGET_HODL_RATIO = 0.8
context.RESERVE_RATIO = 1.0 - context.TARGET_HODL_RATIO
# For all trading pairs in the poloniex bundle, the default denomination
# currently supported by Catalyst is 1/1000th of a full coin. Use this
# constant to scale the price of up to that of a full coin if desired.
context.TICK_SIZE = 1000.0
context.is_buying = True
context.asset = symbol(context.ASSET_NAME)
context.i = 0
def handle_data(context, data):
context.i += 1
starting_cash = context.portfolio.starting_cash
target_hodl_value = context.TARGET_HODL_RATIO * starting_cash
reserve_value = context.RESERVE_RATIO * starting_cash
# Cancel any outstanding orders
orders = get_open_orders(context.asset) or []
for order in orders:
cancel_order(order)
# Stop buying after passing the reserve threshold
cash = context.portfolio.cash
if cash &lt;= reserve_value:
context.is_buying = False
# Retrieve current asset price from pricing data
price = data.current(context.asset, &#39;price&#39;)
# Check if still buying and could (approximately) afford another purchase
if context.is_buying and cash &gt; price:
# Place order to make position in asset equal to target_hodl_value
order_target_value(
context.asset,
target_hodl_value,
limit_price=price*1.1,
stop_price=price*0.9,
)
record(
price=price,
volume=data.current(context.asset, &#39;volume&#39;),
cash=cash,
starting_cash=context.portfolio.starting_cash,
leverage=context.account.leverage,
)
def analyze(context=None, results=None):
import matplotlib.pyplot as plt
# Plot the portfolio and asset data.
ax1 = plt.subplot(611)
results[[&#39;portfolio_value&#39;]].plot(ax=ax1)
ax1.set_ylabel(&#39;Portfolio Value (USD)&#39;)
ax2 = plt.subplot(612, sharex=ax1)
ax2.set_ylabel(&#39;{asset} (USD)&#39;.format(asset=context.ASSET_NAME))
(context.TICK_SIZE * results[[&#39;price&#39;]]).plot(ax=ax2)
trans = results.ix[[t != [] for t in results.transactions]]
buys = trans.ix[
[t[0][&#39;amount&#39;] &gt; 0 for t in trans.transactions]
]
ax2.plot(
buys.index,
context.TICK_SIZE * results.price[buys.index],
&#39;^&#39;,
markersize=10,
color=&#39;g&#39;,
)
ax3 = plt.subplot(613, sharex=ax1)
results[[&#39;leverage&#39;, &#39;alpha&#39;, &#39;beta&#39;]].plot(ax=ax3)
ax3.set_ylabel(&#39;Leverage &#39;)
ax4 = plt.subplot(614, sharex=ax1)
results[[&#39;starting_cash&#39;, &#39;cash&#39;]].plot(ax=ax4)
ax4.set_ylabel(&#39;Cash (USD)&#39;)
results[[
&#39;treasury&#39;,
&#39;algorithm&#39;,
&#39;benchmark&#39;,
]] = results[[
&#39;treasury_period_return&#39;,
&#39;algorithm_period_return&#39;,
&#39;benchmark_period_return&#39;,
]]
ax5 = plt.subplot(615, sharex=ax1)
results[[
&#39;treasury&#39;,
&#39;algorithm&#39;,
&#39;benchmark&#39;,
]].plot(ax=ax5)
ax5.set_ylabel(&#39;Percent Change&#39;)
ax6 = plt.subplot(616, sharex=ax1)
results[[&#39;volume&#39;]].plot(ax=ax6)
ax6.set_ylabel(&#39;Volume (mCoins/5min)&#39;)
plt.legend(loc=3)
# Show the plot.
plt.gcf().set_size_inches(18, 8)
plt.show()
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>[2017-08-11 07:19:46.411748] INFO: Loader: Loading benchmark data for &#39;USDT_BTC&#39; from 1989-12-31 00:00:00+00:00 to 2017-08-09 00:00:00+00:00
[2017-08-11 07:19:46.418983] INFO: Loader: Loading data for /Users/&lt;snipped&gt;/.catalyst/data/USDT_BTC_benchmark.csv failed with error [Unknown string format].
[2017-08-11 07:19:46.419740] INFO: Loader: Cache at /Users/&lt;snipped&gt;/.catalyst/data/USDT_BTC_benchmark.csv does not have data from 1990-01-01 00:00:00+00:00 to 2017-08-09 00:00:00+00:00.
[2017-08-11 07:19:46.420770] INFO: Loader: Downloading benchmark data for &#39;USDT_BTC&#39; from 1989-12-31 00:00:00+00:00 to 2017-08-09 00:00:00+00:00
[2017-08-11 07:19:50.060244] WARNING: Loader: Still don&#39;t have expected data after redownload!
[2017-08-11 07:19:50.097334] WARNING: Loader: Refusing to download new treasury data because a download succeeded at 2017-08-11 06:56:49+00:00.
[2017-08-11 07:19:54.618399] INFO: Performance: Simulated 851 trading days out of 851.
[2017-08-11 07:19:54.619301] INFO: Performance: first open: 2015-03-01 00:00:00+00:00
[2017-08-11 07:19:54.620430] INFO: Performance: last close: 2017-06-28 23:59:00+00:00
</pre></div>
</div>
<div class="figure">
<img alt="png" src="https://i.imgur.com/DS5w47q.png" />
</div>
<div><table border="1" class="dataframe"><thead><tr style="text-align: right;"><th></th><th><p>algo_volatility</p>
</th><th><p>algorithm_period_return</p>
</th><th><p>alpha</p>
</th><th><p>benchmark_period_return</p>
</th><th><p>benchmark_volatility</p>
</th><th><p>beta</p>
</th><th><p>capital_used</p>
</th><th><p>cash</p>
</th><th><p>ending_cash</p>
</th><th><p>ending_exposure</p>
</th><th><p></p>
</th><th><p>starting_cash</p>
</th><th><p>starting_exposure</p>
</th><th><p>starting_value</p>
</th><th><p>trading_days</p>
</th><th><p>transactions</p>
</th><th><p>treasury_period_return</p>
</th><th><p>volume</p>
</th><th><p>treasury</p>
</th><th><p>algorithm</p>
</th><th><p>benchmark</p>
</th></tr></thead><tbody><tr><th><p>2015-03-01 23:59:00+00:00</p>
</th><td><p>NaN</p>
</td><td><p>0.000000</p>
</td><td><p>NaN</p>
</td><td><p>0.045833</p>
</td><td><p>NaN</p>
</td><td><p>NaN</p>
</td><td><p>0.000000</p>
</td><td><p>100000.000000</p>
</td><td><p>100000.000000</p>
</td><td><p>0.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>0.000</p>
</td><td><p>0.000</p>
</td><td><p>1</p>
</td><td><p>[]</p>
</td><td><p>0.0200</p>
</td><td><p>317</p>
</td><td><p>0.0200</p>
</td><td><p>0.000000</p>
</td><td><p>0.045833</p>
</td></tr><tr><th><p>2015-03-02 23:59:00+00:00</p>
</th><td><p>0.000278</p>
</td><td><p>-0.000025</p>
</td><td><p>0.011045</p>
</td><td><p>0.120833</p>
</td><td><p>0.290503</p>
</td><td><p>-0.000956</p>
</td><td><p>-85544.474955</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>85542.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>0.000</p>
</td><td><p>0.000</p>
</td><td><p>2</p>
</td><td><p>[{ucommission: None, uamount: 318, usid:…</p>
</td><td><p>0.0208</p>
</td><td><p>98063</p>
</td><td><p>0.0208</p>
</td><td><p>-0.000025</p>
</td><td><p>0.120833</p>
</td></tr><tr><th><p>2015-03-03 23:59:00+00:00</p>
</th><td><p>0.051796</p>
</td><td><p>-0.005688</p>
</td><td><p>-1.197544</p>
</td><td><p>0.113416</p>
</td><td><p>0.633538</p>
</td><td><p>0.077239</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>84975.642</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>85542.000</p>
</td><td><p>85542.000</p>
</td><td><p>3</p>
</td><td><p>[]</p>
</td><td><p>0.0212</p>
</td><td><p>442983</p>
</td><td><p>0.0212</p>
</td><td><p>-0.005688</p>
</td><td><p>0.113416</p>
</td></tr><tr><th><p>2015-03-04 23:59:00+00:00</p>
</th><td><p>0.342118</p>
</td><td><p>0.034955</p>
</td><td><p>0.401861</p>
</td><td><p>0.166666</p>
</td><td><p>0.524400</p>
</td><td><p>0.181468</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>89040.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>84975.642</p>
</td><td><p>84975.642</p>
</td><td><p>4</p>
</td><td><p>[]</p>
</td><td><p>0.0212</p>
</td><td><p>245889</p>
</td><td><p>0.0212</p>
</td><td><p>0.034955</p>
</td><td><p>0.166666</p>
</td></tr><tr><th><p>2015-03-05 23:59:00+00:00</p>
</th><td><p>0.637226</p>
</td><td><p>-0.038185</p>
</td><td><p>-3.914003</p>
</td><td><p>0.070834</p>
</td><td><p>0.976896</p>
</td><td><p>0.550520</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>81726.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>89040.000</p>
</td><td><p>89040.000</p>
</td><td><p>5</p>
</td><td><p>[]</p>
</td><td><p>0.0211</p>
</td><td><p>117440</p>
</td><td><p>0.0211</p>
</td><td><p>-0.038185</p>
</td><td><p>0.070834</p>
</td></tr><tr><th><p>2015-03-06 23:59:00+00:00</p>
</th><td><p>0.580521</p>
</td><td><p>-0.028645</p>
</td><td><p>-3.100822</p>
</td><td><p>0.083333</p>
</td><td><p>0.874082</p>
</td><td><p>0.546703</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>82680.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>81726.000</p>
</td><td><p>81726.000</p>
</td><td><p>6</p>
</td><td><p>[]</p>
</td><td><p>0.0224</p>
</td><td><p>84197</p>
</td><td><p>0.0224</p>
</td><td><p>-0.028645</p>
</td><td><p>0.083333</p>
</td></tr><tr><th><p>2015-03-07 23:59:00+00:00</p>
</th><td><p>0.530557</p>
</td><td><p>-0.028645</p>
</td><td><p>-2.625704</p>
</td><td><p>0.083333</p>
</td><td><p>0.802793</p>
</td><td><p>0.536589</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>82680.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>82680.000</p>
</td><td><p>82680.000</p>
</td><td><p>7</p>
</td><td><p>[]</p>
</td><td><p>0.0224</p>
</td><td><p>181</p>
</td><td><p>0.0224</p>
</td><td><p>-0.028645</p>
</td><td><p>0.083333</p>
</td></tr><tr><th><p>2015-03-08 23:59:00+00:00</p>
</th><td><p>0.491628</p>
</td><td><p>-0.028645</p>
</td><td><p>-2.276841</p>
</td><td><p>0.083333</p>
</td><td><p>0.746605</p>
</td><td><p>0.529163</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>82680.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>82680.000</p>
</td><td><p>82680.000</p>
</td><td><p>8</p>
</td><td><p>[]</p>
</td><td><p>0.0224</p>
</td><td><p>30900</p>
</td><td><p>0.0224</p>
</td><td><p>-0.028645</p>
</td><td><p>0.083333</p>
</td></tr><tr><th><p>2015-03-09 23:59:00+00:00</p>
</th><td><p>0.467885</p>
</td><td><p>-0.015925</p>
</td><td><p>-1.895269</p>
</td><td><p>0.100000</p>
</td><td><p>0.698764</p>
</td><td><p>0.532652</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>83952.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>82680.000</p>
</td><td><p>82680.000</p>
</td><td><p>9</p>
</td><td><p>[]</p>
</td><td><p>0.0220</p>
</td><td><p>128367</p>
</td><td><p>0.0220</p>
</td><td><p>-0.015925</p>
</td><td><p>0.100000</p>
</td></tr><tr><th><p>2015-03-10 23:59:00+00:00</p>
</th><td><p>0.626552</p>
</td><td><p>0.069935</p>
</td><td><p>-1.625285</p>
</td><td><p>0.212500</p>
</td><td><p>0.800983</p>
</td><td><p>0.676289</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>92538.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>83952.000</p>
</td><td><p>83952.000</p>
</td><td><p>10</p>
</td><td><p>[]</p>
</td><td><p>0.0214</p>
</td><td><p>54961</p>
</td><td><p>0.0214</p>
</td><td><p>0.069935</p>
</td><td><p>0.212500</p>
</td></tr><tr><th><p>2015-03-11 23:59:00+00:00</p>
</th><td><p>0.644515</p>
</td><td><p>0.022235</p>
</td><td><p>-1.727710</p>
</td><td><p>0.150000</p>
</td><td><p>0.834650</p>
</td><td><p>0.684052</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87768.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>92538.000</p>
</td><td><p>92538.000</p>
</td><td><p>11</p>
</td><td><p>[]</p>
</td><td><p>0.0211</p>
</td><td><p>42511</p>
</td><td><p>0.0211</p>
</td><td><p>0.022235</p>
</td><td><p>0.150000</p>
</td></tr><tr><th><p>2015-03-12 23:59:00+00:00</p>
</th><td><p>0.614650</p>
</td><td><p>0.022235</p>
</td><td><p>-1.573455</p>
</td><td><p>0.150000</p>
</td><td><p>0.798403</p>
</td><td><p>0.680882</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87768.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87768.000</p>
</td><td><p>87768.000</p>
</td><td><p>12</p>
</td><td><p>[]</p>
</td><td><p>0.0210</p>
</td><td><p>2909</p>
</td><td><p>0.0210</p>
</td><td><p>0.022235</p>
</td><td><p>0.150000</p>
</td></tr><tr><th><p>2015-03-13 23:59:00+00:00</p>
</th><td><p>0.588942</p>
</td><td><p>0.019405</p>
</td><td><p>-1.454733</p>
</td><td><p>0.146291</p>
</td><td><p>0.767688</p>
</td><td><p>0.677881</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87484.980</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87768.000</p>
</td><td><p>87768.000</p>
</td><td><p>13</p>
</td><td><p>[]</p>
</td><td><p>0.0213</p>
</td><td><p>57613</p>
</td><td><p>0.0213</p>
</td><td><p>0.019405</p>
</td><td><p>0.146291</p>
</td></tr><tr><th><p>2015-03-14 23:59:00+00:00</p>
</th><td><p>0.565911</p>
</td><td><p>0.019373</p>
</td><td><p>-1.344915</p>
</td><td><p>0.146250</p>
</td><td><p>0.739230</p>
</td><td><p>0.675665</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87481.800</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87484.980</p>
</td><td><p>87484.980</p>
</td><td><p>14</p>
</td><td><p>[]</p>
</td><td><p>0.0213</p>
</td><td><p>48310</p>
</td><td><p>0.0213</p>
</td><td><p>0.019373</p>
</td><td><p>0.146250</p>
</td></tr><tr><th><p>2015-03-15 23:59:00+00:00</p>
</th><td><p>0.551394</p>
</td><td><p>0.041659</p>
</td><td><p>-1.191436</p>
</td><td><p>0.175450</p>
</td><td><p>0.714876</p>
</td><td><p>0.680484</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>89710.344</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87481.800</p>
</td><td><p>87481.800</p>
</td><td><p>15</p>
</td><td><p>[]</p>
</td><td><p>0.0213</p>
</td><td><p>29454</p>
</td><td><p>0.0213</p>
</td><td><p>0.041659</p>
</td><td><p>0.175450</p>
</td></tr><tr><th><p>2015-03-16 23:59:00+00:00</p>
</th><td><p>0.541846</p>
</td><td><p>0.019055</p>
</td><td><p>-1.188212</p>
</td><td><p>0.145833</p>
</td><td><p>0.706049</p>
</td><td><p>0.680281</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87450.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>89710.344</p>
</td><td><p>89710.344</p>
</td><td><p>16</p>
</td><td><p>[]</p>
</td><td><p>0.0210</p>
</td><td><p>25564</p>
</td><td><p>0.0210</p>
</td><td><p>0.019055</p>
</td><td><p>0.145833</p>
</td></tr><tr><th><p>2015-03-17 23:59:00+00:00</p>
</th><td><p>0.524682</p>
</td><td><p>0.019055</p>
</td><td><p>-1.115149</p>
</td><td><p>0.145833</p>
</td><td><p>0.684599</p>
</td><td><p>0.678870</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>87450.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87450.000</p>
</td><td><p>87450.000</p>
</td><td><p>17</p>
</td><td><p>[]</p>
</td><td><p>0.0206</p>
</td><td><p>9</p>
</td><td><p>0.0206</p>
</td><td><p>0.019055</p>
</td><td><p>0.145833</p>
</td></tr><tr><th><p>2015-03-18 23:59:00+00:00</p>
</th><td><p>0.532621</p>
</td><td><p>-0.021999</p>
</td><td><p>-1.180440</p>
</td><td><p>0.092041</p>
</td><td><p>0.696261</p>
</td><td><p>0.685307</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>83344.620</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>87450.000</p>
</td><td><p>87450.000</p>
</td><td><p>18</p>
</td><td><p>[]</p>
</td><td><p>0.0193</p>
</td><td><p>164911</p>
</td><td><p>0.0193</p>
</td><td><p>-0.021999</p>
</td><td><p>0.092041</p>
</td></tr><tr><th><p>2015-03-19 23:59:00+00:00</p>
</th><td><p>0.518811</p>
</td><td><p>-0.013234</p>
</td><td><p>-1.096387</p>
</td><td><p>0.103526</p>
</td><td><p>0.676861</p>
</td><td><p>0.686186</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>84221.028</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>83344.620</p>
</td><td><p>83344.620</p>
</td><td><p>19</p>
</td><td><p>[]</p>
</td><td><p>0.0198</p>
</td><td><p>713904</p>
</td><td><p>0.0198</p>
</td><td><p>-0.013234</p>
</td><td><p>0.103526</p>
</td></tr><tr><th><p>2015-03-20 23:59:00+00:00</p>
</th><td><p>0.505168</p>
</td><td><p>-0.017324</p>
</td><td><p>-1.050273</p>
</td><td><p>0.098170</p>
</td><td><p>0.659945</p>
</td><td><p>0.685070</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>83812.080</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>84221.028</p>
</td><td><p>84221.028</p>
</td><td><p>20</p>
</td><td><p>[]</p>
</td><td><p>0.0193</p>
</td><td><p>132725</p>
</td><td><p>0.0193</p>
</td><td><p>-0.017324</p>
</td><td><p>0.098170</p>
</td></tr><tr><th><p>2015-03-21 23:59:00+00:00</p>
</th><td><p>0.492384</p>
</td><td><p>-0.018494</p>
</td><td><p>-1.002051</p>
</td><td><p>0.096637</p>
</td><td><p>0.643679</p>
</td><td><p>0.684283</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>83695.056</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>83812.080</p>
</td><td><p>83812.080</p>
</td><td><p>21</p>
</td><td><p>[]</p>
</td><td><p>0.0193</p>
</td><td><p>201155</p>
</td><td><p>0.0193</p>
</td><td><p>-0.018494</p>
</td><td><p>0.096637</p>
</td></tr><tr><th><p>2015-03-22 23:59:00+00:00</p>
</th><td><p>0.482998</p>
</td><td><p>-0.004744</p>
</td><td><p>-0.927947</p>
</td><td><p>0.114653</p>
</td><td><p>0.629319</p>
</td><td><p>0.686478</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>85070.088</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>83695.056</p>
</td><td><p>83695.056</p>
</td><td><p>22</p>
</td><td><p>[]</p>
</td><td><p>0.0193</p>
</td><td><p>64378</p>
</td><td><p>0.0193</p>
</td><td><p>-0.004744</p>
</td><td><p>0.114653</p>
</td></tr><tr><th><p>2015-03-23 23:59:00+00:00</p>
</th><td><p>0.477523</p>
</td><td><p>-0.026505</p>
</td><td><p>-0.935352</p>
</td><td><p>0.086139</p>
</td><td><p>0.623502</p>
</td><td><p>0.687025</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>82894.014</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>85070.088</p>
</td><td><p>85070.088</p>
</td><td><p>23</p>
</td><td><p>[]</p>
</td><td><p>0.0192</p>
</td><td><p>61850</p>
</td><td><p>0.0192</p>
</td><td><p>-0.026505</p>
</td><td><p>0.086139</p>
</td></tr><tr><th><p>2015-03-24 23:59:00+00:00</p>
</th><td><p>0.504086</p>
</td><td><p>-0.084215</p>
</td><td><p>-1.021023</p>
</td><td><p>0.010523</p>
</td><td><p>0.655188</p>
</td><td><p>0.701025</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>77122.950</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>82894.014</p>
</td><td><p>82894.014</p>
</td><td><p>24</p>
</td><td><p>[]</p>
</td><td><p>0.0188</p>
</td><td><p>490180</p>
</td><td><p>0.0188</p>
</td><td><p>-0.084215</p>
</td><td><p>0.010523</p>
</td></tr><tr><th><p>2015-03-25 23:59:00+00:00</p>
</th><td><p>0.497690</p>
</td><td><p>-0.068474</p>
</td><td><p>-0.952786</p>
</td><td><p>0.031148</p>
</td><td><p>0.644272</p>
</td><td><p>0.704251</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>78697.050</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>77122.950</p>
</td><td><p>77122.950</p>
</td><td><p>25</p>
</td><td><p>[]</p>
</td><td><p>0.0193</p>
</td><td><p>90862</p>
</td><td><p>0.0193</p>
</td><td><p>-0.068474</p>
</td><td><p>0.031148</p>
</td></tr><tr><th><p>2015-03-26 23:59:00+00:00</p>
</th><td><p>0.489730</p>
</td><td><p>-0.084215</p>
</td><td><p>-0.943240</p>
</td><td><p>0.010523</p>
</td><td><p>0.634965</p>
</td><td><p>0.703738</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>77122.950</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>78697.050</p>
</td><td><p>78697.050</p>
</td><td><p>26</p>
</td><td><p>[]</p>
</td><td><p>0.0201</p>
</td><td><p>2299</p>
</td><td><p>0.0201</p>
</td><td><p>-0.084215</p>
</td><td><p>0.010523</p>
</td></tr><tr><th><p>2015-03-27 23:59:00+00:00</p>
</th><td><p>0.495916</p>
</td><td><p>-0.049785</p>
</td><td><p>-0.857592</p>
</td><td><p>0.055636</p>
</td><td><p>0.636644</p>
</td><td><p>0.713671</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>80565.936</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>77122.950</p>
</td><td><p>77122.950</p>
</td><td><p>27</p>
</td><td><p>[]</p>
</td><td><p>0.0195</p>
</td><td><p>663</p>
</td><td><p>0.0195</p>
</td><td><p>-0.049785</p>
</td><td><p>0.055636</p>
</td></tr><tr><th><p>2015-03-28 23:59:00+00:00</p>
</th><td><p>0.488469</p>
</td><td><p>-0.064490</p>
</td><td><p>-0.848769</p>
</td><td><p>0.036368</p>
</td><td><p>0.627920</p>
</td><td><p>0.713212</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>79095.504</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>80565.936</p>
</td><td><p>80565.936</p>
</td><td><p>28</p>
</td><td><p>[]</p>
</td><td><p>0.0195</p>
</td><td><p>7061</p>
</td><td><p>0.0195</p>
</td><td><p>-0.064490</p>
</td><td><p>0.036368</p>
</td></tr><tr><th><p>2015-03-29 23:59:00+00:00</p>
</th><td><p>0.479671</p>
</td><td><p>-0.066903</p>
</td><td><p>-0.822844</p>
</td><td><p>0.033205</p>
</td><td><p>0.616787</p>
</td><td><p>0.712868</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>78854.142</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>79095.504</p>
</td><td><p>79095.504</p>
</td><td><p>29</p>
</td><td><p>[]</p>
</td><td><p>0.0195</p>
</td><td><p>8526</p>
</td><td><p>0.0195</p>
</td><td><p>-0.066903</p>
</td><td><p>0.033205</p>
</td></tr><tr><th><p>2015-03-30 23:59:00+00:00</p>
</th><td><p>0.476306</p>
</td><td><p>-0.046605</p>
</td><td><p>-0.769239</p>
</td><td><p>0.059803</p>
</td><td><p>0.610002</p>
</td><td><p>0.716464</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>80883.936</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>78854.142</p>
</td><td><p>78854.142</p>
</td><td><p>30</p>
</td><td><p>[]</p>
</td><td><p>0.0196</p>
</td><td><p>29654</p>
</td><td><p>0.0196</p>
</td><td><p>-0.046605</p>
</td><td><p>0.059803</p>
</td></tr><tr><th><p></p>
</th><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td><td><p></p>
</td></tr><tr><th><p>2017-05-30 23:59:00+00:00</p>
</th><td><p>0.495432</p>
</td><td><p>5.949752</p>
</td><td><p>-0.016611</p>
</td><td><p>7.916664</p>
</td><td><p>0.554369</p>
</td><td><p>0.888883</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>680519.682</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>701826.000</p>
</td><td><p>701826.000</p>
</td><td><p>822</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>40157964723</p>
</td><td><p>0.0221</p>
</td><td><p>5.949752</p>
</td><td><p>7.916664</p>
</td></tr><tr><th><p>2017-05-31 23:59:00+00:00</p>
</th><td><p>0.495243</p>
</td><td><p>6.102328</p>
</td><td><p>-0.017086</p>
</td><td><p>8.154164</p>
</td><td><p>0.554182</p>
</td><td><p>0.888844</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>695777.322</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>680519.682</p>
</td><td><p>680519.682</p>
</td><td><p>823</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>31098652109</p>
</td><td><p>0.0221</p>
</td><td><p>6.102328</p>
</td><td><p>8.154164</p>
</td></tr><tr><th><p>2017-06-01 23:59:00+00:00</p>
</th><td><p>0.495836</p>
</td><td><p>6.504967</p>
</td><td><p>-0.014668</p>
</td><td><p>8.644144</p>
</td><td><p>0.554541</p>
</td><td><p>0.889303</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>736041.210</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>695777.322</p>
</td><td><p>695777.322</p>
</td><td><p>824</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>40944880757</p>
</td><td><p>0.0221</p>
</td><td><p>6.504967</p>
</td><td><p>8.644144</p>
</td></tr><tr><th><p>2017-06-02 23:59:00+00:00</p>
</th><td><p>0.495948</p>
</td><td><p>6.801995</p>
</td><td><p>-0.013641</p>
</td><td><p>9.033331</p>
</td><td><p>0.554581</p>
</td><td><p>0.889440</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>765744.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>736041.210</p>
</td><td><p>736041.210</p>
</td><td><p>825</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>22364557424</p>
</td><td><p>0.0215</p>
</td><td><p>6.801995</p>
</td><td><p>9.033331</p>
</td></tr><tr><th><p>2017-06-03 23:59:00+00:00</p>
</th><td><p>0.495729</p>
</td><td><p>6.952409</p>
</td><td><p>-0.013100</p>
</td><td><p>9.230418</p>
</td><td><p>0.554317</p>
</td><td><p>0.889470</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>780785.400</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>765744.000</p>
</td><td><p>765744.000</p>
</td><td><p>826</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>23687278961</p>
</td><td><p>0.0215</p>
</td><td><p>6.952409</p>
</td><td><p>9.230418</p>
</td></tr><tr><th><p>2017-06-04 23:59:00+00:00</p>
</th><td><p>0.495450</p>
</td><td><p>7.042244</p>
</td><td><p>-0.012768</p>
</td><td><p>9.348122</p>
</td><td><p>0.553999</p>
</td><td><p>0.889479</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>789768.900</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>780785.400</p>
</td><td><p>780785.400</p>
</td><td><p>827</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>21332021248</p>
</td><td><p>0.0215</p>
</td><td><p>7.042244</p>
</td><td><p>9.348122</p>
</td></tr><tr><th><p>2017-06-05 23:59:00+00:00</p>
</th><td><p>0.496148</p>
</td><td><p>7.524987</p>
</td><td><p>-0.011320</p>
</td><td><p>9.980649</p>
</td><td><p>0.554578</p>
</td><td><p>0.889805</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>838043.208</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>789768.900</p>
</td><td><p>789768.900</p>
</td><td><p>828</p>
</td><td><p>[]</p>
</td><td><p>0.0218</p>
</td><td><p>22372229837</p>
</td><td><p>0.0218</p>
</td><td><p>7.524987</p>
</td><td><p>9.980649</p>
</td></tr><tr><th><p>2017-06-06 23:59:00+00:00</p>
</th><td><p>0.497592</p>
</td><td><p>8.194835</p>
</td><td><p>-0.009554</p>
</td><td><p>10.858330</p>
</td><td><p>0.555841</p>
</td><td><p>0.890368</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>905028.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>838043.208</p>
</td><td><p>838043.208</p>
</td><td><p>829</p>
</td><td><p>[]</p>
</td><td><p>0.0214</p>
</td><td><p>81923184446</p>
</td><td><p>0.0214</p>
</td><td><p>8.194835</p>
</td><td><p>10.858330</p>
</td></tr><tr><th><p>2017-06-07 23:59:00+00:00</p>
</th><td><p>0.498895</p>
</td><td><p>7.557258</p>
</td><td><p>-0.011975</p>
</td><td><p>10.022932</p>
</td><td><p>0.557003</p>
</td><td><p>0.890845</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>841270.272</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>905028.000</p>
</td><td><p>905028.000</p>
</td><td><p>830</p>
</td><td><p>[]</p>
</td><td><p>0.0218</p>
</td><td><p>49070430356</p>
</td><td><p>0.0218</p>
</td><td><p>7.557258</p>
</td><td><p>10.022932</p>
</td></tr><tr><th><p>2017-06-08 23:59:00+00:00</p>
</th><td><p>0.499349</p>
</td><td><p>8.010395</p>
</td><td><p>-0.010676</p>
</td><td><p>10.616664</p>
</td><td><p>0.557357</p>
</td><td><p>0.891092</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>886584.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>841270.272</p>
</td><td><p>841270.272</p>
</td><td><p>831</p>
</td><td><p>[]</p>
</td><td><p>0.0219</p>
</td><td><p>34013412940</p>
</td><td><p>0.0219</p>
</td><td><p>8.010395</p>
</td><td><p>10.616664</p>
</td></tr><tr><th><p>2017-06-09 23:59:00+00:00</p>
</th><td><p>0.499063</p>
</td><td><p>8.099750</p>
</td><td><p>-0.010386</p>
</td><td><p>10.733746</p>
</td><td><p>0.557033</p>
</td><td><p>0.891098</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>895519.482</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>886584.000</p>
</td><td><p>886584.000</p>
</td><td><p>832</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>25275425996</p>
</td><td><p>0.0221</p>
</td><td><p>8.099750</p>
</td><td><p>10.733746</p>
</td></tr><tr><th><p>2017-06-10 23:59:00+00:00</p>
</th><td><p>0.498769</p>
</td><td><p>8.086143</p>
</td><td><p>-0.010416</p>
</td><td><p>10.715915</p>
</td><td><p>0.556705</p>
</td><td><p>0.891098</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>894158.760</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>895519.482</p>
</td><td><p>895519.482</p>
</td><td><p>833</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>30620792046</p>
</td><td><p>0.0221</p>
</td><td><p>8.086143</p>
</td><td><p>10.715915</p>
</td></tr><tr><th><p>2017-06-11 23:59:00+00:00</p>
</th><td><p>0.498971</p>
</td><td><p>8.484533</p>
</td><td><p>-0.009305</p>
</td><td><p>11.237914</p>
</td><td><p>0.556827</p>
</td><td><p>0.891266</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>933997.800</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>894158.760</p>
</td><td><p>894158.760</p>
</td><td><p>834</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>30830678595</p>
</td><td><p>0.0221</p>
</td><td><p>8.484533</p>
</td><td><p>11.237914</p>
</td></tr><tr><th><p>2017-06-12 23:59:00+00:00</p>
</th><td><p>0.503448</p>
</td><td><p>7.320494</p>
</td><td><p>-0.014065</p>
</td><td><p>9.712706</p>
</td><td><p>0.560936</p>
</td><td><p>0.892695</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>817593.900</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>933997.800</p>
</td><td><p>933997.800</p>
</td><td><p>835</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>88704710635</p>
</td><td><p>0.0221</p>
</td><td><p>7.320494</p>
</td><td><p>9.712706</p>
</td></tr><tr><th><p>2017-06-13 23:59:00+00:00</p>
</th><td><p>0.503565</p>
</td><td><p>7.656697</p>
</td><td><p>-0.013054</p>
</td><td><p>10.153225</p>
</td><td><p>0.560981</p>
</td><td><p>0.892830</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>851214.132</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>817593.900</p>
</td><td><p>817593.900</p>
</td><td><p>836</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>42251296767</p>
</td><td><p>0.0221</p>
</td><td><p>7.656697</p>
</td><td><p>10.153225</p>
</td></tr><tr><th><p>2017-06-14 23:59:00+00:00</p>
</th><td><p>0.506845</p>
</td><td><p>6.734516</p>
</td><td><p>-0.016873</p>
</td><td><p>8.944917</p>
</td><td><p>0.563995</p>
</td><td><p>0.893862</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>758996.040</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>851214.132</p>
</td><td><p>851214.132</p>
</td><td><p>837</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>63183088135</p>
</td><td><p>0.0215</p>
</td><td><p>6.734516</p>
</td><td><p>8.944917</p>
</td></tr><tr><th><p>2017-06-15 23:59:00+00:00</p>
</th><td><p>0.506562</p>
</td><td><p>6.695367</p>
</td><td><p>-0.016991</p>
</td><td><p>8.893622</p>
</td><td><p>0.563678</p>
</td><td><p>0.893865</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>755081.142</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>758996.040</p>
</td><td><p>758996.040</p>
</td><td><p>838</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>104677533974</p>
</td><td><p>0.0216</p>
</td><td><p>6.695367</p>
</td><td><p>8.893622</p>
</td></tr><tr><th><p>2017-06-16 23:59:00+00:00</p>
</th><td><p>0.506404</p>
</td><td><p>6.887855</p>
</td><td><p>-0.016343</p>
</td><td><p>9.145831</p>
</td><td><p>0.563472</p>
</td><td><p>0.893913</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>774330.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>755081.142</p>
</td><td><p>755081.142</p>
</td><td><p>839</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>43479966625</p>
</td><td><p>0.0216</p>
</td><td><p>6.887855</p>
</td><td><p>9.145831</p>
</td></tr><tr><th><p>2017-06-17 23:59:00+00:00</p>
</th><td><p>0.507407</p>
</td><td><p>7.435283</p>
</td><td><p>-0.014812</p>
</td><td><p>9.863113</p>
</td><td><p>0.564341</p>
</td><td><p>0.894311</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>829072.746</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>774330.000</p>
</td><td><p>774330.000</p>
</td><td><p>840</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>36800919715</p>
</td><td><p>0.0216</p>
</td><td><p>7.435283</p>
</td><td><p>9.863113</p>
</td></tr><tr><th><p>2017-06-18 23:59:00+00:00</p>
</th><td><p>0.507740</p>
</td><td><p>7.070069</p>
</td><td><p>-0.016112</p>
</td><td><p>9.384581</p>
</td><td><p>0.564605</p>
</td><td><p>0.894482</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>792551.400</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>829072.746</p>
</td><td><p>829072.746</p>
</td><td><p>841</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>46411759478</p>
</td><td><p>0.0216</p>
</td><td><p>7.070069</p>
</td><td><p>9.384581</p>
</td></tr><tr><th><p>2017-06-19 23:59:00+00:00</p>
</th><td><p>0.507754</p>
</td><td><p>7.358645</p>
</td><td><p>-0.015226</p>
</td><td><p>9.762694</p>
</td><td><p>0.564557</p>
</td><td><p>0.894583</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>821408.946</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>792551.400</p>
</td><td><p>792551.400</p>
</td><td><p>842</p>
</td><td><p>[]</p>
</td><td><p>0.0219</p>
</td><td><p>28294406623</p>
</td><td><p>0.0219</p>
</td><td><p>7.358645</p>
</td><td><p>9.762694</p>
</td></tr><tr><th><p>2017-06-20 23:59:00+00:00</p>
</th><td><p>0.507705</p>
</td><td><p>7.628795</p>
</td><td><p>-0.014414</p>
</td><td><p>10.116664</p>
</td><td><p>0.564451</p>
</td><td><p>0.894665</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>848424.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>821408.946</p>
</td><td><p>821408.946</p>
</td><td><p>843</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>36903854052</p>
</td><td><p>0.0216</p>
</td><td><p>7.628795</p>
</td><td><p>10.116664</p>
</td></tr><tr><th><p>2017-06-21 23:59:00+00:00</p>
</th><td><p>0.507531</p>
</td><td><p>7.476155</p>
</td><td><p>-0.014900</p>
</td><td><p>9.916664</p>
</td><td><p>0.564238</p>
</td><td><p>0.894696</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>833160.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>848424.000</p>
</td><td><p>848424.000</p>
</td><td><p>844</p>
</td><td><p>[]</p>
</td><td><p>0.0216</p>
</td><td><p>43815656010</p>
</td><td><p>0.0216</p>
</td><td><p>7.476155</p>
</td><td><p>9.916664</p>
</td></tr><tr><th><p>2017-06-22 23:59:00+00:00</p>
</th><td><p>0.507315</p>
</td><td><p>7.645891</p>
</td><td><p>-0.014372</p>
</td><td><p>10.139065</p>
</td><td><p>0.563979</p>
</td><td><p>0.894725</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>850133.568</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>833160.000</p>
</td><td><p>833160.000</p>
</td><td><p>845</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>22304647568</p>
</td><td><p>0.0215</p>
</td><td><p>7.645891</p>
</td><td><p>10.139065</p>
</td></tr><tr><th><p>2017-06-23 23:59:00+00:00</p>
</th><td><p>0.507020</p>
</td><td><p>7.635155</p>
</td><td><p>-0.014388</p>
</td><td><p>10.124997</p>
</td><td><p>0.563652</p>
</td><td><p>0.894725</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>849060.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>850133.568</p>
</td><td><p>850133.568</p>
</td><td><p>846</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>13090231864</p>
</td><td><p>0.0215</p>
</td><td><p>7.635155</p>
</td><td><p>10.124997</p>
</td></tr><tr><th><p>2017-06-24 23:59:00+00:00</p>
</th><td><p>0.507936</p>
</td><td><p>7.105628</p>
</td><td><p>-0.016304</p>
</td><td><p>9.431173</p>
</td><td><p>0.564463</p>
</td><td><p>0.895061</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>796107.276</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>849060.000</p>
</td><td><p>849060.000</p>
</td><td><p>847</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>34088563732</p>
</td><td><p>0.0215</p>
</td><td><p>7.105628</p>
</td><td><p>9.431173</p>
</td></tr><tr><th><p>2017-06-25 23:59:00+00:00</p>
</th><td><p>0.507675</p>
</td><td><p>7.036714</p>
</td><td><p>-0.016515</p>
</td><td><p>9.340880</p>
</td><td><p>0.564168</p>
</td><td><p>0.895069</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>789215.898</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>796107.276</p>
</td><td><p>796107.276</p>
</td><td><p>848</p>
</td><td><p>[]</p>
</td><td><p>0.0215</p>
</td><td><p>41560204433</p>
</td><td><p>0.0215</p>
</td><td><p>7.036714</p>
</td><td><p>9.340880</p>
</td></tr><tr><th><p>2017-06-26 23:59:00+00:00</p>
</th><td><p>0.507780</p>
</td><td><p>6.761571</p>
</td><td><p>-0.017485</p>
</td><td><p>8.980368</p>
</td><td><p>0.564221</p>
</td><td><p>0.895175</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>761701.584</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>789215.898</p>
</td><td><p>789215.898</p>
</td><td><p>849</p>
</td><td><p>[]</p>
</td><td><p>0.0214</p>
</td><td><p>73840480752</p>
</td><td><p>0.0214</p>
</td><td><p>6.761571</p>
</td><td><p>8.980368</p>
</td></tr><tr><th><p>2017-06-27 23:59:00+00:00</p>
</th><td><p>0.508048</p>
</td><td><p>7.126355</p>
</td><td><p>-0.016390</p>
</td><td><p>9.458331</p>
</td><td><p>0.564409</p>
</td><td><p>0.895349</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>798180.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>761701.584</p>
</td><td><p>761701.584</p>
</td><td><p>850</p>
</td><td><p>[]</p>
</td><td><p>0.0221</p>
</td><td><p>62426319778</p>
</td><td><p>0.0221</p>
</td><td><p>7.126355</p>
</td><td><p>9.458331</p>
</td></tr><tr><th><p>2017-06-28 23:59:00+00:00</p>
</th><td><p>0.507750</p>
</td><td><p>7.135895</p>
</td><td><p>-0.016340</p>
</td><td><p>9.470831</p>
</td><td><p>0.564078</p>
</td><td><p>0.895349</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>799134.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>798180.000</p>
</td><td><p>798180.000</p>
</td><td><p>851</p>
</td><td><p>[]</p>
</td><td><p>0.0222</p>
</td><td><p>39676839183</p>
</td><td><p>0.0222</p>
</td><td><p>7.135895</p>
</td><td><p>9.470831</p>
</td></tr></tbody></table><p><p>851 rows × 45 columns</p>
</p></div><p>Also, instead of defining an output file we are accessing it via the “_&#8221;
variable that will be created in the name space and contain the
performance DataFrame.</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">_</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
<div><table border="1" class="dataframe"><thead><tr style="text-align: right;"><th></th><th><p>algo_volatility</p>
</th><th><p>algorithm_period_return</p>
</th><th><p>alpha</p>
</th><th><p>benchmark_period_return</p>
</th><th><p>benchmark_volatility</p>
</th><th><p>beta</p>
</th><th><p>capital_used</p>
</th><th><p>cash</p>
</th><th><p>ending_cash</p>
</th><th><p>ending_exposure</p>
</th><th><p></p>
</th><th><p>starting_cash</p>
</th><th><p>starting_exposure</p>
</th><th><p>starting_value</p>
</th><th><p>trading_days</p>
</th><th><p>transactions</p>
</th><th><p>treasury_period_return</p>
</th><th><p>volume</p>
</th><th><p>treasury</p>
</th><th><p>algorithm</p>
</th><th><p>benchmark</p>
</th></tr></thead><tbody><tr><th><p>2015-03-01 23:59:00+00:00</p>
</th><td><p>NaN</p>
</td><td><p>0.000000</p>
</td><td><p>NaN</p>
</td><td><p>0.045833</p>
</td><td><p>NaN</p>
</td><td><p>NaN</p>
</td><td><p>0.000000</p>
</td><td><p>100000.000000</p>
</td><td><p>100000.000000</p>
</td><td><p>0.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>0.000</p>
</td><td><p>0.000</p>
</td><td><p>1</p>
</td><td><p>[]</p>
</td><td><p>0.0200</p>
</td><td><p>317</p>
</td><td><p>0.0200</p>
</td><td><p>0.000000</p>
</td><td><p>0.045833</p>
</td></tr><tr><th><p>2015-03-02 23:59:00+00:00</p>
</th><td><p>0.000278</p>
</td><td><p>-0.000025</p>
</td><td><p>0.011045</p>
</td><td><p>0.120833</p>
</td><td><p>0.290503</p>
</td><td><p>-0.000956</p>
</td><td><p>-85544.474955</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>85542.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>0.000</p>
</td><td><p>0.000</p>
</td><td><p>2</p>
</td><td><p>[{ucommission: None, uamount: 318, usid:…</p>
</td><td><p>0.0208</p>
</td><td><p>98063</p>
</td><td><p>0.0208</p>
</td><td><p>-0.000025</p>
</td><td><p>0.120833</p>
</td></tr><tr><th><p>2015-03-03 23:59:00+00:00</p>
</th><td><p>0.051796</p>
</td><td><p>-0.005688</p>
</td><td><p>-1.197544</p>
</td><td><p>0.113416</p>
</td><td><p>0.633538</p>
</td><td><p>0.077239</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>84975.642</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>85542.000</p>
</td><td><p>85542.000</p>
</td><td><p>3</p>
</td><td><p>[]</p>
</td><td><p>0.0212</p>
</td><td><p>442983</p>
</td><td><p>0.0212</p>
</td><td><p>-0.005688</p>
</td><td><p>0.113416</p>
</td></tr><tr><th><p>2015-03-04 23:59:00+00:00</p>
</th><td><p>0.342118</p>
</td><td><p>0.034955</p>
</td><td><p>0.401861</p>
</td><td><p>0.166666</p>
</td><td><p>0.524400</p>
</td><td><p>0.181468</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>89040.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>84975.642</p>
</td><td><p>84975.642</p>
</td><td><p>4</p>
</td><td><p>[]</p>
</td><td><p>0.0212</p>
</td><td><p>245889</p>
</td><td><p>0.0212</p>
</td><td><p>0.034955</p>
</td><td><p>0.166666</p>
</td></tr><tr><th><p>2015-03-05 23:59:00+00:00</p>
</th><td><p>0.637226</p>
</td><td><p>-0.038185</p>
</td><td><p>-3.914003</p>
</td><td><p>0.070834</p>
</td><td><p>0.976896</p>
</td><td><p>0.550520</p>
</td><td><p>0.000000</p>
</td><td><p>14455.525045</p>
</td><td><p>14455.525045</p>
</td><td><p>81726.000</p>
</td><td><p></p>
</td><td><p>100000.0</p>
</td><td><p>89040.000</p>
</td><td><p>89040.000</p>
</td><td><p>5</p>
</td><td><p>[]</p>
</td><td><p>0.0211</p>
</td><td><p>117440</p>
</td><td><p>0.0211</p>
</td><td><p>-0.038185</p>
</td><td><p>0.070834</p>
</td></tr></tbody></table><p><p>5 rows × 45 columns</p>
</p></div></div>
</div>
<div class="section" id="next-steps">
<h2>Next steps<a class="headerlink" href="#next-steps" title="Permalink to this headline"></a></h2>
<p>We hope that this tutorial gave you a little insight into the
architecture, API, and features of Catalyst. For next steps, check
out some of the other <a class="reference internal" href="example-algos.html"><em>example algorithms</em></a>.</p>
<p>Feel free to ask questions on the <code class="docutils literal"><span class="pre">#catalyst_dev</span></code> channel of our
<a class="reference external" href="https://discord.gg/SJK32GY">Discord group</a> and report
problems on our <a class="reference external" href="https://github.com/enigmampc/catalyst/issues">GitHub issue tracker</a>.</p>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="live-trading.html" class="btn btn-neutral float-right" title="Live Trading" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="install.html" class="btn btn-neutral" title="Install" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2017, Enigma MPC, Inc..
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.3',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>