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:
combination (GridCombination) –
state (tradeexecutor.state.state.State | None) –
summary (TradeSummary) –
metrics (DataFrame) –
equity_curve (Series) –
returns (Series) –
universe_options (UniverseOptions) –
cached (bool) –
process_id (int) –
initial_cash (float | None) –
optimiser_search_value (float | None) –
exception (Exception | None) –
start_at (datetime.datetime | None) –
backtest_end_at (datetime.datetime | None) –
analysis_end_at (datetime.datetime | None) –
delivered_to_main_thread_at (datetime.datetime | None) –
- Return type:
None
Methods
__init__
(combination, state, summary, ...[, ...])get_analysis_duration
()get_backtest_duration
()get_cagr
()get_delivery_duration
()Get name for this result for charts.
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 the Sharpe ratio of this grid search result.
Get the Sortino ratio of this grid search result.
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.
How many trades were won.
has_result
(combination)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 in a separate file.
Attributes
For which grid combination this result is
The full back test state
Calculated trade summary
Performance metrics
Needed for visualisations
Needed for visualisations
What backtest data range we used
Was this result read from the earlier run save
Child process that created this result.
Initial cash from the state.
When used with optimiser, store the raw search function result here.
If this grid search failed with an exception store the result here
When this test was started
When this test ended
When we completed the analysis
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
- 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.
- 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:
- 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:
- 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")
- 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")
- get_sharpe()[source]#
Get the Sharpe ratio of this grid search result.
- Returns:
0 if not available (the strategy made no trades).
- Return type:
- get_sortino()[source]#
Get the Sortino ratio of this grid search result.
- Returns:
0 if not available (the strategy made no trades).
- Return type:
- 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_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:
- 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:
- __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:
combination (GridCombination) –
state (tradeexecutor.state.state.State | None) –
summary (TradeSummary) –
metrics (DataFrame) –
equity_curve (Series) –
returns (Series) –
universe_options (UniverseOptions) –
cached (bool) –
process_id (int) –
initial_cash (float | None) –
optimiser_search_value (float | None) –
exception (Exception | None) –
start_at (datetime.datetime | None) –
backtest_end_at (datetime.datetime | None) –
analysis_end_at (datetime.datetime | None) –
delivered_to_main_thread_at (datetime.datetime | None) –
- Return type:
None