Files
catalyst/live-trading.html
T
2018-02-22 22:16:47 -07:00

427 lines
18 KiB
HTML

<!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>Live Trading &mdash; Catalyst 0.4 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="Catalyst 0.4 documentation" href="index.html"/>
<link rel="next" title="Features" href="features.html"/>
<link rel="prev" title="Catalyst Beginner Tutorial" href="beginner-tutorial.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-scroll">
<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></li>
<li class="toctree-l1"><a class="reference internal" href="beginner-tutorial.html">Catalyst Beginner Tutorial</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Live Trading</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#supported-exchanges">Supported Exchanges</a></li>
<li class="toctree-l2"><a class="reference internal" href="#paper-trading-vs-live-trading-modes">Paper Trading vs Live Trading modes</a></li>
<li class="toctree-l2"><a class="reference internal" href="#authentication">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="#currency-symbols">Currency Symbols</a></li>
<li class="toctree-l2"><a class="reference internal" href="#trading-an-algorithm">Trading an Algorithm</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="features.html">Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="example-algos.html">Example Algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="utilities.html">Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="videos.html">Videos</a></li>
<li class="toctree-l1"><a class="reference internal" href="resources.html">Resources</a></li>
<li class="toctree-l1"><a class="reference internal" href="development-guidelines.html">Development Guidelines</a></li>
<li class="toctree-l1"><a class="reference internal" href="releases.html">Release Notes</a></li>
</ul>
</div>
</div>
</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>Live Trading</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/live-trading.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="live-trading">
<h1>Live Trading<a class="headerlink" href="#live-trading" title="Permalink to this headline"></a></h1>
<p>This document explains how to get started with live trading.</p>
<div class="section" id="supported-exchanges">
<h2>Supported Exchanges<a class="headerlink" href="#supported-exchanges" title="Permalink to this headline"></a></h2>
<p>Since version 0.4, Catalyst integrated with <a class="reference external" href="https://github.com/ccxt/ccxt">CCXT</a>,
a cryptocurrency trading library with support for more than 90 exchanges. The
range of CCXT and Catalyst support for each of those exchanges varies greatly.
The most supported exchanges are as follows:</p>
<p>The exchanges available for backtesting are fully supported in live mode:</p>
<ul class="simple">
<li>Bitfinex, id = <code class="docutils literal"><span class="pre">bitfinex</span></code></li>
<li>Bittrex, id = <code class="docutils literal"><span class="pre">bittrex</span></code></li>
<li>Poloniex, id = <code class="docutils literal"><span class="pre">poloniex</span></code></li>
</ul>
<p>Additionally, we have successfully tested the following exchanges:</p>
<ul class="simple">
<li>Binance, id = <code class="docutils literal"><span class="pre">binance</span></code></li>
<li>Bitmex, id = <code class="docutils literal"><span class="pre">bitmex</span></code></li>
<li>GDAX, id = <code class="docutils literal"><span class="pre">gdax</span></code></li>
</ul>
<p>As Catalyst is currently in Alpha and in under active development, you are
encouraged to throughly test any exchange in <em>paper trading</em> mode before trading
<em>live</em> with it.</p>
</div>
<div class="section" id="paper-trading-vs-live-trading-modes">
<h2>Paper Trading vs Live Trading modes<a class="headerlink" href="#paper-trading-vs-live-trading-modes" title="Permalink to this headline"></a></h2>
<p>Catalyst currently supports three different modes in which you can execute your
trading algorithm. The first is <strong>backtesting</strong>, which is covered extensively in
the tutorial, and uses historical data to run your algorithm. There is no
interaction with the exchange in backtesting mode, and this is the first mode
that you should test any new algorithm.</p>
<p>Once you are confident with the simulations that you have obtained with your
algorithm in backtesting, you may switch to live trading, where you have two
different modes:</p>
<ul class="simple">
<li><strong>Paper Trading</strong>: The simulated algorithm runs in real time, and fetches
pricing data in real time from the exchange, but the orders never reach the
exchange, and are instead kept within Catalyst and simulated. No real currency
is bought or sold. Think of it as a <cite>backtesting happening in real time</cite>.</li>
<li><strong>Live Trading</strong>: This is the proper live trading mode in which an algorithm
runs in real time, fetching pricing data from live exchanges and placing
orders against the exchange. Real currency is transacted on the exchange
driven by the algorithm.</li>
</ul>
<p>These three modes are controlled by the following variables:</p>
<table border="1" class="docutils">
<colgroup>
<col width="38%" />
<col width="18%" />
<col width="44%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td rowspan="2">Mode</td>
<td colspan="2">Parameters</td>
</tr>
<tr class="row-even"><td>live</td>
<td>simulate_orders</td>
</tr>
<tr class="row-odd"><td>backtesting</td>
<td>False</td>
<td>True (default)</td>
</tr>
<tr class="row-even"><td>paper trading</td>
<td>True</td>
<td>True</td>
</tr>
<tr class="row-odd"><td>live trading</td>
<td>True</td>
<td>False</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="authentication">
<h2>Authentication<a class="headerlink" href="#authentication" title="Permalink to this headline"></a></h2>
<p>Most exchanges require token key/secret combination for authentication. By
convention, Catalyst uses an <code class="docutils literal"><span class="pre">auth.json</span></code> file to hold this data.</p>
<p>This example illustrates the convention using the <em>Bitfinex</em> exchange.
Here is how to generate key and secret values for the Bitfinex exchange:
<a class="reference external" href="https://docs.bitfinex.com/v1/docs/api-access">https://docs.bitfinex.com/v1/docs/api-access</a>. Most exchanges follow
a similar process.</p>
<p>The auth.json file:</p>
<div class="highlight-json"><div class="highlight"><pre><span></span><span class="p">{</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;bitfinex&quot;</span><span class="p">,</span>
<span class="nt">&quot;key&quot;</span><span class="p">:</span> <span class="s2">&quot;my-key&quot;</span><span class="p">,</span>
<span class="nt">&quot;secret&quot;</span><span class="p">:</span> <span class="s2">&quot;my-secret&quot;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The file goes here: <code class="docutils literal"><span class="pre">~/.catalyst/data/exchanges/bitfinex/auth.json</span></code></p>
<p>Note that the <cite>bitfinex</cite> part in the directory above corresponds to the id of the Bitfinex
exchange as defined in the “Supported Exchanges” section above.
Attempting to run an algorithm where the targeted exchange is missing
its <code class="docutils literal"><span class="pre">auth.json</span></code> file will create the directory structure and create an empty
auth.json file, but will result in an error.</p>
</div>
<div class="section" id="currency-symbols">
<h2>Currency Symbols<a class="headerlink" href="#currency-symbols" title="Permalink to this headline"></a></h2>
<p>Catalyst introduces a universal convention to reference
trading pairs and individual currencies. This
is required to ensure that the <code class="docutils literal"><span class="pre">symbol()</span></code> api predictably
returns the correct asset regardless of the targeted exchange.</p>
<p>Exchanges tend to use their own convention to represent currencies
(e.g. XBT and BTC both represent Bitcoin on different exchanges).
Trading pairs are also inconsistent. For example, Bitfinex
puts the market currency before the base currency without a
separator, Bittrex puts the base currency first and uses a dash
seperator.</p>
<p>Here is the Catalyst convention:</p>
<p><em>[Market Currency]_[Base Currency]</em> all lowercase.</p>
<p>Currency symbols (e.g. btc, eth, ltc) follow the Bittrex convention.</p>
<p>Here are some examples:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span></span><span class="c1"># With Bitfinex</span>
<span class="n">bitcoin_usd_asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s1">&#39;btc_usd&#39;</span><span class="p">)</span>
<span class="n">ethereum_bitcoin_asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s1">&#39;eth_btc&#39;</span><span class="p">)</span>
<span class="c1"># With Bittrex</span>
<span class="n">ethereum_bitcoin_asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s1">&#39;eth_btc&#39;</span><span class="p">)</span>
<span class="n">neo_ethereum_asset</span> <span class="o">=</span> <span class="n">symbol</span><span class="p">(</span><span class="s1">&#39;neo_eth)</span>
</pre></div>
</div>
<p>Note that the trading pairs are always referenced in the same manner.
However, not all trading pairs are available on all exchanges. An
error will occur if the specified trading pair is not trading
on the exchange. To check which currency pairs are available on each
of the supported exchanges, see
<a class="reference external" href="https://www.enigma.co/catalyst/status">Catalyst Market Coverage</a>.</p>
</div>
<div class="section" id="trading-an-algorithm">
<h2>Trading an Algorithm<a class="headerlink" href="#trading-an-algorithm" title="Permalink to this headline"></a></h2>
<p>There is no special convention to follow when writing an
algorithm for live trading. The same algorithm should work in
backtest and live execution mode without modification.</p>
<p>What differs are the arguments provided to the catalyst client or
<cite>run_algorithm()</cite> interface. Here is the same example in both interfaces:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>catalyst live -f my_algo_code -x bitfinex -c btc -n my_algo_name
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">run_algorithm</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="s1">&#39;bitfinex&#39;</span><span class="p">,</span>
<span class="n">live</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">algo_namespace</span><span class="o">=</span><span class="s1">&#39;my_algo_name&#39;</span><span class="p">,</span>
<span class="n">base_currency</span><span class="o">=</span><span class="s1">&#39;btc&#39;</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Here is the breakdown of the new arguments:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">live</span></code>: Boolean flag which enables live trading. It defaults to <code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><code class="docutils literal"><span class="pre">capital_base</span></code>: The amount of base_currency assigned to the strategy.
It has to be lower or equal to the amount of base currency available for
trading on the exchange. For illustration, order_target_percent(asset, 1)
will order the capital_base amount specified here of the specified asset.</li>
<li><code class="docutils literal"><span class="pre">exchange_name</span></code>: The name of the targeted exchange. See the
<a class="reference external" href="https://github.com/ccxt/ccxt/wiki/Exchange-Markets">CCXT Supported Exchanges</a>
for the full list.</li>
<li><code class="docutils literal"><span class="pre">algo_namespace</span></code>: A arbitrary label assigned to your algorithm for
data storage purposes.</li>
<li><code class="docutils literal"><span class="pre">base_currency</span></code>: The base currency used to calculate the
statistics of your algorithm. Currently, the base currency of all
trading pairs of your algorithm must match this value.</li>
<li><code class="docutils literal"><span class="pre">simulate_orders</span></code>: Enables the paper trading mode, in which orders are
simulated in Catalyst instead of processed on the exchange. It defaults to
<code class="docutils literal"><span class="pre">True</span></code>.</li>
<li><code class="docutils literal"><span class="pre">end_date</span></code>: When setting the end_date to a time in the <strong>future</strong>,
it will schedule the live algo to finish gracefully at the specified date.</li>
<li><code class="docutils literal"><span class="pre">start_date</span></code>: (<strong>Will be implemented in the future</strong>)
The live algo starts by default in the present, as mentioned above.
by setting the start_date to a time in the future, the algorithm would
essentially sleep and when the predefined time comes, it would start executing.</li>
</ul>
<p>The <cite>catalyst live</cite> command offers additional parameters.
You can learn more by running the following from the command line:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>catalyst live --help
</pre></div>
</div>
<p>Here is a complete algorithm for reference:
<a class="reference external" href="https://github.com/enigmampc/catalyst/blob/master/catalyst/examples/buy_low_sell_high_live.py">Buy Low and Sell High</a></p>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="features.html" class="btn btn-neutral float-right" title="Features" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="beginner-tutorial.html" class="btn btn-neutral" title="Catalyst Beginner Tutorial" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2018, 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.4',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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>