a4f66628fd
* initial commit * run parametrized algorithm with command line parameters * skeleton: top level structure * OptimizationNodePacket scheme * pass parameters as HashSet * run Lean and read results * call method on optimization completion * refactor public interfaces - close ParameterSet collection; allow only get operations - explicit method to start LeanOptimizer * synchronize RunLean method; the result could come in before the backtest id is set in the collections * another portion of refactoring and interface changes * comments * comments & tests for Extremum, Minimization and Maximization classes * unify optimization paramater values (min, max, step) & mode GridSearch tests - swap min&max if necessary - iterate left => right (negate step value if necessary) & provide default step value if step == 0 - no StackOverflow Exception - parameterSet Id should be global for current generator and retain between steps - test signle point boundary (min == max) * BruteForceStrategy tests * more comments * Update Optimizer assembly information - Update Optimizer projects assembly information to match behavior of the other projects * Tweaks - Adding comments - Replace OnComplete for Ended event - Replace Abort for Dispose - ConsoleLeanOptimizer will keep track of running processes - Each backtest will store results in a separated directory, so they don't fight for the log.txt file. - Adding cmdline option for lean to close automatically - Adding concurrent execution backtest limit - Console optimizer will start Lean minimized - Escape spaces in Json path * remove parameter set generator abstraction layer we don't need this flexibility now. * refactor public methods; Step shouldn't be public * constraints: wip * define contract * comparison operators and tests * specify JsonProperty values * Move SafeMultiply100 to extensions * Throw exception on failed Optimizer.Start * constraints: wip * change finish & dispose process * minor fixes - handle force lean abort - notify consumer if target has been reached * target & constraints; adapt unit tests * Minor Tweaks and fixes - Some logging improvements - Remove Public since not required * Ignore empty ParameterValue * simplify condition * avoid reinitialization * reduce type; force immutable * unit tests for constraints and target value * parse & normalize percent values, i.e. 20% => 0.2 * fixup * Target & Constraint & OptimizationNodePacket unit tests * Add more json unit tests - Adding more json conversion unit tests. Fix bug for Extremum which wasn't using the converter. * LeanOptimizer tests * Estimation results * User thread safe counters * LeanOptimizer unit tests; push OptimizationResult on Ended event * more unit tests * Minor tweaks -Estimate ToString in a single line. -Typos and missing header file * Add base SendUpdate method - Add base SendUpdate method for LeanOptimizer * fix LeanOptimizer test; rely on internal Update rather than timer * Add OptimizationStatus - Add missing commments and OptimizationStatus * EulerSearch implementation: wip * OptimizationParameter custom converter * change the type * make step optional * change folder structure * enumerate optimization parameter using IEnumerable & IEnumerator * unit tests: parameters & objectives * unit tests: strategies * remove redundant TODO * change Euler search boundaries * more Euler tests * prevent race condition * Add account/read endpoint - Adding account/read endpoint. Adding unit test * Add status check before running lean * Minor self review - Adding missing comments, minor changes * remove array parameters * minor changes - tidy up config file, rename variable - accept min less or equal than max * move OptimizationParameter methods to strategies * Minor improvements for BaseResultHandler derivates * minor changes - strict requirements for Step and MinStep values - strategy specific settigs * Add TotalRuntime to estimate Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
405 lines
16 KiB
C#
405 lines
16 KiB
C#
/*
|
|
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
|
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
using System;
|
|
using System.Threading;
|
|
using QuantConnect.Util;
|
|
using QuantConnect.Logging;
|
|
using QuantConnect.Configuration;
|
|
using System.Collections.Concurrent;
|
|
using QuantConnect.Optimizer.Objectives;
|
|
using QuantConnect.Optimizer.Parameters;
|
|
using QuantConnect.Optimizer.Strategies;
|
|
|
|
namespace QuantConnect.Optimizer
|
|
{
|
|
/// <summary>
|
|
/// Base Lean optimizer class in charge of handling an optimization job packet
|
|
/// </summary>
|
|
public abstract class LeanOptimizer : IDisposable
|
|
{
|
|
private readonly int _optimizationUpdateInterval = Config.GetInt("optimization-update-interval", 10);
|
|
|
|
private DateTime _startedAt = DateTime.UtcNow;
|
|
|
|
private DateTime _lastUpdate;
|
|
private int _failedBacktest;
|
|
private int _completedBacktest;
|
|
private volatile bool _disposed;
|
|
private object _statusLock = new object();
|
|
|
|
/// <summary>
|
|
/// The current optimization status
|
|
/// </summary>
|
|
protected OptimizationStatus Status { get; private set; } = OptimizationStatus.New;
|
|
|
|
/// <summary>
|
|
/// The optimization target
|
|
/// </summary>
|
|
protected readonly Target OptimizationTarget;
|
|
|
|
/// <summary>
|
|
/// Collection holding <see cref="ParameterSet"/> for each backtest id we are waiting to finish
|
|
/// </summary>
|
|
protected readonly ConcurrentDictionary<string, ParameterSet> RunningParameterSetForBacktest;
|
|
|
|
/// <summary>
|
|
/// Collection holding <see cref="ParameterSet"/> for each backtest id we are waiting to launch
|
|
/// </summary>
|
|
/// <remarks>We can't launch 1 million backtests at the same time</remarks>
|
|
protected readonly ConcurrentQueue<ParameterSet> PendingParameterSet;
|
|
|
|
/// <summary>
|
|
/// The optimization strategy being used
|
|
/// </summary>
|
|
protected readonly IOptimizationStrategy Strategy;
|
|
|
|
/// <summary>
|
|
/// The optimization packet
|
|
/// </summary>
|
|
protected readonly OptimizationNodePacket NodePacket;
|
|
|
|
/// <summary>
|
|
/// Indicates whether optimizer was disposed
|
|
/// </summary>
|
|
protected bool Disposed => _disposed;
|
|
|
|
/// <summary>
|
|
/// Event triggered when the optimization work ended
|
|
/// </summary>
|
|
public event EventHandler<OptimizationResult> Ended;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance
|
|
/// </summary>
|
|
/// <param name="nodePacket">The optimization node packet to handle</param>
|
|
protected LeanOptimizer(OptimizationNodePacket nodePacket)
|
|
{
|
|
if (nodePacket.OptimizationParameters.IsNullOrEmpty())
|
|
{
|
|
throw new ArgumentException("Cannot start an optimization job with no parameter to optimize");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(nodePacket.Criterion?.Target))
|
|
{
|
|
throw new ArgumentException("Cannot start an optimization job with no target to optimize");
|
|
}
|
|
|
|
NodePacket = nodePacket;
|
|
OptimizationTarget = NodePacket.Criterion;
|
|
OptimizationTarget.Reached += (s, e) =>
|
|
{
|
|
// we've reached the optimization target
|
|
TriggerOnEndEvent();
|
|
};
|
|
|
|
Strategy = (IOptimizationStrategy)Activator.CreateInstance(Type.GetType(NodePacket.OptimizationStrategy));
|
|
|
|
RunningParameterSetForBacktest = new ConcurrentDictionary<string, ParameterSet>();
|
|
PendingParameterSet = new ConcurrentQueue<ParameterSet>();
|
|
|
|
Strategy.Initialize(OptimizationTarget, nodePacket.Constraints, NodePacket.OptimizationParameters, NodePacket.OptimizationStrategySettings);
|
|
|
|
Strategy.NewParameterSet += (s, parameterSet) =>
|
|
{
|
|
if (parameterSet == null)
|
|
{
|
|
// shouldn't happen
|
|
Log.Error($"Strategy.NewParameterSet({GetLogDetails()}): generated a null {nameof(ParameterSet)} instance");
|
|
return;
|
|
}
|
|
LaunchLeanForParameterSet(parameterSet);
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts the optimization
|
|
/// </summary>
|
|
public virtual void Start()
|
|
{
|
|
lock (RunningParameterSetForBacktest)
|
|
{
|
|
Strategy.PushNewResults(OptimizationResult.Initial);
|
|
|
|
// if after we started there are no running parameter sets means we have failed to start
|
|
if (RunningParameterSetForBacktest.Count == 0)
|
|
{
|
|
throw new InvalidOperationException($"LeanOptimizer.Start({GetLogDetails()}): failed to start");
|
|
}
|
|
Log.Trace($"LeanOptimizer.Start({GetLogDetails()}): start ended. Waiting on {RunningParameterSetForBacktest.Count + PendingParameterSet.Count} backtests");
|
|
}
|
|
|
|
SetOptimizationStatus(OptimizationStatus.Running);
|
|
ProcessUpdate(forceSend: true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Triggers the optimization job end event
|
|
/// </summary>
|
|
protected virtual void TriggerOnEndEvent()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
SetOptimizationStatus(OptimizationStatus.Ended);
|
|
|
|
var result = Strategy.Solution;
|
|
if (result != null)
|
|
{
|
|
var constraint = NodePacket.Constraints != null ? $"Constraints: ({string.Join(",", NodePacket.Constraints)})" : string.Empty;
|
|
Log.Trace($"LeanOptimizer.TriggerOnEndEvent({GetLogDetails()}): Optimization has ended. " +
|
|
$"Result for {OptimizationTarget}: was reached using ParameterSet: ({result.ParameterSet}) backtestId '{result.BacktestId}'. " +
|
|
$"{constraint}");
|
|
}
|
|
else
|
|
{
|
|
Log.Trace($"LeanOptimizer.TriggerOnEndEvent({GetLogDetails()}): Optimization has ended. Result was not reached");
|
|
}
|
|
|
|
ProcessUpdate(forceSend: true);
|
|
|
|
Ended?.Invoke(this, result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles starting Lean for a given parameter set
|
|
/// </summary>
|
|
/// <param name="parameterSet">The parameter set for the backtest to run</param>
|
|
/// <returns>The new unique backtest id</returns>
|
|
protected abstract string RunLean(ParameterSet parameterSet);
|
|
|
|
/// <summary>
|
|
/// Handles a new backtest json result matching a requested backtest id
|
|
/// </summary>
|
|
/// <param name="jsonBacktestResult">The backtest json result</param>
|
|
/// <param name="backtestId">The associated backtest id</param>
|
|
protected virtual void NewResult(string jsonBacktestResult, string backtestId)
|
|
{
|
|
lock (RunningParameterSetForBacktest)
|
|
{
|
|
ParameterSet parameterSet;
|
|
|
|
// we take a lock so that there is no race condition with launching Lean adding the new backtest id and receiving the backtest result for that id
|
|
// before it's even in the collection 'ParameterSetForBacktest'
|
|
|
|
if (!RunningParameterSetForBacktest.TryRemove(backtestId, out parameterSet))
|
|
{
|
|
Interlocked.Increment(ref _failedBacktest);
|
|
Log.Error(
|
|
$"LeanOptimizer.NewResult({GetLogDetails()}): Optimization compute job with id '{backtestId}' was not found");
|
|
return;
|
|
}
|
|
|
|
// we got a new result if there are any pending parameterSet to run we can now trigger 1
|
|
// we do this before 'Strategy.PushNewResults' so FIFO is respected
|
|
if (PendingParameterSet.Count > 0)
|
|
{
|
|
ParameterSet pendingParameterSet;
|
|
PendingParameterSet.TryDequeue(out pendingParameterSet);
|
|
LaunchLeanForParameterSet(pendingParameterSet);
|
|
}
|
|
|
|
var result = new OptimizationResult(null, parameterSet, backtestId);
|
|
if (string.IsNullOrEmpty(jsonBacktestResult))
|
|
{
|
|
Interlocked.Increment(ref _failedBacktest);
|
|
Log.Error(
|
|
$"LeanOptimizer.NewResult({GetLogDetails()}): Got null/empty backtest result for backtest id '{backtestId}'");
|
|
}
|
|
else
|
|
{
|
|
Interlocked.Increment(ref _completedBacktest);
|
|
result = new OptimizationResult(jsonBacktestResult, parameterSet, backtestId);
|
|
}
|
|
|
|
// always notify the strategy
|
|
Strategy.PushNewResults(result);
|
|
|
|
// strategy could of added more
|
|
if (RunningParameterSetForBacktest.Count == 0)
|
|
{
|
|
TriggerOnEndEvent();
|
|
}
|
|
else
|
|
{
|
|
ProcessUpdate();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disposes of any resources
|
|
/// </summary>
|
|
public virtual void Dispose()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
_disposed = true;
|
|
|
|
PendingParameterSet.Clear();
|
|
|
|
lock (RunningParameterSetForBacktest)
|
|
{
|
|
foreach (var backtestId in RunningParameterSetForBacktest.Keys)
|
|
{
|
|
ParameterSet parameterSet;
|
|
if (RunningParameterSetForBacktest.TryRemove(backtestId, out parameterSet))
|
|
{
|
|
try
|
|
{
|
|
AbortLean(backtestId);
|
|
}
|
|
catch
|
|
{
|
|
// pass
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the current optimization status and strategy estimates
|
|
/// </summary>
|
|
public OptimizationEstimate GetCurrentEstimate()
|
|
{
|
|
var completedCount = _completedBacktest;
|
|
var runtime = DateTime.UtcNow - _startedAt;
|
|
return new OptimizationEstimate
|
|
{
|
|
TotalBacktest = Strategy.GetTotalBacktestEstimate(),
|
|
CompletedBacktest = completedCount,
|
|
FailedBacktest = _failedBacktest,
|
|
RunningBacktest = RunningParameterSetForBacktest.Count,
|
|
InQueueBacktest = PendingParameterSet.Count,
|
|
AverageBacktest = completedCount > 0 ? new TimeSpan(runtime.Ticks / completedCount) : TimeSpan.Zero,
|
|
TotalRuntime = runtime
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method to have pretty more informative logs
|
|
/// </summary>
|
|
protected string GetLogDetails()
|
|
{
|
|
if (NodePacket.UserId == 0)
|
|
{
|
|
return $"OID {NodePacket.OptimizationId}";
|
|
}
|
|
return $"UI {NodePacket.UserId} PID {NodePacket.ProjectId} OID {NodePacket.OptimizationId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles breaking Lean process
|
|
/// </summary>
|
|
/// <param name="backtestId">Specified backtest id</param>
|
|
protected abstract void AbortLean(string backtestId);
|
|
|
|
/// <summary>
|
|
/// Sends an update of the current optimization status to the user
|
|
/// </summary>
|
|
protected abstract void SendUpdate();
|
|
|
|
/// <summary>
|
|
/// Sets the current optimization status
|
|
/// </summary>
|
|
/// <param name="optimizationStatus">The new optimization status</param>
|
|
protected void SetOptimizationStatus(OptimizationStatus optimizationStatus)
|
|
{
|
|
lock (_statusLock)
|
|
{
|
|
// we never come back from an aborted/ended status
|
|
if (Status != OptimizationStatus.Aborted && Status != OptimizationStatus.Ended)
|
|
{
|
|
Status = optimizationStatus;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Will determine if it's right time to trigger an update call
|
|
/// </summary>
|
|
/// <param name="forceSend">True will force send, skipping interval, useful on start and end</param>
|
|
private void ProcessUpdate(bool forceSend = false)
|
|
{
|
|
if (!forceSend && Status == OptimizationStatus.New)
|
|
{
|
|
// don't send any update until we finish the Start(), will be creating a bunch of backtests don't want to send partial/multiple updates
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
if (forceSend || (now - _lastUpdate > TimeSpan.FromSeconds(_optimizationUpdateInterval)))
|
|
{
|
|
_lastUpdate = now;
|
|
Log.Debug($"LeanOptimizer.ProcessUpdate({GetLogDetails()}): start sending update...");
|
|
|
|
SendUpdate();
|
|
|
|
Log.Debug($"LeanOptimizer.ProcessUpdate({GetLogDetails()}): finished sending update successfully.");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e, "Failed to send status update");
|
|
}
|
|
}
|
|
|
|
private void LaunchLeanForParameterSet(ParameterSet parameterSet)
|
|
{
|
|
if (_disposed || Status == OptimizationStatus.Ended || Status == OptimizationStatus.Aborted)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lock (RunningParameterSetForBacktest)
|
|
{
|
|
if (NodePacket.MaximumConcurrentBacktests != 0 && RunningParameterSetForBacktest.Count >= NodePacket.MaximumConcurrentBacktests)
|
|
{
|
|
// we hit the limit on the concurrent backtests
|
|
PendingParameterSet.Enqueue(parameterSet);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var backtestId = RunLean(parameterSet);
|
|
|
|
if (!string.IsNullOrEmpty(backtestId))
|
|
{
|
|
Log.Trace($"LeanOptimizer.LaunchLeanForParameterSet({GetLogDetails()}): launched backtest '{backtestId}'");
|
|
RunningParameterSetForBacktest.TryAdd(backtestId, parameterSet);
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"LeanOptimizer.LaunchLeanForParameterSet({GetLogDetails()}): Initial/null optimization compute job could not be placed into the queue");
|
|
}
|
|
|
|
ProcessUpdate();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error($"LeanOptimizer.LaunchLeanForParameterSet({GetLogDetails()}): Error encountered while placing optimization message into the queue: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|