<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/algorithmic-trading-using-quantopians-zipline-python-library-in-r-and-backtest-optimizations-by-grid-search-and-parallel-processing/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/business-science-labs/2018/05/31/backtesting-quantopian-zipline-tibbletime-furrr-flyingfox.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We are ready to demo our new <strong>new experimental package for Algorithmic Trading</strong>, <code>flyingfox</code>, which uses <code>reticulate</code> to to bring <a rel="nofollow" href="http://www.zipline.io/" target="_blank">Quantopian’s open source algorithmic trading Python library, <code>Zipline</code></a>, to R. The <code>flyingfox</code> library is part of our <em>NEW Business Science Labs</em> innovation lab, which is dedicated to bringing experimental packages to our followers <em>early on</em> so they can test them out and let us know what they think before they make their way to CRAN. This article includes a long-form code tutorial on how to <strong>perform backtest optimizations of trading algorithms via grid search and parallel processing</strong>. In this article, we’ll show you how to use the combination of <code>tibbletime</code> (time-based extension of <code>tibble</code>) + <code>furrr</code> (a parallel-processing compliment to <code>purrr</code>) + <code>flyingfox</code> (<code>Zipline</code> in R) to <strong>develop a backtested trading algorithm that can be optimized via grid search and parallel processing</strong>. We are releasing this article as a compliment to the R/Finance Conference presentation <a rel="nofollow" href="https://github.com/business-science/presentations/tree/master/2018_06_01_R-Finance-Time-Series-Platform-Tidyverse" target="_blank">“A Time Series Platform For The Tidyverse”</a>, which Matt will present on Saturday (June 2nd, 2018). Enjoy!</p>
<h2>New: Business Science Labs</h2>
<p>I (Davis) am excited to introduce a new open source initiative called <strong>Business Science Labs</strong>. A lot of the experimental work we do is done behind the scenes, and much of it you don’t see early on. What you do see is a “refined” version of what we <em>think</em> you need based on our perception, which is not always reality. We aim to change this. Starting today, we have created <em>Business Science Labs</em>, which is aimed at bringing our experimental software to you earlier so you can test it out and let us know your thoughts!</p>
<p>Our first initiative is to bring <a rel="nofollow" href="http://www.zipline.io/" target="_blank">Quantopian’s Open Source algorithmic trading Python library, <code>Zipline</code></a>, to R via an experimental package called <code>flyingfox</code> (built using the awesome <code>reticulate</code> package).</p>
<h2>What We’re Going To Learn</h2>
<p>Introducing <em>Business Science Labs</em> is exciting, but we really want to educate you on some new packages! In this tutorial, we are going to go over how to <strong>backtest algorithmic trading strategies using parallel processing and Quantopian’s Zipline infrastructure in R</strong>. You’ll gain exposure to a <code>tibbletime</code>, <code>furrr</code>, and our experimental <code>flyingfox</code> package. The general progression is:</p>
<ul>
<li>
<p><code>tibbletime</code>: What it is and why it’s essential to performing scalable time-based calculations in the <code>tidyverse</code></p>
</li>
<li>
<p><code>furrr</code>: Why you need to know this package for speeding up code by processing <code>purrr</code> in parallel</p>
</li>
<li>
<p><code>flyingfox</code>: The story behind the package, and how you can use it to test algorithmic trading strategies</p>
</li>
<li>
<p><code>tibbletime</code> + <code>furrr</code> + <code>flyingfox</code>: Putting it all together to perform parallelized algorithmic trading strategies and analyze time-based performance</p>
</li>
</ul>
<p>Here’s an example of the grid search we perform to determine which are the best combinations of short and long moving averages for the stock symbol JPM (JP Morgan).</p>
<p><img alt="Portfolio Value Grid" src="https://i1.wp.com/www.business-science.io/assets/2018-05-31-flyingfox/portfolio_value_grid.png?w=456" data-recalc-dims="1"></p>
<p>Here’s an example of the time series showing the order (buy/sell) points determined by the moving average crossovers, and the effect on the portfolio value.</p>
<p><img alt="Portfolio Over Time With Buy/Sell Points" src="https://i1.wp.com/www.business-science.io/assets/2018-05-31-flyingfox/portfolio_over_time.png?w=456" data-recalc-dims="1"></p>
<h2>Algorithmic Trading Strategies And Backtesting</h2>
<p><strong>Algorithmic trading</strong> is nothing new. Financial companies have been performing algorithmic trading for years as a way of attempting to “beat” the market. It can be very difficult to do, but some traders have successfully applied advanced algorithms to yield significant profits.</p>
<p>Using an algorithm to trade boils down to <strong>buying and selling</strong>. In the simplest case, when an algorithm detects an asset (a stock) is going to go higher, a buy order is placed. Conversely, when the algorithm detects that an asset is going to go lower, a sell order is placed. Positions are managed by buying and selling all or part of the portfolio of assets. To keep things simple, we’ll focus on just the full buy/sell orders.</p>
<p>One very basic method of algorithmic trading is using <strong>short and long moving averages</strong> to detect shifts in trend. The crossover is the point where a buy/sell order would take place. The figure below shows the price of Halliburton (symbol “HAL”), which a trader would have an initial position in of say 10,000 shares. In a hypothetical case, the trader could use a combination of a 20 day short moving average and a 150 day long moving average and look for buy/sell points at the crossovers. If the trader hypothetically sold his/her position in full on the sell and bought the position back in full, then the trader would stand to avoid a delta loss of approximately $5/share during the downswing, or $50,000.</p>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i0.wp.com/www.business-science.io/figure/source/2018-05-31-backtesting-quantopian-zipline-tibbletime-furrr-flyingfox/unnamed-chunk-1-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>Backtesting</strong> is a strategy that is used to detect how a trading strategy would have performed in the past. It’s impossible to know what the future will bring, but using trading strategies that work in the past helps to instill confidence in an algorithm.</p>
<p><a rel="nofollow" href="https://www.quantopian.com/" target="_blank">Quantopian</a> is a platform designed to enable anyone to develop algorithmic trading strategies. To help its community, Quantopian provides several open source tools. The one we’ll focus on is <code>Zipline</code> for backtesting. There’s one downside: it’s only available in Python.</p>
<p>With the advent of the <code>reticulate</code> package, which enables porting any Python library to R, we took it upon ourselves to test out the viability of porting <code>Zipline</code> to R. Our experiment is called <code>flyingfox</code>.</p>
<h2>RStudio Cloud Experiment Sandbox</h2>
<p>In this code-based tutorial, we’ll use an experimental package called <code>flyingfox</code>. It has several dependencies including Python that require setup time and effort. For those that want to test out <code>flyingfox</code> quickly, we’ve created a <strong>FREE RStudio Cloud Sandbox</strong> for running experiments. You can access the Cloud Sandbox here for FREE: https://rstudio.cloud/project/38291</p>
<p><img alt="RStudio Cloud Sandbox" src="https://i0.wp.com/www.business-science.io/assets/2018-05-31-flyingfox/rstudio_cloud.png?w=456" data-recalc-dims="1"></p>
<h2>Packages Needed For Backtest Optimization</h2>
<p>The meat of this code-tutorial is the section <a rel="nofollow" href="http://www.business-science.io/business-science-labs/2018/05/31/backtesting-quantopian-zipline-tibbletime-furrr-flyingfox.html#optimizations" target="_blank">Backtest Optimization Using tibbletime + furrr + flyingfox </a>. However, before we get to it, we’ll go over the <em>three main packages</em> used to do high-performance backtesting optimizations:</p>
<ol>
<li>
<p><code>tibbletime</code>: What it is, and why it’s essential to performing scalable time-based calculations in the <code>tidyverse</code></p>
</li>
<li>
<p><code>furrr</code>: Why you need to know this package for speeding up code by processing <code>purrr</code> in parallel</p>
</li>
<li>
<p><code>flyingfox</code>: How to use it to test algorithmic trading strategies</p>
</li>
</ol>
<p>Putting It All Together: <code>tibbletime</code> + <code>furrr</code> + <code>flyingfox</code> for <strong>backtesting optimizations performed using parallel processing and grid search!</strong></p>
<h3>Install &amp; Load Libraries</h3>
<p><strong>Install Packages</strong></p>
<p>For this post, you’ll need to install development version of <code>flyingfox</code>.</p>
<figure>
<pre><code data-lang="r"><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;DavisVaughan/flyingfox&quot;</span><span>)</span></code></pre>
</figure>
<p>If you are on windows, you should also install the development version of <code>furrr</code>.</p>
<figure>
<pre><code data-lang="r"><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;DavisVaughan/furrr&quot;</span><span>)</span></code></pre>
</figure>
<p>Other packages you’ll need include <code>tibbletime</code>, <code>furrr</code>, and <code>tidyverse</code>. We’ll also load <code>tidyquant</code> mainly for the <code>ggplot2</code> themes. We’ll install <code>ggrepel</code> to repel overlapping plot labels. You can install these from CRAN using <code>install.packages()</code>.</p>
<p><strong>Load Packages</strong></p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tibbletime</span><span>)</span><span>
</span><span>library</span><span>(</span><span>furrr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>flyingfox</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggrepel</span><span>)</span></code></pre>
</figure>
<p>We’ll cover how a few packages work before jumping into backtesting and optimizations.</p>
<h3>1. tibbletime</h3>
<p>The <code>tibbletime</code> package is a cornerstone for future time series software development efforts at <em>Business Science</em>. We have major plans for this package. Here are some key benefits:</p>
<ul>
<li>Time periods down to milliseconds are supported</li>
<li>Because this is a <code>tibble</code> under the hood, we are able to leverage existing packages for analysis without reinventing the wheel</li>
<li>Scalable grouped analysis is at your fingertips because of <code>collapse_by()</code> and integration with <code>group_by()</code></li>
</ul>
<p>It’s best to learn now, and we’ll go over the basics along with a few commonly used functions: <code>collapse_by()</code>, <code>rollify()</code>, <code>filter_time()</code>, and <code>as_period()</code>.</p>
<p>First, let’s get some data. We’ll use the FANG data set that comes with <code>tibbletime</code>, which includes stock prices for FB, AMZN, NFLX, and GOOG. We recommend using the <code>tidyquant</code> package to get this or other stock data.</p>
<figure>
<pre><code data-lang="r"><span>data</span><span>(</span><span>&quot;FANG&quot;</span><span>)</span><span>
</span><span>FANG</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 4,032 x 8
##    symbol date        open  high   low close    volume adjusted
##    </code></pre></figure>