<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/tidyquant-0-2-0-added-functionality-for-financial-engineers-and-business-analysts/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html"> mattdancho.com - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><code>tidyquant</code>, version 0.2.0, is now available on CRAN. If your not already familiar, <code>tidyquant</code> integrates the best quantitative resources for collecting and analyzing quantitative data, <code>xts</code>, <code>zoo</code>, <code>quantmod</code> and <code>TTR</code>, with the tidy data infrastructure of the <code>tidyverse</code> allowing for seamless interaction between each. I’ll briefly touch on some of the updates. The package is open source, and you can view the code on the <a target="_blank" rel="nofollow" href="https://github.com/mdancho84/tidyquant">tidyquant github page</a>.</p>
<h1>Table of Contents</h1>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#benefits">Who Will Benefit?</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#updates">Updates</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#example1">Example 1: Getting and Visualizing Key Ratios</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#example2">Example 2: Taking the New Zoo Integration for a Spin</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#conclusion">Conclusion</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#recap">Recap</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html#further-reading">Further Reading</a></li>
</ul>
<h1>Who Will Benefit? <a></a></h1>
<p>The <code>tidyquant</code> package was developed with two people in mind:</p>
<ol>
<li>
<p><strong>Financial Engineers</strong>: These individuals systematically analyze financial securities (typically stocks) implementing technical trading rules such as MACD, Bollinger Bands, Moving Averages, etc to determine buy and sell signals in an automated way. Charting and implementing modelling algorithms are highly important.</p>
</li>
<li>
<p><strong>Financial / Business Analysts</strong>: These individuals systematically analyze financial securities, financial statements, key ratios such as valuation (e.g. price to earnings multiples), financial health (e.g. current ratio), efficiency (e.g. inventory turnover). Getting financial and key ratio data is highly important along with charting and to a lesser degree modelling.</p>
</li>
</ol>
<p>For the <strong>financial engineer</strong>, the package is designed to integrate specialty financial functions within the <code>tidyverse</code> so the user doesn’t need to jump back and forth between tibbles (tidy data frames) / data frames and xts / zoo time-series objects. Methods like <code>tq_transform()</code> and <code>tq_mutate()</code> exist to apply the various xts, zoo, quantmod, and TTR functions to data frames, so you never need to leave the tidyverse. Further, if the user needs to switch object classes, coercion functions exist to easily convert (see <code>as_tibble()</code> for converting xts to tibbles / data frames, and <code>as_xts()</code> for converting data frames to xts).</p>
<p>For the <strong>financial analyst</strong>, the package is designed to enable retrieving key financial data fast and analyzing financial data easy and efficient. The core function, <code>tq_get()</code>, has the <code>get</code> argument that can be set to:</p>
<ul>
<li>
<p><em>Stock Index</em>: Retrieve a list of stock symbols for an entire index such as the S&amp;P500 with <code>tq_get(&quot;SP500&quot;, get = &quot;stock.index&quot;)</code>. 18 indexes are available.</p>
</li>
<li>
<p><em>Stock Prices</em>: Retrieve the stock prices for an individual stock such as Apple with <code>tq_get(&quot;AAPL&quot;, get = &quot;stock.prices&quot;)</code>.</p>
</li>
<li>
<p><em>Financial Statements</em>: Retrieve income statements, balance sheets, and cash flow statements for both annual and quarterly periods for an individual stock, <code>tq_get(&quot;AAPL&quot;, get = &quot;financials&quot;)</code>.</p>
</li>
<li>
<p><em>Key Ratios</em>: Retrieve 10-years of historical key ratios (89 total available) for an individual stock, <code>tq_get(&quot;AAPL&quot;, get = &quot;key.ratios&quot;)</code>.</p>
</li>
</ul>
<p>Most importantly, <code>tidyquant</code> is designed to work in the <code>tidyverse</code>. This means users can use <code>dplyr</code> and <code>tidyr</code> verbs to slice and dice data and <code>purrr</code> to map functions at scale. This enables new capabilities for both financial engineers and analysts. Instead of analyzing one stock at a time, <strong>you can now analyze as many stocks as you want at the same time and systematically compare each</strong>. See the <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/investments/2016/10/23/SP500_Analysis.html">S&amp;P500</a> and the more advanced <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/investments/2016/11/30/Russell2000_Analysis.html">Russell 2000</a> posts for tutorials on mapping functions to stock lists.</p>
<p>Ok, enough about the benefits. You can read more about them in the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/tidyquant/vignettes/tidyquant.html">vignette</a>. Let’s discuss the updates, and I’ll go through some examples of the new functionality.</p>
<h1>Updates <a></a></h1>
<p>The major updates are:</p>
<ol>
<li>
<p><strong>Key ratios from Morningstar</strong>: Users can now get 89 different key ratios that span 10 years historically. This is great for users that want to know how EPS, P/E, and even financials have changed over time. The source is <a target="_blank" rel="nofollow" href="https://www.morningstar.com/">Morningstar</a>.</p>
</li>
<li>
<p><strong>zoo integration</strong>: The <code>rollapply</code> functions from the <code>zoo</code> package are now fully integrated with <code>tq_transform</code> and <code>tq_mutate</code>. This means you can calculate rolling averages, maximums, medians, and whatever else your heart desires.</p>
</li>
<li>
<p><strong>Making things more intuitive and hassle-free</strong>: These are small tweaks. The transform and mutate function arguments have changed slightly. The <code>x_fun</code> argument has been replaced with the more intuitive name <code>ohlc_fun</code>, so users know to enter a OHLC function such as Op to select the open price of stock prices. The <code>.x</code> and <code>.y</code> are replaced with <code>x</code> and <code>y</code>, which make more sense and don’t interfere with mapping functions in <code>purrr</code>.</p>
</li>
</ol>
<p>Now, let’s go through some examples.</p>
<h1>Prerequisites</h1>
<p>First, update <code>tidyquant</code> version 0.2.0. Note that you will need the development version 0.2.0.9000 for this post.</p>
<figure>
<pre><code data-lang="r"><span># install.packages(&quot;tidyquant&quot;)
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;mdancho84/tidyquant&quot;</span><span>)</span></code></pre>
</figure>
<p>Next, load <code>tidyquant</code>.</p>
<figure>
<pre><code data-lang="r"><span># Loads tidyquant, tidyverse, lubridate, quantmod, TTR, xts, zoo
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span></code></pre>
</figure>
<p>I also recommend the open-source <a target="_blank" rel="nofollow" href="https://www.rstudio.com/">RStudio</a> IDE, which makes <em>R Programming</em> easy and efficient.</p>
<h1>Example 1: Getting and Visualizing Key Ratios <a></a></h1>
<p><strong>You will need to download the development version for this example due to an issue with retrieving key ratios from stocks listed on the NYSE exchange. Key ratios are only available for stocks listed on the NASDAQ exchange in 0.2.0. To continue, upgrade to 0.2.0.9000 using <code>devtools::install_github(&quot;mdancho84/tidyquant&quot;)</code> to get the latest development version.</strong></p>
<p>Let’s say we want to compare the valuation over time using the price to earnings (P/E) multiple. This is often done when comparing several companies in the same industry to determine those that may be below normal valuation (i.e. the price may be at a discount to historical trends and to peers).</p>
<p>Hypothetically, we’ll select some big banks to visualize the P/E valuation: JP Morgan (JPM), Goldman Sachs (GS), Bank of America (BAC), and Citi Group (C). Before we can visualize all stocks, let’s first get the key ratios for one stock. Use <code>tq_get()</code>, which gets data, and set the <code>get</code> argument to “key.ratios”.</p>
<figure>
<pre><code data-lang="r"><span>JPM_key_ratios</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;JPM&quot;</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;key.ratios&quot;</span><span>)</span><span>
</span><span>JPM_key_ratios</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 7 × 2
##             section               data
##               &lt;chr>             &lt;list>
## 1        Financials &lt;tibble [150 × 5]>
## 2     Profitability &lt;tibble [170 × 5]>
## 3            Growth &lt;tibble [160 × 5]>
## 4         Cash Flow  &lt;tibble [50 × 5]>
## 5  Financial Health &lt;tibble [240 × 5]>
## 6 Efficiency Ratios  &lt;tibble [80 × 5]>
## 7  Valuation Ratios  &lt;tibble [40 × 5]></code></pre>
</figure>
<p>Let’s check out the key ratios by unnesting.</p>
<figure>
<pre><code data-lang="r"><span>JPM_key_ratios</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 890 × 6
##       section sub.section group        category       date  value
##         &lt;chr>       &lt;chr> &lt;dbl>           &lt;chr>     &lt;date>  &lt;dbl>
## 1  Financials  Financials     1 Revenue USD Mil 2006-12-01  61437
## 2  Financials  Financials     1 Revenue USD Mil 2007-12-01  71372
## 3  Financials  Financials     1 Revenue USD Mil 2008-12-01  67252
## 4  Financials  Financials     1 Revenue USD Mil 2009-12-01 100434
## 5  Financials  Financials     1 Revenue USD Mil 2010-12-01 102694
## 6  Financials  Financials     1 Revenue USD Mil 2011-12-01  97234
## 7  Financials  Financials     1 Revenue USD Mil 2012-12-01  97031
## 8  Financials  Financials     1 Revenue USD Mil 2013-12-01  97367
## 9  Financials  Financials     1 Revenue USD Mil 2014-12-01  95112
## 10 Financials  Financials     1 Revenue USD Mil 2015-12-01  93543
## # ... with 880 more rows</code></pre>
</figure>
<p>Yikes, there’s 890 rows of data. We can get the unique categories by selecting the “category” column and using the <code>unique</code> function. We first <code>filter</code> to the section we want, “Valuation Ratios”.</p>
<figure>
<pre><code data-lang="r"><span>JPM_key_ratios</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>section</span><span> </span><span>==</span><span> </span><span>&quot;Valuation Ratios&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>category</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>unique</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 4 × 1
##             category
##                &lt;chr>
## 1  Price to Earnings
## 2     Price to Sales
## 3      Price to Book
## 4 Price to Cash Flow</code></pre>
</figure>
<p>We see that “Price to Earnings” is one of the valuation ratios we can get. Let’s <code>filter</code> and plot with <code>ggplot2</code>.</p>
<figure>
<pre><code data-lang="r"><span>JPM_key_ratios</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>category</span><span> </span><span>==</span><span> </span><span>&quot;Price to Earnings&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;P/E Ratios are Easy to Retrieve with tidyquant&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Price to Earnings&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;JPM Valuation Over Time&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>lims</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span> </span><span>30</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i2.wp.com/www.mattdancho.com/figure/source/2017-1-8-tidyquant-update-0-2-0/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>This is great, but we want to evaluate more than one stock. That’s easy to do with <code>dplyr</code> and <code>purrr</code>. First, we’ll make a function to get the P/E ratios using the same procedure as for one stock. Then we’ll map it to scale to many stocks.</p>
<figure>
<pre><code data-lang="r"><span>get_pe_ratios</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>stock.symbol</span><span>)</span><span> </span><span>{</span><span>
</span><span>tq_get</span><span>(</span><span>stock.symbol</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;key.ratios&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>category</span><span> </span><span>==</span><span> </span><span>&quot;Price to Earnings&quot;</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Now, let’s scale it to a tibble of stocks.</p>
<figure>
<pre><code data-lang="r"><span>bank_stock_pe_ratios</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>(</span><span>symbol</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;JPM&quot;</span><span>,</span><span> </span><span>&quot;GS&quot;</span><span>,</span><span> </span><span>&quot;BAC&quot;</span><span>,</span><span> </span><span>&quot;C&quot;</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>pe.ratio</span><span> </span><span>=</span><span> </span><span>map</span><span>(</span><span>.x</span><span> </span><span>=</span><span> </span><span>symbol</span><span>,</span><span> </span><span>get_pe_ratios</span><span>))</span><span>
</span><span>bank_stock_pe_ratios</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 4 × 2
##   symbol          pe.ratio
##    &lt;chr>            &lt;list>
## 1    JPM &lt;tibble [10 × 6]>
## 2     GS &lt;tibble [10 × 6]>
## 3    BAC &lt;tibble [10 × 6]>
## 4      C &lt;tibble [10 × 6]></code></pre>
</figure>
<p>Now that we have a nested tibble of P/E ratios, we can use the same technique to visualize four stocks as with one stock. We’ll unnest the list to get a single level tibble, then plot using <code>ggplot2</code> tacking on a facet wrap to split the plots by stock.</p>
<figure>
<pre><code data-lang="r"><span>bank_stock_pe_ratios</span><span> </span><span>%>%</span><span>
</span><span>unnest</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>symbol</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;P/E Ratios are Easy to Retrieve with tidyquant&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Price to Earnings&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;JPM, GS, BAC, C: Valuation Over Time&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>lims</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-10</span><span>,</span><span> </span><span>50</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span> </span><span>symbol</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-9" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-8-tidyquant-update-0-2-0/unnamed-chunk-9-1.png?w=456" data-recalc-dims="1"></p>
<p>We now have the price to earnings ratio visualization for the four bank stocks. We can see how the valuation of each stock compares historically and against its peers. Just a few observations:</p>
<ul>
<li>GS has the highest current valuation at almost 15X earnings. JPM, C, and BAC are all priced closer to 10X earnings.</li>
<li>BAC is missing some values, which were cut off by the y-limits. This happened after the financial crisis, which may be a red flag since earnings were impacted more than peers.</li>
<li>C had negative PE multiples in 2009 and 2010. This was the result of the financial crisis. Again, this may be a red flag.</li>
</ul>
<p>The P/E multiple is just one of the 89 key ratios that can be used to evaluate stocks that are now available using <code>tq_get(x, get = &quot;key.ratios&quot;)</code>.</p>
<h1>Example 2: Taking the New Zoo Integration for a Spin <a></a></h1>
<p>The <code>rollapply</code> functions from the <code>zoo</code> package are useful in calculating rolling averages, medians, maximums, etc, which are integral in separating the trend from the noise from time-series. One common technique is use simple moving averages to determine the crossover (which was discussed in my <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/01/tidyquant-introduction.html">last post</a> on <code>tidyquant</code>). A potential issue is that an average is more susceptible to outliers. Instead of using averages, let’s use the zoo functions to get the 15-day and 50-day rolling medians, which are more resistent to noise.</p>
<p>First, we get the past year of stock prices for AAPL using <code>tq_get(get = &quot;stock.prices&quot;, from = today - years(1))</code>.</p>
<figure>
<pre><code data-lang="r"><span>AAPL_prices</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;AAPL&quot;</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.prices&quot;</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>today</span><span>()</span><span> </span><span>-</span><span> </span><span>years</span><span>(</span><span>1</span><span>))</span><span>
</span><span>AAPL_prices</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 252 × 7
##          date   open   high   low  close   volume adjusted
##        &lt;date>  &lt;dbl>  &lt;dbl> &lt;dbl>  &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1  2016-01-08  98.55  99.11 96.76  96.96 70798000 94.84967
## 2  2016-01-11  98.97  99.06 97.34  98.53 49739400 96.38550
## 3  2016-01-12 100.55 100.69 98.84  99.96 49154200 97.78438
## 4  2016-01-13 100.32 101.19 97.30  97.39 62439600 95.27031
## 5  2016-01-14  97.96 100.48 95.74  99.52 63170100 97.35395
## 6  2016-01-15  96.20  97.71 95.36  97.13 79010000 95.01597
## 7  2016-01-19  98.41  98.65 95.50  96.66 53087700 94.55621
## 8  2016-01-20  95.10  98.19 93.42  96.79 72334400 94.68337
## 9  2016-01-21  97.06  97.88 94.94  96.30 52161500 94.20404
## 10 2016-01-22  98.63 101.46 98.37 101.42 65800500 99.21260
## # ... with 242 more rows</code></pre>
</figure>
<p>Next, we use <code>tq_mutate()</code> to add the 15-day and 50-day rolling medians. The first two arguments are <code>ohlc_fun = Cl</code>, which selects the closing price using <code>quantmod</code> OHLC notation, and <code>mutate_fun = rollapply</code>, which sends the closing price to the rollapply function. The next arguments, <code>width</code> and <code>FUN</code> are arguments that are passed to the <code>rollapply</code> function. Width is the number of periods to take the median, and FUN is the function we intend to apply (i.e. median). The workflow is as follows:</p>
<figure>
<pre><code data-lang="r"><span>AAPL_prices</span><span> </span><span>&lt;-</span><span> </span><span>AAPL_prices</span><span> </span><span>%>%</span><span>
</span><span>tq_mutate</span><span>(</span><span>ohlc_fun</span><span> </span><span>=</span><span> </span><span>Cl</span><span>,</span><span> </span><span>mutate_fun</span><span> </span><span>=</span><span> </span><span>rollapply</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>median</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tq_mutate</span><span>(</span><span>ohlc_fun</span><span> </span><span>=</span><span> </span><span>Cl</span><span>,</span><span> </span><span>mutate_fun</span><span> </span><span>=</span><span> </span><span>rollapply</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>50</span><span>,</span><span> </span><span>FUN</span><span> </span><span>=</span><span> </span><span>median</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>median.15</span><span> </span><span>=</span><span> </span><span>rollapply</span><span>,</span><span>
</span><span>median.50</span><span> </span><span>=</span><span> </span><span>rollapply.1</span><span>)</span><span>
</span><span>AAPL_prices</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 252 × 9
##          date   open   high   low  close   volume adjusted median.15
##        &lt;date>  &lt;dbl>  &lt;dbl> &lt;dbl>  &lt;dbl>    &lt;dbl>    &lt;dbl>     &lt;dbl>
## 1  2016-01-08  98.55  99.11 96.76  96.96 70798000 94.84967        NA
## 2  2016-01-11  98.97  99.06 97.34  98.53 49739400 96.38550        NA
## 3  2016-01-12 100.55 100.69 98.84  99.96 49154200 97.78438        NA
## 4  2016-01-13 100.32 101.19 97.30  97.39 62439600 95.27031        NA
## 5  2016-01-14  97.96 100.48 95.74  99.52 63170100 97.35395        NA
## 6  2016-01-15  96.20  97.71 95.36  97.13 79010000 95.01597        NA
## 7  2016-01-19  98.41  98.65 95.50  96.66 53087700 94.55621        NA
## 8  2016-01-20  95.10  98.19 93.42  96.79 72334400 94.68337        NA
## 9  2016-01-21  97.06  97.88 94.94  96.30 52161500 94.20404        NA
## 10 2016-01-22  98.63 101.46 98.37 101.42 65800500 99.21260        NA
## # ... with 242 more rows, and 1 more variables: median.50 &lt;dbl></code></pre>
</figure>
<p>Two new columns, rollapply and rollapply.1, were added to the tibble. We <code>rename</code> these to be more descriptive. The next part is the same visualization code used in the last post. Essentially we <code>gather</code> the prices we wish to visualize so they are in one long tibble with two columns, “type” (close, median.15, and median.50) and “value”. We color each line by “type” using the ggplot aesthetics.</p>
<figure>
<pre><code data-lang="r"><span>my_palette</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;black&quot;</span><span>,</span><span> </span><span>&quot;blue&quot;</span><span>,</span><span> </span><span>&quot;red&quot;</span><span>)</span><span>
</span><span>AAPL_prices</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>date</span><span>,</span><span> </span><span>close</span><span>,</span><span> </span><span>median.15</span><span>,</span><span> </span><span>median.50</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>key</span><span> </span><span>=</span><span> </span><span>type</span><span>,</span><span> </span><span>value</span><span> </span><span>=</span><span> </span><span>price</span><span>,</span><span> </span><span>close</span><span>:</span><span>median.50</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>price</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>my_palette</span><span>)</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>legend.position</span><span>=</span><span>&quot;bottom&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Simple Moving Medians are a Breeze with tidyquant&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>ylab</span><span>(</span><span>&quot;Stock Price&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-12" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-8-tidyquant-update-0-2-0/unnamed-chunk-12-1.png?w=456" data-recalc-dims="1"></p>
<p>And, we’re done. We now have an alternative to the SMA that is more resistant to changes caused by outliers.</p>
<h1>Conclusion <a></a></h1>
<p>The <code>tidyquant</code> package is a useful tool for both financial engineers and financial analysts, with tools to collect, analyze, visualize and model financial data.</p>
<h1>Recap <a></a></h1>
<p>You should now have a good understanding of the benefits and new features of the <code>tidyquant</code> package. We addressed some of the benefits that financial engineers and analysts can get from using the package. We discussed new features including key ratios and the <code>zoo</code> integration. Eighty nine key ratios are now available using <code>tq_get()</code>. The <code>zoo</code> <code>rollapply()</code> function can be used with <code>tq_mutate()</code> and <code>tq_transform()</code>. This example just scratches the surface of the power of <code>tidyquant</code>. See the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/tidyquant/vignettes/tidyquant.html">vignette</a> for a detailed discussion on each of the <code>tidyquant</code> features.</p>
<h1>Further Reading <a></a></h1>
<ol>
<li>
<p><strong><a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/tidyquant/vignettes/tidyquant.html">tidyquant Vignette</a></strong>: This tutorial just scratches the surface of <code>tidyquant</code>. The vignette explains much, much more!</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/">R for Data Science</a></strong>: A free book that thoroughly covers the <code>tidyverse</code> packages.</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="http://www.quantmod.com/">Quantmod Website</a></strong>: Covers many of the <code>quantmod</code> functions. Also, see the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=quantmod">quantmod vignette</a>.</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="http://joshuaulrich.github.io/xts/index.html">Extensible Time-Series Website</a></strong>: Covers many of the <code>xts</code> functions. Also, see the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=xts">xts vignette</a>.</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=TTR">TTR Vignette</a></strong>: Covers each of the <code>TTR</code> functions.</p>
</li>
</ol>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.mattdancho.com/code-tools/2017/01/08/tidyquant-update-0-2-0.html"> mattdancho.com - Articles</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/KRM3A-QbKZ0"></a></div>