From ef9c1addcfac2700d14012aa5734a66ec35da8e3 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 27 Aug 2021 19:54:53 +0200 Subject: [PATCH] Add expired to list of canceled statuses --- freqtrade/constants.py | 2 ++ freqtrade/exchange/exchange.py | 5 +++-- freqtrade/freqtradebot.py | 6 +++--- freqtrade/persistence/models.py | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/freqtrade/constants.py b/freqtrade/constants.py index cde276ac0..efcd1aaca 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -49,6 +49,8 @@ USERPATH_NOTEBOOKS = 'notebooks' TELEGRAM_SETTING_OPTIONS = ['on', 'off', 'silent'] ENV_VAR_PREFIX = 'FREQTRADE__' +NON_OPEN_EXCHANGE_STATES = ('cancelled', 'canceled', 'closed', 'expired') + # Define decimals per coin for outputs # Only used for outputs. diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 79ee94149..26f034e41 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -19,7 +19,8 @@ from ccxt.base.decimal_to_precision import (ROUND_DOWN, ROUND_UP, TICK_SIZE, TRU decimal_to_precision) from pandas import DataFrame -from freqtrade.constants import DEFAULT_AMOUNT_RESERVE_PERCENT, ListPairsWithTimeframes +from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, + ListPairsWithTimeframes) from freqtrade.data.converter import ohlcv_to_dataframe, trades_dict_to_list from freqtrade.exceptions import (DDosProtection, ExchangeError, InsufficientFundsError, InvalidOrderException, OperationalException, PricingError, @@ -804,7 +805,7 @@ class Exchange: :param order: Order dict as returned from fetch_order() :return: True if order has been cancelled without being filled, False otherwise. """ - return (order.get('status') in ('closed', 'canceled', 'cancelled') + return (order.get('status') in NON_OPEN_EXCHANGE_STATES and order.get('filled') == 0.0) @retrier diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index a7451d632..259270483 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -945,7 +945,7 @@ class FreqtradeBot(LoggingMixin): was_trade_fully_canceled = False # Cancelled orders may have the status of 'canceled' or 'closed' - if order['status'] not in ('cancelled', 'canceled', 'closed'): + if order['status'] not in constants.NON_OPEN_EXCHANGE_STATES: filled_val = order.get('filled', 0.0) or 0.0 filled_stake = filled_val * trade.open_rate minstake = self.exchange.get_min_pair_stake_amount( @@ -961,7 +961,7 @@ class FreqtradeBot(LoggingMixin): # Avoid race condition where the order could not be cancelled coz its already filled. # Simply bailing here is the only safe way - as this order will then be # handled in the next iteration. - if corder.get('status') not in ('cancelled', 'canceled', 'closed'): + if corder.get('status') not in constants.NON_OPEN_EXCHANGE_STATES: logger.warning(f"Order {trade.open_order_id} for {trade.pair} not cancelled.") return False else: @@ -1142,7 +1142,7 @@ class FreqtradeBot(LoggingMixin): trade.close_rate_requested = limit trade.sell_reason = sell_reason.sell_reason # In case of market sell orders the order can be closed immediately - if order.get('status', 'unknown') == 'closed': + if order.get('status', 'unknown') in ('closed', 'expired'): self.update_trade_state(trade, trade.open_order_id, order) Trade.commit() diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index 5eaca7966..8c8c1e0a9 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -13,7 +13,7 @@ from sqlalchemy.orm import Query, declarative_base, relationship, scoped_session from sqlalchemy.pool import StaticPool from sqlalchemy.sql.schema import UniqueConstraint -from freqtrade.constants import DATETIME_PRINT_FORMAT +from freqtrade.constants import DATETIME_PRINT_FORMAT, NON_OPEN_EXCHANGE_STATES from freqtrade.enums import SellType from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.misc import safe_value_fallback @@ -159,7 +159,7 @@ class Order(_DECL_BASE): self.order_date = datetime.fromtimestamp(order['timestamp'] / 1000, tz=timezone.utc) self.ft_is_open = True - if self.status in ('closed', 'canceled', 'cancelled'): + if self.status in NON_OPEN_EXCHANGE_STATES: self.ft_is_open = False if (order.get('filled', 0.0) or 0.0) > 0: self.order_filled_date = datetime.now(timezone.utc)