繁体   English   中英

了解盈透证券中的线程 Python API

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM