From 0a713faca84c5039b9245ab6d25ba22c1b4d112e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 7 May 2022 14:53:51 +0200 Subject: [PATCH] Fix some type errors --- freqtrade/optimize/backtesting.py | 22 ++++++++++++++++------ freqtrade/strategy/interface.py | 17 +++++++++-------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 3a3660c39..19922ee57 100755 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -500,7 +500,8 @@ class Backtesting: stake_available = self.wallets.get_available_stake_amount() stake_amount = strategy_safe_wrapper(self.strategy.adjust_trade_position, default_retval=None)( - trade=trade, current_time=row[DATE_IDX].to_pydatetime(), current_rate=row[OPEN_IDX], + trade=trade, # type: ignore[arg-type] + current_time=row[DATE_IDX].to_pydatetime(), current_rate=row[OPEN_IDX], current_profit=current_profit, min_stake=min_stake, max_stake=min(max_stake, stake_available)) @@ -566,7 +567,8 @@ class Backtesting: if order_type == 'limit': close_rate = strategy_safe_wrapper(self.strategy.custom_exit_price, default_retval=close_rate)( - pair=trade.pair, trade=trade, + pair=trade.pair, + trade=trade, # type: ignore[arg-type] current_time=exit_candle_time, proposed_rate=close_rate, current_profit=current_profit, exit_tag=exit_reason) @@ -580,7 +582,10 @@ class Backtesting: time_in_force = self.strategy.order_time_in_force['exit'] if not strategy_safe_wrapper(self.strategy.confirm_trade_exit, default_retval=True)( - pair=trade.pair, trade=trade, order_type='limit', amount=trade.amount, + pair=trade.pair, + trade=trade, # type: ignore[arg-type] + order_type='limit', + amount=trade.amount, rate=close_rate, time_in_force=time_in_force, sell_reason=exit_reason, # deprecated @@ -656,7 +661,7 @@ class Backtesting: return self._get_exit_trade_entry_for_candle(trade, row) def get_valid_price_and_stake( - self, pair: str, row: Tuple, propose_rate: float, stake_amount: Optional[float], + self, pair: str, row: Tuple, propose_rate: float, stake_amount_inp: Optional[float], direction: LongShort, current_time: datetime, entry_tag: Optional[str], trade: Optional[LocalTrade], order_type: str ) -> Tuple[float, float, float, float]: @@ -694,6 +699,8 @@ class Backtesting: ) if self._can_short else 1.0 # Cap leverage between 1.0 and max_leverage. leverage = min(max(leverage, 1.0), max_leverage) + elif stake_amount_inp is not None: + stake_amount = stake_amount_inp min_stake_amount = self.exchange.get_min_pair_stake_amount( pair, propose_rate, -0.05, leverage=leverage) or 0 @@ -901,7 +908,9 @@ class Backtesting: Check if current analyzed order has to be canceled. Returns True if the trade should be Deleted (initial order was canceled). """ - timedout = self.strategy.ft_check_timed_out(trade, order, current_time) + timedout = self.strategy.ft_check_timed_out( + trade, # type: ignore[arg-type] + order, current_time) if timedout: if order.side == trade.entry_side: self.timedout_entry_orders += 1 @@ -930,7 +939,8 @@ class Backtesting: if order.side == trade.entry_side and current_time > order.order_date_utc: requested_rate = strategy_safe_wrapper(self.strategy.adjust_entry_price, default_retval=order.price)( - trade=trade, order=order, pair=trade.pair, current_time=current_time, + trade=trade, # type: ignore[arg-type] + order=order, pair=trade.pair, current_time=current_time, proposed_rate=row[OPEN_IDX], current_order_rate=order.price, entry_tag=trade.enter_tag, side=trade.trade_direction ) # default value is current order price diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 44f7466ec..002a7aca5 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -16,7 +16,7 @@ from freqtrade.enums import (CandleType, ExitCheckTuple, ExitType, SignalDirecti SignalType, TradingMode) from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date, timeframe_to_seconds -from freqtrade.persistence import LocalTrade, Order, PairLocks, Trade +from freqtrade.persistence import Order, PairLocks, Trade from freqtrade.strategy.hyper import HyperStrategyMixin from freqtrade.strategy.informative_decorator import (InformativeData, PopulateIndicators, _create_and_merge_informative_pair, @@ -918,19 +918,20 @@ class IStrategy(ABC, HyperStrategyMixin): if exit_ and not enter: exit_signal = ExitType.EXIT_SIGNAL else: - custom_reason = strategy_safe_wrapper(self.custom_exit, default_retval=False)( + reason_cust = strategy_safe_wrapper(self.custom_exit, default_retval=False)( pair=trade.pair, trade=trade, current_time=current_time, current_rate=current_rate, current_profit=current_profit) - if custom_reason: + if reason_cust: exit_signal = ExitType.CUSTOM_EXIT - if isinstance(custom_reason, str): - if len(custom_reason) > CUSTOM_EXIT_MAX_LENGTH: + if isinstance(reason_cust, str): + custom_reason = reason_cust + if len(reason_cust) > CUSTOM_EXIT_MAX_LENGTH: logger.warning(f'Custom exit reason returned from ' f'custom_exit is too long and was trimmed' f'to {CUSTOM_EXIT_MAX_LENGTH} characters.') - custom_reason = custom_reason[:CUSTOM_EXIT_MAX_LENGTH] + custom_reason = reason_cust[:CUSTOM_EXIT_MAX_LENGTH] else: - custom_reason = None + custom_reason = '' if ( exit_signal == ExitType.CUSTOM_EXIT or (exit_signal == ExitType.EXIT_SIGNAL @@ -1071,7 +1072,7 @@ class IStrategy(ABC, HyperStrategyMixin): else: return current_profit > roi - def ft_check_timed_out(self, trade: LocalTrade, order: Order, + def ft_check_timed_out(self, trade: Trade, order: Order, current_time: datetime) -> bool: """ FT Internal method.