<p><code>tidyquant</code>, version 0.3.0, is a pretty sizable release that includes a little bit for everyone, including <strong>new financial charting and moving average geoms</strong> for use with <code>ggplot2</code>, a new <code>tq_get</code> get option called <code>&quot;key.stats&quot;</code> for retrieving <strong>real-time stock information</strong>, and several nice integrations that improve the ease of <strong>scaling</strong> your analyses. If your not already familiar with <code>tidyquant</code>, it 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 <code>tidyverse</code> allowing for seamless interaction between each. I’ll briefly touch on some of the updates by going through some <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#examples">neat examples</a>. 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/22/tidyquant-update-0-3-0.html#updates">v0.3.0 Updates</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#prerequisites">Prerequisites</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#examples">Examples</a>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example1">Example 1: Enhanced Financial Data Visualizations</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example2">Example 2: Working with Key Statistics</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example3">Example 3: Scaling Your Analysis</a></li>
</ul>
</li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#conclusion">Conclusion</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#recap">Recap</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#further-reading">Further Reading</a></li>
</ul>
<h1>v0.3.0 Updates <a></a></h1>
<blockquote>
<p>tidyquant: Bringing financial analysis to the tidyverse</p>
</blockquote>
<p>When I said this was a big release, I wasn’t kidding. We have some major enhancements in <code>tidyquant</code>:</p>
<ol>
<li>
<p><strong>Financial Visualizations for ggplot2</strong>: Candlestick charts, barcharts, moving averages and Bollinger Bands can be used in the <code>ggplot</code> “grammar of graphics” workflow. There’s a new vignette, <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tidyquant">Charting with tidyquant</a>, that details the new financial charting capabilities.</p>
</li>
<li><strong>Key stats from Yahoo Finance</strong>: Users can now get <em>55 different key statistics in real-time from Yahoo Finance</em> with the new <code>&quot;key.stats&quot;</code> get option. The statistics include Bid, Ask, Day’s High, Day’s Low, Last Trade Price, Current P/E Ratio, and many more most of which change throughout the day. With the addition of the key statistics, <code>tq_get</code> is now truly a <em>one-stop shop for financial information</em>. The user can now get:
<ul>
<li>Real-time key stock statistics with <code>&quot;key.stats&quot;</code></li>
<li>Historical key ratios and financial information over the past 10-years with <code>&quot;key.ratios&quot;</code></li>
<li>Quarterly and annual financial statement data with <code>&quot;financials&quot;</code></li>
<li>Historical daily stock prices with <code>&quot;stock.prices&quot;</code></li>
<li>Stock indexes for 18 different indexes with <code>&quot;stock.index&quot;</code></li>
<li>And more!</li>
</ul>
</li>
<li>
<p><strong>Enhancements that Make Scaling Financial Analysis Simple</strong>:</p>
<ul>
<li>
<p><code>tq_get</code> now accepts multiple stocks in the form of either a character vector (e.g. <code>c(&quot;AAPL&quot;, &quot;GOOG&quot;, &quot;FB&quot;)</code>) or a data frame with the stocks in the first column. This means scaling is ridiculously simple now. A call to <code>tq_get(c(&quot;AAPL&quot;, &quot;GOOG&quot;, &quot;FB&quot;), get = &quot;stock.prices&quot;)</code> now gets the 10-years of daily stock prices for all three stocks in one data frame!</p>
</li>
<li>
<p><code>tq_mutate</code> and <code>tq_transform</code> now work with grouped data frames. This means that you can extend the <code>xts</code>, <code>zoo</code>, <code>quantmod</code> and <code>TTR</code> functions to grouped data frames the same way that you can with <code>dplyr::mutate</code>. In addition, you can now more easily rename the transformed / mutated data frame, with the <code>col_rename</code> argument. All of this saves you time and requires less code!</p>
</li>
</ul>
</li>
</ol>
<p>This concludes the major changes. Now, let’s go through some examples!</p>
<h1>Prerequisites <a></a></h1>
<p>First, update to <code>tidyquant</code> v0.3.0.</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;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>Examples</h1>
<p>We’ve got some neat examples to show off the new capabilities:</p>
<ol>
<li>
<p><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example1">Enhanced Financial Data Visualizations</a>: We’ll check out how to use the new <code>tidyquant</code> geoms with <code>ggplot2</code>, which provide great visualizations for time-series and stock data!</p>
</li>
<li>
<p><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example2">Working with Key Statistics</a>: We’ll investigate the new <code>tq_get</code> get option, <code>get = &quot;key.stats&quot;</code>, which enables <strong>access to real-time, intraday trading information</strong>!</p>
</li>
<li>
<p><a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example3">Scaling Your Analysis</a>: We’ll test out some of the new scaling features that make it even easier to scale your analysis from one security to many!</p>
</li>
</ol>
<h2>Example 1: Enhanced Financial Data Visualizations <a></a></h2>
<p><img alt="Multiple Stocks" src="https://i0.wp.com/www.mattdancho.com/assets/tidyquant-030.png?w=456" data-recalc-dims="1"></p>
<p>I absolutely love these new ggplot geoms that come packaged with <code>tidyquant</code>, and I’m really excited to show them off! Two new chart types come packaged with <code>tidyquant</code> v0.3.0: <code>geom_candlestick</code> and <code>geom_barchart</code> (not to be confused with <code>geom_bar</code>). In this post, we’ll focus on the candlestick chart, but the barchart works in a very similar manner.</p>
<p>Before we start, let’s get some data using <code>tq_get</code>. The first call gets a single stock (nothing new here), and the second call retrieves the FANG stocks using the <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example3">new scaling functionality</a> by piping (<code>%>%</code>) a character vector of symbols to <code>tq_get</code> (there are other ways too!).</p>
<figure>
<pre><code data-lang="r"><span>AMZN</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;AMZN&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>&quot;2007-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2017-01-01&quot;</span><span>)</span><span>
</span><span>FANG</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;FB&quot;</span><span>,</span><span> </span><span>&quot;AMZN&quot;</span><span>,</span><span> </span><span>&quot;NFLX&quot;</span><span>,</span><span> </span><span>&quot;GOOG&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tq_get</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>&quot;2007-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2017-01-01&quot;</span><span>)</span></code></pre>
</figure>
<p>Before v0.3.0, we used <code>geom_line</code> to create a line chart like so. Note that <code>coord_x_date</code> is a new <code>tidyquant</code> coordinate function that enables zooming in a part of the chart without out-of-bounds data loss (<code>scale_x_date</code> is similar but causes out-of-bounds data loss which wreaks havoc on moving average geoms).</p>
<figure>
<pre><code data-lang="r"><span># Setup dates for zoom window
</span><span>end</span><span> </span><span>&lt;-</span><span> </span><span>ymd</span><span>(</span><span>&quot;2017-01-01&quot;</span><span>)</span><span>
</span><span>start</span><span> </span><span>&lt;-</span><span> </span><span>end</span><span> </span><span>-</span><span> </span><span>weeks</span><span>(</span><span>20</span><span>)</span><span>
</span><span># Create chart
</span><span>AMZN</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>close</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;AMZN: Line Chart the Previous Way&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;This is good, but doesn't give us open, high, low, or direction information&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;Closing Price&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_x_date</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>start</span><span>,</span><span> </span><span>end</span><span>),</span><span>
</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>700</span><span>,</span><span> </span><span>870</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i2.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>With <code>tidyquant</code>, we can replace the <code>geom_line</code> with <code>geom_candlestick</code> to create a beautiful candlestick chart that shows open, high, low, close, and direction visually. The only real difference is that we need to specify the aesthetic arguments, <code>open</code>, <code>high</code>, <code>low</code> and <code>close</code>. Everything else can stay the same.</p>
<figure>
<pre><code data-lang="r"><span>AMZN</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>close</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_candlestick</span><span>(</span><span>aes</span><span>(</span><span>open</span><span> </span><span>=</span><span> </span><span>open</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</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;AMZN: New Candlestick Geom!&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Visually shows open, high, low, and close information, along with direction&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;Closing Price&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_x_date</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>start</span><span>,</span><span> </span><span>end</span><span>),</span><span>
</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>700</span><span>,</span><span> </span><span>870</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>Pretty sweet! Let’s take this a step further with moving averages. The moving average geom, <code>geom_ma</code>, is used to quickly draw moving average lines using a moving average function, <code>ma_fun</code>, that is one of seven from the <code>TTR</code> package. We can use these to <em>“rapid prototype”</em> moving averages, enabling us to quickly identify changes in trends. Let’s add 15 and 50-day moving averages. Note that <code>geom_ma</code> takes arguments to control the moving average function (<code>ma_fun = SMA</code> and <code>n = 15</code>) and arguments to control the line such as <code>color = &quot;red&quot;</code> or <code>linetype = 4</code>.</p>
<figure>
<pre><code data-lang="r"><span>AMZN</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>close</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_candlestick</span><span>(</span><span>aes</span><span>(</span><span>open</span><span> </span><span>=</span><span> </span><span>open</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_ma</span><span>(</span><span>ma_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>15</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span>geom_ma</span><span>(</span><span>ma_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>50</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>4</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</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;AMZN: New Candlestick Geom + Moving Averages!&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Adding MA's is fast and easy enabling rapid prototyping&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;Closing Price&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_x_date</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>start</span><span>,</span><span> </span><span>end</span><span>),</span><span>
</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>700</span><span>,</span><span> </span><span>870</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i0.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>We can also use <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Bollinger_Bands">Bollinger Bands</a> to help visualize volatility. BBands take a moving average, such as <code>ma_fun = SMA</code> from <code>TTR</code>, and a standard deviation, <code>sd = 2</code> by default. Because BBands depend on the high, low and close prices, we need to add these as aesthetic arguments. Let’s use a 20-day simple moving average with two standard deviations. We can see that there were two periods, one in October and one in November, that had higher volatility.</p>
<figure>
<pre><code data-lang="r"><span>AMZN</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>close</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_candlestick</span><span>(</span><span>aes</span><span>(</span><span>open</span><span> </span><span>=</span><span> </span><span>open</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bbands</span><span>(</span><span>aes</span><span>(</span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>),</span><span>
</span><span>ma_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</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;AMZN: New Candlestick Geom + BBands!&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Quickly visualize volatility&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;Closing Price&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_x_date</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>start</span><span>,</span><span> </span><span>end</span><span>),</span><span>
</span><span>ylim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>700</span><span>,</span><span> </span><span>870</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-7" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<p>Last, we can visualize multiple stocks at once by adding a <code>group</code> aesthetic and tacking on a <code>facet_wrap</code> at the end of the <code>ggplot</code> workflow. Note that the out-of-bounds data becomes important to the scale of the facet: too much data and the y-axis is off scale, too little data and the moving average is thrown off. An easy way to adjust is to use <code>filter()</code> to subtract double the moving average number of periods (<code>2 * n</code>) from the start date of the data. This reduces the out-of-bounds data without eliminating data that the moving average function needs for calculations.</p>
<figure>
<pre><code data-lang="r"><span>n</span><span> </span><span>&lt;-</span><span> </span><span>20</span><span>
</span><span>FANG</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>date</span><span> </span><span>>=</span><span> </span><span>start</span><span> </span><span>-</span><span> </span><span>days</span><span>(</span><span>2</span><span> </span><span>*</span><span> </span><span>n</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>close</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>symbol.x</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_candlestick</span><span>(</span><span>aes</span><span>(</span><span>open</span><span> </span><span>=</span><span> </span><span>open</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</span><span>))</span><span> </span><span>+</span><span> </span><span>geom_bbands</span><span>(</span><span>aes</span><span>(</span><span>high</span><span> </span><span>=</span><span> </span><span>high</span><span>,</span><span> </span><span>low</span><span> </span><span>=</span><span> </span><span>low</span><span>,</span><span> </span><span>close</span><span> </span><span>=</span><span> </span><span>close</span><span>),</span><span>
</span><span>ma_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>n</span><span>,</span><span> </span><span>sd</span><span> </span><span>=</span><span> </span><span>2</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.5</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;Multiple Stocks at Once!&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Quickly visualize the volatility of four stocks at once&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;Closing Price&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>coord_x_date</span><span>(</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>start</span><span>,</span><span> </span><span>end</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span> </span><span>symbol.x</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-8" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<h2>Example 2: Working with Key Statistics <a></a></h2>
<p>New to <code>tq_get</code> is the get option <code>get = &quot;key.stats&quot;</code>. So, what are key stats? Yahoo Finance has an amazing list of <strong>real-time statistics</strong> such as bid price, ask price, day’s high, day’s low, change, and many more features that change throughout the day. Key stats are our access to <em>live data</em>, the most current features of a stock / company, many of which are accurate to the second that they are retrieved. Pretty neat!</p>
<h5>Getting Key Stats</h5>
<p>Let’s get some key stats, and see what’s inside. We get key stats using the <code>tq_get</code> function, setting <code>get = &quot;key.stats&quot;</code>. When we show the data, it’s kind of messy (there’s a reason) so I’ve just listed the first ten column names. It comes in the form of a one row tibble (tidy data frame) that has 55 columns, one for each key stat.</p>
<figure>
<pre><code data-lang="r"><span>AAPL_key_stats</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;key.stats&quot;</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>AAPL_key_stats</span><span>)[</span><span>1</span><span>:</span><span>10</span><span>]</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##  [1] &quot;Ask&quot;                               
##  [2] &quot;Ask.Size&quot;                          
##  [3] &quot;Average.Daily.Volume&quot;              
##  [4] &quot;Bid&quot;                               
##  [5] &quot;Bid.Size&quot;                          
##  [6] &quot;Book.Value&quot;                        
##  [7] &quot;Change&quot;                            
##  [8] &quot;Change.From.200.day.Moving.Average&quot;
##  [9] &quot;Change.From.50.day.Moving.Average&quot; 
## [10] &quot;Change.From.52.week.High&quot;</code></pre>
</figure>
<p>The reason that the data comes this way is because, using the <a target="_blank" rel="nofollow" href="http://www.mattdancho.com/code-tools/2017/01/22/tidyquant-update-0-3-0.html#example3">new scaling capability</a>, we can get key stats for multiple stocks, and the rows get stacked on top of each other. This makes comparing key stats very easy!</p>
<figure>
<pre><code data-lang="r"><span>c</span><span>(</span><span>&quot;FB&quot;</span><span>,</span><span> </span><span>&quot;AMZN&quot;</span><span>,</span><span> </span><span>&quot;NFLX&quot;</span><span>,</span><span> </span><span>&quot;GOOG&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tq_get</span><span>(</span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;key.stats&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>symbol.x</span><span>,</span><span> </span><span>Bid</span><span>,</span><span> </span><span>Bid.Size</span><span>,</span><span> </span><span>Ask</span><span>,</span><span> </span><span>Ask.Size</span><span>,</span><span> </span><span>Open</span><span>,</span><span> </span><span>Change</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 4 × 7
##   symbol.x    Bid Bid.Size    Ask Ask.Size   Open Change
##      &lt;chr>  &lt;dbl>    &lt;int>  &lt;dbl>    &lt;int>  &lt;dbl>  &lt;dbl>
## 1       FB 127.00      300 127.07      100 128.10  -0.51
## 2     AMZN 807.52      100 808.73      100 815.28  -0.71
## 3     NFLX 138.30      100 138.60      300 139.36   0.19
## 4     GOOG 800.00      100 814.00      100 806.91   2.85</code></pre>
</figure>
<h5>Retrieve Real-Time Data at Periodic Intervals</h5>
<p>Something great about real-time data is that it can be collected at periodic intervals when trading is in-session! The following code chunk when run will retrieve stock prices at a periodic interval:</p>
<figure>
<pre><code data-lang="r"><span>collect_real_time_data</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>interval_sec</span><span>,</span><span> </span><span>n</span><span>)</span><span> </span><span>{</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>()</span><span>
</span><span>while</span><span> </span><span>(</span><span>n</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span> </span><span>{</span><span>
</span><span>data</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>data</span><span>,</span><span> </span><span>tq_get</span><span>(</span><span>x</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;key.stats&quot;</span><span>))</span><span>
</span><span>Sys.sleep</span><span>(</span><span>interval_sec</span><span>)</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>n</span><span> </span><span>-</span><span> </span><span>1</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>data</span><span>)</span><span>
</span><span>}</span><span>
</span><span>collect_real_time_data</span><span>(</span><span>&quot;AAPL&quot;</span><span>,</span><span> </span><span>interval_sec</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>5</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>Ask</span><span>,</span><span> </span><span>Ask.Size</span><span>,</span><span> </span><span>Bid</span><span>,</span><span> </span><span>Bid.Size</span><span>,</span><span> </span><span>Open</span><span>,</span><span> </span><span>Change</span><span>)</span></code></pre>
</figure>
<h5>Comparing Historical Data to Current Data</h5>
<p>We now have <code>get = &quot;key.stats&quot;</code> for current stats and with v0.2.0 we got <code>get = &quot;key.ratios&quot;</code> for 10-years of historical ratios. When combined, we can now compare current attributes to historical trends. To put into perspective, we will investigate the <strong>P/E Ratio: Comparing Historical Trends Versus Current Value</strong> for AAPL. The <a target="_blank" rel="nofollow" href="http://www.investopedia.com/terms/p/price-earningsratio.asp">P/E ratio</a> is a measure of the stock valuation. Stocks are considered “expensive” when they trade above historical averages or above industry averages.</p>
<p>We already have the <strong>key stats</strong> from AAPL, so getting the current P/E Ratio is very easy.</p>
<figure>
<pre><code data-lang="r"><span>AAPL_key_stats</span><span>$</span><span>PE.Ratio</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] 14.44</code></pre>
</figure>
<p>Due to the amount of data and time-series nature, the <strong>key ratios</strong> come as a nested tibble, grouped by section type.</p>
<figure>
<pre><code data-lang="r"><span>AAPL_key_ratios</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;key.ratios&quot;</span><span>)</span><span>
</span><span>AAPL_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>We need to get the historical P/E Ratios, which are in the “Valuation Ratios” section. We will do a series of filtering and unnesting to peel away the layers and isolate the “Price to Earnings” time-series data.</p>
<figure>
<pre><code data-lang="r"><span>AAPL_historical_pe_ratios</span><span> </span><span>&lt;-</span><span> </span><span>AAPL_key_ratios</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>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>select</span><span>(</span><span>category</span><span>,</span><span> </span><span>date</span><span>,</span><span> </span><span>value</span><span>)</span><span>
</span><span>AAPL_historical_pe_ratios</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 10 × 3
##             category       date    value
##                &lt;chr>     &lt;date>    &lt;dbl>
## 1  Price to Earnings 2007-12-31 46.02575
## 2  Price to Earnings 2008-12-31 11.44933
## 3  Price to Earnings 2009-12-31 21.09266
## 4  Price to Earnings 2010-12-31 19.43144
## 5  Price to Earnings 2011-12-30 13.34155
## 6  Price to Earnings 2012-12-31 11.07157
## 7  Price to Earnings 2013-12-31 13.29209
## 8  Price to Earnings 2014-12-31 16.46026
## 9  Price to Earnings 2015-12-31 11.16801
## 10 Price to Earnings 2016-12-30 13.93742</code></pre>
</figure>
<p>Now, we are ready to visualize the <em>P/E Ratio: Comparing Historical Trends Versus Current Value</em> for AAPL. The visualization below is inspired by <a target="_blank" rel="nofollow" href="http://r-statistics.co/">r-statistics.co</a>, an awesome resource for <code>ggplot2</code> and R analysis. We add the following:</p>
<ul>
<li>Geoms:
<ol>
<li><code>geom_line()</code> and <code>geom_point()</code> to chart the historical data</li>
<li><code>geom_ma()</code> to chart the three period simple moving average (the three period average helps identify the trend through the noise)</li>
<li><code>geom_hline()</code> to add a horizontal line at current P/E Ratio obtained from key stats.</li>
</ol>
</li>
<li>Legend: We manipulate the colors with <code>scale_color_manual()</code> and the position in the <code>theme()</code> function.</li>
<li>Logo: A logo is generated as a <code>grob</code> (grid graphical object) using the <code>grid</code> and <code>png</code> packages. The function <code>annotate_custom()</code> allows us to simply add to the ggplot workflow. See <a target="_blank" rel="nofollow" href="http://r-statistics.co/Complete-Ggplot2-Tutorial-Part2-Customizing-Theme-With-R-Code.html#Add%20an%20Image%20in%20Background">Add an Image to Background</a> for a tutorial.</li>
</ul>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>grid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>png</span><span>)</span><span>
</span><span>img</span><span> </span><span>&lt;-</span><span> </span><span>png</span><span>::</span><span>readPNG</span><span>(</span><span>&quot;../img/tidyquant-logo.png&quot;</span><span>)</span><span> </span><span>g_pic</span><span> </span><span>&lt;-</span><span> </span><span>grid</span><span>::</span><span>rasterGrob</span><span>(</span><span>img</span><span>,</span><span> </span><span>interpolate</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>AAPL_historical_pe_ratios</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>aes</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;historical.pe&quot;</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_ma</span><span>(</span><span>aes</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;historical.ma&quot;</span><span>),</span><span> </span><span>ma_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>AAPL_key_stats</span><span>$</span><span>PE.Ratio</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</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;AAPL P/E Ratio: Comparing Historical Trends Versus Current Value&quot;</span><span>,</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Awesome historical analysis is possible with the combo of get options: 'key.stats' + 'key.ratios'&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>name</span><span> </span><span>=</span><span> </span><span>'Legend'</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>'legend'</span><span>,</span><span>
</span><span>values</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'historical.pe'</span><span> </span><span>=</span><span> </span><span>'black'</span><span>,</span><span>
</span><span>'historical.ma'</span><span> </span><span>=</span><span> </span><span>'blue'</span><span>),</span><span> </span><span>labels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>'3-Year Moving Average'</span><span>,</span><span>
</span><span>'Historical PE'</span><span>))</span><span> </span><span>+</span><span>
</span><span>annotate</span><span>(</span><span>&quot;text&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Current PE&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>ymd</span><span>(</span><span>&quot;2016-06-01&quot;</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>16</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.justification</span><span>=</span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>),</span><span> </span><span>legend.position</span><span>=</span><span>c</span><span>(</span><span>0.75</span><span>,</span><span> </span><span>0.85</span><span>))</span><span> </span><span>+</span><span>
</span><span>annotation_custom</span><span>(</span><span>g_pic</span><span>,</span><span> </span><span>xmin</span><span> </span><span>=</span><span> </span><span>&quot;2015-01-01&quot;</span><span> </span><span>%>%</span><span> </span><span>ymd</span><span>()</span><span> </span><span>%>%</span><span> </span><span>as.numeric</span><span>(),</span><span> </span><span>xmax</span><span> </span><span>=</span><span> </span><span>&quot;2017-01-01&quot;</span><span> </span><span>%>%</span><span> </span><span>ymd</span><span>()</span><span> </span><span>%>%</span><span> </span><span>as.numeric</span><span>(),</span><span> </span><span>ymin</span><span> </span><span>=</span><span> </span><span>30</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>45</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-15" src="https://i1.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-15-1.png?w=456" data-recalc-dims="1"></p>
<p>The chart shows that the current valuation is slightly above the recent historical valuation indicating that the stock prices is slightly “expensive”. However, given that the P/E ratio is below the current SP500 average of 25, courtesy of <a target="_blank" rel="nofollow" href="http://www.multpl.com/">www.multpl.com</a>, one could also consider this stock “inexpensive”. It just depends on your perspective. <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<h2>Example 3: Scaling Your Analysis <a></a></h2>
<p>Probably the single most important benefit of performing financial analysis in the <code>tidyverse</code> is the ability to scale. Based on some excellent feedback from <a target="_blank" rel="nofollow" href="https://twitter.com/KanAugust">@KanAugust</a>, I have made scaling even easier. There’s two new options for scaling:</p>
<p><strong>New Option 1: Passing a character vector of symbols</strong>:</p>
<p>Send a character vector in the form <code>c(&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;)</code> to <code>tq_get</code>. A new column is generated, <code>symbol.x</code>, with the symbols that were passed to the <code>x</code> argument.</p>
<figure>
<pre><code data-lang="r"><span>c</span><span>(</span><span>&quot;FB&quot;</span><span>,</span><span> </span><span>&quot;GOOG&quot;</span><span>,</span><span> </span><span>&quot;AAPL&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tq_get</span><span>(</span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.prices&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 6,238 × 8
##    symbol.x       date  open  high   low close    volume adjusted
##       &lt;chr>     &lt;date> &lt;dbl> &lt;dbl> &lt;dbl> &lt;dbl>     &lt;dbl>    &lt;dbl>
## 1        FB 2012-05-18 42.05 45.00 38.00 38.23 573576400    38.23
## 2        FB 2012-05-21 36.53 36.66 33.00 34.03 168192700    34.03
## 3        FB 2012-05-22 32.61 33.59 30.94 31.00 101786600    31.00
## 4        FB 2012-05-23 31.37 32.50 31.36 32.00  73600000    32.00
## 5        FB 2012-05-24 32.95 33.21 31.77 33.03  50237200    33.03
## 6        FB 2012-05-25 32.90 32.95 31.11 31.91  37149800    31.91
## 7        FB 2012-05-29 31.48 31.69 28.65 28.84  78063400    28.84
## 8        FB 2012-05-30 28.70 29.55 27.86 28.19  57267900    28.19
## 9        FB 2012-05-31 28.55 29.67 26.83 29.60 111639200    29.60
## 10       FB 2012-06-01 28.89 29.15 27.39 27.72  41855500    27.72
## # ... with 6,228 more rows</code></pre>
</figure>
<p><strong>New Option 2: Passing a tibble with symbols in the first column</strong>:</p>
<p>We can combine <code>tq_get</code> calls using <code>get = &quot;stock.index&quot;</code> and <code>get = &quot;stock.prices&quot;</code> to pass a stock index to get stock prices. I’ve added <code>slice(1:3)</code> to get the first three stocks from the index, which reduces the download time. If you remove <code>slice(1:3)</code>, you will get the historical prices for all stocks in an index in the next step!</p>
<p>First, get stocks from an index.</p>
<figure>
<pre><code data-lang="r"><span>NASDAQ_first_three</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;NASDAQ100&quot;</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.index&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>slice</span><span>(</span><span>1</span><span>:</span><span>3</span><span>)</span><span> </span><span>NASDAQ_first_three</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 3 × 2
##   symbol             company
##    &lt;chr>               &lt;chr>
## 1    MMM                  3M
## 2    ABT ABBOTT LABORATORIES
## 3   ABBV          ABBVIE INC</code></pre>
</figure>
<p>Then get stock prices. Note that symbols must be in the first column.</p>
<figure>
<pre><code data-lang="r"><span>NASDAQ_first_three</span><span> </span><span>&lt;-</span><span> </span><span>NASDAQ_first_three</span><span> </span><span>%>%</span><span>
</span><span>tq_get</span><span>(</span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.prices&quot;</span><span>)</span><span>
</span><span>NASDAQ_first_three</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 6,083 × 9
##    symbol company       date  open  high   low close  volume adjusted
##     &lt;chr>   &lt;chr>     &lt;date> &lt;dbl> &lt;dbl> &lt;dbl> &lt;dbl>   &lt;dbl>    &lt;dbl>
## 1     MMM      3M 2007-01-03 77.53 78.85 77.38 78.26 3781500 60.31064
## 2     MMM      3M 2007-01-04 78.40 78.41 77.45 77.95 2968400 60.07174
## 3     MMM      3M 2007-01-05 77.89 77.90 77.01 77.42 2765200 59.66330
## 4     MMM      3M 2007-01-08 77.42 78.04 76.97 77.59 2434500 59.79431
## 5     MMM      3M 2007-01-09 78.00 78.23 77.44 77.68 1896800 59.86367
## 6     MMM      3M 2007-01-10 77.31 77.96 77.04 77.85 1787500 59.99468
## 7     MMM      3M 2007-01-11 78.05 79.03 77.88 78.65 2372500 60.61120
## 8     MMM      3M 2007-01-12 78.41 79.50 78.22 79.36 2582200 61.15835
## 9     MMM      3M 2007-01-16 79.48 79.62 78.92 79.56 2526600 61.31248
## 10    MMM      3M 2007-01-17 79.33 79.51 78.75 78.91 2711300 60.81156
## # ... with 6,073 more rows</code></pre>
</figure>
<p>We can also use <code>tq_mutate</code> and <code>tq_transform</code> with <code>dplyr::group_by</code> to scale analyses! Thanks to some great feedback from <a target="_blank" rel="nofollow" href="https://twitter.com/dvaughan32">@dvaughan32</a>, the <code>col_rename</code> argument is available to conveniently rename the newly transformed / mutated columns.</p>
<figure>
<pre><code data-lang="r"><span>NASDAQ_first_three</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>symbol</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>Ad</span><span>,</span><span> </span><span>mutate_fun</span><span> </span><span>=</span><span> </span><span>SMA</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>col_rename</span><span> </span><span>=</span><span> </span><span>&quot;SMA.5&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>(</span><span>open</span><span>:</span><span>volume</span><span>))</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## Source: local data frame [6,083 x 5]
## Groups: symbol [3]
## 
##    symbol company       date adjusted    SMA.5
##     &lt;chr>   &lt;chr>     &lt;date>    &lt;dbl>    &lt;dbl>
## 1     MMM      3M 2007-01-03 60.31064       NA
## 2     MMM      3M 2007-01-04 60.07174       NA
## 3     MMM      3M 2007-01-05 59.66330       NA
## 4     MMM      3M 2007-01-08 59.79431       NA
## 5     MMM      3M 2007-01-09 59.86367 59.94073
## 6     MMM      3M 2007-01-10 59.99468 59.87754
## 7     MMM      3M 2007-01-11 60.61120 59.98543
## 8     MMM      3M 2007-01-12 61.15835 60.28444
## 9     MMM      3M 2007-01-16 61.31248 60.58807
## 10    MMM      3M 2007-01-17 60.81156 60.77765
## # ... with 6,073 more rows</code></pre>
</figure>
<p><strong>Here’s a powerful example</strong>: We can use <code>group_by</code> and <code>tq_transform</code> to collect annual returns for a tibble of stock prices for multiple stocks. The result can be piped to <code>ggplot</code> for charting.</p>
<figure>
<pre><code data-lang="r"><span>NASDAQ_first_three</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>company</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>tq_transform</span><span>(</span><span>Ad</span><span>,</span><span> </span><span>periodReturn</span><span>,</span><span> </span><span>period</span><span> </span><span>=</span><span> </span><span>&quot;yearly&quot;</span><span>,</span><span> </span><span>col_rename</span><span> </span><span>=</span><span> </span><span>&quot;Annual.Returns&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>year</span><span>(</span><span>date</span><span>),</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>Annual.Returns</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>company</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>position</span><span> </span><span>=</span><span> </span><span>&quot;dodge&quot;</span><span>,</span><span> </span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>labels</span><span> </span><span>=</span><span> </span><span>scales</span><span>::</span><span>percent</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_continuous</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>2007</span><span>,</span><span> </span><span>2017</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>1</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;First three companies in SP500: Charting Annual Returns&quot;</span><span>,</span><span> </span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;tq_mutate and tq_transform can be used at scale to quickly visualize returns&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-20" src="https://i2.wp.com/www.mattdancho.com/figure/source/2017-1-22-tidyquant-update-0-3-0/unnamed-chunk-20-1.png?w=456" data-recalc-dims="1"></p>
<h1>Conclusions <a></a></h1>
<p>The <code>tidyquant</code> package has several enhancements for financial analysis:</p>
<ul>
<li>
<p>New <code>ggplot2</code> geoms for candlestick charts, barcharts, moving averages, and Bollinger Bands, and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tidyquant">a brand new vignette</a> to help guide users on charting capabilities.</p>
</li>
<li>
<p>New <code>get = &quot;key.stats&quot;</code> for current stats on stocks: 55 total are available. The key stats compliment the key ratios (`get = “key.ratios”), which contain 10-years of historical information on various key ratios and financial information.</p>
</li>
<li>
<p>New capabilities for scaling financial analyses to many stocks:</p>
<ul>
<li>Using <code>tq_get</code> with character vectors or tibbles of stocks</li>
<li>Using <code>tq_mutate</code> / <code>tq_transform</code> with <code>dplyr::group_by</code></li>
</ul>
</li>
</ul>
<p>With these updates, we can really do full financial analyses without ever leaving the <code>tidyverse</code>!</p>
<h1>Recap <a></a></h1>
<p>We went over a few examples to illustrate the main updates to <code>tidyquant</code>:</p>
<ol>
<li>
<p>The first example showed an implementation of several new <code>tidyquant</code> geoms that work with <code>ggplot2</code>: <code>geom_candlestick</code> / <code>geom_barchart</code>, <code>geom_ma</code>, and <code>geom_bbands</code>.</p>
</li>
<li>
<p>The second example showed use of the new <code>tq_get</code> get option, <code>get = &quot;key.stats&quot;</code>. The key stats provide real-time data from Yahoo Finance, and are a handy complement to the historical data provided using get options, <code>&quot;stock.prices&quot;</code>, <code>&quot;key.ratios&quot;</code>, and <code>&quot;financials&quot;</code>.</p>
</li>
<li>
<p>The third and final example showed some of the improvements in scaling analysis with the <code>tidyverse</code>. You can now pipe multiple symbols into <code>tq_get</code> to scale any of the get options, and you can use <code>tq_mutate</code> and <code>tq_transform</code> with <code>dplyr::group_by</code>.</p>
</li>
</ol>
<p>I hope you enjoy the new features as much as I did creating them. As always there’s more to come! <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/2/72x72/1f642.png"></p>
<h1>Further Reading <a></a></h1>
<ol>
<li>
<p><strong><a target="_blank" rel="nofollow" href="http://r-statistics.co/">r-statistics.co</a></strong>: You need to check out this website, which contains a wealth of quality, up-to-date R information. The <a target="_blank" rel="nofollow" href="http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html">Top 50 ggplot2 visualizations</a> is amazing. This is now my go-to reference on <code>ggplot2</code>.</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tidyquant">Tidyquant Vignettes</a></strong>: This tutorial just scratches the surface of <code>tidyquant</code>. The vignettes explain 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 CRAN site</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 on CRAN</a></strong>: The reference manual covers each of the <code>TTR</code> functions.</p>
</li>
<li>
<p><strong><a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=zoo">Zoo Vignettes</a></strong>: Covers the <code>zoo</code> rollapply functions as well as other usage.</p>
</li>
</ol>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/isimc2X3h5Q">