52691

IBrokers twsFOP call in R

I'm trying to get the following to pull some data from IB (Nasdaq 100 e-mini futures options data). I am using the snapShot callback (included below). Could someone tell me what is wrong with my code?

require(IBrokers) tws <- twsConnect() test3<- twsFOP("NQ","GLOBEX",expiry="20141121",strike="4000",right="C") test4 <- reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot)

Thanks a bunch. I've searched high and low online and found little documentation on twsFOP, besides the CRAN documentation which points to twsFuture. Snapshot call included below:

snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...) { if (missing(eWrapper)) eWrapper <- eWrapper() names(eWrapper$.Data$data) <- eWrapper$.Data$symbols con <- twsCon[[1]] if (inherits(twsCon, "twsPlayback")) { sys.time <- NULL while (TRUE) { if (!is.null(timestamp)) { last.time <- sys.time sys.time <- as.POSIXct(strptime(paste(readBin(con, character(), 2), collapse = " "), timestamp)) if (!is.null(last.time)) { Sys.sleep((sys.time - last.time) * playback) } curMsg <- .Internal(readBin(con, "character", 1L, NA_integer_, TRUE, FALSE)) if (length(curMsg) < 1) next processMsg(curMsg, con, eWrapper, format(sys.time, timestamp), file, ...) } else { curMsg <- readBin(con, character(), 1) if (length(curMsg) < 1) next processMsg(curMsg, con, eWrapper, timestamp, file, ...) if (curMsg == .twsIncomingMSG$REAL_TIME_BARS) Sys.sleep(5 * playback) } } } else { while (TRUE) { socketSelect(list(con), FALSE, NULL) curMsg <- .Internal(readBin(con, "character", 1L, NA_integer_, TRUE, FALSE)) if (!is.null(timestamp)) { processMsg(curMsg, con, eWrapper, format(Sys.time(), timestamp), file, ...) } else { processMsg(curMsg, con, eWrapper, timestamp, file, ...) } if (!any(sapply(eWrapper$.Data$data, is.na))) return(do.call(rbind, lapply(eWrapper$.Data$data, as.data.frame))) } } }

Answer1:

The function is not going to return until there is a price update.

If I change the instrument to a future, it works just fine.

test3<- twsFUT("NQ","GLOBEX",expiry="20141219") test4 <- reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot) test4 # BidSize BidPrice AskPrice AskSize Last LastSize Volume #NQZ4 14 3984 3984.25 1 3984.25 11 1702

Your FOP instrument appears to be valid because you can call reqContractDetails(tws, test3) and you get back all the contract details.

Finally, using the market data call with the FOP contract looks like it is correct as well. I'm able to connect to the market data farm using your code...

test3<- twsFOP("NQ","GLOBEX",expiry="20141121",strike="4000",right="C") reqMktData(tws, test3, eventWrapper=eWrapper.data(length(1)),CALLBACK=snapShot) #2 -1 2104 Market data farm connection is OK:usfuture #2 -1 2106 HMDS data farm connection is OK:ushmds.us #2 -1 2107 HMDS data farm connection is inactive but should be available upon demand.cashhmds #2 -1 2106 HMDS data farm connection is OK:ushmds

Now we just need to wait until there is a price update.

<hr>

If you want the last price without waiting for an update, you can pull it using reqHistoricalData, with the current time as the endDateTime. If you want data for the Bid, Ask, and Trades, then you have to make 3 separate requests. Here's how to get the last trade from the historical data service

dat <- reqHistoricalData(tws, test3, endDateTime=paste(format(Sys.time(), "%Y%m%d %H:%M:%S")), barSize="1 min", duration="5 D", useRTH=0, whatToShow="TRADES") #waiting for TWS reply on NQ .... done. last(dat) # NQX4 C4000.Open NQX4 C4000.High NQX4 C4000.Low NQX4 C4000.Close NQX4 C4000.Volume NQX4 C4000.WAP 2014-10-01 16:14:00 101.75 101.75 101.75 101.75 0 101.75 NQX4 C4000.hasGaps NQX4 C4000.Count 2014-10-01 16:14:00 0 0

You'd need to use whatToShow="BID" and whatToShow="ASK" to get Bid and Ask data.

Recommend

  • EntityFramework enable-migrations ArgumentException
  • Custom Animation Object for Transition in Android?
  • Sitecore using Glass mapper and MVC
  • UITableView Multiselect in MonoTouch
  • Greek letters in a GUI - PYTHON
  • Objective-C : getting error on console while trying to display app on ipad device?
  • Imports in __init__.py and `import as` statement
  • Escaping single quotes in JDBC with MySql
  • Sympy: working with equalities manually
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • Bad interaction between Zope2 XML-RPC and AT Image mutator?
  • Getting short path in python
  • Getting last autonumber in access
  • How to handle AllServersUnavailable Exception
  • Websockets service method fails during R startup
  • XCode can't find symbols for a specific iOS library/framework project
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • embed rChart in Markdown
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Running Map reduces the dimensions of the matrices
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?