0
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']()

有人知道我在调用 get_portfolio 函数时做错了什么,因为我不断收到以下错误消息:

E1136:Value 'self.exchange.get_portfolio' is unsubscriptable
4

2 回答 2

1

你在通话中有一个小错误:

self.exchange.get_portfolio是一个函数,所以你首先必须调用它,然后你可以从返回的字典中引用条目。

哦,我刚刚看到,您还必须在symbol之前插入:

qty = self.exchange.get_portfolio()[<YOUR_SYMBOL>]['currentQty']

如果你不知道符号,你可以使用keys列出你的字典所有键的函数:

port = self.exchange.get_portfolio()
port_keys = port.keys()
qty = port[port_keys[<SOME KEY NUMBER>]]['currentQty']
于 2018-11-10T16:11:41.783 回答
0

你应该这样做:

qty = self.exchange.get_portfolio()
qty = qty[qty.keys()[0]]['currentQty']

或单行:

qty = self.exchange.get_portfolio()[self.exchange.get_portfolio().keys()[0]]['currentQty']
于 2018-11-10T16:25:53.883 回答