title: Parallel request historical options chain prices / Last known price in IBrokers (R)
body: <p>I am trying to create <strong>Current</strong> Options Chain (options per strike, per expiration) for a ticker. </p>
  
  <pre><code>library(IBrokers)    
  tws &lt;- twsConnect()
  # Lets say only Call prices
  AA &lt;- reqContractDetails(tws, twsOption(local="", right="C", symbol="AAPL"))
  </code></pre>
  
  <p>Native implementation with <strong><code>snapshot</code></strong> is too slow:</p>
  
  <pre><code>reqMktData(tws, AA[1:2], snapshot = TRUE)
  </code></pre>
  
  <p>It waits around <strong><code>11 sec</code></strong> per contract (Current number of Contracts is 626)</p>
  
  <hr>
  
  <p>Another implementation:</p>
  
  <pre><code>snapShot &lt;- function (twsCon, eWrapper, timestamp, file, playback = 1, ...)
  {
    if (missing(eWrapper))
      eWrapper &lt;- eWrapper()
    names(eWrapper$.Data$data) &lt;- eWrapper$.Data$symbols
    con &lt;- twsCon[[1]]
    if (inherits(twsCon, "twsPlayback")) {
      sys.time &lt;- NULL
      while (TRUE) {
        if (!is.null(timestamp)) {
          last.time &lt;- sys.time
          sys.time &lt;- as.POSIXct(strptime(paste(readBin(con,
                                                        character(), 2), collapse = " "), timestamp))
          if (!is.null(last.time)) {
            Sys.sleep((sys.time - last.time) * playback)
          }
          curMsg &lt;- .Internal(readBin(con, "character",
                                      1L, NA_integer_, TRUE, FALSE))
          if (length(curMsg) &lt; 1)
            next
          processMsg(curMsg, con, eWrapper, format(sys.time,
                                                   timestamp), file, ...)
        }
        else {
          curMsg &lt;- readBin(con, character(), 1)
          if (length(curMsg) &lt; 1)
            next
          processMsg(curMsg, con, eWrapper, timestamp,
                     file, ...)
          if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
            Sys.sleep(5 * playback)
        }
      }
    }
    else {
      evalWithTimeout(
      while (TRUE) {
        socketSelect(list(con), FALSE, NULL)
        curMsg &lt;- .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)))
      }, timeout=5, onTimeout="warning")
    }
  } 
  
  reqMktData(tws, AA[1:20], eventWrapper=eWrapper.data(20),CALLBACK=snapShot)
  </code></pre>
  
  <p>It avoids waiting ( 11 secs ).</p>
  
  <p>But this doesn't work if there is no real-time data or markets are closed.</p>
  
  <p>So, I want to get only the last known price even if markets are closed.
  <strong><br>This is my pseudo-solution:</strong></p>
  
  <pre><code>reqHistoricalData(tws, AA[[1]]$contract, whatToShow='BID', barSize = "1 min", duration = "60 S")
  </code></pre>
  
  <p><strong>Is there a way to parallelize this solution so it will call for historical price of several contract?</strong></p>
  
  <p>Currently it spends around <strong><code>2.3 seconds</code></strong> per contract, while previous solution is able to get 20-30 contracts with the same time spent.</p>