freqtrade/freqtrade

View on GitHub
freqtrade/optimize/backtesting.py

Summary

Maintainability
F
1 wk
Test Coverage

File backtesting.py has 1147 lines of code (exceeds 250 allowed). Consider refactoring.
Open

# pragma pylint: disable=missing-docstring, W0212, too-many-arguments

"""
This module contains the backtesting logic
"""
Severity: Major
Found in freqtrade/optimize/backtesting.py - About 2 days to fix

    Function _get_exit_for_signal has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
    Open

        def _get_exit_for_signal(
                self, trade: LocalTrade, row: Tuple, exit_: ExitCheckTuple,
                current_time: datetime,
                amount: Optional[float] = None) -> Optional[LocalTrade]:
    
    
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 3 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _get_adjust_trade_entry_for_candle has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def _get_adjust_trade_entry_for_candle(
                self, trade: LocalTrade, row: Tuple, current_time: datetime
        ) -> LocalTrade:
            current_rate: float = row[OPEN_IDX]
            current_profit = trade.calc_profit_ratio(current_rate)
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _get_close_rate_for_stoploss has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def _get_close_rate_for_stoploss(self, row: Tuple, trade: LocalTrade, exit: ExitCheckTuple,
                                         trade_dur: int) -> float:
            # our stoploss was already lower than candle high,
            # possibly due to a cancelled trade exit.
            # exit at open price.
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function backtest has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def backtest(self, processed: Dict,
                     start_date: datetime, end_date: datetime) -> Dict[str, Any]:
            """
            Implement backtesting functionality
    
    
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _get_close_rate_for_roi has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
    Open

        def _get_close_rate_for_roi(self, row: Tuple, trade: LocalTrade, exit: ExitCheckTuple,
                                    trade_dur: int) -> float:
            is_short = trade.is_short or False
            leverage = trade.leverage or 1.0
            side_1 = -1 if is_short else 1
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _get_ohlcv_as_lists has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
    Open

        def _get_ohlcv_as_lists(self, processed: Dict[str, DataFrame]) -> Dict[str, Tuple]:
            """
            Helper function to convert a processed dataframes into lists for performance reasons.
    
            Used by backtest() - so keep this optimized for performance.
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function backtest_loop has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
    Open

        def backtest_loop(
                self, row: Tuple, pair: str, current_time: datetime, end_date: datetime,
                open_trade_count_start: int, trade_dir: Optional[LongShort],
                is_first: bool = True) -> int:
            """
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _enter_trade has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
    Open

        def _enter_trade(self, pair: str, row: Tuple, direction: LongShort,
                         stake_amount: Optional[float] = None,
                         trade: Optional[LocalTrade] = None,
                         requested_rate: Optional[float] = None,
                         requested_stake: Optional[float] = None,
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function get_valid_price_and_stake has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

        def get_valid_price_and_stake(
            self, pair: str, row: Tuple, propose_rate: float, stake_amount: float,
            direction: LongShort, current_time: datetime, entry_tag: Optional[str],
            trade: Optional[LocalTrade], order_type: str, price_precision: Optional[float]
        ) -> Tuple[float, float, float, float]:
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function __init__ has 44 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        def __init__(self, config: Config, exchange: Optional[Exchange] = None) -> None:
    
            LoggingMixin.show_output = False
            self.config = config
            self.results: BacktestResultType = get_BacktestResultType_default()
    Severity: Minor
    Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

      Function __init__ has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

          def __init__(self, config: Config, exchange: Optional[Exchange] = None) -> None:
      
              LoggingMixin.show_output = False
              self.config = config
              self.results: BacktestResultType = get_BacktestResultType_default()
      Severity: Minor
      Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function check_order_replace has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

          def check_order_replace(self, trade: LocalTrade, order: Order, current_time,
                                  row: Tuple) -> bool:
              """
              Check if current analyzed entry order has to be replaced and do so.
              If user requested cancellation and there are no filled orders in the trade will
      Severity: Minor
      Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Consider simplifying this complex logical expression.
      Open

                  if (trade_dur == 0 and (
                      (
                          is_short
                          # Red candle (for longs)
                          and row[OPEN_IDX] < row[CLOSE_IDX]  # Red candle
      Severity: Critical
      Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

        Function get_valid_price_and_stake has 11 arguments (exceeds 4 allowed). Consider refactoring.
        Open

            def get_valid_price_and_stake(
        Severity: Major
        Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

          Function load_bt_data_detail has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
          Open

              def load_bt_data_detail(self) -> None:
                  """
                  Loads backtest detail data (smaller timeframe) if necessary.
                  """
                  if self.timeframe_detail:
          Severity: Minor
          Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function _check_trade_exit has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
          Open

              def _check_trade_exit(
                      self, trade: LocalTrade, row: Tuple, current_time: datetime
              ) -> Optional[LocalTrade]:
          
                  self._run_funding_fees(trade, current_time)
          Severity: Minor
          Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function _enter_trade has 9 arguments (exceeds 4 allowed). Consider refactoring.
          Open

              def _enter_trade(self, pair: str, row: Tuple, direction: LongShort,
          Severity: Major
          Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

            Function backtest_loop has 8 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                def backtest_loop(
            Severity: Major
            Found in freqtrade/optimize/backtesting.py - About 1 hr to fix

              Function check_order_cancel has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
              Open

                  def check_order_cancel(
                          self, trade: LocalTrade, order: Order, current_time: datetime) -> Optional[bool]:
                      """
                      Check if current analyzed order has to be canceled.
                      Returns True if the trade should be Deleted (initial order was canceled),
              Severity: Minor
              Found in freqtrade/optimize/backtesting.py - About 55 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function _exit_trade has 6 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                  def _exit_trade(self, trade: LocalTrade, sell_row: Tuple, close_rate: float,
              Severity: Minor
              Found in freqtrade/optimize/backtesting.py - About 45 mins to fix

                Function _process_exit_order has 6 arguments (exceeds 4 allowed). Consider refactoring.
                Open

                    def _process_exit_order(
                Severity: Minor
                Found in freqtrade/optimize/backtesting.py - About 45 mins to fix

                  Function _get_exit_for_signal has 6 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                      def _get_exit_for_signal(
                  Severity: Minor
                  Found in freqtrade/optimize/backtesting.py - About 45 mins to fix

                    Consider simplifying this complex logical expression.
                    Open

                                if (
                                    not self.strategy.use_custom_stoploss and self.strategy.trailing_stop
                                    and self.strategy.trailing_only_offset_is_reached
                                    and self.strategy.trailing_stop_positive_offset is not None
                                    and self.strategy.trailing_stop_positive
                    Severity: Major
                    Found in freqtrade/optimize/backtesting.py - About 40 mins to fix

                      Consider simplifying this complex logical expression.
                      Open

                              if (
                                  (self._position_stacking or len(LocalTrade.bt_trades_open_pp[pair]) == 0)
                                  and is_first
                                  and current_time != end_date
                                  and trade_dir is not None
                      Severity: Major
                      Found in freqtrade/optimize/backtesting.py - About 40 mins to fix

                        Function _get_close_rate has 5 arguments (exceeds 4 allowed). Consider refactoring.
                        Open

                            def _get_close_rate(self, row: Tuple, trade: LocalTrade, exit: ExitCheckTuple,
                        Severity: Minor
                        Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                          Function _get_close_rate_for_stoploss has 5 arguments (exceeds 4 allowed). Consider refactoring.
                          Open

                              def _get_close_rate_for_stoploss(self, row: Tuple, trade: LocalTrade, exit: ExitCheckTuple,
                          Severity: Minor
                          Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                            Function _try_close_open_order has 5 arguments (exceeds 4 allowed). Consider refactoring.
                            Open

                                def _try_close_open_order(
                            Severity: Minor
                            Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                              Function _get_close_rate_for_roi has 5 arguments (exceeds 4 allowed). Consider refactoring.
                              Open

                                  def _get_close_rate_for_roi(self, row: Tuple, trade: LocalTrade, exit: ExitCheckTuple,
                              Severity: Minor
                              Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                                Function validate_row has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                Open

                                    def validate_row(
                                Severity: Minor
                                Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                                  Function check_order_replace has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                  Open

                                      def check_order_replace(self, trade: LocalTrade, order: Order, current_time,
                                  Severity: Minor
                                  Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                                    Function handle_left_open has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        def handle_left_open(self, open_trades: Dict[str, List[LocalTrade]],
                                                             data: Dict[str, List[Tuple]]) -> None:
                                            """
                                            Handling of left open trades at the end of backtesting
                                            """
                                    Severity: Minor
                                    Found in freqtrade/optimize/backtesting.py - About 35 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Avoid too many return statements within this function.
                                    Open

                                            return stoploss_value
                                    Severity: Major
                                    Found in freqtrade/optimize/backtesting.py - About 30 mins to fix

                                      Avoid too many return statements within this function.
                                      Open

                                              return trade
                                      Severity: Major
                                      Found in freqtrade/optimize/backtesting.py - About 30 mins to fix

                                        There are no issues that match your filters.

                                        Category
                                        Status