问题
Would anyone be able to advise how to use R IBrokers reqOpenOrders properly?
> tws=twsConnect(clientId=66,host='localhost',port='7497')
> reqOpenOrders(twsconn=tws)
TWS Message: 2 -1 2104 Market data farm connection is OK:usopt
TWS Message: 2 -1 2104 Market data farm connection is OK:usfarm
TWS Message: 2 -1 2106 HMDS data farm connection is OK:ushmds
TWS OrderStatus: orderId=565 status=PreSubmitted filled=0 remaining=1 averageFillPrice=0
TWS OrderStatus: orderId=566 status=PreSubmitted filled=0 remaining=1 averageFillPrice=0
I wanted to get a list of my outstanding orders. The command above hangs and it must be stopped in order to return to the R prompt. Thanks.
回答1:
I think I got it. The functionality needs to be implemented into a few files of the IBrokers package. In processMsg.R
we need to add the following to the part if(curMsg == .twsIncomingMSG$OPEN_ORDER)
:
if(curMsg == .twsIncomingMSG$OPEN_ORDER) {
msg <- readBin(con, "character", 84)
x = eWrapper$openOrder(curMsg, msg, timestamp, file, ...)
return(x)
Next, implement the function openOrder
in eWrapper.R
as follows:
openOrder <- function(curMsg, msg, timestamp, file, ...) {
x = e_open_order(msg)
return(x)
}
Then in eventHandlers.R
, alter the e_open_order
as follows:
`e_open_order` <- function(msg) {
contents = msg
...
return(eoo)
}
This event handler nicely bootstraps the data from the TWS returning message into the appropriate structures, twsContract, twsOrder and twsOrderState. Then, I created the following function:
gs_GetOpenOrders = function(twscon){
# Check if connected to TWS
if(!is.twsConnection(twscon))
stop('requires twsConnection object')
else
con = twscon[[1]]
# Send message requesting open orders
ver = "1"
writeBin(c(.twsOutgoingMSG$REQ_OPEN_ORDERS,ver),con)
# Receive message with content of open orders
ewr = eWrapper()
socketSelect(list(con),FALSE,NULL)
msg = list()
k = 0
while(TRUE) {
curmsg = readBin(con, character(), 1)
if(length(curmsg)==0)
break
if (curmsg==.twsIncomingMSG$OPEN_ORDER){
k = k+1
msg[[k]] = processMsg(curmsg,con,ewr)
}
else
processMsg(curmsg,con,ewr)
}
return(msg)
}
The result is the list variable msg
. Each element of the list in turn is a list containing the open orders data into the structures twsContract, twsOrder and twsOrderState. From there one can simply get, display and use the data in any way desired. It looks like the same is true for pretty much all the other functionality in IBrokers, it's just that some of it is already implemented.
回答2:
I created a function inspired by reqAccountUpdates.R in package IBrokers_0.9, it returns a list of open orders.
.reqOpenOrders <- function(twsconn) {
if( !is.twsConnection(twsconn))
stop('requires twsConnection object')
con <- twsconn[[1]]
VERSION <- "1"
writeBin(c(.twsOutgoingMSG$REQ_OPEN_ORDERS,VERSION), con)
}
reqopenorders_cb <- function(twsconn) {
.reqOpenOrders(twsconn)
open_orders=list()
con <- twsconn[[1]]
eW <- eWrapper()
while(TRUE) {
socketSelect(list(con), FALSE, NULL)
curMsg <- readBin(con, character(), 1L)
if(curMsg == .twsIncomingMSG$OPEN_ORDER){
open_orders[[length(open_orders)+1]]=processMsg(curMsg, con, eW,
timestamp=NULL,file="")
} else {
processMsg(curMsg, con, eW,timestamp=NULL,file="")
}
if(curMsg == .twsIncomingMSG$OPEN_ORDER_END)
break
}
return(open_orders)
}
来源:https://stackoverflow.com/questions/34703679/r-ibrokers-reqopenorders-hangs