GridSearchResult#

API documentation for tradeexecutor.backtest.grid_search.GridSearchResult Python class in Trading Strategy framework.

class GridSearchResult[source]#

Bases: object

Result for one grid combination.

  • Result for one grid search combination

  • Calculate various statistics and curves ready in a multiprocess worker

  • Results can be cached on a disk, as a pickle

  • Some of the data might not be available or discarded as per GridSearchDataRetention

__init__(combination, state, summary, metrics, equity_curve, returns, universe_options, cached=False, process_id=None, initial_cash=None)#
Parameters:
Return type:

None

Methods

__init__(combination, state, summary, ...[, ...])

get_cagr()

get_label()

Get name for this result for charts.

get_max_drawdown()

get_metric(name)

Get a performance metric from quantstats.

get_parameter(name)

Get a combination parameter value used to produce this search.

get_sharpe()

Get the Sharpe ratio of this grid search result.

get_trade_count()

How many trades this strategy made.

has_result(combination)

load(combination)

Deserialised from the cached Python pickle.

save()

Serialise as Python pickle.

Attributes

combination

For which grid combination this result is

state

The full back test state

summary

Calculated trade summary

metrics

Performance metrics

equity_curve

Needed for visualisations

returns

Needed for visualisations

universe_options

What backtest data range we used

cached

Was this result read from the earlier run save

process_id

Child process that created this result.

initial_cash

Initial cash from the state.

combination: GridCombination#

For which grid combination this result is

state: tradeexecutor.state.state.State | None#

The full back test state

By the default, grid search execution drops these, as saving and loading them takes extra time, space, and state is not used to compare grid search results.

summary: TradeSummary#

Calculated trade summary

Internal stats calculated about trades

metrics: DataFrame#

Performance metrics

Use QuantStats lib to calculate these stats.

equity_curve: Series#

Needed for visualisations

returns: Series#

Needed for visualisations

universe_options: UniverseOptions#

What backtest data range we used

cached: bool#

Was this result read from the earlier run save

process_id: int#

Child process that created this result.

Only applicable to multiprocessing

initial_cash: float | None#

Initial cash from the state.

Copied here from the state, as it is needed to draw equity curves. Not available in legacy data.

get_label()[source]#

Get name for this result for charts.

  • Label is grid search parameter key values

  • Includes only searched parameters as label

Return type:

str

get_metric(name)[source]#

Get a performance metric from quantstats.

A shortcut method.

Example:

grid_search_results = perform_grid_search(
    decide_trades,
    strategy_universe,
    combinations,
    max_workers=8,
    trading_strategy_engine_version="0.4",
    multiprocess=True,
)

print("Sharpe of the first result", grid_search_results[0].get_metric("Sharpe")
Parameters:

name (str) – See quantstats for examples

Returns:

Performance metrics value

Return type:

float

get_sharpe()[source]#

Get the Sharpe ratio of this grid search result.

Returns:

0 if not available (the strategy made no trades).

Return type:

float

get_parameter(name)[source]#

Get a combination parameter value used to produce this search.

Useful in filtering.

filtered_results = [r for r in grid_search_results if r.combination.get_parameter("regime_filter_ma_length") is None]
print(f"Grid search results without regime filter: {len(filtered_resutls)}")
Parameters:

name – Parameter name

Raises:

ValueError – If parameter is missing.

Return type:

object

get_trade_count()[source]#

How many trades this strategy made.

Return type:

int

static load(combination)[source]#

Deserialised from the cached Python pickle.

Parameters:

combination (GridCombination) –

save()[source]#

Serialise as Python pickle.

__init__(combination, state, summary, metrics, equity_curve, returns, universe_options, cached=False, process_id=None, initial_cash=None)#
Parameters:
Return type:

None