diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 02:08:08 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 02:08:08 +0100 |
commit | 2308a1c4c0d2514dc9fc9acf235f9e5a33299f63 (patch) | |
tree | baebcf53f977f0d9fdc947f886bc66776ab38c43 | |
parent | 774c099c19b131d150e4db693f9689c415bb36b6 (diff) | |
download | Trader-2308a1c4c0d2514dc9fc9acf235f9e5a33299f63.tar.gz Trader-2308a1c4c0d2514dc9fc9acf235f9e5a33299f63.tar.zst Trader-2308a1c4c0d2514dc9fc9acf235f9e5a33299f63.zip |
create wrapper for poloniex class
-rw-r--r-- | ccxt_wrapper.py | 461 | ||||
-rw-r--r-- | market.py | 19 |
2 files changed, 243 insertions, 237 deletions
diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index 19b9020..4ed2116 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py | |||
@@ -5,242 +5,231 @@ def _cw_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 | Exchange.sum = _cw_exchange_sum | 6 | Exchange.sum = _cw_exchange_sum |
7 | 7 | ||
8 | def _cw_poloniex_fetch_balance(self, params={}): | 8 | class poloniexE(poloniex): |
9 | self.load_markets() | 9 | def fetch_balance(self, params={}): |
10 | balances = self.privatePostReturnCompleteBalances(self.extend({ | 10 | self.load_markets() |
11 | 'account': 'all', | 11 | balances = self.privatePostReturnCompleteBalances(self.extend({ |
12 | }, params)) | 12 | 'account': 'all', |
13 | result = {'info': balances} | 13 | }, params)) |
14 | currencies = list(balances.keys()) | 14 | result = {'info': balances} |
15 | for c in range(0, len(currencies)): | 15 | currencies = list(balances.keys()) |
16 | id = currencies[c] | 16 | for c in range(0, len(currencies)): |
17 | balance = balances[id] | 17 | id = currencies[c] |
18 | currency = self.common_currency_code(id) | 18 | balance = balances[id] |
19 | account = { | 19 | currency = self.common_currency_code(id) |
20 | 'free': decimal.Decimal(balance['available']), | 20 | account = { |
21 | 'used': decimal.Decimal(balance['onOrders']), | 21 | 'free': decimal.Decimal(balance['available']), |
22 | 'total': decimal.Decimal(0.0), | 22 | 'used': decimal.Decimal(balance['onOrders']), |
23 | 'total': decimal.Decimal(0.0), | ||
24 | } | ||
25 | account['total'] = self.sum(account['free'], account['used']) | ||
26 | result[currency] = account | ||
27 | return self.parse_balance(result) | ||
28 | |||
29 | def fetch_margin_balance(self): | ||
30 | """ | ||
31 | portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) | ||
32 | See DASH/BTC positions | ||
33 | {'amount': '-0.10000000', -> DASH empruntés | ||
34 | 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) | ||
35 | 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur | ||
36 | 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) | ||
37 | 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) | ||
38 | 'total': '0.00681856', -> valeur totale empruntée en BTC | ||
39 | 'type': 'short'} | ||
40 | """ | ||
41 | positions = self.privatePostGetMarginPosition({"currencyPair": "all"}) | ||
42 | parsed = {} | ||
43 | for symbol, position in positions.items(): | ||
44 | if position["type"] == "none": | ||
45 | continue | ||
46 | base_currency, currency = symbol.split("_") | ||
47 | parsed[currency] = { | ||
48 | "amount": decimal.Decimal(position["amount"]), | ||
49 | "borrowedPrice": decimal.Decimal(position["basePrice"]), | ||
50 | "lendingFees": decimal.Decimal(position["lendingFees"]), | ||
51 | "pl": decimal.Decimal(position["pl"]), | ||
52 | "liquidationPrice": decimal.Decimal(position["liquidationPrice"]), | ||
53 | "type": position["type"], | ||
54 | "total": decimal.Decimal(position["total"]), | ||
55 | "baseCurrency": base_currency, | ||
56 | } | ||
57 | return parsed | ||
58 | |||
59 | def fetch_balance_per_type(self): | ||
60 | balances = self.privatePostReturnAvailableAccountBalances() | ||
61 | result = {'info': balances} | ||
62 | for key, balance in balances.items(): | ||
63 | result[key] = {} | ||
64 | for currency, amount in balance.items(): | ||
65 | if currency not in result: | ||
66 | result[currency] = {} | ||
67 | result[currency][key] = decimal.Decimal(amount) | ||
68 | result[key][currency] = decimal.Decimal(amount) | ||
69 | return result | ||
70 | |||
71 | def fetch_all_balances(self): | ||
72 | exchange_balances = self.fetch_balance() | ||
73 | margin_balances = self.fetch_margin_balance() | ||
74 | balances_per_type = self.fetch_balance_per_type() | ||
75 | |||
76 | all_balances = {} | ||
77 | in_positions = {} | ||
78 | |||
79 | for currency, exchange_balance in exchange_balances.items(): | ||
80 | if currency in ["info", "free", "used", "total"]: | ||
81 | continue | ||
82 | |||
83 | margin_balance = margin_balances.get(currency, {}) | ||
84 | balance_per_type = balances_per_type.get(currency, {}) | ||
85 | |||
86 | all_balances[currency] = { | ||
87 | "total": exchange_balance["total"] + margin_balance.get("amount", 0), | ||
88 | "exchange_used": exchange_balance["used"], | ||
89 | "exchange_total": exchange_balance["total"] - balance_per_type.get("margin", 0), | ||
90 | "exchange_free": exchange_balance["free"] - balance_per_type.get("margin", 0), | ||
91 | "margin_free": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), | ||
92 | "margin_borrowed": 0, | ||
93 | "margin_total": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), | ||
94 | "margin_lending_fees": margin_balance.get("lendingFees", 0), | ||
95 | "margin_pending_gain": margin_balance.get("pl", 0), | ||
96 | "margin_position_type": margin_balance.get("type", None), | ||
97 | "margin_liquidation_price": margin_balance.get("liquidationPrice", 0), | ||
98 | "margin_borrowed_base_price": margin_balance.get("total", 0), | ||
99 | "margin_borrowed_base_currency": margin_balance.get("baseCurrency", None), | ||
23 | } | 100 | } |
24 | account['total'] = self.sum(account['free'], account['used']) | 101 | if len(margin_balance) > 0: |
25 | result[currency] = account | 102 | if margin_balance["baseCurrency"] not in in_positions: |
26 | return self.parse_balance(result) | 103 | in_positions[margin_balance["baseCurrency"]] = 0 |
27 | poloniex.fetch_balance = _cw_poloniex_fetch_balance | 104 | in_positions[margin_balance["baseCurrency"]] += margin_balance["total"] |
28 | 105 | ||
29 | def _cw_poloniex_fetch_margin_balance(self): | 106 | for currency, in_position in in_positions.items(): |
30 | """ | 107 | all_balances[currency]["total"] += in_position |
31 | portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) | 108 | all_balances[currency]["margin_total"] += in_position |
32 | See DASH/BTC positions | 109 | all_balances[currency]["margin_borrowed"] += in_position |
33 | {'amount': '-0.10000000', -> DASH empruntés | 110 | |
34 | 'basePrice': '0.06818560', -> à ce prix là (0.06828800 demandé * (1-0.15%)) | 111 | return all_balances |
35 | 'lendingFees': '0.00000000', -> ce que je dois à mon créditeur | 112 | |
36 | 'liquidationPrice': '0.15107132', -> prix auquel ça sera liquidé (dépend de ce que j’ai déjà sur mon compte margin) | 113 | def parse_ticker(self, ticker, market=None): |
37 | 'pl': '-0.00000371', -> plus-value latente si je rachète tout de suite (négatif = perdu) | 114 | timestamp = self.milliseconds() |
38 | 'total': '0.00681856', -> valeur totale empruntée en BTC | 115 | symbol = None |
39 | 'type': 'short'} | 116 | if market: |
40 | """ | 117 | symbol = market['symbol'] |
41 | positions = self.privatePostGetMarginPosition({"currencyPair": "all"}) | 118 | return { |
42 | parsed = {} | 119 | 'symbol': symbol, |
43 | for symbol, position in positions.items(): | 120 | 'timestamp': timestamp, |
44 | if position["type"] == "none": | 121 | 'datetime': self.iso8601(timestamp), |
45 | continue | 122 | 'high': decimal.Decimal(ticker['high24hr']), |
46 | base_currency, currency = symbol.split("_") | 123 | 'low': decimal.Decimal(ticker['low24hr']), |
47 | parsed[currency] = { | 124 | 'bid': decimal.Decimal(ticker['highestBid']), |
48 | "amount": decimal.Decimal(position["amount"]), | 125 | 'ask': decimal.Decimal(ticker['lowestAsk']), |
49 | "borrowedPrice": decimal.Decimal(position["basePrice"]), | 126 | 'vwap': None, |
50 | "lendingFees": decimal.Decimal(position["lendingFees"]), | 127 | 'open': None, |
51 | "pl": decimal.Decimal(position["pl"]), | 128 | 'close': None, |
52 | "liquidationPrice": decimal.Decimal(position["liquidationPrice"]), | 129 | 'first': None, |
53 | "type": position["type"], | 130 | 'last': decimal.Decimal(ticker['last']), |
54 | "total": decimal.Decimal(position["total"]), | 131 | 'change': decimal.Decimal(ticker['percentChange']), |
55 | "baseCurrency": base_currency, | 132 | 'percentage': None, |
133 | 'average': None, | ||
134 | 'baseVolume': decimal.Decimal(ticker['quoteVolume']), | ||
135 | 'quoteVolume': decimal.Decimal(ticker['baseVolume']), | ||
136 | 'info': ticker, | ||
137 | } | ||
138 | |||
139 | def create_margin_order(self, symbol, type, side, amount, price=None, lending_rate=None, params={}): | ||
140 | if type == 'market': | ||
141 | raise ExchangeError(self.id + ' allows limit orders only') | ||
142 | self.load_markets() | ||
143 | method = 'privatePostMargin' + self.capitalize(side) | ||
144 | market = self.market(symbol) | ||
145 | price = float(price) | ||
146 | amount = float(amount) | ||
147 | if lending_rate is not None: | ||
148 | params = self.extend({"lendingRate": lending_rate}, params) | ||
149 | response = getattr(self, method)(self.extend({ | ||
150 | 'currencyPair': market['id'], | ||
151 | 'rate': self.price_to_precision(symbol, price), | ||
152 | 'amount': self.amount_to_precision(symbol, amount), | ||
153 | }, params)) | ||
154 | timestamp = self.milliseconds() | ||
155 | order = self.parse_order(self.extend({ | ||
156 | 'timestamp': timestamp, | ||
157 | 'status': 'open', | ||
158 | 'type': type, | ||
159 | 'side': side, | ||
160 | 'price': price, | ||
161 | 'amount': amount, | ||
162 | }, response), market) | ||
163 | id = order['id'] | ||
164 | self.orders[id] = order | ||
165 | return self.extend({'info': response}, order) | ||
166 | |||
167 | def create_exchange_order(self, symbol, type, side, amount, price=None, params={}): | ||
168 | return super(poloniexE, self).create_order(symbol, type, side, amount, price=price, params=params) | ||
169 | |||
170 | def create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): | ||
171 | if account == "exchange": | ||
172 | return self.create_exchange_order(symbol, type, side, amount, price=price, params=params) | ||
173 | elif account == "margin": | ||
174 | return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) | ||
175 | else: | ||
176 | raise NotImplementedError | ||
177 | |||
178 | def order_precision(self, symbol): | ||
179 | return 8 | ||
180 | |||
181 | def transfer_balance(self, currency, amount, from_account, to_account): | ||
182 | result = self.privatePostTransferBalance({ | ||
183 | "currency": currency, | ||
184 | "amount": amount, | ||
185 | "fromAccount": from_account, | ||
186 | "toAccount": to_account, | ||
187 | "confirmed": 1}) | ||
188 | return result["success"] == 1 | ||
189 | |||
190 | def close_margin_position(self, currency, base_currency): | ||
191 | """ | ||
192 | closeMarginPosition({"currencyPair": "BTC_DASH"}) | ||
193 | fermer la position au prix du marché | ||
194 | """ | ||
195 | symbol = "{}_{}".format(base_currency, currency) | ||
196 | self.privatePostCloseMarginPosition({"currencyPair": symbol}) | ||
197 | |||
198 | def tradable_balances(self): | ||
199 | """ | ||
200 | portfolio.market.privatePostReturnTradableBalances() | ||
201 | Returns tradable balances in margin | ||
202 | 'BTC_DASH': {'BTC': '0.01266999', 'DASH': '0.08574839'}, | ||
203 | Je peux emprunter jusqu’à 0.08574839 DASH ou 0.01266999 BTC (une position est déjà ouverte) | ||
204 | 'BTC_CLAM': {'BTC': '0.00585143', 'CLAM': '7.79300395'}, | ||
205 | Je peux emprunter 7.7 CLAM pour les vendre contre des BTC, ou emprunter 0.00585143 BTC pour acheter des CLAM | ||
206 | """ | ||
207 | |||
208 | tradable_balances = self.privatePostReturnTradableBalances() | ||
209 | for symbol, balances in tradable_balances.items(): | ||
210 | for currency, balance in balances.items(): | ||
211 | balances[currency] = decimal.Decimal(balance) | ||
212 | return tradable_balances | ||
213 | |||
214 | def margin_summary(self): | ||
215 | """ | ||
216 | portfolio.market.privatePostReturnMarginAccountSummary() | ||
217 | Returns current informations for margin | ||
218 | {'currentMargin': '1.49680968', -> marge (ne doit pas descendre sous 20% / 0.2) | ||
219 | = netValue / totalBorrowedValue | ||
220 | 'lendingFees': '0.00000000', -> fees totaux | ||
221 | 'netValue': '0.01008254', -> balance + plus-value | ||
222 | 'pl': '0.00008254', -> plus value latente (somme des positions) | ||
223 | 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée | ||
224 | 'totalValue': '0.01000000'} -> valeur totale en compte | ||
225 | """ | ||
226 | summary = self.privatePostReturnMarginAccountSummary() | ||
227 | |||
228 | return { | ||
229 | "current_margin": decimal.Decimal(summary["currentMargin"]), | ||
230 | "lending_fees": decimal.Decimal(summary["lendingFees"]), | ||
231 | "gains": decimal.Decimal(summary["pl"]), | ||
232 | "total_borrowed": decimal.Decimal(summary["totalBorrowedValue"]), | ||
233 | "total": decimal.Decimal(summary["totalValue"]), | ||
56 | } | 234 | } |
57 | return parsed | ||
58 | poloniex.fetch_margin_balance = _cw_poloniex_fetch_margin_balance | ||
59 | |||
60 | def _cw_poloniex_fetch_balance_per_type(self): | ||
61 | balances = self.privatePostReturnAvailableAccountBalances() | ||
62 | result = {'info': balances} | ||
63 | for key, balance in balances.items(): | ||
64 | result[key] = {} | ||
65 | for currency, amount in balance.items(): | ||
66 | if currency not in result: | ||
67 | result[currency] = {} | ||
68 | result[currency][key] = decimal.Decimal(amount) | ||
69 | result[key][currency] = decimal.Decimal(amount) | ||
70 | return result | ||
71 | poloniex.fetch_balance_per_type = _cw_poloniex_fetch_balance_per_type | ||
72 | |||
73 | def _cw_poloniex_fetch_all_balances(self): | ||
74 | exchange_balances = self.fetch_balance() | ||
75 | margin_balances = self.fetch_margin_balance() | ||
76 | balances_per_type = self.fetch_balance_per_type() | ||
77 | |||
78 | all_balances = {} | ||
79 | in_positions = {} | ||
80 | |||
81 | for currency, exchange_balance in exchange_balances.items(): | ||
82 | if currency in ["info", "free", "used", "total"]: | ||
83 | continue | ||
84 | |||
85 | margin_balance = margin_balances.get(currency, {}) | ||
86 | balance_per_type = balances_per_type.get(currency, {}) | ||
87 | |||
88 | all_balances[currency] = { | ||
89 | "total": exchange_balance["total"] + margin_balance.get("amount", 0), | ||
90 | "exchange_used": exchange_balance["used"], | ||
91 | "exchange_total": exchange_balance["total"] - balance_per_type.get("margin", 0), | ||
92 | "exchange_free": exchange_balance["free"] - balance_per_type.get("margin", 0), | ||
93 | "margin_free": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), | ||
94 | "margin_borrowed": 0, | ||
95 | "margin_total": balance_per_type.get("margin", 0) + margin_balance.get("amount", 0), | ||
96 | "margin_lending_fees": margin_balance.get("lendingFees", 0), | ||
97 | "margin_pending_gain": margin_balance.get("pl", 0), | ||
98 | "margin_position_type": margin_balance.get("type", None), | ||
99 | "margin_liquidation_price": margin_balance.get("liquidationPrice", 0), | ||
100 | "margin_borrowed_base_price": margin_balance.get("total", 0), | ||
101 | "margin_borrowed_base_currency": margin_balance.get("baseCurrency", None), | ||
102 | } | ||
103 | if len(margin_balance) > 0: | ||
104 | if margin_balance["baseCurrency"] not in in_positions: | ||
105 | in_positions[margin_balance["baseCurrency"]] = 0 | ||
106 | in_positions[margin_balance["baseCurrency"]] += margin_balance["total"] | ||
107 | |||
108 | for currency, in_position in in_positions.items(): | ||
109 | all_balances[currency]["total"] += in_position | ||
110 | all_balances[currency]["margin_total"] += in_position | ||
111 | all_balances[currency]["margin_borrowed"] += in_position | ||
112 | |||
113 | return all_balances | ||
114 | poloniex.fetch_all_balances = _cw_poloniex_fetch_all_balances | ||
115 | |||
116 | def _cw_poloniex_parse_ticker(self, ticker, market=None): | ||
117 | timestamp = self.milliseconds() | ||
118 | symbol = None | ||
119 | if market: | ||
120 | symbol = market['symbol'] | ||
121 | return { | ||
122 | 'symbol': symbol, | ||
123 | 'timestamp': timestamp, | ||
124 | 'datetime': self.iso8601(timestamp), | ||
125 | 'high': decimal.Decimal(ticker['high24hr']), | ||
126 | 'low': decimal.Decimal(ticker['low24hr']), | ||
127 | 'bid': decimal.Decimal(ticker['highestBid']), | ||
128 | 'ask': decimal.Decimal(ticker['lowestAsk']), | ||
129 | 'vwap': None, | ||
130 | 'open': None, | ||
131 | 'close': None, | ||
132 | 'first': None, | ||
133 | 'last': decimal.Decimal(ticker['last']), | ||
134 | 'change': decimal.Decimal(ticker['percentChange']), | ||
135 | 'percentage': None, | ||
136 | 'average': None, | ||
137 | 'baseVolume': decimal.Decimal(ticker['quoteVolume']), | ||
138 | 'quoteVolume': decimal.Decimal(ticker['baseVolume']), | ||
139 | 'info': ticker, | ||
140 | } | ||
141 | poloniex.parse_ticker = _cw_poloniex_parse_ticker | ||
142 | |||
143 | def _cw_poloniex_create_margin_order(self, symbol, type, side, amount, price=None, lending_rate=None, params={}): | ||
144 | if type == 'market': | ||
145 | raise ExchangeError(self.id + ' allows limit orders only') | ||
146 | self.load_markets() | ||
147 | method = 'privatePostMargin' + self.capitalize(side) | ||
148 | market = self.market(symbol) | ||
149 | price = float(price) | ||
150 | amount = float(amount) | ||
151 | if lending_rate is not None: | ||
152 | params = self.extend({"lendingRate": lending_rate}, params) | ||
153 | response = getattr(self, method)(self.extend({ | ||
154 | 'currencyPair': market['id'], | ||
155 | 'rate': self.price_to_precision(symbol, price), | ||
156 | 'amount': self.amount_to_precision(symbol, amount), | ||
157 | }, params)) | ||
158 | timestamp = self.milliseconds() | ||
159 | order = self.parse_order(self.extend({ | ||
160 | 'timestamp': timestamp, | ||
161 | 'status': 'open', | ||
162 | 'type': type, | ||
163 | 'side': side, | ||
164 | 'price': price, | ||
165 | 'amount': amount, | ||
166 | }, response), market) | ||
167 | id = order['id'] | ||
168 | self.orders[id] = order | ||
169 | return self.extend({'info': response}, order) | ||
170 | poloniex.create_margin_order = _cw_poloniex_create_margin_order | ||
171 | |||
172 | def _cw_poloniex_create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): | ||
173 | if account == "exchange": | ||
174 | return self.create_exchange_order(symbol, type, side, amount, price=price, params=params) | ||
175 | elif account == "margin": | ||
176 | return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) | ||
177 | else: | ||
178 | raise NotImplementedError | ||
179 | |||
180 | def _cw_poloniex_order_precision(self, symbol): | ||
181 | return 8 | ||
182 | |||
183 | poloniex.create_exchange_order = poloniex.create_order | ||
184 | poloniex.create_order = _cw_poloniex_create_order | ||
185 | poloniex.order_precision = _cw_poloniex_order_precision | ||
186 | |||
187 | def _cw_poloniex_transfer_balance(self, currency, amount, from_account, to_account): | ||
188 | result = self.privatePostTransferBalance({ | ||
189 | "currency": currency, | ||
190 | "amount": amount, | ||
191 | "fromAccount": from_account, | ||
192 | "toAccount": to_account, | ||
193 | "confirmed": 1}) | ||
194 | return result["success"] == 1 | ||
195 | poloniex.transfer_balance = _cw_poloniex_transfer_balance | ||
196 | |||
197 | def _cw_poloniex_close_margin_position(self, currency, base_currency): | ||
198 | """ | ||
199 | closeMarginPosition({"currencyPair": "BTC_DASH"}) | ||
200 | fermer la position au prix du marché | ||
201 | """ | ||
202 | symbol = "{}_{}".format(base_currency, currency) | ||
203 | self.privatePostCloseMarginPosition({"currencyPair": symbol}) | ||
204 | poloniex.close_margin_position = _cw_poloniex_close_margin_position | ||
205 | |||
206 | def _cw_poloniex_tradable_balances(self): | ||
207 | """ | ||
208 | portfolio.market.privatePostReturnTradableBalances() | ||
209 | Returns tradable balances in margin | ||
210 | 'BTC_DASH': {'BTC': '0.01266999', 'DASH': '0.08574839'}, | ||
211 | Je peux emprunter jusqu’à 0.08574839 DASH ou 0.01266999 BTC (une position est déjà ouverte) | ||
212 | 'BTC_CLAM': {'BTC': '0.00585143', 'CLAM': '7.79300395'}, | ||
213 | Je peux emprunter 7.7 CLAM pour les vendre contre des BTC, ou emprunter 0.00585143 BTC pour acheter des CLAM | ||
214 | """ | ||
215 | |||
216 | tradable_balances = self.privatePostReturnTradableBalances() | ||
217 | for symbol, balances in tradable_balances.items(): | ||
218 | for currency, balance in balances.items(): | ||
219 | balances[currency] = decimal.Decimal(balance) | ||
220 | return tradable_balances | ||
221 | poloniex.fetch_tradable_balances = _cw_poloniex_tradable_balances | ||
222 | |||
223 | def _cw_poloniex_margin_summary(self): | ||
224 | """ | ||
225 | portfolio.market.privatePostReturnMarginAccountSummary() | ||
226 | Returns current informations for margin | ||
227 | {'currentMargin': '1.49680968', -> marge (ne doit pas descendre sous 20% / 0.2) | ||
228 | = netValue / totalBorrowedValue | ||
229 | 'lendingFees': '0.00000000', -> fees totaux | ||
230 | 'netValue': '0.01008254', -> balance + plus-value | ||
231 | 'pl': '0.00008254', -> plus value latente (somme des positions) | ||
232 | 'totalBorrowedValue': '0.00673602', -> valeur en BTC empruntée | ||
233 | 'totalValue': '0.01000000'} -> valeur totale en compte | ||
234 | """ | ||
235 | summary = self.privatePostReturnMarginAccountSummary() | ||
236 | |||
237 | return { | ||
238 | "current_margin": decimal.Decimal(summary["currentMargin"]), | ||
239 | "lending_fees": decimal.Decimal(summary["lendingFees"]), | ||
240 | "gains": decimal.Decimal(summary["pl"]), | ||
241 | "total_borrowed": decimal.Decimal(summary["totalBorrowedValue"]), | ||
242 | "total": decimal.Decimal(summary["totalValue"]), | ||
243 | } | ||
244 | poloniex.margin_summary = _cw_poloniex_margin_summary | ||
245 | |||
246 | 235 | ||
@@ -1,7 +1,24 @@ | |||
1 | import ccxt_wrapper as ccxt | 1 | import ccxt_wrapper as ccxt |
2 | 2 | ||
3 | market = ccxt.poloniex({ | 3 | market = ccxt.poloniexE({ |
4 | "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", | 4 | "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", |
5 | "secret": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", | 5 | "secret": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
6 | }) | 6 | }) |
7 | 7 | ||
8 | |||
9 | # For requests logging | ||
10 | requests = [] | ||
11 | market.session.origin_request = market.session.request | ||
12 | |||
13 | def request_wrap(self, *args, **kwargs): | ||
14 | r = self.origin_request(*args, **kwargs) | ||
15 | requests.append({ | ||
16 | "method": args[0], | ||
17 | "url": args[1], | ||
18 | "body": kwargs["data"], | ||
19 | "headers": kwargs["headers"], | ||
20 | "status": r.status_code, | ||
21 | "response": r.text, | ||
22 | }) | ||
23 | return r | ||
24 | market.session.request = request_wrap.__get__(market.session, market.session.__class__) | ||