Python:E1136:Value';self.exchange.get#u公文包';不可预订
当我调用get_portfolio函数时,是否有人知道我做错了什么,因为我一直收到以下错误消息:Python:E1136:Value';self.exchange.get#u公文包';不可预订,python,algorithm,bitmex,Python,Algorithm,Bitmex,当我调用get_portfolio函数时,是否有人知道我做错了什么,因为我一直收到以下错误消息: def get_portfolio(self): contracts = settings.CONTRACTS portfolio = {} for symbol in contracts: position = self.bitmex.position(symbol=symbol) instrument
def get_portfolio(self):
contracts = settings.CONTRACTS
portfolio = {}
for symbol in contracts:
position = self.bitmex.position(symbol=symbol)
instrument = self.bitmex.instrument(symbol=symbol)
if instrument['isQuanto']:
future_type = "Quanto"
elif instrument['isInverse']:
future_type = "Inverse"
elif not instrument['isQuanto'] and not instrument['isInverse']:
future_type = "Linear"
else:
raise NotImplementedError("Unknown future type; not quanto or inverse: %s" % instrument['symbol'])
if instrument['underlyingToSettleMultiplier'] is None:
multiplier = float(instrument['multiplier']) / float(instrument['quoteToSettleMultiplier'])
else:
multiplier = float(instrument['multiplier']) / float(instrument['underlyingToSettleMultiplier'])
portfolio[symbol] = {
"currentQty": float(position['currentQty']),
"futureType": future_type,
"multiplier": multiplier,
"markPrice": float(instrument['markPrice']),
"spot": float(instrument['indicativeSettlePrice'])
}
return portfolio
qty = self.exchange.get_portfolio['currentQty']()
您在通话中有一个小错误:
self.exchange.get\u portfolio
是一个函数,因此首先必须调用它,然后才能引用返回的dict中的条目
哦,我刚才看到,您还必须在以下内容之前插入您的符号
:
E1136:Value 'self.exchange.get_portfolio' is unsubscriptable
您应该按照以下步骤进行操作:
port = self.exchange.get_portfolio()
port_keys = port.keys()
qty = port[port_keys[<SOME KEY NUMBER>]]['currentQty']
或在一行中:
qty = self.exchange.get_portfolio()
qty = qty[qty.keys()[0]]['currentQty']
别忘了接受答案,这样别人就会知道答案已经解决了;)@VarunReddy user8408080他可能不知道符号的值,请看下面我的工作答案。您认为我还应该指出,有一个
键
函数?@user8408080实际上我无法测试它,因为您也无法测试它,但由于符号是在函数调用方内部创建的,因此可能不知道它的值。我已更改它以符合您的建议
qty = self.exchange.get_portfolio()
qty = qty[qty.keys()[0]]['currentQty']
qty = self.exchange.get_portfolio()[self.exchange.get_portfolio().keys()[0]]['currentQty']