SlideShare a Scribd company logo
Introduction   Extension Architecture    Real-Time Interface   Historical Interface   Implementation Notes   Future Work




                Building Market Data
                  Interfaces For R
                                           Rory Winston




                                        rory@theresearchkitchen.com
Introduction     Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Contents



       1       Introduction

       1       Extension Architecture

       1       Real-Time Interface

       1       Historical Interface

       1       Implementation Notes

       1       Future Work
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




About Me




               Independent Software Consultant
               M.Sc. Applied Computing, 2000
               M.Sc. Finance, 2008
               Working in the financial sector, building trading systems in
               Java/C++
               Interested in practical applications of functional languages and
               machine learning
               Relatively recent convert to R
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




The Reuters Marketfeed System


               Ubiquitous (especially in Foreign Exchange)
               APIs, Excel plugins, graphical workbenches, and trading GUIs
               Used by traders, structurers, quants, analysts....
               A market data item is identified by a RIC (Reuters Information
               Code)
               Format: CODE=[specifiers]
               Examples (FX): EUR=, EUR=EBS, GBPJPY=D2
               Examples (Equities): MSFT.O, IBM.N
               Examples (Fixed Income): EUR3M=, US30YT=RR
               Each market data item contains a number of data fields, e.g.
               BID, ASK
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Reuters Desktop

       Example Reuters 3000 session:
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Reuters Interfaces
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Extension Architecture
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Potential Uses:



               Testing real-time trading algorithms
               Market data collection and validation
               Dynamic portfolio optimization algorithms
               Trading cost / spread analysis
               Real-time interest rate curve building
               Econometric analysis
               Applying R’s vast library of computational statistics and
               machine learning routines to tick data
               Turn R into a "trading workbench"
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Example - Real Time Subscription


       Real-time interface:

                   rsub <- function(duration, items, callback)


       The call rsub will subscribe to the specified rate(s) for the duration
       of time specified by duration (ms). When a tick arrives, the
       callback function callback is invoked, with a data frame
       containing the fields specified in items.

       Multiple market data items may be subscribed to, and any
       combination of fields may be be specified.

       Uses the underlying RFA API, which provides a C++ interface to
       real-time market updates.
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Real-Time Example


       # Specify field names to retrieve
       fields <- c("BID","ASK","TIMCOR")

       # Subscribe to EUR/USD and GBP/USD ticks
       items <- list()
       items[[1]] <- c("IDN_SELECTFEED", "EUR=", fields)
       items[[2]] <- c("IDN_SELECTFEED", "GBP=", fields)

       # Simple Callback Function
       f <- function(df) { print(paste("Received",df)) }

       # Subscribe for 1 hour
       ONE_HOUR <- 1000*(60)^2
       rsub(ONE_HOUR, items, callback)
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Real-Time C++ Interface


       I used the .Call interface, as it enabled me to easily deal with the
       data frame arguments and callback functions.


       DL_EXPORT SEXP subscribe(SEXP t, SEXP spec, SEXP callback) {
       ...
       }



       Boost provides nice type-based visitor functionality, which I use to
       convert between Reuters wire-level data type and native R
       SEXP-based types.
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Example - Historical Fetch


       Historical interface:

         fetch <- function(code, n=0, t="d", sd=NULL, ed=NULL)



       Returns a data frame with data specified by code. The number of
       items returned can be specified by n, or it is defined as the number
       of items of the specified periodicity t (e.g. "d" = "daily") between
       start date sd and end date ed.

       Uses the Reuters SFC API to obtain historical data.
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Historical Interface Examples

       > fetch("IBM.N", n=20)
                Date    CLS    OPN     HI     LO      VOL   VWAP
       1 2008-07-31 127.98 127.77 129.50 127.76 2334800 128.4172
       2 2008-07-30 128.86 128.42 129.00 127.10 1936400 128.2872
       ...
       > fetch("EUR=", sd="1/1/2007", ed="1/1/2008")
                 Date    BID    OPN     HI     LO     ASK
       1   2008-01-01 1.4587 1.4585 1.4608 1.4576 1.4591
       2   2007-12-31 1.4589 1.4719 1.4747 1.4568 1.4592
       ...
       > fetch("EUR3M=", n=20, t="w")
                Date    BID    OPN     HI     LO     ASK
       1 2008-07-25 -75.60 -77.60 -74.96 -79.30 -74.41
       2 2008-07-18 -76.94 -76.30 -75.44 -81.10 -76.04
       ...
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Historical Interface Example




       msft.vol <- fetch("MSFT.O",sd="1/1/2008",ed="7/1/2008")

       layout(matrix(c(1,1,1,1,2,2), 3, 2, T))

       plot(msft.vol$Date, msft.vol$CLS,
        main="MSFT Close Price", type="l", ylab="", xlab="Date")

       plot(msft.vol$Date, msft.vol$VOL,
        main="Volume", type='h', ylab="", xlab="")
