簡體   English   中英

Python:E1136:值“self.exchange.get_portfolio”是不可訂閱的

[英]Python: E1136:Value 'self.exchange.get_portfolio' is unsubscriptable

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

您在通話中有一個小錯誤:

self.exchange.get_portfolio是一個函數,因此您首先必須調用它, 然后才能引用返回的dict中的條目。

哦,我剛剛看到,您還必須在前面插入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']

您應該按照以下步驟進行操作:

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

或單行:

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

如何調用函數? 嘗試了各種各樣的排列,但沒有到達任何地方

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM