Meta Analysis
Optimization Analysis
Read Optimization Results
To get the results of an optimization, call the ReadOptimizationread_optimization method with the optimization Id.
var optimization = api.ReadOptimization(optimizationId);
optimization = api.read_optimization(optimization_id)
The following table provides links to documentation that explains how to get the optimization Id, depending on the platform you use:
| Platform | Optimization Id |
|---|---|
| Cloud Platform | Get Optimization Id |
| Local Platform | Get Optimization Id |
| CLI |
The ReadOptimizationread_optimization method returns an Optimization object, which have the following attributes:
Example
Example 1: Read Optimization Results
The following example reads the last completed optimization job and obtains the optimum paramteters in a jupyter notebook.
// Load the necessary assemblies.
#load "../Initialize.csx"
#load "../QuantConnect.csx"
using QuantConnect;
using QuantConnect.Api;
using QuantConnect.Research;
// Instantiate QuantBook instance for researching.
var qb = new QuantBook();
// Get optimization job list in the current project.
var optimizations = api.ListOptimizations(qb.ProjectId)
// Get the last completed optimizations to study.
var optimizationId = optimizations.Where(x => x.Status == OptimizationStatus.Completed)
.OrderByDescending(x => x.Created)
.First()
.OptimizationId;
var optimization = api.ReadOptimization(optimizationId);
// Obtain the backtest with the best Sharpe Ratio.
var bestBacktest = optimization.Backtests.Values.MaxBy(x => x.Statistics["SharpeRatio"])
// Obtain the parameter set of the backtest with the best result.
var parameterSet = bestBacktest.ParameterSet;
Console.WriteLine(parameterSet.ToString()); # Instantiate QuantBook instance for researching.
qb = QuantBook()
# Get optimization job list in the current project.
optimizations = api.list_optimizations(qb.project_id)
# Get the last completed optimizations to study.
optimization_id = sorted(
[x for x in optimizations if x.status == OptimizationStatus.COMPLETED],
key=lambda x: x.created,
reverse=True
)[0].optimization_id
optimization = api.read_optimization(optimization_id)
# Obtain the backtest with the best Sharpe Ratio.
best_backtest = max(optimization.backtests.values(), key=lambda x: x.statistics["SharpeRatio"])
# Obtain the parameter set of the backtest with the best result.
parameter_set = best_backtest.parameter_set
print(parameter_set)