[英]Understanding threading in Interactive Brokers Python API
我在 Python 中閱讀了一些關於線程的教程,以更好地了解盈透證券 API。 但我仍然不明白為什么下面的代碼不起作用:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import threading
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.last_price_list = []
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def tickPrice(self, reqId, tickType, price, attrib):
self.last_price_list = self.last_price_list + [price]
print("Tick Price. Ticker Id:", reqId, "tickType:", TickTypeEnum.to_str(tickType), "Price:", price, end=' ')
print("_______________________")
app = TestApp()
app.connect("127.0.0.1", 4002, 0)
# allow time to connect to server
time.sleep(1)
contract = Contract()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
app.reqMarketDataType(4) # switch to delayed-frozen data if live is not available
app.reqMktData(1, contract, "", False, False, [])
api_thread = threading.Thread(target=app.run())
api_thread.start()
while True:
print(len(app.last_price_list))
time.sleep(2)
這是我打斷它之前產生的結果:
Error: -1 2104 Market data farm connection is OK:usfarm
Error: -1 2107 HMDS data farm connection is inactive but should be available upon demand.ushmds
Error: -1 2158 Sec-def data farm connection is OK:secdefil
Error: 1 10167 Requested market data is not subscribed. Displaying delayed market data.
Tick Price. Ticker Id: 1 tickType: DELAYED_BID Price: 152.41 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_ASK Price: 152.46 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_LAST Price: 152.44 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_HIGH Price: 155.04 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_LOW Price: 152.28 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_CLOSE Price: 154.09 _______________________
Tick Price. Ticker Id: 1 tickType: DELAYED_OPEN Price: 154.04 _______________________
我不明白為什么這一行不打印任何東西print(len(app.last_price_list))
(target=app.run)
運行后沒有括號,你應該給出被調用方法的名稱,不要實際調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.