From c77a6addf511e7eea3e6937a384b8c281b776074 Mon Sep 17 00:00:00 2001 From: Yazeed Al Oyoun Date: Tue, 4 Feb 2020 03:19:56 +0100 Subject: [PATCH] introduction of new config with docs --- config.json.example | 1 + config_binance.json.example | 1 + config_full.json.example | 1 + config_kraken.json.example | 1 + docs/configuration.md | 1 + freqtrade/constants.py | 17 ++++---- freqtrade/freqtradebot.py | 84 +++++++++++++++++++++---------------- tests/conftest.py | 1 + 8 files changed, 64 insertions(+), 43 deletions(-) diff --git a/config.json.example b/config.json.example index 8b85e71eb..dc2bd6beb 100644 --- a/config.json.example +++ b/config.json.example @@ -13,6 +13,7 @@ }, "bid_strategy": { "ask_last_balance": 0.0, + "timeout_even_if_buy_signal_valid": true, "use_order_book": false, "order_book_top": 1, "check_depth_of_market": { diff --git a/config_binance.json.example b/config_binance.json.example index 0521a3a35..b5ed62488 100644 --- a/config_binance.json.example +++ b/config_binance.json.example @@ -13,6 +13,7 @@ }, "bid_strategy": { "use_order_book": false, + "timeout_even_if_buy_signal_valid": true, "ask_last_balance": 0.0, "order_book_top": 1, "check_depth_of_market": { diff --git a/config_full.json.example b/config_full.json.example index 82d8bd04a..f191e7b77 100644 --- a/config_full.json.example +++ b/config_full.json.example @@ -26,6 +26,7 @@ }, "bid_strategy": { "use_order_book": false, + "timeout_even_if_buy_signal_valid": true, "ask_last_balance": 0.0, "order_book_top": 1, "check_depth_of_market": { diff --git a/config_kraken.json.example b/config_kraken.json.example index a527b569d..7f64c3402 100644 --- a/config_kraken.json.example +++ b/config_kraken.json.example @@ -13,6 +13,7 @@ }, "bid_strategy": { "use_order_book": false, + "timeout_even_if_buy_signal_valid": true, "ask_last_balance": 0.0, "order_book_top": 1, "check_depth_of_market": { diff --git a/docs/configuration.md b/docs/configuration.md index f2d0fa5f2..2f13afaa3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -61,6 +61,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi | `unfilledtimeout.buy` | **Required.** How long (in minutes) the bot will wait for an unfilled buy order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).
***Datatype:*** *Integer* | `unfilledtimeout.sell` | **Required.** How long (in minutes) the bot will wait for an unfilled sell order to complete, after which the order will be cancelled. [Strategy Override](#parameters-in-the-strategy).
***Datatype:*** *Integer* | `bid_strategy.ask_last_balance` | **Required.** Set the bidding price. More information [below](#buy-price-without-orderbook). +| `bid_strategy.timeout_even_if_buy_signal_valid` | **Required.** Choose whether you would prefer buy orders to [timeout even if buy signal was still valid](#timeout-even-if-buy-signal-valid). If false, your buy orders will not timeout and will ignore your unfilledtimeout for buy orders, so use this option wisely.
*Defaults to `true`.*
***Datatype:*** *Boolean* | `bid_strategy.use_order_book` | Enable buying using the rates in [Order Book Bids](#buy-price-with-orderbook-enabled).
***Datatype:*** *Boolean* | `bid_strategy.order_book_top` | Bot will use the top N rate in Order Book Bids to buy. I.e. a value of 2 will allow the bot to pick the 2nd bid rate in [Order Book Bids](#buy-price-with-orderbook-enabled).
*Defaults to `1`.*
***Datatype:*** *Positive Integer* | `bid_strategy. check_depth_of_market.enabled` | Do not buy if the difference of buy orders and sell orders is met in Order Book. [Check market depth](#check-depth-of-market).
*Defaults to `false`.*
***Datatype:*** *Boolean* diff --git a/freqtrade/constants.py b/freqtrade/constants.py index 53bc4af53..eae7f4d8d 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -76,7 +76,7 @@ CONF_SCHEMA = { 'amend_last_stake_amount': {'type': 'boolean', 'default': False}, 'last_stake_amount_min_ratio': { 'type': 'number', 'minimum': 0.0, 'maximum': 1.0, 'default': 0.5 - }, + }, 'fiat_display_currency': {'type': 'string', 'enum': SUPPORTED_FIAT}, 'dry_run': {'type': 'boolean'}, 'dry_run_wallet': {'type': 'number', 'default': DRY_RUN_WALLET}, @@ -109,15 +109,16 @@ CONF_SCHEMA = { 'minimum': 0, 'maximum': 1, 'exclusiveMaximum': False, - 'use_order_book': {'type': 'boolean'}, - 'order_book_top': {'type': 'integer', 'maximum': 20, 'minimum': 1}, - 'check_depth_of_market': { - 'type': 'object', - 'properties': { + }, + 'timeout_even_if_buy_signal_valid': {'type': 'boolean'}, + 'use_order_book': {'type': 'boolean'}, + 'order_book_top': {'type': 'integer', 'maximum': 20, 'minimum': 1}, + 'check_depth_of_market': { + 'type': 'object', + 'properties': { 'enabled': {'type': 'boolean'}, 'bids_to_ask_delta': {'type': 'number', 'minimum': 0}, - } - }, + } }, }, 'required': ['ask_last_balance'] diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 1a9cbfa64..d7c9f5ca6 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -845,44 +845,58 @@ class FreqtradeBot: :return: True if order was fully cancelled """ reason = "cancelled due to timeout" - if order['status'] != 'canceled': - corder = self.exchange.cancel_order(trade.open_order_id, trade.pair) - else: - # Order was cancelled already, so we can reuse the existing dict - corder = order - reason = "canceled on Exchange" - if corder.get('remaining', order['remaining']) == order['amount']: - # if trade is not partially completed, just delete the trade - self.handle_buy_order_full_cancel(trade, reason) - return True + # running get_signal on historical data fetched + (buy, sell) = self.strategy.get_signal( + trade.pair, self.strategy.ticker_interval, + self.dataprovider.ohlcv(trade.pair, self.strategy.ticker_interval)) - # if trade is partially complete, edit the stake details for the trade - # and close the order - # cancel_order may not contain the full order dict, so we need to fallback - # to the order dict aquired before cancelling. - # we need to fall back to the values from order if corder does not contain these keys. - trade.amount = order['amount'] - corder.get('remaining', order['remaining']) - trade.stake_amount = trade.amount * trade.open_rate - # verify if fees were taken from amount to avoid problems during selling - try: - new_amount = self.get_real_amount(trade, corder if 'fee' in corder else order, - trade.amount) - if not isclose(order['amount'], new_amount, abs_tol=constants.MATH_CLOSE_PREC): - trade.amount = new_amount - # Fee was applied, so set to 0 - trade.fee_open = 0 - trade.recalc_open_trade_price() - except DependencyException as e: - logger.warning("Could not update trade amount: %s", e) + # get config for bid strategy + config_bid_strategy = self.config.get('bid_strategy', {}) - trade.open_order_id = None - logger.info('Partial buy order timeout for %s.', trade) - self.rpc.send_msg({ - 'type': RPCMessageType.STATUS_NOTIFICATION, - 'status': f'Remaining buy order for {trade.pair} cancelled due to timeout' - }) - return False + # proceed to cancel buy order by timeout if timeout_even_if_buy_signal_valid + # is true (original behaviour) -OR- + # cancel buy order only if buying condition is no longer valid OR if there's + # a sell signal present + if config_bid_strategy.get('timeout_even_if_buy_signal_valid', True) or (not buy or sell): + if order['status'] != 'canceled': + corder = self.exchange.cancel_order(trade.open_order_id, trade.pair) + else: + # Order was cancelled already, so we can reuse the existing dict + corder = order + reason = "canceled on exchange" + + if corder.get('remaining', order['remaining']) == order['amount']: + # if trade is not partially completed, just delete the trade + self.handle_buy_order_full_cancel(trade, reason) + return True + + # if trade is partially complete, edit the stake details for the trade + # and close the order + # cancel_order may not contain the full order dict, so we need to fallback + # to the order dict aquired before cancelling. + # we need to fall back to the values from order if corder does not contain these keys. + trade.amount = order['amount'] - corder.get('remaining', order['remaining']) + trade.stake_amount = trade.amount * trade.open_rate + # verify if fees were taken from amount to avoid problems during selling + try: + new_amount = self.get_real_amount(trade, corder if 'fee' in corder else order, + trade.amount) + if not isclose(order['amount'], new_amount, abs_tol=constants.MATH_CLOSE_PREC): + trade.amount = new_amount + # Fee was applied, so set to 0 + trade.fee_open = 0 + trade.recalc_open_trade_price() + except DependencyException as e: + logger.warning("Could not update trade amount: %s", e) + + trade.open_order_id = None + logger.info(f"Partial buy order timeout for {trade.pair}") + self.rpc.send_msg({ + 'type': RPCMessageType.STATUS_NOTIFICATION, + 'status': f"Remaining buy order for {trade.pair} cancelled due to timeout" + }) + return False def handle_timedout_limit_sell(self, trade: Trade, order: Dict) -> bool: """ diff --git a/tests/conftest.py b/tests/conftest.py index 395388f73..068e42d75 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -216,6 +216,7 @@ def default_conf(testdatadir): }, "bid_strategy": { "ask_last_balance": 0.0, + "timeout_even_if_buy_signal_valid": True, "use_order_book": False, "order_book_top": 1, "check_depth_of_market": {