Lean  $LEAN_TAG$
OptimizationStepParameterEnumerator.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 System;
17 
19 {
20  /// <summary>
21  /// Enumerates all possible values for specific optimization parameter
22  /// </summary>
23  public class OptimizationStepParameterEnumerator : OptimizationParameterEnumerator<OptimizationStepParameter>
24  {
25  /// <summary>
26  /// Creates an instance of <see cref="OptimizationStepParameterEnumerator"/>
27  /// </summary>
28  /// <param name="optimizationParameter">Step-based optimization parameter</param>
29  public OptimizationStepParameterEnumerator(OptimizationStepParameter optimizationParameter) : base(optimizationParameter)
30  {
31  }
32 
33  /// <summary>
34  /// Gets the element in the collection at the current position of the enumerator.
35  /// </summary>
36  /// <returns>The element in the collection at the current position of the enumerator.</returns>
37  public override string Current
38  {
39  get
40  {
41  var value = OptimizationParameter.MinValue + Index * OptimizationParameter.Step;
42  if (Index == -1 || value > OptimizationParameter.MaxValue)
43  throw new InvalidOperationException();
44 
45  return value.ToStringInvariant();
46  }
47  }
48 
49  /// <summary>
50  /// Advances the enumerator to the next element of the collection.
51  /// </summary>
52  /// <returns> true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
53  /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>
54  public override bool MoveNext()
55  {
56  var value = OptimizationParameter.MinValue + (Index + 1) * OptimizationParameter.Step;
57  if (value <= OptimizationParameter.MaxValue)
58  {
59  Index++;
60  return true;
61  }
62 
63  return false;
64  }
65  }
66 }