Strategy Optimization¶
Find optimal parameters using grid search and Bayesian optimization.
📋 Notebook Information
- RustyBT Version: 0.1.2+
- Last Validated: 2025-11-07
- API Compatibility: Verified ✅
- Documentation: API Reference
In [1]:
Copied!
from rustybt.analytics import setup_notebook
setup_notebook()
from rustybt.analytics import setup_notebook
setup_notebook()
✅ Notebook environment configured successfully - Async/await support enabled - Pandas display options optimized - Progress bars configured
Grid Search Example¶
In [2]:
Copied!
# Example: Grid search optimization
import numpy as np
from rustybt.optimization import (
Optimizer,
ParameterSpace,
DiscreteParameter,
ObjectiveFunction,
)
from rustybt.optimization.search import GridSearchAlgorithm
# Define parameter space
param_space = ParameterSpace(
parameters=[
DiscreteParameter(name='fast_period', min_value=10, max_value=30, step=10),
DiscreteParameter(name='slow_period', min_value=50, max_value=100, step=25),
]
)
# Define backtest function
# This should run your strategy with the given parameters and return results
def backtest_with_params(params):
"""
Run backtest with given parameters.
Returns a dict with at least 'sharpe_ratio' key.
In practice, this would call run_algorithm() with your strategy.
"""
# Example: return results from run_algorithm()
# results = run_algorithm(
# start=pd.Timestamp('2020-01-01', tz='utc'),
# end=pd.Timestamp('2023-12-31', tz='utc'),
# initialize=lambda context: MyStrategy().initialize(
# context,
# fast_period=params['fast_period'],
# slow_period=params['slow_period']
# ),
# handle_data=MyStrategy().handle_data,
# capital_base=100000.0,
# bundle='yfinance',
# )
# return {'sharpe_ratio': results['sharpe'].iloc[-1]}
# Placeholder return for demonstration
return {
'sharpe_ratio': np.random.uniform(0.5, 2.0),
'total_return': np.random.uniform(0.1, 0.5),
'max_drawdown': np.random.uniform(-0.3, -0.1),
}
# Create optimizer
search_algo = GridSearchAlgorithm(parameter_space=param_space)
objective = ObjectiveFunction(metric="sharpe_ratio")
optimizer = Optimizer(
parameter_space=param_space,
search_algorithm=search_algo,
objective_function=objective,
backtest_function=backtest_with_params,
max_trials=12, # 3 fast_period values * 4 slow_period values
)
# Run optimization
# best_result = optimizer.optimize()
# print(f"Best parameters: {best_result.parameters}")
# print(f"Best Sharpe ratio: {best_result.score}")
# Example: Grid search optimization
import numpy as np
from rustybt.optimization import (
Optimizer,
ParameterSpace,
DiscreteParameter,
ObjectiveFunction,
)
from rustybt.optimization.search import GridSearchAlgorithm
# Define parameter space
param_space = ParameterSpace(
parameters=[
DiscreteParameter(name='fast_period', min_value=10, max_value=30, step=10),
DiscreteParameter(name='slow_period', min_value=50, max_value=100, step=25),
]
)
# Define backtest function
# This should run your strategy with the given parameters and return results
def backtest_with_params(params):
"""
Run backtest with given parameters.
Returns a dict with at least 'sharpe_ratio' key.
In practice, this would call run_algorithm() with your strategy.
"""
# Example: return results from run_algorithm()
# results = run_algorithm(
# start=pd.Timestamp('2020-01-01', tz='utc'),
# end=pd.Timestamp('2023-12-31', tz='utc'),
# initialize=lambda context: MyStrategy().initialize(
# context,
# fast_period=params['fast_period'],
# slow_period=params['slow_period']
# ),
# handle_data=MyStrategy().handle_data,
# capital_base=100000.0,
# bundle='yfinance',
# )
# return {'sharpe_ratio': results['sharpe'].iloc[-1]}
# Placeholder return for demonstration
return {
'sharpe_ratio': np.random.uniform(0.5, 2.0),
'total_return': np.random.uniform(0.1, 0.5),
'max_drawdown': np.random.uniform(-0.3, -0.1),
}
# Create optimizer
search_algo = GridSearchAlgorithm(parameter_space=param_space)
objective = ObjectiveFunction(metric="sharpe_ratio")
optimizer = Optimizer(
parameter_space=param_space,
search_algorithm=search_algo,
objective_function=objective,
backtest_function=backtest_with_params,
max_trials=12, # 3 fast_period values * 4 slow_period values
)
# Run optimization
# best_result = optimizer.optimize()
# print(f"Best parameters: {best_result.parameters}")
# print(f"Best Sharpe ratio: {best_result.score}")