- self.total = Amount(currency, total_value)
- self.free = Amount(currency, free_value)
- self.used = Amount(currency, used_value)
-
- @classmethod
- def from_hash(cls, currency, hash_):
- return cls(currency, hash_["total"], hash_["free"], hash_["used"])
-
- @classmethod
- def in_currency(cls, other_currency, market, compute_value="average", type="total"):
- amounts = {}
- for currency in cls.known_balances:
- balance = cls.known_balances[currency]
- other_currency_amount = getattr(balance, type)\
- .in_currency(other_currency, market, compute_value=compute_value)
- amounts[currency] = other_currency_amount
- return amounts
-
- @classmethod
- def currencies(cls):
- return cls.known_balances.keys()
-
- @classmethod
- def _fill_balances(cls, hash_):
- for key in hash_:
- if key in ["info", "free", "used", "total"]:
- continue
- if hash_[key]["total"] > 0 or key in cls.known_balances:
- cls.known_balances[key] = cls.from_hash(key, hash_[key])
-
- @classmethod
- def fetch_balances(cls, market):
- cls._fill_balances(market.fetch_balance())
- return cls.known_balances
-
- @classmethod
- def dispatch_assets(cls, amount, repartition=None):
- if repartition is None:
- repartition = Portfolio.repartition_pertenthousand()
- sum_pertenthousand = sum([v for k, v in repartition.items()])
- amounts = {}
- for currency, ptt in repartition.items():
- amounts[currency] = ptt * amount / sum_pertenthousand
- if currency not in cls.known_balances:
- cls.known_balances[currency] = cls(currency, 0, 0, 0)
- return amounts
-
- @classmethod
- def prepare_trades(cls, market, base_currency="BTC", compute_value="average"):
- cls.fetch_balances(market)
- values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
- total_base_value = sum(values_in_base.values())
- new_repartition = cls.dispatch_assets(total_base_value)
- # Recompute it in case we have new currencies
- values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
- Trade.compute_trades(values_in_base, new_repartition, market=market)
-
- @classmethod
- def update_trades(cls, market, base_currency="BTC", compute_value="average", only=None):
- cls.fetch_balances(market)
- values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
- total_base_value = sum(values_in_base.values())
- new_repartition = cls.dispatch_assets(total_base_value)
- Trade.compute_trades(values_in_base, new_repartition, only=only, market=market)
-
- @classmethod
- def prepare_trades_to_sell_all(cls, market, base_currency="BTC", compute_value="average"):
- cls.fetch_balances(market)
- values_in_base = cls.in_currency(base_currency, market, compute_value=compute_value)
- total_base_value = sum(values_in_base.values())
- new_repartition = cls.dispatch_assets(total_base_value, repartition={ base_currency: 1 })
- Trade.compute_trades(values_in_base, new_repartition, market=market)
+ for key in ["total",
+ "exchange_total", "exchange_used", "exchange_free",
+ "margin_total", "margin_borrowed", "margin_free"]:
+ setattr(self, key, Amount(currency, hash_.get(key, 0)))
+
+ self.margin_position_type = hash_.get("margin_position_type")
+
+ if hash_.get("margin_borrowed_base_currency") is not None:
+ base_currency = hash_["margin_borrowed_base_currency"]
+ for key in [
+ "margin_liquidation_price",
+ "margin_pending_gain",
+ "margin_lending_fees",
+ "margin_borrowed_base_price"
+ ]:
+ setattr(self, key, Amount(base_currency, hash_.get(key, 0)))