Installation

  1. Install R.

  2. Install RStudio.

  3. Open RStudio and install devtools package by running install.packages( 'devtools' ). Depending on your platform for devtools to work on Windows install Rtools, on OSX install Xcode, on Linux install r-devel or r-base-dev.

  4. Install QuantTools package by running:

    install.packages( QuantTools )
    # or Install QuantTools development version
    # devtools::install_bitbucket( 'quanttools/QuantTools' )
  5. Make sure Rcpp package is installed and works. Running these lines should give you 4:

    library( QuantTools )
    Rcpp::evalCpp( code = '2 + 2' )

Congratulations, you are setup now! In case you have any trouble installing the components drop some comments.

Settings

QuantTools package different settings which are very useful if you plan to build backtesting and market data storage system. For complete list of settings run ?QuantTools_settings in RStudio.

Get Market Data

Download From The Internet

With QuantTools download market data is very simple. You are welcome to download data from Yahoo, Google, Finam or IQFeed servers.

Note:

  • Intraday tick data provided only by Finam and IQFeed
  • You need to have IQFeed subscription to get data from their servers. If IQFeed software installed on your system please update the following settings:

    QuantTools_settings( settings = list(
    iqfeed_host = 'localhost',
    iqfeed_port = 'iqfeed port number'
    ) )

Daily Candles

library( QuantTools )
from = '2016-01-01'
to   = '2016-06-01'
symbol = 'AAPL'

Finam

get_finam_data( symbol, from, to )
##            date   open   high    low  close  volume
##   1: 2016-01-04 102.48 105.36 102.00 105.34 3891905
##   2: 2016-01-05 105.75 105.78 102.42 102.71 3225980
##  ---                                               
## 103: 2016-06-01  99.00  99.54  98.33  98.43 2299445
## 104: 2016-06-02  97.67  97.84  96.63  97.71 2592315

Google

get_google_data( symbol, from, to )
##            date   open   high    low  close   volume
##   1: 2016-01-04 102.61 105.37 102.00 105.35 67281190
##   2: 2016-01-05 105.75 105.85 102.41 102.71 55790992
##  ---                                                
## 103: 2016-05-31  99.60 100.40  98.82  99.86 42307212
## 104: 2016-06-01  99.02  99.54  98.33  98.46 29173285

Yahoo

get_yahoo_data( symbol, from, to )
##            date   open   high    low  close   volume adj close split_coeff
##   1: 2016-01-04 102.61 105.37 102.00 105.35 67649400 103.58618           1
##   2: 2016-01-05 105.75 105.85 102.41 102.71 55791000 100.99038           1
##  ---                                                                      
## 103: 2016-05-31  99.60 100.40  98.82  99.86 42307200  99.32195           1
## 104: 2016-06-01  99.02  99.54  98.33  98.46 29173300  97.92949           1

IQFeed

get_iqfeed_data( symbol, from, to )
##            date   open    high    low  close
##   1: 2016-01-04 102.61 105.368 102.00 105.35
##   2: 2016-01-05 105.75 105.850 102.41 102.71
##  ---                                        
## 103: 2016-05-31  99.60 100.400  98.82  99.86
## 104: 2016-06-01  99.02  99.540  98.33  98.46

Intraday Candles

Finam

get_finam_data( symbol, from, to, period = 'hour' )
##                     time   open   high    low   close volume
##   1: 2016-01-04 09:00:00 102.48 103.69 102.00 102.700 657731
##   2: 2016-01-04 10:00:00 102.68 103.93 102.44 103.345 918556
##  ---                                                        
## 697: 2016-06-01 14:00:00  98.99  99.18  98.90  99.005 248585
## 698: 2016-06-01 15:00:00  99.01  99.03  98.33  98.430 556923

IQFeed

get_iqfeed_data( symbol, from, to, period = 'hour' )
##                      time   open   high    low  close volume
##    1: 2016-01-04 05:00:00 105.45 105.45 103.40 103.40  35521
##    2: 2016-01-04 06:00:00 103.55 103.77 103.48 103.68   7043
##   ---                                                       
## 1634: 2016-05-31 19:00:00  99.86  99.91  99.85  99.85   4950
## 1635: 2016-05-31 20:00:00  99.85  99.95  99.81  99.95   9200

Intraday Ticks

Lets download for single day only

# most recent non weekend date
from = format( Sys.Date() - switch( format( Sys.Date(), '%u' ), '6' = 1, '7' = 2, 0 ) )

Finam

get_finam_data( symbol, from, to = from, period = 'tick' )
##                       time   price volume
##     1: 2016-10-10 09:30:00 115.080    100
##     2: 2016-10-10 09:30:00 115.090    100
##    ---                                   
## 15622: 2016-10-10 15:09:00 116.435    100
## 15623: 2016-10-10 15:09:03 116.440    100

IQFeed

get_iqfeed_data( symbol, from, to = from, period = 'tick' )
##                        time  price volume     size    bid    ask tick_id basis_for_last trade_market_center trade_conditions
##      1: 2016-10-10 04:00:00 114.15     50       50 113.86 113.93    1032              O                  11             8717
##      2: 2016-10-10 04:00:00 114.25    100      150 113.86 113.93    1036              E                  11             173D
##     ---                                                                                                                     
## 164099: 2016-10-10 15:09:40 116.44    103 28967078 116.43 116.44 1320337              C                  19               01
## 164100: 2016-10-10 15:09:40 116.43    103 28967181 116.43 116.44 1320338              C                  19               01

Store Market Data

Tick market data is a lot of information and when downlading from internet it takes not only time but some data providers allows you to download only several month of data. If you want to test your strategies for a longer period you have to store data locally. It is a big problem managing and storing tick market data. QuantTools makes this process easily controlled. You just setup storage parameters and 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. Here is an example script which do all that:

Setup

Finam

symbols = c( 'GAZP', 'SBER', 'VTBR' )
path = paste( path.expand('~') , 'Market Data', 'finam', sep = '/' )
start_date = '2016-01-01'

settings = list(
  finam_storage = path,
  finam_storage_from = start_date,
  finam_symbols = symbols
)
QuantTools_settings( settings )

store_finam_data()

IQFeed

symbols = c( 'AAPL','IBM','PG','WFC','YHOO','GBPUSD.FXCM','EURUSD.FXCM' )
path = paste( path.expand('~') , 'Market Data', 'iqfeed', sep = '/' )
start_date = '2016-01-01'

settings = list(
  iqfeed_storage = path,
  iqfeed_storage_from = start_date,
  iqfeed_symbols = symbols
)
QuantTools_settings( settings )

store_iqfeed_data()

Note: rerun the above script when you need to add most recent data to local storage. It is a good practice to schedule the script in a system to run it on daily basis.

Get From Local Storage

Just set local = T as get_market_data argument to retrieve data from local storage.

get_iqfeed_data( symbol = 'AAPL', from = '2016-09-08', to = '2016-09-12', period = 'tick', local = T )

Plot Time Series Data

QuantTools provides plot_ts function to plot time series data without weekend, holidays and overnight gaps. Lets visualize some data:

## get data
aapl = get_finam_data( 'AAPL', '2016-01-01', '2016-09-01', period = '15min' )
## add some indicators
aapl[, sma := sma( close, 50 ) ]
aapl[, ema := ema( close, 50 ) ]
## plot
plot_ts( aapl )

To plot smaller intervals use %bw% function:

## plot selected month
plot_ts( aapl[ time %bw% '2016-05' ] )
plot_ts( aapl[ , .( time, sma, ema ) ], col = c( 'red', 'blue' ), add = T )

## plot selected date
plot_ts( aapl[ time %bw% '2016-05-09' ] )
plot_ts( aapl[ , .( time, sma, ema ) ], col = c( 'red', 'blue' ), add = T )

## plot time interval
library( fasttime )
plot_ts( aapl[ time %bw% fastPOSIXct( paste( '2016-05-09', c( '12:00', '13:30') ) ) ] )
plot_ts( aapl[ , .( time, sma, ema ) ], col = c( 'red', 'blue' ), add = T )

Note: for a list of available parameters see ?plot_ts

Back Testing

QuantTools allows you to write trading models using its C++ API. It is a little more code so it has its own section. Please see Examples on top of this site.

© 2016 Stanislav Kovalevsky. All rights reserved.