(对不起,如果术语/解释不正确)我想打印“我的美元账户余额是:xxx”。目的是了解如何使用类方法参数(不确定这是否是正确的术语)作为代码输入但是,结果是:“我的美元账户余额为:无”。帐户余额打印在单独的行上。
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")))