Lean  $LEAN_TAG$
ConstantIndicator.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 using QuantConnect.Data;
18 
20 {
21  /// <summary>
22  /// An indicator that will always return the same value.
23  /// </summary>
24  /// <typeparam name="T">The type of input this indicator takes</typeparam>
25  public sealed class ConstantIndicator<T> : IndicatorBase<T>
26  where T : IBaseData
27  {
28  private readonly decimal _value;
29 
30  /// <summary>
31  /// Gets true since the ConstantIndicator is always ready to return the same value
32  /// </summary>
33  public override bool IsReady => true;
34 
35  /// <summary>
36  /// Creates a new ConstantIndicator that will always return the specified value
37  /// </summary>
38  /// <param name="name">The name of this indicator</param>
39  /// <param name="value">The constant value to be returned</param>
40  public ConstantIndicator(string name, decimal value)
41  : base(name)
42  {
43  _value = value;
44 
45  // set this immediately so it always has the .Value property correctly set,
46  // the time will be updated anytime this indicators Update method gets called.
47  Current = new IndicatorDataPoint(DateTime.MinValue, value);
48  }
49 
50  /// <summary>
51  /// Computes the next value of this indicator from the given state
52  /// </summary>
53  /// <param name="input">The input given to the indicator</param>
54  /// <returns>A new value for this indicator</returns>
55  protected override decimal ComputeNextValue(T input)
56  {
57  return _value;
58  }
59 
60  /// <summary>
61  /// Resets this indicator to its initial state
62  /// </summary>
63  public override void Reset()
64  {
65  base.Reset();
66 
67  // re-initialize the current value, constant should ALWAYS return this value
68  Current = new IndicatorDataPoint(DateTime.MinValue, _value);
69  }
70  }
71 }