+def poloniex_fetch_all_balances(self):
+ exchange_balances = self.fetch_balance()
+ margin_balances = self.fetch_margin_balance()
+ balances_per_type = self.fetch_balance_per_type()
+
+ all_balances = {}
+ in_positions = {}
+
+ for currency, exchange_balance in exchange_balances.items():
+ if currency in ["info", "free", "used", "total"]:
+ continue
+
+ margin_balance = margin_balances.get(currency, {})
+ balance_per_type = balances_per_type.get(currency, {})
+
+ all_balances[currency] = {
+ "total": exchange_balance["total"] + margin_balance.get("amount", 0),
+ "exchange_used": exchange_balance["used"],
+ "exchange_total": exchange_balance["total"] - balance_per_type.get("margin", 0),
+ "exchange_free": exchange_balance["free"] - balance_per_type.get("margin", 0),
+ "margin_free": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0),
+ "margin_borrowed": 0,
+ "margin_total": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0),
+ "margin_lending_fees": margin_balance.get("lendingFees", 0),
+ "margin_pending_gain": margin_balance.get("pl", 0),
+ "margin_position_type": margin_balance.get("type", None),
+ "margin_liquidation_price": margin_balance.get("liquidationPrice", 0),
+ "margin_borrowed_base_price": margin_balance.get("total", 0),
+ "margin_borrowed_base_currency": margin_balance.get("baseCurrency", None),
+ }
+ if len(margin_balance) > 0:
+ if margin_balance["baseCurrency"] not in in_positions:
+ in_positions[margin_balance["baseCurrency"]] = 0
+ in_positions[margin_balance["baseCurrency"]] += margin_balance["total"]
+
+ for currency, in_position in in_positions.items():
+ all_balances[currency]["total"] += in_position
+ all_balances[currency]["margin_total"] += in_position
+ all_balances[currency]["margin_borrowed"] += in_position
+
+ return all_balances
+ccxt.poloniex.fetch_all_balances = poloniex_fetch_all_balances
+