我正在尝试打印持有的股票(或其他金融资产)的数量。我在 nextValidId 下定义了一个股票合约,并定义了如下位置:
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.nextorderId = orderId
print('The next valid order id is: ', self.nextorderId)
self.stock_contract = Contract()
self.stock_contract.symbol = "APPL"
self.stock_contract.secType = "STK"
self.stock_contract.exchange = "SMART"
self.stock_contract.currency = "USD"
app.reqMktData(508, self.stock_contract, "", False, False, [])
app.reqPositions()
def position(self, account, contract, position, avgCost):
super().position(account, contract, position, avgCost)
print(contract, position)
if contract == 'stock_contract':
self.quantity_held = position
if contract == app.stock_contract:
self.quantity_held = position
然后我请求位置并尝试按如下方式打印结果:
app.reqPositions()
print("Apple held: " + str(app.quantity_held))
app.reqPositions() 打印合同和头寸,但我想将持有的数量作为字符串语句的一部分。我收到“Apple 持有:0”,但这是错误的。