我有个问题。在我的程序中间,请看我下面的大写问题。不知何故,这两个列表都是空的。类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....
发布于 2021-04-18 04:18:38
您应该为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)https://stackoverflow.com/questions/67116212
复制相似问题