Lean  $LEAN_TAG$
OptimizationStepParameter.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using Newtonsoft.Json;
17 using System;
18 
20 {
21  /// <summary>
22  /// Defines the step based optimization parameter
23  /// </summary>
25  {
26  /// <summary>
27  /// Minimum value of optimization parameter, applicable for boundary conditions
28  /// </summary>
29  [JsonProperty("min")]
30  public decimal MinValue { get; }
31 
32  /// <summary>
33  /// Maximum value of optimization parameter, applicable for boundary conditions
34  /// </summary>
35  [JsonProperty("max")]
36  public decimal MaxValue { get; }
37 
38  /// <summary>
39  /// Movement, should be positive
40  /// </summary>
41  [JsonProperty("step")]
42  public decimal? Step { get; set; }
43 
44  /// <summary>
45  /// Minimal possible movement for current parameter, should be positive
46  /// </summary>
47  /// <remarks>Used by <see cref="Strategies.EulerSearchOptimizationStrategy"/> to determine when this parameter can no longer be optimized</remarks>
48  [JsonProperty("minStep")]
49  public decimal? MinStep { get; set; }
50 
51  /// <summary>
52  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
53  /// </summary>
54  /// <param name="name">parameter name</param>
55  /// <param name="min">minimal value</param>
56  /// <param name="max">maximal value</param>
57  public OptimizationStepParameter(string name, decimal min, decimal max)
58  : base(name)
59  {
60  if (min > max)
61  {
62  throw new ArgumentException(Messages.OptimizationStepParameter.InvalidStepRange(min, max));
63  }
64 
65  MinValue = min;
66  MaxValue = max;
67  }
68 
69  /// <summary>
70  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
71  /// </summary>
72  /// <param name="name">parameter name</param>
73  /// <param name="min">minimal value</param>
74  /// <param name="max">maximal value</param>
75  /// <param name="step">movement</param>
76  public OptimizationStepParameter(string name, decimal min, decimal max, decimal step)
77  : this(name, min, max, step, step)
78  {
79 
80  }
81 
82  /// <summary>
83  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
84  /// </summary>
85  /// <param name="name">parameter name</param>
86  /// <param name="min">minimal value</param>
87  /// <param name="max">maximal value</param>
88  /// <param name="step">movement</param>
89  /// <param name="minStep">minimal possible movement</param>
90  public OptimizationStepParameter(string name, decimal min, decimal max, decimal step, decimal minStep) : this(name, min, max)
91  {
92  // with zero step algorithm can go to infinite loop, use default step value
93  if (step <= 0)
94  {
95  throw new ArgumentException(Messages.OptimizationStepParameter.NonPositiveStepValue(nameof(step), step));
96  }
97 
98  // EulerSearch algorithm can go to infinite range division if Min step is not provided, use Step as default
99  if (minStep <= 0)
100  {
101  throw new ArgumentException(Messages.OptimizationStepParameter.NonPositiveStepValue(nameof(minStep), minStep));
102  }
103 
104  if (step < minStep)
105  {
106  throw new ArgumentException(Messages.OptimizationStepParameter.StepLessThanMinStep);
107  }
108 
109  Step = step;
110  MinStep = minStep;
111  }
112  }
113 }