如何从IBapi for Python获取实时数据流?

如何从IBapi for Python获取实时数据流?,python,interactive-brokers,ib-api,Python,Interactive Brokers,Ib Api,我对IBapi和Python还不熟悉,现在正试图编写一个程序来持续地从IBapi获取实时的市场数据 当我运行代码时,数据滴答声确实出现了一段时间,就像几秒钟一样。然后,它将停止自身,系统退出 我无法找出键盘、系统存在的原因。 有没有专家能帮助指出为什么会突然出现这个问题,以及如何保持它的运行?多谢各位 from ibapi.client import EClient from ibapi.wrapper import EWrapper import threading import sys im

我对IBapi和Python还不熟悉,现在正试图编写一个程序来持续地从IBapi获取实时的市场数据

当我运行代码时,数据滴答声确实出现了一段时间,就像几秒钟一样。然后,它将停止自身,系统退出

我无法找出键盘、系统存在的原因。 有没有专家能帮助指出为什么会突然出现这个问题,以及如何保持它的运行?多谢各位

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
import threading
import sys
import queue

i = 0

class MyWrapper(EWrapper):

def nextValidId(self, orderId:int):
    print("setting nextValidOrderId: %d", orderId)
   # self.nextValidOrderId = orderId
    # start program here or use threading
    #app.reqContractDetails(4444, contract)

def contractDetails(self, reqId, contractDetails):
    print(reqId, contractDetails.contract)# my version doesnt use summary

def contractDetailsEnd(self, reqId):
    print("ContractDetailsEnd. ", reqId)
    # this is the logical end of your program

def error(self, reqId, errorCode, errorString):
    print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)


def tickPrice(self, tickerid , tickType, price, attrib):
    ##overriden method

    ## For simplicity I'm ignoring these but they could be useful to you...
    ## See the documentation http://interactivebrokers.github.io/tws-api/md_receive.html#gsc.tab=0
    # attrib.canAutoExecute
    # attrib.pastLimit
    global i
    i += 1
    print(i, " Tick type:", tickType, " Price:", price)



def tickSize(self, tickerid, tickType, size):
    ## overriden method
    global i
    i += 1
    print(i, " Tick type:", tickType, " Size:", size)


def tickString(self, tickerid, tickType, value):
    ## overriden method

    ## value is a string, make it a float, and then in the parent class will be resolved to int if size
    global i
    i += 1
    print(i, " Tick type:", tickType, " Value:", value)


def tickGeneric(self, tickerid, tickType, value):
    ## overriden method       
    global i
    i += 1
    print(i, " Tick type:", tickType, " value:", value)

wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=1)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))

from ibapi.contract import Contract
contract = Contract()
contract.symbol = "ES"
contract.secType = "FUT"
contract.currency = "USD"
contract.exchange = "GLOBEX"
contract.localSymbol="ESH9"

app.reqMktData(1, contract, "", False, False, [])
print(app.isConnected())
app.run()
示例结果如下所示:

