Skip to content

contango.strategy.rule_based.cartesian_search

cartesian_search(strategy_cls: Type[StrategyT], param_grid: Mapping[str, Sequence[Any]]) -> Iterator[tuple[Callable[[], StrategyT], dict[str, Any]]]

Generates every parameter combination for a strategy class.

Parameters:

Name Type Description Default
strategy_cls Type[StrategyT]

The strategy to perform a Cartesian search on.

required
param_grid Mapping[str, Sequence[Any]]

A mapping of strings to sequences of values to be tested and parameterized into the strategy cls.

required

Yields:

Type Description
Callable[[], StrategyT]

A tuple of (factory, kwargs), where factory is a callable that

dict[str, Any]

constructs strategy_cls(**kwargs).

Source code in src\contango\strategy\rule_based\cartesian_search.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def cartesian_search(
    strategy_cls: Type[StrategyT],
    param_grid: Mapping[str, Sequence[Any]],
) -> Iterator[tuple[Callable[[], StrategyT], dict[str, Any]]]:
    """
    Generates every parameter combination for a strategy class.

    Args:
        strategy_cls: The strategy to perform a Cartesian search on.
        param_grid: A mapping of strings to sequences of values to be 
                    tested and parameterized into the strategy cls.

    Yields:
        A tuple of `(factory, kwargs)`, where factory is a callable that
        constructs `strategy_cls(**kwargs)`.
    """
    keys = list(param_grid.keys())
    for combo in product(*param_grid.values()):
        kwargs = dict(zip(keys, combo))
        yield partial(strategy_cls, **kwargs), kwargs