Update __init__.py

Also setting price "rate" to a precision CCXT informs us the exchange/pair accepts

Moved from round() to math floor and ceil to explicitly round_down on amount and up on price.
This commit is contained in:
creslin 2018-07-29 13:34:57 +00:00 committed by GitHub
parent 0c035b276c
commit ce74a5b97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import logging
from random import randint from random import randint
from typing import List, Dict, Any, Optional from typing import List, Dict, Any, Optional
from datetime import datetime from datetime import datetime
from math import floor, ceil
import ccxt import ccxt
import arrow import arrow
@ -95,10 +96,6 @@ class Exchange(object):
except (KeyError, AttributeError): except (KeyError, AttributeError):
raise OperationalException(f'Exchange {name} is not supported') raise OperationalException(f'Exchange {name} is not supported')
# check if config has sandbox = True, if so use ['test'] sandbox API from CCXT
if (exchange_config.get('sandbox')):
api.urls['api'] = api.urls['test']
return api return api
@property @property
@ -154,18 +151,38 @@ class Exchange(object):
""" """
return endpoint in self._api.has and self._api.has[endpoint] return endpoint in self._api.has and self._api.has[endpoint]
def symbol_prec(self, pair, amount: float): def symbol_amount_prec(self, pair, amount: float):
''' '''
Returns the amount to buy or sell to the precision the Exchange accepts Returns the amount to buy or sell to a precision the Exchange accepts
Rounded down
:param pair: the symbol
:param amount: amount :param amount: amount
:return: amount :return: amount
''' '''
if self._api.markets[pair]['precision']['amount']: if self._api.markets[pair]['precision']['amount'] > 0:
symbol_prec = self._api.markets[pair]['precision']['amount'] symbol_prec = self._api.markets[pair]['precision']['amount']
amount = round(amount, symbol_prec) multiplier = int('1' + ('0' * symbol_prec))
big_amount = amount * multiplier
round_down = floor(big_amount)
amount = round_down / multiplier
return amount return amount
def symbol_price_prec(self, pair, price: float):
'''
Returns the price buying or selling with to the precision the Exchange accepts
Rounds up
:param pair: the symbol
:param price: amount
:return: price
'''
if self._api.markets[pair]['precision']['price'] > 0:
symbol_prec = self._api.markets[pair]['precision']['price']
multiplier = int('1' + ('0' * symbol_prec))
big_price = price * multiplier
round_up = ceil(big_price)
price = round_up / multiplier
return price
def buy(self, pair: str, rate: float, amount: float) -> Dict: def buy(self, pair: str, rate: float, amount: float) -> Dict:
if self._conf['dry_run']: if self._conf['dry_run']:
order_id = f'dry_run_buy_{randint(0, 10**6)}' order_id = f'dry_run_buy_{randint(0, 10**6)}'
@ -183,8 +200,9 @@ class Exchange(object):
return {'id': order_id} return {'id': order_id}
try: try:
# Set the precision accepted by the exchange # Set the precision for amount and price(rate) as accepted by the exchange
amount = self.symbol_prec(pair, amount) amount = self.symbol_amount_prec(pair, amount)
rate = self.symbol_price_prec(pair, rate)
return self._api.create_limit_buy_order(pair, amount, rate) return self._api.create_limit_buy_order(pair, amount, rate)
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
@ -219,8 +237,9 @@ class Exchange(object):
return {'id': order_id} return {'id': order_id}
try: try:
# Set the precision accepted by the exchange # Set the precision for amount and price(rate) as accepted by the exchange
amount = self.symbol_prec(pair, amount) amount = self.symbol_amount_prec(pair, amount)
rate = self.symbol_price_prec(pair, rate)
return self._api.create_limit_sell_order(pair, amount, rate) return self._api.create_limit_sell_order(pair, amount, rate)
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e: