book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Meta Analysis

Optimization Analysis

Introduction

Load your optimization results into the Research Environment to analyze how different combinations of parameters affect the algorithm's performance.

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:

PlatformOptimization Id
Cloud PlatformGet Optimization Id
Local PlatformGet 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)

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: