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>
170 lines
7.2 KiB
C#
170 lines
7.2 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.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using QuantConnect.Logging;
|
|
using QuantConnect.Packets;
|
|
|
|
namespace QuantConnect.Parameters
|
|
{
|
|
/// <summary>
|
|
/// Specifies a field or property is a parameter that can be set
|
|
/// from an <see cref="AlgorithmNodePacket.Parameters"/> dictionary
|
|
/// </summary>
|
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
|
public class ParameterAttribute : Attribute
|
|
{
|
|
/// <summary>
|
|
/// Specifies the binding flags used by this implementation to resolve parameter attributes
|
|
/// </summary>
|
|
public const BindingFlags BindingFlags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance;
|
|
|
|
private static readonly string ParameterAttributeNameProperty = "Name";
|
|
|
|
/// <summary>
|
|
/// Gets the name of this parameter
|
|
/// </summary>
|
|
public string Name { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ParameterAttribute"/> class
|
|
/// </summary>
|
|
/// <param name="name">The name of the parameter. If null is specified
|
|
/// then the field or property name will be used</param>
|
|
public ParameterAttribute(string name = null)
|
|
{
|
|
Name = name;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses reflections to inspect the instance for any parameter attributes.
|
|
/// If a value is found in the parameters dictionary, it is set.
|
|
/// </summary>
|
|
/// <param name="parameters">The parameters dictionary</param>
|
|
/// <param name="instance">The instance to set parameters on</param>
|
|
public static void ApplyAttributes(Dictionary<string, string> parameters, object instance)
|
|
{
|
|
if (instance == null) throw new ArgumentNullException(nameof(instance));
|
|
|
|
var type = instance.GetType();
|
|
|
|
// get all fields/properties on the instance
|
|
var members = type.GetFields(BindingFlags).Concat<MemberInfo>(type.GetProperties(BindingFlags));
|
|
foreach (var memberInfo in members)
|
|
{
|
|
var fieldInfo = memberInfo as FieldInfo;
|
|
var propertyInfo = memberInfo as PropertyInfo;
|
|
|
|
// this line make static analysis a little happier, but should never actually throw
|
|
if (fieldInfo == null && propertyInfo == null)
|
|
{
|
|
throw new InvalidOperationException("Resolved member that is neither FieldInfo or PropertyInfo");
|
|
}
|
|
|
|
// check the member for our custom attribute
|
|
var attribute = memberInfo.GetCustomAttribute<ParameterAttribute>();
|
|
if (attribute == null) continue;
|
|
|
|
// if no name is specified in the attribute then use the member name
|
|
var parameterName = attribute.Name ?? memberInfo.Name;
|
|
|
|
// get the parameter string value to apply to the member
|
|
string parameterValue;
|
|
if (!parameters.TryGetValue(parameterName, out parameterValue)) continue;
|
|
|
|
if (string.IsNullOrEmpty(parameterValue))
|
|
{
|
|
Log.Error($"ParameterAttribute.ApplyAttributes(): parameter '{parameterName}' provided value is null/empty, skipping");
|
|
continue;
|
|
}
|
|
|
|
// if it's a read-only property with a parameter value we can't really do anything, bail
|
|
if (propertyInfo != null && !propertyInfo.CanWrite)
|
|
{
|
|
var message = $"The specified property is read only: {propertyInfo.DeclaringType}.{propertyInfo.Name}";
|
|
throw new InvalidOperationException(message);
|
|
}
|
|
|
|
// resolve the member type
|
|
var memberType = fieldInfo != null ? fieldInfo.FieldType : propertyInfo.PropertyType;
|
|
|
|
// convert the parameter string value to the member type
|
|
var value = parameterValue.ConvertTo(memberType);
|
|
|
|
// set the value to the field/property
|
|
if (fieldInfo != null)
|
|
{
|
|
fieldInfo.SetValue(instance, value);
|
|
}
|
|
else
|
|
{
|
|
propertyInfo.SetValue(instance, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resolves all parameter attributes from the specified compiled assembly path
|
|
/// </summary>
|
|
/// <param name="assembly">The assembly to inspect</param>
|
|
/// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
|
|
public static Dictionary<string, string> GetParametersFromAssembly(Assembly assembly)
|
|
{
|
|
var parameters = new Dictionary<string, string>();
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
foreach (var kvp in GetParametersFromType(type))
|
|
{
|
|
parameters[kvp.Key] = kvp.Value;
|
|
}
|
|
}
|
|
return parameters;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resolves all parameter attributes from the specified type
|
|
/// </summary>
|
|
/// <param name="type">The type to inspect</param>
|
|
/// <returns>Parameters dictionary keyed by parameter name with a value of the member type</returns>
|
|
public static IEnumerable<KeyValuePair<string, string>> GetParametersFromType(Type type)
|
|
{
|
|
foreach (var field in type.GetFields(BindingFlags))
|
|
{
|
|
var attribute = field.GetCustomAttribute<ParameterAttribute>();
|
|
if (attribute != null)
|
|
{
|
|
var parameterName = attribute.Name ?? field.Name;
|
|
yield return new KeyValuePair<string, string>(parameterName, field.FieldType.GetBetterTypeName());
|
|
}
|
|
}
|
|
|
|
foreach (var property in type.GetProperties(BindingFlags))
|
|
{
|
|
// ignore non-writeable properties
|
|
if (!property.CanWrite) continue;
|
|
var attribute = property.GetCustomAttribute<ParameterAttribute>();
|
|
if (attribute != null)
|
|
{
|
|
var parameterName = attribute.Name ?? property.Name;
|
|
yield return new KeyValuePair<string, string>(parameterName, property.PropertyType.GetBetterTypeName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|