Align method names to internal ccxt names

These methods are reimplemented from ccxt so we can test their behaviour.
This commit is contained in:
Matthias 2020-01-12 14:55:05 +01:00
parent b60d7ad42f
commit 5fcab1eee8
8 changed files with 47 additions and 46 deletions

View File

@ -41,7 +41,7 @@ class Binance(Exchange):
""" """
ordertype = "stop_loss_limit" ordertype = "stop_loss_limit"
stop_price = self.symbol_price_prec(pair, stop_price) stop_price = self.price_to_precision(pair, stop_price)
# Ensure rate is less than stop price # Ensure rate is less than stop price
if stop_price <= rate: if stop_price <= rate:
@ -57,9 +57,9 @@ class Binance(Exchange):
params = self._params.copy() params = self._params.copy()
params.update({'stopPrice': stop_price}) params.update({'stopPrice': stop_price})
amount = self.symbol_amount_prec(pair, amount) amount = self.amount_to_precision(pair, amount)
rate = self.symbol_price_prec(pair, rate) rate = self.price_to_precision(pair, rate)
order = self._api.create_order(pair, ordertype, 'sell', order = self._api.create_order(pair, ordertype, 'sell',
amount, rate, params) amount, rate, params)

View File

@ -7,7 +7,6 @@ import inspect
import logging import logging
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timezone from datetime import datetime, timezone
from math import ceil
from random import randint from random import randint
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
@ -368,10 +367,11 @@ class Exchange:
""" """
return endpoint in self._api.has and self._api.has[endpoint] return endpoint in self._api.has and self._api.has[endpoint]
def symbol_amount_prec(self, pair, amount: float) -> float: def amount_to_precision(self, pair, amount: float) -> float:
''' '''
Returns the amount to buy or sell to a precision the Exchange accepts Returns the amount to buy or sell to a precision the Exchange accepts
Rounded down Reimplementation of ccxt internal methods - ensuring we can test the result is correct
based on our definitions.
''' '''
if self.markets[pair]['precision']['amount']: if self.markets[pair]['precision']['amount']:
amount = float(decimal_to_precision(amount, rounding_mode=TRUNCATE, amount = float(decimal_to_precision(amount, rounding_mode=TRUNCATE,
@ -381,10 +381,11 @@ class Exchange:
return amount return amount
def symbol_price_prec(self, pair, price: float) -> float: def price_to_precision(self, pair, price: float) -> float:
''' '''
Returns the price buying or selling with to the precision the Exchange accepts Returns the price buying or selling with to the precision the Exchange accepts
Rounds up Reimplementation of ccxt internal methods - ensuring we can test the result is correct
based on our definitions.
''' '''
if self.markets[pair]['precision']['price']: if self.markets[pair]['precision']['price']:
price = float(decimal_to_precision(price, rounding_mode=ROUND, price = float(decimal_to_precision(price, rounding_mode=ROUND,
@ -396,7 +397,7 @@ class Exchange:
def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float,
rate: float, params: Dict = {}) -> Dict[str, Any]: rate: float, params: Dict = {}) -> Dict[str, Any]:
order_id = f'dry_run_{side}_{randint(0, 10**6)}' order_id = f'dry_run_{side}_{randint(0, 10**6)}'
_amount = self.symbol_amount_prec(pair, amount) _amount = self.amount_to_precision(pair, amount)
dry_order = { dry_order = {
"id": order_id, "id": order_id,
'pair': pair, 'pair': pair,
@ -431,13 +432,13 @@ class Exchange:
rate: float, params: Dict = {}) -> Dict: rate: float, params: Dict = {}) -> Dict:
try: try:
# Set the precision for amount and price(rate) as accepted by the exchange # Set the precision for amount and price(rate) as accepted by the exchange
amount = self.symbol_amount_prec(pair, amount) amount = self.amount_to_precision(pair, amount)
needs_price = (ordertype != 'market' needs_price = (ordertype != 'market'
or self._api.options.get("createMarketBuyOrderRequiresPrice", False)) or self._api.options.get("createMarketBuyOrderRequiresPrice", False))
rate = self.symbol_price_prec(pair, rate) if needs_price else None rate_for_order = self.price_to_precision(pair, rate) if needs_price else None
return self._api.create_order(pair, ordertype, side, return self._api.create_order(pair, ordertype, side,
amount, rate, params) amount, rate_for_order, params)
except ccxt.InsufficientFunds as e: except ccxt.InsufficientFunds as e:
raise DependencyException( raise DependencyException(

View File

@ -35,8 +35,8 @@ class PrecisionFilter(IPairList):
""" """
stop_price = ticker['ask'] * stoploss stop_price = ticker['ask'] * stoploss
# Adjust stop-prices to precision # Adjust stop-prices to precision
sp = self._exchange.symbol_price_prec(ticker["symbol"], stop_price) sp = self._exchange.price_to_precision(ticker["symbol"], stop_price)
stop_gap_price = self._exchange.symbol_price_prec(ticker["symbol"], stop_price * 0.99) stop_gap_price = self._exchange.price_to_precision(ticker["symbol"], stop_price * 0.99)
logger.debug(f"{ticker['symbol']} - {sp} : {stop_gap_price}") logger.debug(f"{ticker['symbol']} - {sp} : {stop_gap_price}")
if sp <= stop_gap_price: if sp <= stop_gap_price:
logger.info(f"Removed {ticker['symbol']} from whitelist, " logger.info(f"Removed {ticker['symbol']} from whitelist, "

View File

@ -22,8 +22,8 @@ def test_stoploss_limit_order(default_conf, mocker):
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
@ -71,8 +71,8 @@ def test_stoploss_limit_order_dry_run(default_conf, mocker):
api_mock = MagicMock() api_mock = MagicMock()
order_type = 'stop_loss_limit' order_type = 'stop_loss_limit'
default_conf['dry_run'] = True default_conf['dry_run'] = True
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')

View File

@ -173,7 +173,7 @@ def test_validate_order_time_in_force(default_conf, mocker, caplog):
ex.validate_order_time_in_force(tif2) ex.validate_order_time_in_force(tif2)
def test_symbol_amount_prec(default_conf, mocker): def test_amount_to_precision(default_conf, mocker):
''' '''
Test rounds down to 4 Decimal places Test rounds down to 4 Decimal places
''' '''
@ -190,17 +190,17 @@ def test_symbol_amount_prec(default_conf, mocker):
amount = 2.34559 amount = 2.34559
pair = 'ETH/BTC' pair = 'ETH/BTC'
amount = exchange.symbol_amount_prec(pair, amount) amount = exchange.amount_to_precision(pair, amount)
assert amount == 2.3455 assert amount == 2.3455
markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'amount': 0.0001}}}) markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'amount': 0.0001}}})
mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=4)) mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=4))
mocker.patch('freqtrade.exchange.Exchange.markets', markets) mocker.patch('freqtrade.exchange.Exchange.markets', markets)
amount = exchange.symbol_amount_prec(pair, amount) amount = exchange.amount_to_precision(pair, amount)
assert amount == 2.3455 assert amount == 2.3455
def test_symbol_price_prec(default_conf, mocker): def test_sprice_to_precision(default_conf, mocker):
''' '''
Test rounds up to 4 decimal places Test rounds up to 4 decimal places
''' '''
@ -212,14 +212,14 @@ def test_symbol_price_prec(default_conf, mocker):
price = 2.34559 price = 2.34559
pair = 'ETH/BTC' pair = 'ETH/BTC'
price = exchange.symbol_price_prec(pair, price) price = exchange.price_to_precision(pair, price)
assert price == 2.3456 assert price == 2.3456
markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'price': 0.0001}}}) markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'price': 0.0001}}})
mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=4)) mocker.patch('freqtrade.exchange.Exchange.precisionMode', PropertyMock(return_value=4))
mocker.patch('freqtrade.exchange.Exchange.markets', markets) mocker.patch('freqtrade.exchange.Exchange.markets', markets)
price = exchange.symbol_price_prec(pair, price) price = exchange.price_to_precision(pair, price)
assert price == 2.3456 assert price == 2.3456
@ -615,8 +615,8 @@ def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice,
} }
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
order = exchange.create_order( order = exchange.create_order(
@ -656,8 +656,8 @@ def test_buy_prod(default_conf, mocker, exchange_name):
} }
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
order = exchange.buy(pair='ETH/BTC', ordertype=order_type, order = exchange.buy(pair='ETH/BTC', ordertype=order_type,
@ -730,8 +730,8 @@ def test_buy_considers_time_in_force(default_conf, mocker, exchange_name):
} }
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
order_type = 'limit' order_type = 'limit'
@ -792,8 +792,8 @@ def test_sell_prod(default_conf, mocker, exchange_name):
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200) order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
@ -856,8 +856,8 @@ def test_sell_considers_time_in_force(default_conf, mocker, exchange_name):
}) })
api_mock.options = {} api_mock.options = {}
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name) exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
order_type = 'limit' order_type = 'limit'

View File

@ -21,8 +21,8 @@ def test_buy_kraken_trading_agreement(default_conf, mocker):
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
order = exchange.buy(pair='ETH/BTC', ordertype=order_type, order = exchange.buy(pair='ETH/BTC', ordertype=order_type,
@ -53,8 +53,8 @@ def test_sell_kraken_trading_agreement(default_conf, mocker):
}) })
default_conf['dry_run'] = False default_conf['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200) order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)

View File

@ -2435,8 +2435,8 @@ def test_execute_sell_with_stoploss_on_exchange(default_conf, ticker, fee, ticke
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_fee=fee, get_fee=fee,
symbol_amount_prec=lambda s, x, y: y, amount_to_precision=lambda s, x, y: y,
symbol_price_prec=lambda s, x, y: y, price_to_precision=lambda s, x, y: y,
stoploss_limit=stoploss_limit, stoploss_limit=stoploss_limit,
cancel_order=cancel_order, cancel_order=cancel_order,
) )
@ -2478,8 +2478,8 @@ def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, ticker, f
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_fee=fee, get_fee=fee,
symbol_amount_prec=lambda s, x, y: y, amount_to_precision=lambda s, x, y: y,
symbol_price_prec=lambda s, x, y: y, price_to_precision=lambda s, x, y: y,
) )
stoploss_limit = MagicMock(return_value={ stoploss_limit = MagicMock(return_value={

View File

@ -58,8 +58,8 @@ def test_may_execute_sell_stoploss_on_exchange_multi(default_conf, ticker, fee,
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_fee=fee, get_fee=fee,
symbol_amount_prec=lambda s, x, y: y, amount_to_precision=lambda s, x, y: y,
symbol_price_prec=lambda s, x, y: y, price_to_precision=lambda s, x, y: y,
get_order=stoploss_order_mock, get_order=stoploss_order_mock,
cancel_order=cancel_order_mock, cancel_order=cancel_order_mock,
) )
@ -137,8 +137,8 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, limit_buy_order, moc
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
fetch_ticker=ticker, fetch_ticker=ticker,
get_fee=fee, get_fee=fee,
symbol_amount_prec=lambda s, x, y: y, amount_to_precision=lambda s, x, y: y,
symbol_price_prec=lambda s, x, y: y, price_to_precision=lambda s, x, y: y,
) )
mocker.patch.multiple( mocker.patch.multiple(