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  #pragma warning disable CS1574
45  /// <summary>
46  /// Minimal possible movement for current parameter, should be positive
47  /// </summary>
48  /// <remarks>Used by <see cref="Strategies.EulerSearchOptimizationStrategy"/> to determine when this parameter can no longer be optimized</remarks>
49  [JsonProperty("minStep")]
50  #pragma warning restore CS1574
51  public decimal? MinStep { get; set; }
52 
53  /// <summary>
54  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
55  /// </summary>
56  /// <param name="name">parameter name</param>
57  /// <param name="min">minimal value</param>
58  /// <param name="max">maximal value</param>
59  public OptimizationStepParameter(string name, decimal min, decimal max)
60  : base(name)
61  {
62  if (min > max)
63  {
64  throw new ArgumentException(Messages.OptimizationStepParameter.InvalidStepRange(min, max));
65  }
66 
67  MinValue = min;
68  MaxValue = max;
69  }
70 
71  /// <summary>
72  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
73  /// </summary>
74  /// <param name="name">parameter name</param>
75  /// <param name="min">minimal value</param>
76  /// <param name="max">maximal value</param>
77  /// <param name="step">movement</param>
78  public OptimizationStepParameter(string name, decimal min, decimal max, decimal step)
79  : this(name, min, max, step, step)
80  {
81 
82  }
83 
84  /// <summary>
85  /// Create an instance of <see cref="OptimizationParameter"/> based on configuration
86  /// </summary>
87  /// <param name="name">parameter name</param>
88  /// <param name="min">minimal value</param>
89  /// <param name="max">maximal value</param>
90  /// <param name="step">movement</param>
91  /// <param name="minStep">minimal possible movement</param>
92  public OptimizationStepParameter(string name, decimal min, decimal max, decimal step, decimal minStep) : this(name, min, max)
93  {
94  // with zero step algorithm can go to infinite loop, use default step value
95  if (step <= 0)
96  {
97  throw new ArgumentException(Messages.OptimizationStepParameter.NonPositiveStepValue(nameof(step), step));
98  }
99 
100  // EulerSearch algorithm can go to infinite range division if Min step is not provided, use Step as default
101  if (minStep <= 0)
102  {
103  throw new ArgumentException(Messages.OptimizationStepParameter.NonPositiveStepValue(nameof(minStep), minStep));
104  }
105 
106  if (step < minStep)
107  {
108  throw new ArgumentException(Messages.OptimizationStepParameter.StepLessThanMinStep);
109  }
110 
111  Step = step;
112  MinStep = minStep;
113  }
114  }
115 }