【问题标题】:Interactive Brokers TWS, Python API盈透证券交易平台、Python API
【发布时间】:2021-04-15 21:21:50
【问题描述】:

我有一个问题。在我的程序中间查看下面大写的问题。 不知何故,这两个列表是空的。 TestApp 类中的函数位置似乎没有被调用。 任何帮助表示赞赏。我被困在这里,没有一些帮助找不到解决方案。

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.position_symbols=[]
        self.position_shares=[]

    def error(self, reqId, errorCode, errorString):
        print('Error: ', reqId, " ", errorCode, " ", errorString)
        
    def position(self, account, contract, position, avgCost):
        super().position(account, contract, position, avgCost)
        print("Position", contract.symbol, position, avgCost)
        self.position_symbols.append(contract.symbol)
        self.position_shares.append(str(position)) 
      
    def nextValidId(self, orderId):
        self.nextOrderId = orderId
        self.start()

    def start(self):
        self.reqPositions()
         
    def stop(self):
        self.done = True
        self.cancelScannerSubscription(1)
        self.disconnect()

class MyThread(Thread, EWrapper, EClient): 
    ib=None
    def __init__(self):
        self.ib=TestApp()
        Thread.__init__(self)
      
        self.start()
      
        self.ib.connect('127.0.0.1', 7497, 0)
        app = TestApp()
        app.nextOrderId = 0
        
    def run(self):
        
        for i in range (332, 345, 2):

            time.sleep(4)
            loopindex = i % numsymbol

            # THOSE TWO LINES OF CODES ARE NOT WORKING. 
            # PROBABLY DUE TO THREADING ISSUES. 
            # THE TWO LISTS ARE EMPTY

            print(self.ib.position_symbols)
            print(self.ib.position_shares)

my = None
while True:
    user_input = input("What do I do?")
    if user_input == "start thread":
        if my == None: 
            my = MyThread()

    elif user_input.upper() in ["REMOVE", "ADD"]:
        do something....
        

【问题讨论】:

    标签: python api tws


    【解决方案1】:

    您应该为 EClient.run() 方法使用新线程。然后在主线程中执行其他所有操作。

    现在可以了:

    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper
    from threading import Thread
    import time
    
    
    class TestApp(EWrapper, EClient):
        def __init__(self):
            EClient.__init__(self, self)
            self.position_symbols = []
            self.position_shares = []
    
        def error(self, reqId, errorCode, errorString):
            print('Error: ', reqId, " ", errorCode, " ", errorString)
    
        def position(self, account, contract, position, avgCost):
            super().position(account, contract, position, avgCost)
            print("Position", contract.symbol, position, avgCost)
            self.position_symbols.append(contract.symbol)
            self.position_shares.append(str(position))
    
        def nextValidId(self, orderId):
            self.nextOrderId = orderId
            self.start()
    
        def start(self):
            self.reqPositions()
    
        def stop(self):
            self.done = True
            self.cancelScannerSubscription(1)
            self.disconnect()
    
    
    # class MyThread(Thread, EWrapper, EClient):
    #     ib = None
    #
    #     def __init__(self):
    #         self.ib = TestApp()
    #         Thread.__init__(self)
    #
    #         self.start()
    #
    #         self.ib.connect('127.0.0.1', 7497, 0)
    #         app = TestApp()
    #         app.nextOrderId = 0
    #
    #     def run(self):
    #         for i in range(332, 345, 2):
    #             time.sleep(4)
    #             # loopindex = i % numsymbol
    #
    #             # THOSE TWO LINES OF CODES ARE NOT WORKING.
    #             # PROBABLY DUE TO THREADING ISSUES.
    #             # THE TWO LISTS ARE EMPTY
    #
    #             print(self.ib.position_symbols)
    #             print(self.ib.position_shares)
    
    
    app = TestApp()
    app.connect('127.0.0.1', 7497, 0)
    
    my = None
    while True:
        app.nextOrderId = 0
        user_input = input("What do I do?")
        if user_input == "start thread":
            if my == None:
                my = Thread(target=app.run)
                my.start()
    
        elif user_input.upper() in ["REMOVE", "ADD"]:
            # do
            # something....
            pass
    
        for i in range(332, 345, 2):
            time.sleep(4)
            # loopindex = i % numsymbol
    
            # NOW WORKING
    
            print(app.position_symbols)
            print(app.position_shares)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多