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, optimiser_search_value=None, exception=None, start_at=None, backtest_end_at=None, analysis_end_at=None, delivered_to_main_thread_at=None)#
Parameters:
Return type:

None

Methods

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

get_analysis_duration()

get_backtest_duration()

get_cagr()

get_delivery_duration()

get_label()

Get name for this result for charts.

get_max_drawdown()

Get the maximum 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_sortino()

Get the Sortino ratio of this grid search result.

get_trade_count()

How many trades this strategy made.

get_trade_summary_metric(name)

Return one of the trade summart metrics.

get_truncated_label([max_len])

Get name for this result for charts.

get_win_rate()

How many trades were won.

has_result(combination)

hydrate_state()

Get the grid search result full state.

load(combination)

Deserialised from the cached Python pickle.

save([include_state])

Serialise the result as Python pickle and state as separate file.

save_state()

Save state in a separate file.

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.

optimiser_search_value

When used with optimiser, store the raw search function result here.

exception

If this grid search failed with an exception store the result here

start_at

When this test was started

backtest_end_at

When this test ended

analysis_end_at

When we completed the analysis

delivered_to_main_thread_at

When we the main thread received this object (unpickled).

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. Optimiser saves the state as a separate file and you can load it with hydrate_state().

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.

optimiser_search_value: float | None#

When used with optimiser, store the raw search function result here.

Allows to debug and display optimiser progress for complex optimiser search functions like BalancedSharpeAndMaxDrawdownOptimisationFunction.

This is not stored as the part of disk data, because the user can change the search function without the need to regenerate search results. See analyse_optimiser_result() for more information.

exception: Exception | None#

If this grid search failed with an exception store the result here

start_at: datetime.datetime | None#

When this test was started

backtest_end_at: datetime.datetime | None#

When this test ended

analysis_end_at: datetime.datetime | None#

When we completed the analysis

delivered_to_main_thread_at: datetime.datetime | None#

When we the main thread received this object (unpickled).

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_truncated_label(max_len=30)[source]#

Get name for this result for charts.

  • For multiple parameters the label gets too long to render

  • Chop it shorted

Return type:

str

get_metric(name)[source]#

Get a performance metric from quantstats.

See also get_summary_metric()

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_trade_summary_metric(name)[source]#

Return one of the trade summart metrics.

  • Get one of summary card attributes. see summary.

See also get_metric().

Example:

return result.get_summary_metric("win_rate")
Parameters:

name (str) –

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_sortino()[source]#

Get the Sortino ratio of this grid search result.

Returns:

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

Return type:

float

get_max_drawdown()[source]#

Get the maximum drawdown.

Returns:

Always a negative number.

Return pd.NA if not results (no trades, etc.)

Return type:

float | pandas._libs.missing.NAType

get_win_rate()[source]#

How many trades were won.

Returns:

0 if we made zero 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(include_state=False)[source]#

Serialise the result as Python pickle and state as separate file.

Parameters:

state – State is saved also on the disk

save_state()[source]#

Save state in a separate file.

  • Not a part of the core metrics pickle

  • We use pickle and not JSON as it is faster

  • Called by save()

hydrate_state()[source]#

Get the grid search result full state.

  • Make it part of the result object

  • By default we do not load these, because it is too much overhad

Return type:

State

__init__(combination, state, summary, metrics, equity_curve, returns, universe_options, cached=False, process_id=None, initial_cash=None, optimiser_search_value=None, exception=None, start_at=None, backtest_end_at=None, analysis_end_at=None, delivered_to_main_thread_at=None)#
Parameters:
Return type:

None