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

223 views Asked by At
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']()

Does anybody know what I am doing wrong when I am calling the get_portfolio function because I keep getting this error message:

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

There are 2 answers

5
user8408080 On

You have a little mistake in the call:

self.exchange.get_portfolio is a function, so you first have to call it and then you can reference the entries from the returned dict.

Oh I just saw, that you also have to insert your symbol before:

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

If you don't know the symbols, you can use the keys function which lists all the keys of your dict:

port = self.exchange.get_portfolio()
port_keys = port.keys()
qty = port[port_keys[<SOME KEY NUMBER>]]['currentQty']
0
Geeocode On

You should do it as follows:

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

or in a single row:

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