req_mkt_data | R Documentation |
Fetch live (or 15-minute delayed) market data.
req_mkt_data(
contract,
data_name,
channel = "async",
mktDataType = "REALTIME",
genericTickList = NULL,
snapshot = FALSE,
regulatorySnapshot = FALSE
)
contract |
Named character vector of contract parameter(s). The name of each element identifies the parameter – for example, |
data_name |
Character vector of length 1.
If not supplied, |
channel |
One of the following:
|
mktDataType |
Either:
|
genericTickList |
Not yet implemented, stay tuned |
snapshot |
If set to TRUE, the subscription created by |
regulatorySnapshot |
Not yet implemented, stay tuned |
The attributes canAutoExecute, pastLimit, and preOpen are unnecessary in InteractiveTradeR because you can easily create your own flags that accept market data as input. The aren't that much use anyway, and are mostly holdovers from earlier versions & systems.
req_mkt_data()
can't be called in Sync Mode. Instead, this function
must be implimented as follows:
Create a connection to Interactive Brokers with create_new_connections() if a connection doesn't already exist
Call req_mkt_data()
to start a market data subscription
Call read_sock_drawer() to refresh the mkt_data slate. Do this as often as you need to keep mkt_data up-to-date.
Call cancel_mkt_data() when you're finished
If snapshot = TRUE
, then the subscription will destroy itself once a
full market data snapshot has been obtained by read_sock_drawer(), so
there's no need to call cancel_mkt_data().
Data fetched by req_mkt_data()
is stored in the mkt_data slate
under the name specified in the data_name argument (or the default
data name "X_symbol" if no data_name not supplied).
Other market data:
cancel_mkt_data()
,
req_market_data_type()
# --> This example may be run on a paper trading account with no market data
# feed. This will be the case if you start up a new IB account without
# purchasing a subscription. In that case, you'll see a "Market Data not
# subscribed..." message, which means everything is operating normally. If you
# purchase a data subscription, this message will not appear.
# Create a connection to IB
create_new_connections()
#### Snapshot Example: Exxon stock
################################################################################
# Fetch a delayed market data snapshot for Exxon (XOM)
req_mkt_data(
contract = c(
symbol = "XOM",
secType = "STK",
currency = "USD",
exchange = "SMART"
),
snapshot = TRUE,
data_name = "Exxon_delayed",
mktDataType = "DELAYED"
)
# Note that a "mkt_data" subscription now appears:
subscriptions$mkt_data
# For the sake of this example, wait 3 seconds or so to be sure that the data
# has time to arrive.
Sys.sleep(3)
# Read the data off the sock and update "mkt_data"
read_sock_drawer()
# Now you have a market data snapshot in your mkt_data slate! You can access it
# with the "$" operator, like this:
mkt_data$Exxon_delayed$TICK_PRICE
mkt_data$Exxon_delayed$TICK_SIZE
mkt_data$Exxon_delayed$`Delayed Last Timestamp`
mkt_data$Exxon_delayed$TICK_REQ_PARAMS
# ...etc
# Note that the mkt_data subscription has been destroyed. This is because you
# set snapshot to TRUE -- when read_sock_drawer() finds a completed snapshot,
# it automatically updates the subscription.
subscriptions$mkt_data
#### Subscription Example: Five stocks
################################################################################
# Snapshots are great if you just want to quickly fetch market data for a
# contract and move on. Often, however, you'll want to stay up-to-date on market
# data by creating a persistent subscription, causing the data in the mkt_data
# slate to update every time read_sock_drawer() is called.
# To do this, first create a sock for yourself (if you didn't already):
create_new_connections()
# Now, set up market data subscriptions to Tesla, Apple, Facebook, 3M,
# Interactive Brokers and whatever else you want to follow. Below, the walk()
# function from package "purrr" is used to call req_mkt_data on each stock.
c("TSLA", "AAPL", "FB", "MMM", "IBKR") %>%
purrr::walk(
function(stock_symbol){
req_mkt_data(
contract = c(
symbol = stock_symbol,
secType = "STK",
currency = "USD",
exchange = "SMART"
),
mktDataType = "DELAYED",
snapshot = FALSE,
channel = "async"
)
}
)
# Note that the mkt_data subscriptions you just created appear in the
# subscriptions variable:
subscriptions$mkt_data
# Wait a bit for the data to come through for example's sake...
Sys.sleep(3)
# Read the sock drawer as many times as you want
read_sock_drawer()
# Access your data:
mkt_data$`1_TSLA`$TICK_PRICE
mkt_data$`4_MMM`$TICK_PRICE
# ...etc
# Because these subscriptions aren't for snapshots, they'll stay active until
# cancelled. When you're ready, cancel any/all of your subscriptions:
cancel_mkt_data()
# Subscriptions are gone!
subscriptions$mkt_data
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.