X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=ccxt_wrapper.py;h=c4aa94d6cbe7ccfb5c9c38447bc9b59162a1bbe0;hb=e1fd4859353b6968f2f4032740ff707046dbf794;hp=bedf84b47dd3d908ace71fefde768e7cab693ea2;hpb=7d5da019e51adea12a1ad52e07fc71c1ee1de6ab;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index bedf84b..c4aa94d 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -47,6 +47,8 @@ class poloniexE(poloniex): self.session._parent = self def request_wrap(self, *args, **kwargs): + kwargs["headers"]["X-market-id"] = str(self._parent._market.market_id) + kwargs["headers"]["X-user-id"] = str(self._parent._market.user_id) try: r = self.origin_request(*args, **kwargs) self._parent._market.report.log_http_request(args[0], @@ -64,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"}) @@ -230,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({ @@ -380,13 +359,58 @@ 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): + """ + Wrapped to avoid the currency translation + """ + return currency + + def currency_id(self, currency): + """ + Wrapped to avoid the currency translation + """ + return currency