2016-01-10 79 views
0

任何人都可以建議如何正確使用R IBrokers reqOpenOrders?R IBrokers reqOpenOrders掛起

> 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 

我想獲得我的未完成訂單清單。上面的命令掛起並且必須停止才能返回到R提示符。謝謝。

回答

0

我想我明白了。該功能需要在IBrokers軟件包的幾個文件中實施。在processMsg.R,我們需要將以下內容添加到部分if(curMsg == .twsIncomingMSG$OPEN_ORDER)

if(curMsg == .twsIncomingMSG$OPEN_ORDER) { 
    msg <- readBin(con, "character", 84) 
    x = eWrapper$openOrder(curMsg, msg, timestamp, file, ...) 
    return(x) 

下,實現該功能openOrdereWrapper.R如下:

openOrder <- function(curMsg, msg, timestamp, file, ...) { 
    x = e_open_order(msg) 
    return(x) 
} 

然後在eventHandlers.R,改變e_open_order如下:

`e_open_order` <- function(msg) { 
    contents = msg 
... 
    return(eoo) 
} 

該事件處理程序很好地引導TWS ret中的數據將消息放入適當的結構中,twsContract,twsOrder和twsOrderState。然後,我創建了以下功能:

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) 
} 

結果是列表變量msg。列表中的每個元素依次是包含打開的訂單數據到結構twsContract,twsOrder和twsOrderState的列表。從那裏可以簡單地獲取,顯示和使用任何想要的數據。對於IBrokers中的其他所有功能來說,看起來也是如此,只是其中一些已經實現了。

+0

你提到的文件processMessage.R,ewrapper.R。我在哪裏可以找到這些文件? – aajkaltak