<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/linking-r-to-iqfeed-with-the-quanttools-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.thertrader.com/2017/06/04/linking-r-to-iqfeed-with-the-quanttools-package/"> R – The R Trader</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>IQFeed provides streaming data services and trading solutions that cover the Agricultural, Energy and Financial marketplace. It is a well known and recognized data feed provider geared toward retail users and small institutions. The subscription price starts at around $80/month.</p>
<p>Stanislav Kovalevsky has developed a package called <a rel="nofollow" href="https://quanttools.bitbucket.io/_site/index.html" target="_blank">QuantTools</a>. It is an all in one package designed to enhance quantitative trading modelling. It allows to download and organize historical market data from multiple sources like Yahoo, Google, Finam, MOEX and IQFeed. The feature that interests me the most is the ability to link IQFeed to R. I’ve been using IQFeed for a few years and I’m happy with it (I’m not affiliated to the company in any way). More information can be found <a rel="nofollow" href="http://www.iqfeed.net/" target="_blank">here</a>. I’ve been looking for an integration within R for a while and here it is. As a result, after I ran a few tests, I moved my code that was still in Python into R. Just for completeness, here’s a <a rel="nofollow" href="https://www.quantstart.com/articles/Downloading-Historical-Intraday-US-Equities-From-DTN-IQFeed-with-Python" target="_blank">link</a> that explains how to download historical data from IQFeed using Python.</p>
<p>QuantTools offers four main functionalities: Get market data, Store/Retrieve market data, Plot time series data and  Back testing</p>
<ul>
<li><strong>Get Market Data</strong></li>
</ul>
<p>First make sure that IQfeed is open. You can either download daily or intraday data. The below code downloads daily prices (Open, High, Low, Close) for SPY from 1st Jan 2017 to 1st June 2017.</p>
<pre>
## Generic parameters 
library(QuantTools) 
from = '2016-01-01' 
to = '2016-06-01' 
symbol = 'SPY' 

## Request data 
get_iqfeed_data(symbol, from, to) 
</pre>
<p>The below code downloads intraday data from 1st May 2017 to 3rd May 2017.</p>
<pre>
## Generic parameters
library(QuantTools)
from = '2016-05-01' 
to = '2016-05-03'
symbol = 'SPY'

## Request data 
get_iqfeed_data(symbol, from, to, period = 'tick') 
</pre>
<p>Note the period parameter. It can take any of the following values: tick, 1min, 5min, 10min, 15min, 30min, hour, day, week, month, depending on the frequency you need.</p>
<ul>
<li><strong>Store/Retrieve Market Data</strong></li>
</ul>
<p>QuantTools makes the process of managing and storing tick market data easy. You just setup storage parameters and you are ready to go. The parameters are where, since what date and which symbols you would like to be stored. Any time you can add more symbols and if they are not present in a storage, QuantTools tries to get the data from specified start date. The code below will save the data in the following directory: “C:/Users/Arnaud/Documents/Market Data/iqfeed”.  There is one sub folder by instrument and the data is aved in .rds  files.</p>
<pre>
library(QuantTools)
settings = list(
 iqfeed_storage = paste( path.expand('~') , 'Market Data', 'iqfeed', sep = '/'),
 iqfeed_symbols = c('SPY', 'QQQ'),
 iqfeed_storage_from = format(Sys.Date() - 3)
)
QuantTools_settings(settings)

# Update storage with data from last date available until today
store_iqfeed_data()
</pre>
<p>You can also store data between specific dates. Replace the last line of code above with one of the below</p>
<pre>
# Update storage with data from last date available until specified date
store_iqfeed_data(to = format(Sys.Date()))

# Update storage with data between from and to dates,
store_iqfeed_data(from = format(Sys.Date() - 3), to = format(Sys.Date()))
</pre>
<p>Now should you want to get back some of the data you stored, just run something like:</p>
<pre>
get_iqfeed_data(symbol = 'SPY', from = '2017-06-01', to = '2017-06-02', period = 'tick', local = TRUE)
</pre>
<p>Note that only ticks are supported in local storage so period must be ‘tick’</p>
<ul>
<li><strong>Plot time series data</strong></li>
</ul>
<p>QuantTools provides <code>plot_ts</code> function to plot time series data without weekend, holidays and overnight gaps. In the example below, I first retrieve the data stored above, then select the first 100 price observations and finally draw the chart</p>
<pre><code></code></pre>
<pre>
## Retrieve previously stored data
spy = get_iqfeed_data(symbol = 'SPY', 
 from = '2017-06-01', 
 to = '2017-06-02', 
 period = 'tick', 
 local = TRUE)

## Select the first 100 rows
spy_price = spy[,.(time,price)][1:100]

## Plot
plot_ts(spy_price)
</pre>
<p><a rel="nofollow" href="https://i2.wp.com/www.thertrader.com/wp-content/uploads/2017/06/Rplot.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.thertrader.com/wp-content/uploads/2017/06/Rplot-300x178.png?resize=300%2C178" alt="" class="wp-image-1605" data-recalc-dims="1"></a></p>
<p>Two things to notice: First <code>spy</code> is a data.table object hence the syntax above. To get a  quick overview of data.table capabilities have a look at this excellent <a rel="nofollow" href="https://s3.amazonaws.com/assets.datacamp.com/img/blog/data+table+cheat+sheet.pdf?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">cheat sheet</a> from <a rel="nofollow" href="https://www.datacamp.com/?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">DataCamp</a>. Second the <code>local</code> parameter is <code>TRUE</code> as the data is retrieved from internal storage.</p>
<ul>
<li><strong>Back testing</strong></li>
</ul>
<p>QuantTools allows to write your own trading strategy using its C++ API. I’m not going to elaborate on this as this is basically C++ code. You can refer to the Examples section on QuantTools website.</p>
<p>Overall I find the package extremely useful and well documented. The only missing bit is the live feed between R and IQFeed which will make the package a real end to end solution.</p>
<p>As usual any comments welcome</p>
	<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.thertrader.com/2017/06/04/linking-r-to-iqfeed-with-the-quanttools-package/"> R – The R Trader</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/a9WYhLHthEc" height="1"></a></div>