serverVersion:148 connectionTime:b'20190227 17:05:55 CST'
True
setting nextValidOrderId: %d 1
Error. Id:  -1  Code:  2104  Msg:  Market data farm connection is OK:usfarm.nj
Error. Id:  -1  Code:  2104  Msg:  Market data farm connection is OK:hfarm
Error. Id:  -1  Code:  2104  Msg:  Market data farm connection is OK:usfuture
Error. Id:  -1  Code:  2104  Msg:  Market data farm connection is OK:usfarm
Error. Id:  -1  Code:  2106  Msg:  HMDS data farm connection is OK:hkhmds
Error. Id:  -1  Code:  2106  Msg:  HMDS data farm connection is OK:ushmds
1  Tick type: 45  Value: 1551258356
2  Tick type: 4  Price: 2782.5
3  Tick type: 5  Size: 1
4  Tick type: 5  Size: 1
5  Tick type: 8  Size: 75821
6  Tick type: 6  Price: 2793.25
7  Tick type: 7  Price: 2780.5
8  Tick type: 9  Price: 2791.5
9  Tick type: 14  Price: 2788.0
10  Tick type: 1  Price: 2782.25
11  Tick type: 0  Size: 41
12  Tick type: 2  Price: 2782.5
13  Tick type: 3  Size: 46
14  Tick type: 0  Size: 41
15  Tick type: 3  Size: 46
16  Tick type: 0  Size: 42
17  Tick type: 3  Size: 48
18  Tick type: 0  Size: 29
19  Tick type: 3  Size: 64
20  Tick type: 0  Size: 37
21  Tick type: 3  Size: 56
22  Tick type: 0  Size: 39
23  Tick type: 3  Size: 54
24  Tick type: 45  Value: 1551258361
25  Tick type: 5  Size: 44
26  Tick type: 8  Size: 75865
27  Tick type: 1  Price: 2782.5
28  Tick type: 0  Size: 31
29  Tick type: 2  Price: 2782.75
30  Tick type: 3  Size: 91
31  Tick type: 0  Size: 31
32  Tick type: 3  Size: 91
33  Tick type: 0  Size: 12
34  Tick type: 3  Size: 79
35  Tick type: 45  Value: 1551258362
36  Tick type: 5  Size: 1
37  Tick type: 8  Size: 75866
38  Tick type: 45  Value: 1551258363
39  Tick type: 4  Price: 2782.75
40  Tick type: 5  Size: 1
41  Tick type: 8  Size: 75867
42  Tick type: 0  Size: 11
43  Tick type: 3  Size: 78
44  Tick type: 0  Size: 12
45  Tick type: 3  Size: 79
46  Tick type: 45  Value: 1551258365
47  Tick type: 4  Price: 2782.5
48  Tick type: 5  Size: 1
49  Tick type: 8  Size: 75868
50  Tick type: 0  Size: 13
51  Tick type: 45  Value: 1551258366
52  Tick type: 4  Price: 2782.75
53  Tick type: 5  Size: 5
54  Tick type: 5  Size: 5
55  Tick type: 8  Size: 75873
56  Tick type: 0  Size: 30
57  Tick type: 3  Size: 65
58  Tick type: 45  Value: 1551258367
59  Tick type: 4  Price: 2782.5
60  Tick type: 5  Size: 4
61  Tick type: 5  Size: 4
62  Tick type: 8  Size: 75877
63  Tick type: 0  Size: 13
64  Tick type: 3  Size: 63
65  Tick type: 0  Size: 9
66  Tick type: 3  Size: 70
67  Tick type: 3  Size: 66
68  Tick type: 45  Value: 1551258370
69  Tick type: 4  Price: 2782.75
70  Tick type: 5  Size: 1
71  Tick type: 5  Size: 1
72  Tick type: 8  Size: 75878
73  Tick type: 0  Size: 17
74  Tick type: 3  Size: 65
75  Tick type: 4  Price: 2782.5
76  Tick type: 5  Size: 2
77  Tick type: 5  Size: 2
78  Tick type: 8  Size: 75880
79  Tick type: 0  Size: 4
80  Tick type: 3  Size: 68
81  Tick type: 5  Size: 3
82  Tick type: 8  Size: 75883
83  Tick type: 0  Size: 5
84  Tick type: 3  Size: 72
85  Tick type: 45  Value: 1551258371
86  Tick type: 5  Size: 1
87  Tick type: 8  Size: 75884
88  Tick type: 0  Size: 12
89  Tick type: 3  Size: 67
90  Tick type: 1  Price: 2782.25
91  Tick type: 0  Size: 64
92  Tick type: 2  Price: 2782.5
93  Tick type: 3  Size: 2
94  Tick type: 0  Size: 64
95  Tick type: 3  Size: 2
96  Tick type: 45  Value: 1551258373
97  Tick type: 4  Price: 2782.25
98  Tick type: 5  Size: 5
99  Tick type: 5  Size: 5
100  Tick type: 8  Size: 75897
101  Tick type: 0  Size: 52
102  Tick type: 3  Size: 28
103  Tick type: 0  Size: 48
104  Tick type: 3  Size: 45
105  Tick type: 45  Value: 1551258376
106  Tick type: 5  Size: 12
107  Tick type: 8  Size: 75909
108  Tick type: 0  Size: 29
109  Tick type: 3  Size: 41
An exception has occurred, use %tb to see the full traceback.

