BaptisteZloch/Quant-Invest-Lab

View on GitHub
quant_invest_lab/backtest.py

Summary

Maintainability
F
6 days
Test Coverage

File backtest.py has 742 lines of code (exceeds 250 allowed). Consider refactoring.
Open

from datetime import datetime
from typing import Callable, Literal, Optional, Union, Tuple
import pandas as pd
import numpy as np
from tqdm import tqdm
Severity: Major
Found in quant_invest_lab/backtest.py - About 1 day to fix

    Function __ohlc_backtest_one_position_type has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring.
    Open

    def __ohlc_backtest_one_position_type(
        ohlcv_df: pd.DataFrame,
        entry_function: Callable[[pd.Series, pd.Series], bool],
        exit_function: Callable[[pd.Series, pd.Series, int], bool],
        position_type: Literal["long", "short"],
    Severity: Minor
    Found in quant_invest_lab/backtest.py - About 6 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 __price_backtest_one_position_type has a Cognitive Complexity of 42 (exceeds 5 allowed). Consider refactoring.
    Open

    def __price_backtest_one_position_type(
        price_df: pd.DataFrame,
        entry_function: Callable[[pd.Series, pd.Series], bool],
        exit_function: Callable[[pd.Series, pd.Series, int], bool],
        position_type: Literal["long", "short"],
    Severity: Minor
    Found in quant_invest_lab/backtest.py - About 6 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

    Consider simplifying this complex logical expression.
    Open

            if position_opened is False and entry_function(row, previous_row) is True:
                position_opened = True
                current_trade["entry_date"] = index
                current_trade["entry_price"] = row.Close
                current_trade["entry_reason"] = "Entry position triggered"
    Severity: Critical
    Found in quant_invest_lab/backtest.py - About 3 hrs to fix

      Consider simplifying this complex logical expression.
      Open

              if position_opened is False and entry_function(row, previous_row) is True:
                  position_opened = True
                  current_trade["entry_date"] = index
                  current_trade["entry_price"] = row["Price"]
                  current_trade["entry_reason"] = "Entry position triggered"
      Severity: Critical
      Found in quant_invest_lab/backtest.py - About 3 hrs to fix

        Function price_short_only_backtester has 14 arguments (exceeds 4 allowed). Consider refactoring.
        Open

        def price_short_only_backtester(
        Severity: Major
        Found in quant_invest_lab/backtest.py - About 1 hr to fix

          Function price_long_only_backtester has 14 arguments (exceeds 4 allowed). Consider refactoring.
          Open

          def price_long_only_backtester(
          Severity: Major
          Found in quant_invest_lab/backtest.py - About 1 hr to fix

            Function ohlc_long_only_backtester has 13 arguments (exceeds 4 allowed). Consider refactoring.
            Open

            def ohlc_long_only_backtester(
            Severity: Major
            Found in quant_invest_lab/backtest.py - About 1 hr to fix

              Function ohlc_short_only_backtester has 13 arguments (exceeds 4 allowed). Consider refactoring.
              Open

              def ohlc_short_only_backtester(
              Severity: Major
              Found in quant_invest_lab/backtest.py - About 1 hr to fix

                Function price_short_only_backtester has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                Open

                def price_short_only_backtester(
                    df: pd.DataFrame,
                    price_column: str,
                    short_entry_function: Callable[[pd.Series, pd.Series], bool],
                    short_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                Severity: Minor
                Found in quant_invest_lab/backtest.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 ohlc_long_only_backtester has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                Open

                def ohlc_long_only_backtester(
                    df: pd.DataFrame,
                    long_entry_function: Callable[[pd.Series, pd.Series], bool],
                    long_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                    timeframe: Timeframe,
                Severity: Minor
                Found in quant_invest_lab/backtest.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 price_long_only_backtester has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                Open

                def price_long_only_backtester(
                    df: pd.DataFrame,
                    price_column: str,
                    long_entry_function: Callable[[pd.Series, pd.Series], bool],
                    long_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                Severity: Minor
                Found in quant_invest_lab/backtest.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 ohlc_short_only_backtester has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                Open

                def ohlc_short_only_backtester(
                    df: pd.DataFrame,
                    short_entry_function: Callable[[pd.Series, pd.Series], bool],
                    short_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                    timeframe: Timeframe,
                Severity: Minor
                Found in quant_invest_lab/backtest.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 price_long_only_backtester has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                def price_long_only_backtester(
                    df: pd.DataFrame,
                    price_column: str,
                    long_entry_function: Callable[[pd.Series, pd.Series], bool],
                    long_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                Severity: Minor
                Found in quant_invest_lab/backtest.py - About 1 hr to fix

                  Function price_short_only_backtester has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                  def price_short_only_backtester(
                      df: pd.DataFrame,
                      price_column: str,
                      short_entry_function: Callable[[pd.Series, pd.Series], bool],
                      short_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                  Severity: Minor
                  Found in quant_invest_lab/backtest.py - About 1 hr to fix

                    Function ohlc_short_only_backtester has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                    def ohlc_short_only_backtester(
                        df: pd.DataFrame,
                        short_entry_function: Callable[[pd.Series, pd.Series], bool],
                        short_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                        timeframe: Timeframe,
                    Severity: Minor
                    Found in quant_invest_lab/backtest.py - About 1 hr to fix

                      Function ohlc_long_only_backtester has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                      def ohlc_long_only_backtester(
                          df: pd.DataFrame,
                          long_entry_function: Callable[[pd.Series, pd.Series], bool],
                          long_exit_function: Callable[[pd.Series, pd.Series, int], bool],
                          timeframe: Timeframe,
                      Severity: Minor
                      Found in quant_invest_lab/backtest.py - About 1 hr to fix

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

                        def __ohlc_backtest_one_position_type(
                        Severity: Major
                        Found in quant_invest_lab/backtest.py - About 1 hr to fix

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

                          def __price_backtest_one_position_type(
                          Severity: Major
                          Found in quant_invest_lab/backtest.py - About 1 hr to fix

                            Avoid too many return statements within this function.
                            Open

                                    return ohlcv_df
                            Severity: Major
                            Found in quant_invest_lab/backtest.py - About 30 mins to fix

                              Avoid too many return statements within this function.
                              Open

                                      return ohlcv_df
                              Severity: Major
                              Found in quant_invest_lab/backtest.py - About 30 mins to fix

                                Avoid too many return statements within this function.
                                Open

                                        return price_df
                                Severity: Major
                                Found in quant_invest_lab/backtest.py - About 30 mins to fix

                                  Avoid too many return statements within this function.
                                  Open

                                          return price_df
                                  Severity: Major
                                  Found in quant_invest_lab/backtest.py - About 30 mins to fix

                                    There are no issues that match your filters.

                                    Category
                                    Status