IBpy Ewrapper 方法不仅有效 EClientSocket



嗨,我正试图使用IBPy按这里:https://github.com/blampe/IbPy,它的反应非常奇怪。调用EClientSocket类的方法返回数据,但是任何调用EWrapper或EWrapper方法的EClientSocket方法返回None和/或有问题。我意识到IB的API在Java的来源是异步的,但我不知道它在哪里断裂。我在TWS中启用了DDE/Socket连接,甚至指定了一个客户端id(100)。

我正在使用IB TWS演示从这个链接:https://www.interactivebrokers.com/en/index.php?f=553&twsdemo=1和Python 3.4。我的IBpy版本是使用pip install ib-api安装的。

下面是我的代码:
from ib.opt import ibConnection, Connection, message
from ib.ext.Contract import Contract
from ib.ext.EWrapper import EWrapper
from ib.ext.EClientSocket import EClientSocket
from ib.ext.ExecutionFilter import ExecutionFilter
from ib.ext.Order import Order
import time
from time import sleep
def reply_handler(msg):
print("Reply:", msg)
def create_contract(symbol, sec_type, exch, prim_exch, curr):
contract = Contract()
contract.m_symbol = symbol
contract.m_sec_type = sec_type
contract.m_exch = exch
contract.m_prim_exch = prim_exch
contract.m_curr = curr
return contract
if __name__ == "__main__":
tws_conn = ibConnection(host='localhost', port=7496, clientId=100)
tws_conn.connect()
tws_conn.registerAll(reply_handler)
contract = Contract()
contract.m_symbol = 'GE'
contract.m_exchange = 'SMART'
contract.m_localSymbol = 'GE'
contract.m_primaryExch = 'SMART'
contract.m_currency = 'USD'
contract.m_secType = 'STK'
#EClientSocket only methods
reply_handler(tws_conn.isConnected())
reply_handler(tws_conn.serverVersion())
reply_handler(tws_conn.TwsConnectionTime())
#EWrapper methods or calls to EWrapper methods
reply_handler(tws_conn.reqCurrentTime())
reply_handler(tws_conn.reqAccountUpdates(1, ''))
reply_handler(tws_conn.currentTime())
reply_handler(tws_conn.reqMktData(1, contract, '', False))
reply_handler(tws_conn.contractDetails(1, contract))

下面是脚本运行时的控制台输出:

服务器版本:76

连接时间:20150529 23:29:54 PST

回复:真

回复:76

回复:20150529 23:29:54 PST

回答:没有

回答:没有

回复:currentTime time=None

回答:没有

回答:没有

回复:contractDetails reqId=1, contractDetails=ib.ext.Contract。合同对象在0x000000000287FB70

回答:没有

你可以看到,在第三个方法最后一次调用EClientSocket后,它停止工作。我已经研究了IB和IBpy的文档,这个特殊的问题在Stackoverflow上也没有提到。谢谢你的帮助!

reply_handler()中包装调用对您来说有点混乱。它在tws_conn对象中注册以获得所有回复,这就是您所需要做的。任何不像tws_conn.isConnected()这样的套接字请求只需要在print()中,这都是reply_handler does,但它使它看起来像EReader的回复-它不是。

同样在tws_conn.contractDetails...你忘了req

from ib.opt import ibConnection, Connection, message
from ib.ext.Contract import Contract
from ib.ext.EWrapper import EWrapper
from ib.ext.EClientSocket import EClientSocket
def reply_handler(msg):
    print("Reply:", msg)
tws_conn = ibConnection(host='localhost', port=7496, clientId=1)
tws_conn.registerAll(reply_handler)
tws_conn.connect()

contract = Contract()
contract.m_symbol = 'GE'
contract.m_exchange = 'SMART'
contract.m_currency = 'USD'
contract.m_secType = 'STK'
#EClientSocket only methods
reply_handler(tws_conn.isConnected())
reply_handler(tws_conn.serverVersion())
reply_handler(tws_conn.TwsConnectionTime())
#EWrapper methods or calls to EWrapper methods
tws_conn.reqCurrentTime()
tws_conn.reqContractDetails(1, contract)
#tws_conn.disconnect()#don't forget at some point

好了,我算出来了。简直简单得令人难以置信。

显然,如果您调用IBs API并且在每次调用后不暂停,则处理程序不会返回任何内容。这在API文档中找不到。

所以time.sleep(1)作为方法的最后一行或者调用方法后的下一行是很好的

相关内容

  • 没有找到相关文章

最新更新