Introduction   Extension Architecture     Real-Time Interface       Historical Interface   Implementation Notes   Future Work




Historical Interface Example

                                                         MSFT Close Price




                          34
                          32
                          30
                          28




                                    Jan            Mar                      May                   Jul

                                                                 Date



                                                                Volume
                          2.0e+08
                          5.0e+07




                                    Jan            Mar                      May                   Jul
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Historical Fetch C++ Interface



       Again using the .Call interface:



       DL_EXPORT SEXP fetch(SEXP item, SEXP nItems,
        SEXP intervalCode, SEXP startDate, SEXP endDate) {
       ...



       Code handles missing dates (e.g. market holidays), and coercion of
       underlying datatypes to appropriate R datatypes
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Notes on Implementation

               Reuters Issues:
                      Reuters APIs are generally awful (newer APIs, i.e. RFA are slightly
                      better)
                      Confusing / inconsistent functionality; Peculiarities with C++
                      runtime
               R-related issues:
                      Generally very few, once you get used to the idiosyncratic
                      "Lisp-via-C-macros" style;
                      Some issues with asynchronous real-time updates and the
                      single-threaded R interpreter
               The combination of Boost, C++ and R is extremely powerful
               Combining this with market data functionality adds a new
               dimension of capabilities
               FX, options, futures, forwards, interest rates, bonds, indices,
               equities...even news updates
Introduction   Extension Architecture   Real-Time Interface   Historical Interface   Implementation Notes   Future Work




Future Work


       There are still some issues to work on, mainly some configuration
       and threading issues in the real-time extension

       Mathworks bundle a number of financial data feed providers in their
       DataFeed toolbox for Matlab:
       http://www.mathworks.com/products/datafeed/
       Maybe a similar extension (bundling say, connections to Reuters,
       Bloomberg, Lim, Thomson, etc) for R may be a useful addition?

       A financial "showcase application" for R would be great: maybe a
       financial analysts’ toolkit, similar to the Holt system (combining
       elements of statistical learning and quantitative accounting
       analysis).

More Related Content

PDF
Introduction to kdb+
PDF
Streaming Data in R
PDF
The road ahead for scientific computing with Python
PDF
Introduction to Streaming with Apache Flink
PDF
End to-end large messages processing with Kafka Streams & Kafka Connect
PDF
Introduction to Streaming with Apache Flink
PPTX
Ronan Corkery, kdb+ developer at Kx Systems: “Kdb+: How Wall Street Tech can ...
PDF
FPGAs as Components in Heterogeneous HPC Systems (paraFPGA 2015 keynote)
Introduction to kdb+
Streaming Data in R
The road ahead for scientific computing with Python
Introduction to Streaming with Apache Flink
End to-end large messages processing with Kafka Streams & Kafka Connect
Introduction to Streaming with Apache Flink
Ronan Corkery, kdb+ developer at Kx Systems: “Kdb+: How Wall Street Tech can ...
FPGAs as Components in Heterogeneous HPC Systems (paraFPGA 2015 keynote)

Similar to Real-TIme Market Data in R (20)

PPTX
OneTick and the R mathematical language, a presentation from R in Finance
PPT
Scalable Data Analysis in R -- Lee Edlefsen
PDF
peRm R group. Review of packages for r for market data downloading and analysis
PDF
SimpleR: tips, tricks & tools
PDF
廣宣學堂: R programming for_quantitative_finance_0623
PDF
Many Sources, Many Sinks, One Stream With Joel Eaton | Current 2022
PDF
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
PDF
R in finance: Introduction to R and Its Applications in Finance
PDF
Warp 10 - The most advanced Time Series Platform
PDF
2015 FOSS4G Track: Spatiotemporal Interface Development: Using Web Technologi...
PPTX
Linked Data Tutorial
PDF
C for Financial Markets The Wiley Finance Series Daniel J. Duffy
PPTX
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
PPT
Interactive Data Corporation
PPTX
Data analytics with R
PDF
Getting started with R & Hadoop
PDF
Running R on Hadoop - CHUG - 20120815
PDF
Big Data Analysis Starts with R
PPTX
A Step Towards Reproducibility in R
PDF
Sap hr material_for_use_in_initial_stages_of_training
OneTick and the R mathematical language, a presentation from R in Finance
Scalable Data Analysis in R -- Lee Edlefsen
peRm R group. Review of packages for r for market data downloading and analysis
SimpleR: tips, tricks & tools
廣宣學堂: R programming for_quantitative_finance_0623
Many Sources, Many Sinks, One Stream With Joel Eaton | Current 2022
OpenRepGrid – An Open Source Software for the Analysis of Repertory Grids
R in finance: Introduction to R and Its Applications in Finance
Warp 10 - The most advanced Time Series Platform
2015 FOSS4G Track: Spatiotemporal Interface Development: Using Web Technologi...
Linked Data Tutorial
C for Financial Markets The Wiley Finance Series Daniel J. Duffy
apidays New York 2025 - The FINOS Common Domain Model for Capital Markets by ...
Interactive Data Corporation
Data analytics with R
Getting started with R & Hadoop
Running R on Hadoop - CHUG - 20120815
Big Data Analysis Starts with R
A Step Towards Reproducibility in R
Sap hr material_for_use_in_initial_stages_of_training
Ad

More from Rory Winston (6)

PDF
Building A Trading Desk On Analytics
PDF
The Modern FX Desk
PDF
KDB+/R Integration
PDF
An Analytics Toolkit Tour
PDF
Creating R Packages
PDF
Streaming Data and Concurrency in R
Building A Trading Desk On Analytics
The Modern FX Desk
KDB+/R Integration
An Analytics Toolkit Tour
Creating R Packages
Streaming Data and Concurrency in R
Ad

Recently uploaded (20)

PDF
Deliverable file - Regulatory guideline analysis.pdf
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PPTX
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PPT
Chapter four Project-Preparation material
DOCX
Business Management - unit 1 and 2
PPTX
New Microsoft PowerPoint Presentation - Copy.pptx
PPTX
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PDF
How to Get Funding for Your Trucking Business
PPTX
3. HISTORICAL PERSPECTIVE UNIIT 3^..pptx
PDF
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
PPTX
Probability Distribution, binomial distribution, poisson distribution
PDF
Digital Marketing & E-commerce Certificate Glossary.pdf.................
PDF
Chapter 5_Foreign Exchange Market in .pdf
Deliverable file - Regulatory guideline analysis.pdf
unit 1 COST ACCOUNTING AND COST SHEET
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Chapter four Project-Preparation material
Business Management - unit 1 and 2
New Microsoft PowerPoint Presentation - Copy.pptx
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
2025 Product Deck V1.0.pptxCATALOGTCLCIA
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Roadmap Map-digital Banking feature MB,IB,AB
Ôn tập tiếng anh trong kinh doanh nâng cao
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
How to Get Funding for Your Trucking Business
3. HISTORICAL PERSPECTIVE UNIIT 3^..pptx
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
Probability Distribution, binomial distribution, poisson distribution
Digital Marketing & E-commerce Certificate Glossary.pdf.................
Chapter 5_Foreign Exchange Market in .pdf

Real-TIme Market Data in R

  • 1. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Building Market Data Interfaces For R Rory Winston rory@theresearchkitchen.com
  • 2. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Contents 1 Introduction 1 Extension Architecture 1 Real-Time Interface 1 Historical Interface 1 Implementation Notes 1 Future Work
  • 3. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work About Me Independent Software Consultant M.Sc. Applied Computing, 2000 M.Sc. Finance, 2008 Working in the financial sector, building trading systems in Java/C++ Interested in practical applications of functional languages and machine learning Relatively recent convert to R
  • 4. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work The Reuters Marketfeed System Ubiquitous (especially in Foreign Exchange) APIs, Excel plugins, graphical workbenches, and trading GUIs Used by traders, structurers, quants, analysts.... A market data item is identified by a RIC (Reuters Information Code) Format: CODE=[specifiers] Examples (FX): EUR=, EUR=EBS, GBPJPY=D2 Examples (Equities): MSFT.O, IBM.N Examples (Fixed Income): EUR3M=, US30YT=RR Each market data item contains a number of data fields, e.g. BID, ASK
  • 5. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Reuters Desktop Example Reuters 3000 session:
  • 6. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Reuters Interfaces
  • 7. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Extension Architecture
  • 8. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Potential Uses: Testing real-time trading algorithms Market data collection and validation Dynamic portfolio optimization algorithms Trading cost / spread analysis Real-time interest rate curve building Econometric analysis Applying R’s vast library of computational statistics and machine learning routines to tick data Turn R into a "trading workbench"
  • 9. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Example - Real Time Subscription Real-time interface: rsub <- function(duration, items, callback) The call rsub will subscribe to the specified rate(s) for the duration of time specified by duration (ms). When a tick arrives, the callback function callback is invoked, with a data frame containing the fields specified in items. Multiple market data items may be subscribed to, and any combination of fields may be be specified. Uses the underlying RFA API, which provides a C++ interface to real-time market updates.
  • 10. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Real-Time Example # Specify field names to retrieve fields <- c("BID","ASK","TIMCOR") # Subscribe to EUR/USD and GBP/USD ticks items <- list() items[[1]] <- c("IDN_SELECTFEED", "EUR=", fields) items[[2]] <- c("IDN_SELECTFEED", "GBP=", fields) # Simple Callback Function f <- function(df) { print(paste("Received",df)) } # Subscribe for 1 hour ONE_HOUR <- 1000*(60)^2 rsub(ONE_HOUR, items, callback)
  • 11. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Real-Time C++ Interface I used the .Call interface, as it enabled me to easily deal with the data frame arguments and callback functions. DL_EXPORT SEXP subscribe(SEXP t, SEXP spec, SEXP callback) { ... } Boost provides nice type-based visitor functionality, which I use to convert between Reuters wire-level data type and native R SEXP-based types.
  • 12. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Example - Historical Fetch Historical interface: fetch <- function(code, n=0, t="d", sd=NULL, ed=NULL) Returns a data frame with data specified by code. The number of items returned can be specified by n, or it is defined as the number of items of the specified periodicity t (e.g. "d" = "daily") between start date sd and end date ed. Uses the Reuters SFC API to obtain historical data.
  • 13. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Historical Interface Examples > fetch("IBM.N", n=20) Date CLS OPN HI LO VOL VWAP 1 2008-07-31 127.98 127.77 129.50 127.76 2334800 128.4172 2 2008-07-30 128.86 128.42 129.00 127.10 1936400 128.2872 ... > fetch("EUR=", sd="1/1/2007", ed="1/1/2008") Date BID OPN HI LO ASK 1 2008-01-01 1.4587 1.4585 1.4608 1.4576 1.4591 2 2007-12-31 1.4589 1.4719 1.4747 1.4568 1.4592 ... > fetch("EUR3M=", n=20, t="w") Date BID OPN HI LO ASK 1 2008-07-25 -75.60 -77.60 -74.96 -79.30 -74.41 2 2008-07-18 -76.94 -76.30 -75.44 -81.10 -76.04 ...
  • 14. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Historical Interface Example msft.vol <- fetch("MSFT.O",sd="1/1/2008",ed="7/1/2008") layout(matrix(c(1,1,1,1,2,2), 3, 2, T)) plot(msft.vol$Date, msft.vol$CLS, main="MSFT Close Price", type="l", ylab="", xlab="Date") plot(msft.vol$Date, msft.vol$VOL, main="Volume", type='h', ylab="", xlab="")
  • 15. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Historical Interface Example MSFT Close Price 34 32 30 28 Jan Mar May Jul Date Volume 2.0e+08 5.0e+07 Jan Mar May Jul
  • 16. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Historical Fetch C++ Interface Again using the .Call interface: DL_EXPORT SEXP fetch(SEXP item, SEXP nItems, SEXP intervalCode, SEXP startDate, SEXP endDate) { ... Code handles missing dates (e.g. market holidays), and coercion of underlying datatypes to appropriate R datatypes
  • 17. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Notes on Implementation Reuters Issues: Reuters APIs are generally awful (newer APIs, i.e. RFA are slightly better) Confusing / inconsistent functionality; Peculiarities with C++ runtime R-related issues: Generally very few, once you get used to the idiosyncratic "Lisp-via-C-macros" style; Some issues with asynchronous real-time updates and the single-threaded R interpreter The combination of Boost, C++ and R is extremely powerful Combining this with market data functionality adds a new dimension of capabilities FX, options, futures, forwards, interest rates, bonds, indices, equities...even news updates
  • 18. Introduction Extension Architecture Real-Time Interface Historical Interface Implementation Notes Future Work Future Work There are still some issues to work on, mainly some configuration and threading issues in the real-time extension Mathworks bundle a number of financial data feed providers in their DataFeed toolbox for Matlab: http://www.mathworks.com/products/datafeed/ Maybe a similar extension (bundling say, connections to Reuters, Bloomberg, Lim, Thomson, etc) for R may be a useful addition? A financial "showcase application" for R would be great: maybe a financial analysts’ toolkit, similar to the Holt system (combining elements of statistical learning and quantitative accounting analysis).