<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/alphavantager-an-r-interface-to-the-free-alpha-vantage-financial-data-api/"></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/09/03/alphavantager-0-1-0.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’re excited to announce the <code>alphavantager</code> package, a lightweight R interface to the Alpha Vantage API! <a rel="nofollow" href="https://www.alphavantage.co/" target="_blank">Alpha Vantage</a> is a <strong>FREE API for retreiving real-time and historical financial data</strong>. It’s very easy to use, and, with the recent <em>glitch</em> with the <em>Yahoo Finance</em> API, <em>Alpha Vantage</em> is a solid alternative for retrieving financial data for FREE! It’s definitely worth checking out if you are interested in financial analysis. We’ll go through the <code>alphavantager</code> R interface in this post to show you how easy it is to get <strong>real-time and historical financial data</strong>. In the near future, we have plans to incorporate the <code>alphavantager</code> into <code>tidyquant</code> to enable scaling from one equity to many.</p>
<p>If you like what you read, please <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/09/03/alphavantager-0-1-0.html#social" target="_blank">follow us on social media</a> to stay up on the latest <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/09/03/alphavantager-0-1-0.html#contact" target="_blank">Business Science</a> news, events and information! As always, we are interested in both expanding our <em>network of data scientists</em> and seeking <em>new clients interested in applying data science to business and finance</em>. If interested, <a rel="nofollow" href="http://www.business-science.io/contact.html" target="_blank">contact us</a>.</p>
<h2>Alpha Vantage</h2>
<p>Alpha Vantage is a free service that enables users to get real-time and historical equity data. New users will need to visit <a rel="nofollow" href="https://www.alphavantage.co/" target="_blank">Alpha Vantage</a> and obtain an API key.</p>
<p><img alt="Alpha Vantage" src="https://i1.wp.com/www.business-science.io/assets/alphavantager-0-1-0.png?w=456" data-recalc-dims="1"></p>
<h2>R Interface: Getting Started</h2>
<p>The <code>alphavantager</code> package provides a convenient and lightweight interface to the Alpha Vantage API.</p>
<p>To get started, install the package from CRAN or from GitHub:</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;alphavantager&quot;</span><span>)</span><span>
</span><span># Or
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;business-science/alphavantager&quot;</span><span>)</span></code></pre>
</figure>
<p>Load the package.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>alphavantager</span><span>)</span></code></pre>
</figure>
<p>Set your API key (get one from <a rel="nofollow" href="https://www.alphavantage.co/" target="_blank">Alpha Vantage</a> if you don’t already have one… it’s free).</p>
<figure>
<pre><code data-lang="r"><span>av_api_key</span><span>(</span><span>&quot;YOUR_API_KEY&quot;</span><span>)</span><span>
</span><span>print</span><span>(</span><span>av_api_key</span><span>())</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] &quot;YOUR_API_KEY&quot;</code></pre>
</figure>
<h2>Getting Financial Data from Alpha Vantage</h2>
<p>Now, you’re ready to get financial data via <code>av_get()</code>, which accepts the same<sup>1</sup> arguments as the <a rel="nofollow" href="https://www.alphavantage.co/documentation/" target="_blank">API Documentation</a> parameters. The function is setup with two primary arguments, <code>symbol</code> and <code>av_fun</code>, which accepts an equity and one of the API “function” parameters. You can pass additional API parameters via the <code>...</code>.</p>
<figure>
<pre><code data-lang="r"><span># Function is streamlined and user adds additional parameters via ... </span><span>args</span><span>(</span><span>av_get</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## function (symbol = NULL, av_fun, ...) 
## NULL</code></pre>
</figure>
<p>Let’s take <code>av_get()</code> for a test spin!</p>
<h4>Time Series Data</h4>
<p>We can get <strong>real-time intraday stock data</strong> by setting <code>av_fun = &quot;TIME_SERIES_INTRADAY&quot;</code> and the <code>interval</code> to one of the available resolutions (“1min”, “5min”, “15min”, “30min” or “60min”). We can also get daily, daily adjusted, weekly and monthly time series. Note that only 100 rows are returned by default, add the parameter <code>outputsize = &quot;full&quot;</code> to get the full number of rows.</p>
<figure>
<pre><code data-lang="r"><span>av_get</span><span>(</span><span>symbol</span><span> </span><span>=</span><span> </span><span>&quot;MSFT&quot;</span><span>,</span><span> </span><span>av_fun</span><span> </span><span>=</span><span> </span><span>&quot;TIME_SERIES_INTRADAY&quot;</span><span>,</span><span> </span><span>interval</span><span> </span><span>=</span><span> </span><span>&quot;15min&quot;</span><span>,</span><span> </span><span>outputsize</span><span> </span><span>=</span><span> </span><span>&quot;compact&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 100 x 6
##              timestamp   open    high    low  close volume
##                 </code></pre></figure>