Skip to content

trading.optimizer.experiments.backtest_experiment_grid

trading.optimizer.experiments.backtest_experiment_grid

BacktestExperimentGrid

BacktestExperimentGrid(base_experiment: BacktestExperiment, param_space: dict[str, list[Any]])

Allows for BacktestExperiment instances to be tested upon a variety of parameters without the use of nested loops.

Initializes BacktestExperimentGrid.

Parameters:

Name Type Description Default
base_experiment BacktestExperiment

The base experiment to test through the grid.

required
param_space dict[str, list[Any]]

A dictionary of parameter names matching to the desired values to test for the parameter.

required
Source code in src\contango\trading\optimizer\experiments\backtest_experiment_grid.py
30
31
32
33
34
35
36
37
38
39
def __init__(self, base_experiment: BacktestExperiment, param_space: dict[str, list[Any]]):
    """
    Initializes `BacktestExperimentGrid`.

    Args:
        base_experiment: The base experiment to test through the grid.
        param_space: A dictionary of parameter names matching to the desired values to test for the parameter.
    """
    self.base = base_experiment
    self.param_space = param_space

generate

generate() -> list[BacktestExperiment]

Generates a list of BacktestExperiment instances with the given parameter space at initialization.

Source code in src\contango\trading\optimizer\experiments\backtest_experiment_grid.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def generate(self) -> list[BacktestExperiment]:
    """
    Generates a list of `BacktestExperiment` instances with the given parameter space at initialization.
    """
    keys = list(self.param_space.keys())
    values = list(self.param_space.values())

    experiments: list[BacktestExperiment] = []

    for combo in product(*values):
        params = dict(zip(keys, combo))

        experiments.append(
            BacktestExperiment(
                strategy_factory=self.base.strategy_factory,
                parameters=params,
                dataset=self.base.dataset,
                config=self.base.config
            )
        )

    return experiments