Learn R Programming

IBrokers (version 0.10-2)

reqMktDepth: Request Market Depth Feed from TWS

Description

Allows for streaming market depth (order book) data to be handled in R.

Usage

reqMktDepth(conn, 
            Contract,
            tickerId = "1",
            numRows = "20",
            timeStamp = TRUE,
            playback = 1,
            file = "",
            verbose = TRUE,
            eventWrapper = eWrapper(),
            CALLBACK = twsCALLBACK, ...)

cancelMktDepth(conn,tickerId)

Value

The book depth.

Arguments

conn

a valid twsConnection connection

Contract

twsContract object(s) requested data for

tickerId

the ticker id to associate with the returned data

numRows

depth of book

timeStamp

include R time stamps

playback

playback speed adjustment

file

passed to internal cat calls. See associated help.

verbose

print diagnostics?

eventWrapper

callback closure

CALLBACK

main reciever loop

...

additional args

Author

Jeffrey A. Ryan

Details

This function provides R level access to book data as returned by the TWS API. The Interactive Brokers documentation should be reference for the exact meaning of the returned data.

timeStamps is unique to the R API in that each incoming signal will be marked with a (potentially) unique timestamp. Alternatively it is possible to pass a formatting string for use in format(Sys.time()). To suppress the time stamp set the argument to NULL.

Callbacks, via eventUpdateMktDepth, eventUpdateMktDepthL2, or CALLBACK are designed to allow for R level processing of the real-time data stream.

The first two correspond to actions based upon the actual signal recieved. These may be user-defined functions taking the appropriate arguments. Each message recieved (each update to the market depth) will invoke one of these callbacks. By default when nothing is specified, the code will call the default method for printing the results to the screen via cat.

Note that the use of the argument file will be passed to these cat calls, and therefore it will be possible to use the functionality of cat directly - e.g. piping output or writing to a connection. The simplest use of file would be to specify the name of a file to append the output of the stream to.

The CALLBACK argument is used for more control of the incoming results. This requires user-level error checking as well as TWS API interaction. It is here for advanced use and until documented should be left alone.

References

Interactive Brokers API: https://interactivebrokers.github.io/tws-api/index.html

See Also

twsConnect,twsContract

Examples

Run this code
if (FALSE) {
tws <- twsConnect()
contract <- twsEquity("QQQQ","SMART","ISLAND")
reqMktDepth(tws, contract)

# write to a file
reqMktDepth(tws, contract, file='out.dat')
}

Run the code above in your browser using DataLab