Linking R to IQFeed with the QuantTools package

By The R Trader

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.

Stanislav Kovalevsky has developed a package called QuantTools. 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 here. 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 link that explains how to download historical data from IQFeed using Python.

QuantTools offers four main functionalities: Get market data, Store/Retrieve market data, Plot time series data and Back testing

  • Get Market Data

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.

## Generic parameters 
library(QuantTools) 
from = '2016-01-01' 
to = '2016-06-01' 
symbol = 'SPY' 

## Request data 
get_iqfeed_data(symbol, from, to) 

The below code downloads intraday data from 1st May 2017 to 3rd May 2017.

## Generic parameters
library(QuantTools)
from = '2016-05-01' 
to = '2016-05-03'
symbol = 'SPY'

## Request data 
get_iqfeed_data(symbol, from, to, period = 'tick') 

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.

  • Store/Retrieve Market Data

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.

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()

You can also store data between specific dates. Replace the last line of code above with one of the below

# 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()))

Now should you want to get back some of the data you stored, just run something like:

get_iqfeed_data(symbol = 'SPY', from = '2017-06-01', to = '2017-06-02', period = 'tick', local = TRUE)

Note that only ticks are supported in local storage so period must be ‘tick’

  • Plot time series data

QuantTools provides plot_ts 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

## 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)

Two things to notice: First spy is a data.table object hence the syntax above. To get a quick overview of data.table capabilities have a look at this excellent cheat sheet from DataCamp. Second the local parameter is TRUE as the data is retrieved from internal storage.

  • Back testing

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.

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.

As usual any comments welcome

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.