(如果术语/解释不正确,很抱歉)我想打印“我的账户余额是: xxx”。其目的是了解如何使用类方法参数(不确定这是否是正确的术语)作为代码输入,但结果是:“我的帐户余额为: None”。帐户余额是打印的,但在一个单独的行。
from ibapi.wrapper import EWrapper  # handles incoming messages
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.contract_details = {}
        self.bardata = {}  # initialise directory to store bar data
        self.USD_cash_balance = 0
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        self.nextorderId = orderId
        print('The next valid order id is: ', self.nextorderId)
    def accountSummary(self, reqId: int, account: str, tag: str, value: str,
                       currency: str):
        if tag == "CashBalance":
            print(value)
        IBapi.USD_cash_balance = value
        if reqId == 131:
            return value
def run_loop():
    app.run()  # starts communication with TWS
app = IBapi()
app.nextorderId = None
app.connect('127.0.0.1', 7497, 123)
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)  # Algo doesn't have "daemon=True"
api_thread.start()
# Check if the API is connected via orderid
while True:
    if isinstance(app.nextorderId,
                  int):  # the IB API sends out the next available order ID as soon as connection is made
        # The isinstance() function returns True if the specified object is of the specified type, otherwise False.
        print('connected')
        break
    else:
        print('waiting for connection')
        time.sleep(2)
print("My account balance in USD is: " + str(app.reqAccountSummary(131, "All", "$LEDGER:USD")))发布于 2021-12-05 10:38:32
首先,您没有将USD_cash_balance分配给类,而是分配给类实例(使用self而不是类名)。其次,你必须等待回复,有很多方法可以做到这一点。一种是等待值并对其进行休眠,如果有必要,可以通过实现超时/重试来扩展它。您还可以使用队列。另外,当你完成任务时,别忘了断开你的会话。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper  # handles incoming messages
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
        self.contract_details = {}
        self.bardata = {}  # initialise directory to store bar data
        self.USD_cash_balance = 0
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)
        self.nextorderId = orderId
        print('The next valid order id is: ', self.nextorderId)
    def accountSummary(self, reqId: int, account: str, tag: str, value: str,
                       currency: str):
        if tag == "CashBalance" and reqId == 131:
            self.USD_cash_balance = value
def run_loop():
    app.run()  # starts communication with TWS
app = IBapi()
app.nextorderId = None
app.connect('127.0.0.1', 7600, 123)
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)  # Algo doesn't have "daemon=True"
api_thread.start()
# Check if the API is connected via orderid
while True:
    if isinstance(app.nextorderId,
                  int):  # the IB API sends out the next available order ID as soon as connection is made
        # The isinstance() function returns True if the specified object is of the specified type, otherwise False.
        print('connected')
        break
    else:
        print('waiting for connection')
        time.sleep(2)
while not getattr(app, 'USD_cash_balance', None):
    app.reqAccountSummary(131, "All", "$LEDGER:USD")
    time.sleep(0.5)
print("My account balance in USD is: " + app.USD_cash_balance)
app.disconnect()https://stackoverflow.com/questions/70233017
复制相似问题