diff options
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 92 |
1 files changed, 92 insertions, 0 deletions
@@ -4,6 +4,7 @@ import decimal | |||
4 | def exchange_sum(self, *args): | 4 | def exchange_sum(self, *args): |
5 | return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) | 5 | return sum([arg for arg in args if isinstance(arg, (float, int, decimal.Decimal))]) |
6 | ccxt.Exchange.sum = exchange_sum | 6 | ccxt.Exchange.sum = exchange_sum |
7 | |||
7 | def poloniex_fetch_balance(self, params={}): | 8 | def poloniex_fetch_balance(self, params={}): |
8 | self.load_markets() | 9 | self.load_markets() |
9 | balances = self.privatePostReturnCompleteBalances(self.extend({ | 10 | balances = self.privatePostReturnCompleteBalances(self.extend({ |
@@ -25,6 +26,39 @@ def poloniex_fetch_balance(self, params={}): | |||
25 | return self.parse_balance(result) | 26 | return self.parse_balance(result) |
26 | ccxt.poloniex.fetch_balance = poloniex_fetch_balance | 27 | ccxt.poloniex.fetch_balance = poloniex_fetch_balance |
27 | 28 | ||
29 | def poloniex_fetch_margin_balances(self): | ||
30 | positions = self.privatePostGetMarginPosition({"currencyPair": "all"}) | ||
31 | parsed = {} | ||
32 | for symbol, position in positions.items(): | ||
33 | if position["type"] == "none": | ||
34 | continue | ||
35 | base_currency, currency = symbol.split("_") | ||
36 | parsed[currency] = { | ||
37 | "amount": decimal.Decimal(position["amount"]), | ||
38 | "borrowedPrice": decimal.Decimal(position["basePrice"]), | ||
39 | "lendingFees": decimal.Decimal(position["lendingFees"]), | ||
40 | "pl": decimal.Decimal(position["pl"]), | ||
41 | "liquidationPrice": decimal.Decimal(position["liquidationPrice"]), | ||
42 | "type": position["type"], | ||
43 | "total": decimal.Decimal(position["total"]), | ||
44 | "base_currency": base_currency, | ||
45 | } | ||
46 | return parsed | ||
47 | ccxt.poloniex.fetch_margin_balances = poloniex_fetch_margin_balances | ||
48 | |||
49 | def poloniex_fetch_balance_with_margin(self, params={}): | ||
50 | exchange_balance = self.fetch_balance(params=params) | ||
51 | margin_balances = self.fetch_margin_balances() | ||
52 | |||
53 | for currency, balance in margin_balances.items(): | ||
54 | assert exchange_balance[currency]["total"] == 0 | ||
55 | assert balance["type"] == "short" | ||
56 | exchange_balance[currency]["total"] = balance["amount"] | ||
57 | exchange_balance[currency]["marginPosition"] = balance | ||
58 | return exchange_balance | ||
59 | ccxt.poloniex.fetch_balance_with_margin = poloniex_fetch_balance_with_margin | ||
60 | |||
61 | |||
28 | def poloniex_fetch_balance_per_type(self): | 62 | def poloniex_fetch_balance_per_type(self): |
29 | balances = self.privatePostReturnAvailableAccountBalances() | 63 | balances = self.privatePostReturnAvailableAccountBalances() |
30 | result = {'info': balances} | 64 | result = {'info': balances} |
@@ -92,6 +126,7 @@ def poloniex_create_margin_order(self, symbol, type, side, amount, price=None, l | |||
92 | id = order['id'] | 126 | id = order['id'] |
93 | self.orders[id] = order | 127 | self.orders[id] = order |
94 | return self.extend({'info': response}, order) | 128 | return self.extend({'info': response}, order) |
129 | ccxt.poloniex.create_margin_order = poloniex_create_margin_order | ||
95 | 130 | ||
96 | def poloniex_create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): | 131 | def poloniex_create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): |
97 | if account == "exchange": | 132 | if account == "exchange": |
@@ -100,8 +135,65 @@ def poloniex_create_order(self, symbol, type, side, amount, price=None, account= | |||
100 | return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) | 135 | return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) |
101 | else: | 136 | else: |
102 | raise NotImplementedError | 137 | raise NotImplementedError |
138 | |||
139 | def poloniex_order_precision(self, symbol): | ||
140 | return 8 | ||
141 | |||
103 | ccxt.poloniex.create_exchange_order = ccxt.poloniex.create_order | 142 | ccxt.poloniex.create_exchange_order = ccxt.poloniex.create_order |
104 | ccxt.poloniex.create_order = poloniex_create_order | 143 | ccxt.poloniex.create_order = poloniex_create_order |
144 | ccxt.poloniex.order_precision = poloniex_order_precision | ||
145 | |||
146 | def poloniex_transfer_balance(self, currency, amount, from_account, to_account): | ||
147 | result = self.privatePostTransferBalance({ | ||
148 | "currency": currency, | ||
149 | "amount": amount, | ||
150 | "fromAccount": from_account, | ||
151 | "toAccount": to_account, | ||
152 | "confirmed": 1}) | ||
153 | return result["success"] == 1 | ||
154 | ccxt.poloniex.transfer_balance = poloniex_transfer_balance | ||
155 | |||
156 | # portfolio.market.create_order("DASH/BTC", "limit", "sell", 0.1, price=0.06828800, account="margin") | ||
157 | |||
158 | # portfolio.market.privatePostReturnTradableBalances() | ||
159 | # Returns tradable balances in margin | ||
160 | # 'BTC_DASH': {'BTC': '0.01266999', 'DASH': '0.08574839'}, | ||
161 | # Je peux emprunter jusqu’à 0.08574839 DASH ou 0.01266999 BTC (une position est | ||
162 | # déjà ouverte) | ||
163 | # 'BTC_CLAM': {'BTC': '0.00585143', 'CLAM': '7.79300395'}, | ||
164 | # Je peux emprunter 7.7 CLAM pour les vendre contre des BTC, ou emprunter | ||
165 | # 0.00585143 BTC pour acheter des CLAM | ||
166 | |||
167 | # portfolio.market.privatePostReturnMarginAccountSummary() | ||
168 | # Returns current informations for margin | ||
169 | # {'currentMargin': '1.49680968', -> marge (ne doit pas descendre sous 20% / 0.2) | ||
170 | # = netValue / totalBorrowedValue | ||
171 | # 'lendingFees': '0.00000000', -> fees totaux | ||
172 | # 'netValue': '0.01008254', -> balance + plus-value | ||
173 | # 'pl': '0.00008254', -> plus value latente (somme des positions) | ||
174 | # 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée | ||
175 | # 'totalValue': '0.01000000'} -> valeur totale en compte | ||
176 | |||
177 | |||
178 | # portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) | ||
179 | # See DASH/BTC positions | ||
180 | # {'amount': '-0.10000000', -> DASH empruntés | ||
181 | # 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) | ||
182 | # 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur | ||
183 | # 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) | ||
184 | # 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) | ||
185 | # 'total': '0.00681856', -> valeur totale empruntée en BTC | ||
186 | # 'type': 'short'} | ||
187 | |||
188 | |||
189 | # closeMarginPosition({"currencyPair": "BTC_DASH"}) : fermer la position au prix | ||
190 | # du marché | ||
191 | # Nécessaire à la fin | ||
192 | # portfolio.market.create_order("DASH/BTC", "limit", "buy", 0.1, price=0.06726487, account="margin") | ||
193 | |||
194 | # portfolio.market.fetch_balance_per_type() | ||
195 | # Ne suffit pas pour calculer les positions: ne contient que les 0.01 envoyés | ||
196 | # TODO: vérifier si fetch_balance marque ces 0.01 comme disponibles -> oui | ||
105 | 197 | ||
106 | market = ccxt.poloniex({ | 198 | market = ccxt.poloniex({ |
107 | "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", | 199 | "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", |