Advertisement
Guest User

Untitled

a guest
Mar 11th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #difference to the other is timer which waits 10s and
  2.  
  3.  
  4. library(IBrokers)
  5. #tws <- twsConnect()
  6.  
  7. eWrapper.data.Last <- function(n) {
  8. eW <- eWrapper(NULL) # use basic template
  9. eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,2),nc=2),0),
  10. .Dimnames=list(NULL,c("LastSize","Last")))),n))
  11.  
  12. eW$tickPrice <- function(curMsg, msg, timestamp, file, ...)
  13. {
  14. tickType = msg[3]
  15. msg <- as.numeric(msg)
  16. id <- msg[2] #as.numeric(msg[2])
  17. data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
  18. attr(data[[id]],"index") <- as.numeric(Sys.time())
  19. nr.data <- NROW(data[[id]])
  20. if(tickType == .twsTickType$LAST) {
  21. data[[id]][nr.data,2] <- msg[4]
  22. }
  23. eW$assign.Data("data", data)
  24. c(curMsg, msg)
  25. }
  26. eW$tickSize <- function(curMsg, msg, timestamp, file, ...)
  27. {
  28. data <- eW$get.Data("data")
  29. tickType = msg[3]
  30. msg <- as.numeric(msg)
  31. id <- as.numeric(msg[2])
  32. attr(data[[id]],"index") <- as.numeric(Sys.time())
  33. nr.data <- NROW(data[[id]])
  34. if(tickType == .twsTickType$LAST_SIZE) {
  35. data[[id]][nr.data,1] <- msg[4]
  36. }
  37. eW$assign.Data("data", data)
  38. c(curMsg, msg)
  39. }
  40. return(eW)
  41. }
  42.  
  43. snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...)
  44. {
  45. if (missing(eWrapper))
  46. eWrapper <- eWrapper()
  47. names(eWrapper$.Data$data) <- eWrapper$.Data$symbols
  48. con <- twsCon[[1]]
  49. starttime <- Sys.time()
  50. if (inherits(twsCon, "twsPlayback")) {
  51. sys.time <- NULL
  52.  
  53. while (TRUE) {
  54. if (!is.null(timestamp)) {
  55. last.time <- sys.time
  56. sys.time <- as.POSIXct(strptime(paste(readBin(con,
  57. character(), 2), collapse = " "), timestamp))
  58. if (!is.null(last.time)) {
  59. Sys.sleep((sys.time - last.time) * playback)
  60. }
  61. curMsg <- .Internal(readBin(con, "character",
  62. 1L, NA_integer_, TRUE, FALSE))
  63. if (length(curMsg) < 1)
  64. next
  65. processMsg(curMsg, con, eWrapper, format(sys.time,
  66. timestamp), file, ...)
  67. }
  68. else {
  69. curMsg <- readBin(con, character(), 1)
  70. if (length(curMsg) < 1)
  71. next
  72. processMsg(curMsg, con, eWrapper, timestamp,
  73. file, ...)
  74. if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
  75. Sys.sleep(5 * playback)
  76. }
  77. }
  78. }
  79. else {
  80. evalWithTimeout(
  81. while (TRUE) {
  82.  
  83. socketSelect(list(con), FALSE, NULL)
  84. curMsg <- .Internal(readBin(con, "character", 1L,
  85. NA_integer_, TRUE, FALSE))
  86. if (!is.null(timestamp)) {
  87. processMsg(curMsg, con, eWrapper, format(Sys.time(),
  88. timestamp), file, ...)
  89. }
  90. else {
  91. processMsg(curMsg, con, eWrapper, timestamp,
  92. file, ...)
  93. }
  94. if (!any(sapply(eWrapper$.Data$data, is.na)) | Sys.time()-starttime > 10 ) {
  95. cat(Sys.time()-starttime )
  96. return(do.call(rbind, lapply(eWrapper$.Data$data, as.data.frame)))
  97.  
  98. }
  99. }, timeout=20, onTimeout="warning" )
  100. cat(Sys.time()-starttime )
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement