From e495ffec78a0aa0d666863cbd81e06dd988478d3 Mon Sep 17 00:00:00 2001 From: iuvbio Date: Wed, 20 Feb 2019 02:38:16 +0100 Subject: [PATCH 1/7] align dry_run_orders --- freqtrade/exchange/__init__.py | 68 +++++++++++++++------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 2b6d13fcf..078c8e6be 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -283,21 +283,32 @@ class Exchange(object): price = ceil(big_price) / pow(10, symbol_prec) return price + def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, + rate: float, params: Dict = {}) -> Dict: + order_id = f'dry_run_buy_{randint(0, 10**6)}' + dry_order = { + "id": order_id, + 'pair': pair, + 'price': rate, + 'amount': amount, + 'type': ordertype, + 'side': 'buy', + 'remaining': 0.0, + 'datetime': arrow.utcnow().isoformat(), + 'status': 'closed', + 'fee': None # should this be None or skipped? + } + return order_id, dry_order + + def create_order(self, pair: str, ordertype: str, side: str, amount: float, + rate: float, params: Dict = {}) -> Dict: + pass # TODO: finish this + def buy(self, pair: str, ordertype: str, amount: float, rate: float, time_in_force) -> Dict: if self._conf['dry_run']: - order_id = f'dry_run_buy_{randint(0, 10**6)}' - self._dry_run_open_orders[order_id] = { - 'pair': pair, - 'price': rate, - 'amount': amount, - 'type': ordertype, - 'side': 'buy', - 'remaining': 0.0, - 'datetime': arrow.utcnow().isoformat(), - 'status': 'closed', - 'fee': None - } + order_id, dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) + self._dry_run_open_orders[order_id] = dry_order return {'id': order_id} try: @@ -331,17 +342,8 @@ class Exchange(object): def sell(self, pair: str, ordertype: str, amount: float, rate: float, time_in_force='gtc') -> Dict: if self._conf['dry_run']: - order_id = f'dry_run_sell_{randint(0, 10**6)}' - self._dry_run_open_orders[order_id] = { - 'pair': pair, - 'price': rate, - 'amount': amount, - 'type': ordertype, - 'side': 'sell', - 'remaining': 0.0, - 'datetime': arrow.utcnow().isoformat(), - 'status': 'closed' - } + order_id, dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) + self._dry_run_open_orders[order_id] = dry_order return {'id': order_id} try: @@ -389,21 +391,11 @@ class Exchange(object): 'In stoploss limit order, stop price should be more than limit price') if self._conf['dry_run']: - order_id = f'dry_run_buy_{randint(0, 10**6)}' - self._dry_run_open_orders[order_id] = { - 'info': {}, - 'id': order_id, - 'pair': pair, - 'price': stop_price, - 'amount': amount, - 'type': 'stop_loss_limit', - 'side': 'sell', - 'remaining': amount, - 'datetime': arrow.utcnow().isoformat(), - 'status': 'open', - 'fee': None - } - return self._dry_run_open_orders[order_id] + order_id, dry_order = self.dry_run_order( + pair, "stop_loss_limit", "sell", amount, stop_price, rate) + dry_order.update({"info": {}, "remaining": amount, "status": "open"}) + self._dry_run_open_orders[order_id] = dry_order + return dry_order try: From b5758e67f90680afbbe696f64d71add57952ab45 Mon Sep 17 00:00:00 2001 From: iuvbio Date: Thu, 21 Feb 2019 00:29:59 +0100 Subject: [PATCH 2/7] order creation cleanup --- freqtrade/exchange/__init__.py | 129 ++++++++++++--------------------- 1 file changed, 46 insertions(+), 83 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 078c8e6be..170f94182 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -284,9 +284,9 @@ class Exchange(object): return price def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, - rate: float, params: Dict = {}) -> Dict: - order_id = f'dry_run_buy_{randint(0, 10**6)}' - dry_order = { + rate: float, params: Dict = {}) -> Tuple[str, Dict[str, Any]]: + order_id = f'dry_run_{side}_{randint(0, 10**6)}' + dry_order = { # TODO: ad additional entry should be added for stoploss limit "id": order_id, 'pair': pair, 'price': rate, @@ -302,84 +302,68 @@ class Exchange(object): def create_order(self, pair: str, ordertype: str, side: str, amount: float, rate: float, params: Dict = {}) -> Dict: - pass # TODO: finish this + try: + return self._api.create_order(pair, ordertype, side, + amount, rate, params) + + except ccxt.InsufficientFunds as e: + raise DependencyException( + f'Insufficient funds to create {ordertype} {side} order on market {pair}.' + f'Tried to {side} amount {amount} at rate {rate} (total {rate*amount}).' + f'Message: {e}') + except ccxt.InvalidOrder as e: + raise DependencyException( + f'Could not create {ordertype} {side} order on market {pair}.' + f'Tried to {side} amount {amount} at rate {rate} (total {rate*amount}).' + f'Message: {e}') + except (ccxt.NetworkError, ccxt.ExchangeError) as e: + raise TemporaryError( + f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') + except ccxt.BaseError as e: + raise OperationalException(e) def buy(self, pair: str, ordertype: str, amount: float, rate: float, time_in_force) -> Dict: + if self._conf['dry_run']: order_id, dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) self._dry_run_open_orders[order_id] = dry_order return {'id': order_id} - try: - # Set the precision for amount and price(rate) as accepted by the exchange - amount = self.symbol_amount_prec(pair, amount) - rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None + # Set the precision for amount and price(rate) as accepted by the exchange + amount = self.symbol_amount_prec(pair, amount) + rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None - params = self._params.copy() - if time_in_force != 'gtc': - params.update({'timeInForce': time_in_force}) + params = self._params.copy() + if time_in_force != 'gtc': + params.update({'timeInForce': time_in_force}) - return self._api.create_order(pair, ordertype, 'buy', - amount, rate, params) - - except ccxt.InsufficientFunds as e: - raise DependencyException( - f'Insufficient funds to create limit buy order on market {pair}.' - f'Tried to buy amount {amount} at rate {rate} (total {rate*amount}).' - f'Message: {e}') - except ccxt.InvalidOrder as e: - raise DependencyException( - f'Could not create limit buy order on market {pair}.' - f'Tried to buy amount {amount} at rate {rate} (total {rate*amount}).' - f'Message: {e}') - except (ccxt.NetworkError, ccxt.ExchangeError) as e: - raise TemporaryError( - f'Could not place buy order due to {e.__class__.__name__}. Message: {e}') - except ccxt.BaseError as e: - raise OperationalException(e) + return self.create_order(pair, ordertype, 'buy', amount, rate, params) def sell(self, pair: str, ordertype: str, amount: float, rate: float, time_in_force='gtc') -> Dict: + if self._conf['dry_run']: order_id, dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) self._dry_run_open_orders[order_id] = dry_order return {'id': order_id} - try: - # Set the precision for amount and price(rate) as accepted by the exchange - amount = self.symbol_amount_prec(pair, amount) - rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None + # Set the precision for amount and price(rate) as accepted by the exchange + amount = self.symbol_amount_prec(pair, amount) + rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None - params = self._params.copy() - if time_in_force != 'gtc': - params.update({'timeInForce': time_in_force}) + params = self._params.copy() + if time_in_force != 'gtc': + params.update({'timeInForce': time_in_force}) - return self._api.create_order(pair, ordertype, 'sell', - amount, rate, params) - - except ccxt.InsufficientFunds as e: - raise DependencyException( - f'Insufficient funds to create limit sell order on market {pair}.' - f'Tried to sell amount {amount} at rate {rate} (total {rate*amount}).' - f'Message: {e}') - except ccxt.InvalidOrder as e: - raise DependencyException( - f'Could not create limit sell order on market {pair}.' - f'Tried to sell amount {amount} at rate {rate} (total {rate*amount}).' - f'Message: {e}') - except (ccxt.NetworkError, ccxt.ExchangeError) as e: - raise TemporaryError( - f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') - except ccxt.BaseError as e: - raise OperationalException(e) + return self.create_order(pair, ordertype, 'sell', amount, rate, params) def stoploss_limit(self, pair: str, amount: float, stop_price: float, rate: float) -> Dict: """ creates a stoploss limit order. NOTICE: it is not supported by all exchanges. only binance is tested for now. """ - + ordertype = "stop_loss_limit" # Set the precision for amount and price(rate) as accepted by the exchange amount = self.symbol_amount_prec(pair, amount) rate = self.symbol_price_prec(pair, rate) @@ -392,39 +376,18 @@ class Exchange(object): if self._conf['dry_run']: order_id, dry_order = self.dry_run_order( - pair, "stop_loss_limit", "sell", amount, stop_price, rate) + pair, ordertype, "sell", amount, stop_price) dry_order.update({"info": {}, "remaining": amount, "status": "open"}) self._dry_run_open_orders[order_id] = dry_order return dry_order - try: + params = self._params.copy() + params.update({'stopPrice': stop_price}) - params = self._params.copy() - params.update({'stopPrice': stop_price}) - - order = self._api.create_order(pair, 'stop_loss_limit', 'sell', - amount, rate, params) - logger.info('stoploss limit order added for %s. ' - 'stop price: %s. limit: %s' % (pair, stop_price, rate)) - return order - - except ccxt.InsufficientFunds as e: - raise DependencyException( - f'Insufficient funds to place stoploss limit order on market {pair}. ' - f'Tried to put a stoploss amount {amount} with ' - f'stop {stop_price} and limit {rate} (total {rate*amount}).' - f'Message: {e}') - except ccxt.InvalidOrder as e: - raise DependencyException( - f'Could not place stoploss limit order on market {pair}.' - f'Tried to place stoploss amount {amount} with ' - f'stop {stop_price} and limit {rate} (total {rate*amount}).' - f'Message: {e}') - except (ccxt.NetworkError, ccxt.ExchangeError) as e: - raise TemporaryError( - f'Could not place stoploss limit order due to {e.__class__.__name__}. Message: {e}') - except ccxt.BaseError as e: - raise OperationalException(e) + order = self.create_order(pair, ordertype, 'sell', amount, rate, params) + logger.info('stoploss limit order added for %s. ' + 'stop price: %s. limit: %s' % (pair, stop_price, rate)) + return order @retrier def get_balance(self, currency: str) -> float: From 69bb6ebaf6506ad7f65a72379880ea87d9539344 Mon Sep 17 00:00:00 2001 From: iuvbio Date: Thu, 21 Feb 2019 22:43:15 +0100 Subject: [PATCH 3/7] fix comments --- freqtrade/exchange/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index db77d0d5c..2580bf6b1 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -285,7 +285,7 @@ class Exchange(object): def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, rate: float, params: Dict = {}) -> Tuple[str, Dict[str, Any]]: order_id = f'dry_run_{side}_{randint(0, 10**6)}' - dry_order = { # TODO: ad additional entry should be added for stoploss limit + dry_order = { # TODO: additional entry should be added for stoploss limit "id": order_id, 'pair': pair, 'price': rate, @@ -295,7 +295,7 @@ class Exchange(object): 'remaining': 0.0, 'datetime': arrow.utcnow().isoformat(), 'status': 'closed', - 'fee': None # should this be None or skipped? + 'fee': None } return order_id, dry_order From b79d967371c175f38706bb6409181d71d85f09de Mon Sep 17 00:00:00 2001 From: iuvbio Date: Fri, 22 Feb 2019 01:48:35 +0100 Subject: [PATCH 4/7] add tests, further consolidate orders --- freqtrade/exchange/__init__.py | 36 +++++++--------- freqtrade/tests/exchange/test_exchange.py | 50 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 2580bf6b1..26fd56b59 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -283,7 +283,7 @@ class Exchange(object): return price def dry_run_order(self, pair: str, ordertype: str, side: str, amount: float, - rate: float, params: Dict = {}) -> Tuple[str, Dict[str, Any]]: + rate: float, params: Dict = {}) -> Dict[str, Any]: order_id = f'dry_run_{side}_{randint(0, 10**6)}' dry_order = { # TODO: additional entry should be added for stoploss limit "id": order_id, @@ -297,11 +297,15 @@ class Exchange(object): 'status': 'closed', 'fee': None } - return order_id, dry_order + return dry_order def create_order(self, pair: str, ordertype: str, side: str, amount: float, rate: float, params: Dict = {}) -> Dict: try: + # Set the precision for amount and price(rate) as accepted by the exchange + amount = self.symbol_amount_prec(pair, amount) + rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None + return self._api.create_order(pair, ordertype, side, amount, rate, params) @@ -325,13 +329,9 @@ class Exchange(object): rate: float, time_in_force) -> Dict: if self._conf['dry_run']: - order_id, dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) - self._dry_run_open_orders[order_id] = dry_order - return {'id': order_id} - - # Set the precision for amount and price(rate) as accepted by the exchange - amount = self.symbol_amount_prec(pair, amount) - rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None + dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) + self._dry_run_open_orders[dry_order["id"]] = dry_order + return {"id": dry_order["id"]} params = self._params.copy() if time_in_force != 'gtc': @@ -343,13 +343,9 @@ class Exchange(object): rate: float, time_in_force='gtc') -> Dict: if self._conf['dry_run']: - order_id, dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) - self._dry_run_open_orders[order_id] = dry_order - return {'id': order_id} - - # Set the precision for amount and price(rate) as accepted by the exchange - amount = self.symbol_amount_prec(pair, amount) - rate = self.symbol_price_prec(pair, rate) if ordertype != 'market' else None + dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) + self._dry_run_open_orders[dry_order["id"]] = dry_order + return {"id": dry_order["id"]} params = self._params.copy() if time_in_force != 'gtc': @@ -363,9 +359,7 @@ class Exchange(object): NOTICE: it is not supported by all exchanges. only binance is tested for now. """ ordertype = "stop_loss_limit" - # Set the precision for amount and price(rate) as accepted by the exchange - amount = self.symbol_amount_prec(pair, amount) - rate = self.symbol_price_prec(pair, rate) + stop_price = self.symbol_price_prec(pair, stop_price) # Ensure rate is less than stop price @@ -374,10 +368,10 @@ class Exchange(object): 'In stoploss limit order, stop price should be more than limit price') if self._conf['dry_run']: - order_id, dry_order = self.dry_run_order( + dry_order = self.dry_run_order( pair, ordertype, "sell", amount, stop_price) dry_order.update({"info": {}, "remaining": amount, "status": "open"}) - self._dry_run_open_orders[order_id] = dry_order + self._dry_run_open_orders[dry_order["id"]] = dry_order return dry_order params = self._params.copy() diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index 72919103c..c646c0db7 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -441,6 +441,56 @@ def test_exchange_has(default_conf, mocker): assert not exchange.exchange_has("deadbeef") +@pytest.mark.parametrize("side", [ + ("buy"), + ("sell") +]) +def test_dry_run_order(default_conf, mocker, side): + default_conf['dry_run'] = True + exchange = get_patched_exchange(mocker, default_conf) + + order = exchange.dry_run_order( + pair='ETH/BTC', ordertype='limit', side=side, amount=1, rate=200) + assert 'id' in order + assert f'dry_run_{side}_' in order["id"] + + +@pytest.mark.parametrize("side", [ + ("buy"), + ("sell") +]) +@pytest.mark.parametrize("ordertype,rate", [ + ("market", None), + ("limit", 200), + ("stop_loss_limit", 200) +]) +def test_create_order(default_conf, mocker, side, ordertype, rate): + api_mock = MagicMock() + order_id = 'test_prod_{}_{}'.format(side, randint(0, 10 ** 6)) + api_mock.create_order = MagicMock(return_value={ + 'id': order_id, + 'info': { + 'foo': 'bar' + } + }) + default_conf['dry_run'] = False + mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y) + mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y) + exchange = get_patched_exchange(mocker, default_conf, api_mock) + + order = exchange.create_order( + pair='ETH/BTC', ordertype=ordertype, side=side, amount=1, rate=200) + + assert 'id' in order + assert 'info' in order + assert order['id'] == order_id + assert api_mock.create_order.call_args[0][0] == 'ETH/BTC' + assert api_mock.create_order.call_args[0][1] == ordertype + assert api_mock.create_order.call_args[0][2] == side + assert api_mock.create_order.call_args[0][3] == 1 + assert api_mock.create_order.call_args[0][4] is rate + + def test_buy_dry_run(default_conf, mocker): default_conf['dry_run'] = True exchange = get_patched_exchange(mocker, default_conf) From 9a097214a615f40aa10473b13984cf5d61a7c9d2 Mon Sep 17 00:00:00 2001 From: iuvbio Date: Fri, 22 Feb 2019 19:22:48 +0100 Subject: [PATCH 5/7] return complete dry_order in buy and sell --- freqtrade/exchange/exchange.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 26fd56b59..c3d562aa9 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -331,7 +331,7 @@ class Exchange(object): if self._conf['dry_run']: dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) self._dry_run_open_orders[dry_order["id"]] = dry_order - return {"id": dry_order["id"]} + return dry_order # {"id": dry_order["id"]} params = self._params.copy() if time_in_force != 'gtc': @@ -345,7 +345,7 @@ class Exchange(object): if self._conf['dry_run']: dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) self._dry_run_open_orders[dry_order["id"]] = dry_order - return {"id": dry_order["id"]} + return dry_order # {"id": dry_order["id"]} params = self._params.copy() if time_in_force != 'gtc': @@ -527,7 +527,7 @@ class Exchange(object): interval_in_sec = constants.TICKER_INTERVAL_MINUTES[ticker_interval] * 60 return not ((self._pairs_last_refresh_time.get((pair, ticker_interval), 0) - + interval_in_sec) >= arrow.utcnow().timestamp) + + interval_in_sec) >= arrow.utcnow().timestamp) @retrier_async async def _async_get_candle_history(self, pair: str, tick_interval: str, From ec6794b9ba6810ce176e6adc1c9af68114501db4 Mon Sep 17 00:00:00 2001 From: iuvbio Date: Sat, 23 Feb 2019 16:03:15 +0100 Subject: [PATCH 6/7] fix dry_orders --- freqtrade/exchange/exchange.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index c3d562aa9..94c0e6b3b 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -290,15 +290,28 @@ class Exchange(object): 'pair': pair, 'price': rate, 'amount': amount, + "cost": amount * rate, 'type': ordertype, 'side': 'buy', - 'remaining': 0.0, + 'remaining': amount, 'datetime': arrow.utcnow().isoformat(), - 'status': 'closed', - 'fee': None + 'status': "open", + 'fee': None, + "info": {} } + self.store_dry_order(dry_order) return dry_order + def store_dry_order(self, dry_order: Dict) -> None: + closed_order = dry_order.copy() + if closed_order["type"] in ["market", "limit"]: + closed_order.update({ + "status": "closed", + "filled": closed_order["amount"], + "remaining": 0 + }) + self._dry_run_open_orders[closed_order["id"]] = closed_order + def create_order(self, pair: str, ordertype: str, side: str, amount: float, rate: float, params: Dict = {}) -> Dict: try: @@ -330,8 +343,7 @@ class Exchange(object): if self._conf['dry_run']: dry_order = self.dry_run_order(pair, ordertype, "buy", amount, rate) - self._dry_run_open_orders[dry_order["id"]] = dry_order - return dry_order # {"id": dry_order["id"]} + return dry_order params = self._params.copy() if time_in_force != 'gtc': @@ -344,8 +356,7 @@ class Exchange(object): if self._conf['dry_run']: dry_order = self.dry_run_order(pair, ordertype, "sell", amount, rate) - self._dry_run_open_orders[dry_order["id"]] = dry_order - return dry_order # {"id": dry_order["id"]} + return dry_order params = self._params.copy() if time_in_force != 'gtc': @@ -370,8 +381,6 @@ class Exchange(object): if self._conf['dry_run']: dry_order = self.dry_run_order( pair, ordertype, "sell", amount, stop_price) - dry_order.update({"info": {}, "remaining": amount, "status": "open"}) - self._dry_run_open_orders[dry_order["id"]] = dry_order return dry_order params = self._params.copy() @@ -586,9 +595,6 @@ class Exchange(object): def get_order(self, order_id: str, pair: str) -> Dict: if self._conf['dry_run']: order = self._dry_run_open_orders[order_id] - order.update({ - 'id': order_id - }) return order try: return self._api.fetch_order(order_id, pair) From 403ed48c3e06537005e0efa024b727f8f2a7dccc Mon Sep 17 00:00:00 2001 From: iuvbio Date: Sat, 23 Feb 2019 16:28:13 +0100 Subject: [PATCH 7/7] rename _store_dry_order --- freqtrade/exchange/exchange.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 94c0e6b3b..23926d00f 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -299,10 +299,10 @@ class Exchange(object): 'fee': None, "info": {} } - self.store_dry_order(dry_order) + self._store_dry_order(dry_order) return dry_order - def store_dry_order(self, dry_order: Dict) -> None: + def _store_dry_order(self, dry_order: Dict) -> None: closed_order = dry_order.copy() if closed_order["type"] in ["market", "limit"]: closed_order.update({