SystemExit
完全回溯如下:

Traceback (most recent call last):

  File "<ipython-input-9-9e6a0c5dfe6c>", line 1, in <module>
    runfile('C:/readIB_test.py', wdir='C:/Desktop')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/readIB_test.py", line 86, in <module>
    app.run()

  File "C:\ProgramData\Anaconda3\lib\site-packages\ibapi-9.73.7-py3.7.egg\ibapi\client.py", line 228, in run
    self.keyboardInterruptHard()

  File "C:\ProgramData\Anaconda3\lib\site-packages\ibapi-9.73.7-py3.7.egg\ibapi\client.py", line 215, in keyboardInterruptHard
    raise SystemExit()

SystemExit
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('C:/readIB\u test.py',wdir='C:/Desktop')
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第668行,在runfile中
execfile(文件名、命名空间)
文件“C:\ProgramData\Anaconda3\lib\site packages\spyder\u kernels\customize\spydercustomize.py”,第108行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/readIB_test.py”,第86行,在
app.run()
文件“C:\ProgramData\Anaconda3\lib\site packages\ibapi-9.73.7-py3.7.egg\ibapi\client.py”,第228行,正在运行
self.keyboardInterruptHard()
keyboardInterruptHard中的文件“C:\ProgramData\Anaconda3\lib\site packages\ibapi-9.73.7-py3.7.egg\ibapi\client.py”,第215行
提升系统退出()
系统出口

最后我发现问题是由IBapi本身引起的。 client.py中的run()的内部计时器设置为20。 不知道为什么他们会像这样发布API来引用文档,循环应该无限运行。 我只是注释掉超时行,然后我的代码就可以不停地工作了

Org client.py:

def run(self):
        """This is the function that has the message loop."""
        timeStart = time.time()
        timeOut = 20

        try:
            while not self.done and (self.isConnected()
                        or not self.msg_queue.empty()):
                if time.time() - timeStart > timeOut: # stop application after timeout
                    self.keyboardInterrupt()
                    self.keyboardInterruptHard()
我已注释掉超时行:

    def run(self):
        """This is the function that has the message loop."""
        #timeStart = time.time()
        #timeOut = 20

        try:
            while not self.done and (self.isConnected()
                        or not self.msg_queue.empty()):
                #if time.time() - timeStart > timeOut: # stop application after timeout
                    #self.keyboardInterrupt()
                    #self.keyboardInterruptHard()

相关@stovfl感谢回复,经过审核。看起来我的案子有点不同。我的程序与TW在同一台服务器上运行,即使我重新启动了TW,问题依然存在。另外,我没有被卡住,但是得到了一个导致程序退出的异常。在上面添加了完整的回溯。它只指向带有keyboardInterruptHard raise SystemExit()的client.py。我不知道是什么原因造成的,这也没用
self.keyboardInterruptHard()
看起来像是按下一个键来结束程序。是的,这没有帮助,我无法进一步调试导致问题的原因。我确信我没有按任何键结束程序。我很惊讶这个错误曾经存在。以防万一有人读到这篇文章,它已经在2018年12月的github回购协议中修复。最新版本9.75没有这个bug。我在我的9.73版本中也找不到它。我不知道为什么,但我之前显示的代码来自我从他们的网站下载的IBapi windows 9.75版本。我刚刚尝试下载并再次检查它现在布赖恩是正确的,我找不到这样的超时=20代码了。我已经检查了上次修改的文件是在2019年3月8日,也许他们已经修复了它。更糟糕的是,在9.75的发行说明中也没有提到它。