[R-SIG-Finance] how to get quotes with IBrokers
zerdna
azege at yahoo.com
Fri Aug 20 01:27:43 CEST 2010
Appreciate everyones help and comment. Jeff -- you are my savior:). Indeed,
the code you posted works and fairly fast. Here is the code where tickers
are in nms, there are 495 of them , i am breaking them in chunks of 20
chunkLen=20
bInd<-seq(1,length(nms),by=chunkLen)
eInd<-bInd+chunkLen-1
quote<-list()
tws<-twsConnect()
for(i in 1:length(bInd)){
nm<-nms[bInd[i]:eInd[i]]
tm<-system.time(quote[[i]]<-reqMktData(tws, lapply(nm,twsSTK),
eventWrapper=eWrapper.data(length(nm)),CALLBACK=snapShot))
print(paste("time for chunk ",i,"equal ", tm[3],sep=""))
}
===========================================================
When there is no problems with tickers code runs sort of fine. If there are
bad tickers for which no data is returned or stocks with small volume the
code hangs waiting for NAs in data to be filled. I hacked it a little using
my knowledge of how long a query should take for a given chunk of stocks, so
if it hangs more than 10 times that, i return results with NA. So if there
are problems with data, code hangs for a while, then chugs along. However,
here is an output on good set of stocks.
===========================================
2 -1 2104 Market data farm connection is OK:cashfarm
2 -1 2104 Market data farm connection is OK:usfarm
[1] "time for chunk 1equal 1.296"
[1] "time for chunk 2equal 0.904999999999998"
[1] "time for chunk 3equal 1.145"
[1] "time for chunk 4equal 1.315"
[1] "time for chunk 5equal 1.156"
[1] "time for chunk 6equal 1.183"
[1] "time for chunk 7equal 1.585"
[1] "time for chunk 8equal 1.304"
[1] "time for chunk 9equal 1.205"
[1] "time for chunk 10equal 1.46"
[1] "time for chunk 11equal 1.132"
[1] "time for chunk 12equal 1.209"
[1] "time for chunk 13equal 1.22600000000001"
[1] "time for chunk 14equal 0.958000000000006"
[1] "time for chunk 15equal 1.18"
[1] "time for chunk 16equal 1.249"
[1] "time for chunk 17equal 1.518"
[1] "time for chunk 18equal 1.03"
[1] "time for chunk 19equal 1.169"
[1] "time for chunk 20equal 1.384"
[1] "time for chunk 21equal 1.138"
[1] "time for chunk 22equal 1.165"
[1] "time for chunk 23equal 1.189"
[1] "time for chunk 24equal 1.21"
2 19 200 No security definition has been found for the request
2 18 200 No security definition has been found for the request
2 20 200 No security definition has been found for the request
2 16 200 No security definition has been found for the request
2 17 200 No security definition has been found for the request
[1] "time for chunk 25equal 24.986"
===========================
As you could see, it hanged on the last chunk of stocks and if you look at
the last dataframe returned, last 5 stocks are completely NAs. Remember that
last chunk was 15 stocks as opposed to the rest of them that are 20 stocks
chunks. Not sure what's going on exactly here, as i am not fluent with how
memory of closures operates, but seems like once size for .Data in
eWrapper.data is assigned, it doesn't change on next iteration of the loop.
This causes unnecessary complications with NAs left over, maybe Jeff could
comment on that. I could just misunderstand things, or it could be something
that should be cleaned up. For example, if i just request five stocks after
running the loop, which seems legitimate to me
reqMktData(tws, lapply(nms[1:5],twsSTK),
eventWrapper=eWrapper.data(length(nms[1:5])),CALLBACK=snapShot)
i get an error
Error in `*tmp*`[[id]] : subscript out of bounds
Also, in general, is there a trick to step into closures with debugger? I
tried with mtrace, doesn't seem to do it.
--
View this message in context: http://r.789695.n4.nabble.com/how-to-get-quotes-with-IBrokers-tp2329328p2332013.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list