<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/demo-week-classmonday-lt-tidyquant/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/code-tools/2017/10/23/demo_week_tidyquant.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’ve got an exciting week ahead of us at <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/23/demo_week_tidyquant.html#contact" target="_blank">Business Science</a>: we’re launching our <strong>first ever Business Science Demo Week</strong>. Every day this week we are demoing an R package: <code>tidyquant</code> (Monday), <code>timetk</code> (Tuesday), <code>sweep</code> (Wednesday), <code>tibbletime</code> (Thursday) and <code>h2o</code> (Friday)! <strong>That’s five packages in five days</strong>! We’ll give you intel on <strong>what you need to know about these packages to go from zero to hero</strong>. First up is <code>tidyquant</code>, our flagship package that’s useful for financial and time series analysis. Here we go!</p>
<h1>tidyquant: What’s It Used For?</h1>
<p>Six reasons to use <code>tidyquant</code>:</p>
<ol>
<li>
<p><strong>Getting web data from Yahoo! Finance, FRED Database, Quandl and more</strong></p>
</li>
<li>
<p><strong>Tidy application of financial and time series functions from <code>xts</code>, <code>zoo</code>, <code>quantmod</code>, <code>TTR</code> and <code>PerformanceAnalytics</code></strong></p>
</li>
<li>
<p><strong>Graphing</strong>: Beautiful themes and financial geoms (e.g. <code>geom_ma</code>)</p>
</li>
<li>
<p><strong>Aggregating portfolios</strong></p>
</li>
<li>
<p><strong>Financial performance analysis and portfolio attribution metrics</strong></p>
</li>
<li>
<p><strong>Great base for financial and time series analysis</strong>: The tidyquant stack automatically loads the <code>tidyverse</code> and various time series / financial packages behind the scenes making it a great starting point for any financial or time series analysis</p>
</li>
</ol>
<p>We’ll go over first two points in this demo. See the <a rel="nofollow" href="https://business-science.github.io/tidyquant/" target="_blank">documentation</a> for other topics.</p>
<p><img alt="Demo Week: tidyquant" src="https://i0.wp.com/www.business-science.io/assets/demo-week-tidyquant.png?w=456" data-recalc-dims="1"></p>
<h1>Load Libraries</h1>
<p>If you don’t have it installed, install <code>tidyquant</code>.</p>
<figure>
<pre><code data-lang="r"><span># Install libraries</span><span>
</span><span>install.packages</span><span>(</span><span>&quot;tidyquant&quot;</span><span>)</span></code></pre>
</figure>
<p>Load the <code>tidyquant</code> package, which is all you need for the demo.</p>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Loads tidyverse, financial pkgs, used to get and manipulate data</span></code></pre>
</figure>
<h1>Getting data: tq_get</h1>
<p>Use <code>tq_get()</code> to get data from the web. There’s a bunch of API’s it connects to including Yahoo! Finance, FRED Economic Database, Quandl, and more!</p>
<h3>Getting Stock Prices (Yahoo! Finance)</h3>
<p>Pass a vector of stock symbols to <code>tq_get()</code> setting <code>get = &quot;stock.prices&quot;</code>. You can add <code>from</code> and <code>to</code> arguments to bound the daily stock prices to a window.</p>
<figure>
<pre><code data-lang="r"><span># Get Stock Prices from Yahoo! Finance</span><span>
</span><span># Create a vector of stock symbols</span><span>
</span><span>FANG_symbols</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># Pass symbols to tq_get to get daily prices</span><span>
</span><span>FANG_data_d</span><span> </span><span>&lt;-</span><span> </span><span>FANG_symbols</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;2014-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2016-12-31&quot;</span><span>)</span><span>
</span><span># Show the result</span><span>
</span><span>FANG_data_d</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 3,024 x 8
##    symbol       date  open  high   low close   volume adjusted
##     </code></pre></figure>