X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=market.py;h=979fbbf68cc4fcb3c69c4cca3f92b50af8e8e9e1;hb=006a20846236ad365ec814f848f5fbf7e3dc7d3c;hp=1e1e083447bd4b319a7edb35b749592be0408cf3;hpb=350ed24de673dc125be9e2fdecb0f1abc7835b41;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/market.py b/market.py index 1e1e083..979fbbf 100644 --- a/market.py +++ b/market.py @@ -26,7 +26,18 @@ def poloniex_fetch_balance(self, params={}): return self.parse_balance(result) ccxt.poloniex.fetch_balance = poloniex_fetch_balance -def poloniex_fetch_margin_balances(self): +def poloniex_fetch_margin_balance(self): + """ + portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) + See DASH/BTC positions + {'amount': '-0.10000000', -> DASH empruntés + 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) + 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur + 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) + 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) + 'total': '0.00681856', -> valeur totale empruntée en BTC + 'type': 'short'} + """ positions = self.privatePostGetMarginPosition({"currencyPair": "all"}) parsed = {} for symbol, position in positions.items(): @@ -41,23 +52,10 @@ def poloniex_fetch_margin_balances(self): "liquidationPrice": decimal.Decimal(position["liquidationPrice"]), "type": position["type"], "total": decimal.Decimal(position["total"]), - "base_currency": base_currency, + "baseCurrency": base_currency, } return parsed -ccxt.poloniex.fetch_margin_balances = poloniex_fetch_margin_balances - -def poloniex_fetch_balance_with_margin(self, params={}): - exchange_balance = self.fetch_balance(params=params) - margin_balances = self.fetch_margin_balances() - - for currency, balance in margin_balances.items(): - assert exchange_balance[currency]["total"] == 0 - assert balance["type"] == "short" - exchange_balance[currency]["total"] = balance["amount"] - exchange_balance[currency]["marginPosition"] = balance - return exchange_balance -ccxt.poloniex.fetch_balance_with_margin = poloniex_fetch_balance_with_margin - +ccxt.poloniex.fetch_margin_balance = poloniex_fetch_margin_balance def poloniex_fetch_balance_per_type(self): balances = self.privatePostReturnAvailableAccountBalances() @@ -72,6 +70,49 @@ def poloniex_fetch_balance_per_type(self): return result ccxt.poloniex.fetch_balance_per_type = poloniex_fetch_balance_per_type +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 + def poloniex_parse_ticker(self, ticker, market=None): timestamp = self.milliseconds() symbol = None @@ -153,48 +194,54 @@ def poloniex_transfer_balance(self, currency, amount, from_account, to_account): return result["success"] == 1 ccxt.poloniex.transfer_balance = poloniex_transfer_balance -# portfolio.market.create_order("DASH/BTC", "limit", "sell", 0.1, price=0.06828800, account="margin") - -# portfolio.market.privatePostReturnTradableBalances() -# Returns tradable balances in margin -# 'BTC_DASH': {'BTC': '0.01266999', 'DASH': '0.08574839'}, -# Je peux emprunter jusqu’à 0.08574839 DASH ou 0.01266999 BTC (une position est -# déjà ouverte) -# 'BTC_CLAM': {'BTC': '0.00585143', 'CLAM': '7.79300395'}, -# Je peux emprunter 7.7 CLAM pour les vendre contre des BTC, ou emprunter -# 0.00585143 BTC pour acheter des CLAM - -# portfolio.market.privatePostReturnMarginAccountSummary() -# Returns current informations for margin -# {'currentMargin': '1.49680968', -> marge (ne doit pas descendre sous 20% / 0.2) -# = netValue / totalBorrowedValue -# 'lendingFees': '0.00000000', -> fees totaux -# 'netValue': '0.01008254', -> balance + plus-value -# 'pl': '0.00008254', -> plus value latente (somme des positions) -# 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée -# 'totalValue': '0.01000000'} -> valeur totale en compte - - -# portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) -# See DASH/BTC positions -# {'amount': '-0.10000000', -> DASH empruntés -# 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) -# 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur -# 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) -# 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) -# 'total': '0.00681856', -> valeur totale empruntée en BTC -# 'type': 'short'} - - -# closeMarginPosition({"currencyPair": "BTC_DASH"}) : fermer la position au prix -# du marché -# Nécessaire à la fin -# portfolio.market.create_order("DASH/BTC", "limit", "buy", 0.1, price=0.06726487, account="margin") - -# portfolio.market.fetch_balance_per_type() -# Ne suffit pas pour calculer les positions: ne contient que les 0.01 envoyés -# TODO: vérifier si fetch_balance marque ces 0.01 comme disponibles -> oui +def poloniex_close_margin_position(self, currency, base_currency): + """ + closeMarginPosition({"currencyPair": "BTC_DASH"}) + fermer la position au prix du marché + """ + symbol = "{}_{}".format(base_currency, currency) + self.privatePostCloseMarginPosition({"currencyPair": symbol}) +ccxt.poloniex.close_margin_position = poloniex_close_margin_position + +def poloniex_tradable_balances(self): + """ + portfolio.market.privatePostReturnTradableBalances() + Returns tradable balances in margin + 'BTC_DASH': {'BTC': '0.01266999', 'DASH': '0.08574839'}, + Je peux emprunter jusqu’à 0.08574839 DASH ou 0.01266999 BTC (une position est déjà ouverte) + 'BTC_CLAM': {'BTC': '0.00585143', 'CLAM': '7.79300395'}, + Je peux emprunter 7.7 CLAM pour les vendre contre des BTC, ou emprunter 0.00585143 BTC pour acheter des CLAM + """ + + tradable_balances = self.privatePostReturnTradableBalances() + for symbol, balances in tradable_balances.items(): + for currency, balance in balances.items(): + balances[currency] = decimal.Decimal(balance) + return tradable_balances +ccxt.poloniex.fetch_tradable_balances = poloniex_tradable_balances + +def poloniex_margin_summary(self): + """ + portfolio.market.privatePostReturnMarginAccountSummary() + Returns current informations for margin + {'currentMargin': '1.49680968', -> marge (ne doit pas descendre sous 20% / 0.2) + = netValue / totalBorrowedValue + 'lendingFees': '0.00000000', -> fees totaux + 'netValue': '0.01008254', -> balance + plus-value + 'pl': '0.00008254', -> plus value latente (somme des positions) + 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée + 'totalValue': '0.01000000'} -> valeur totale en compte + """ + summary = self.privatePostReturnMarginAccountSummary() + return { + "current_margin": decimal.Decimal(summary["currentMargin"]), + "lending_fees": decimal.Decimal(summary["lendingFees"]), + "gains": decimal.Decimal(summary["pl"]), + "total_borrowed": decimal.Decimal(summary["totalBorrowedValue"]), + "total": decimal.Decimal(summary["totalValue"]), + } +ccxt.poloniex.margin_summary = poloniex_margin_summary market = ccxt.poloniex({ "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", "secret": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",