]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - ccxt_wrapper.py
Fix price imprecision due to floats
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / ccxt_wrapper.py
index d2c9b4ce26550b0074f6632ddc2bdc972eb17bd2..f30c7d20fd6434e2ae36a22e11676ba27da5235b 100644 (file)
@@ -232,39 +232,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({
@@ -382,14 +352,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):
         """