X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ccxt_wrapper.py;h=aaccc612ba036ff92123d377aed2ff208c02725d;hb=refs%2Fheads%2Fmaster;hp=d2c9b4ce26550b0074f6632ddc2bdc972eb17bd2;hpb=1178378ab68a426762b9e45d28a5ce97a7727c7d;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index d2c9b4c..aaccc61 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -66,6 +66,13 @@ class poloniexE(poloniex): def nanoseconds(): return int(time.time() * 1000000000) + def is_dust_trade(self, amount, rate): + if abs(amount) < decimal.Decimal("0.000001"): + return True + if abs(amount * rate) < decimal.Decimal("0.0001"): + return True + return False + def fetch_margin_balance(self): """ portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) @@ -232,39 +239,9 @@ class poloniexE(poloniex): return all_balances - def create_exchange_order(self, symbol, type, side, amount, price=None, params={}): - return super().create_order(symbol, type, side, amount, price=price, params=params) - - def create_margin_order(self, symbol, type, side, amount, price=None, lending_rate=None, params={}): - if type == 'market': - raise ExchangeError(self.id + ' allows limit orders only') - self.load_markets() - method = 'privatePostMargin' + self.capitalize(side) - market = self.market(symbol) - price = float(price) - amount = float(amount) - if lending_rate is not None: - params = self.extend({"lendingRate": lending_rate}, params) - response = getattr(self, method)(self.extend({ - 'currencyPair': market['id'], - 'rate': self.price_to_precision(symbol, price), - 'amount': self.amount_to_precision(symbol, amount), - }, params)) - timestamp = self.milliseconds() - order = self.parse_order(self.extend({ - 'timestamp': timestamp, - 'status': 'open', - 'type': type, - 'side': side, - 'price': price, - 'amount': amount, - }, response), market) - id = order['id'] - self.orders[id] = order - return self.extend({'info': response}, order) - def order_precision(self, symbol): - return 8 + self.load_markets() + return self.markets[symbol]['precision']['price'] def transfer_balance(self, currency, amount, from_account, to_account): result = self.privatePostTransferBalance({ @@ -322,6 +299,10 @@ class poloniexE(poloniex): "total": decimal.Decimal(summary["totalValue"]), } + def fetch_nth_order_book(self, symbol, action, number): + book = self.fetch_order_book(symbol, limit=number) + return decimal.Decimal(book[action + "s"][-1][0]) + def nonce(self): """ Wrapped to allow nonce with other libraries @@ -382,14 +363,49 @@ class poloniexE(poloniex): def create_order(self, symbol, type, side, amount, price=None, account="exchange", lending_rate=None, params={}): """ - Wrapped to handle margin and exchange accounts + Wrapped to handle margin and exchange accounts, and get decimals """ + if type == 'market': + raise ExchangeError(self.id + ' allows limit orders only') + self.load_markets() if account == "exchange": - return self.create_exchange_order(symbol, type, side, amount, price=price, params=params) + method = 'privatePost' + self.capitalize(side) elif account == "margin": - return self.create_margin_order(symbol, type, side, amount, price=price, lending_rate=lending_rate, params=params) + method = 'privatePostMargin' + self.capitalize(side) + if lending_rate is not None: + params = self.extend({"lendingRate": lending_rate}, params) else: raise NotImplementedError + market = self.market(symbol) + response = getattr(self, method)(self.extend({ + 'currencyPair': market['id'], + 'rate': self.price_to_precision(symbol, price), + 'amount': self.amount_to_precision(symbol, amount), + }, params)) + timestamp = self.milliseconds() + order = self.parse_order(self.extend({ + 'timestamp': timestamp, + 'status': 'open', + 'type': type, + 'side': side, + 'price': price, + 'amount': amount, + }, response), market) + id = order['id'] + self.orders[id] = order + return self.extend({'info': response}, order) + + def price_to_precision(self, symbol, price): + """ + Wrapped to avoid float + """ + return ('{:.' + str(self.markets[symbol]['precision']['price']) + 'f}').format(price).rstrip("0").rstrip(".") + + def amount_to_precision(self, symbol, amount): + """ + Wrapped to avoid float + """ + return ('{:.' + str(self.markets[symbol]['precision']['amount']) + 'f}').format(amount).rstrip("0").rstrip(".") def common_currency_code(self, currency): """