Lean  $LEAN_TAG$
Delay.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 
17 {
18  /// <summary>
19  /// An indicator that delays its input for a certain period
20  /// </summary>
21  public class Delay : WindowIndicator<IndicatorDataPoint>, IIndicatorWarmUpPeriodProvider
22  {
23  /// <summary>
24  /// Creates a new Delay indicator that delays its input by the specified period
25  /// </summary>
26  /// <param name="period">The period to delay input, must be greater than zero</param>
27  public Delay(int period)
28  : this($"DELAY({period})", period)
29  {
30  }
31 
32  /// <summary>
33  /// Creates a new Delay indicator that delays its input by the specified period
34  /// </summary>
35  /// <param name="name">Name of the delay window indicator</param>
36  /// <param name="period">The period to delay input, must be greater than zero</param>
37  public Delay(string name, int period)
38  : base(name, period)
39  {
40  }
41 
42  /// <summary>
43  /// Gets a flag indicating when this indicator is ready and fully initialized
44  /// </summary>
45  public override bool IsReady => Samples > Period;
46 
47  /// <summary>
48  /// Required period, in data points, for the indicator to be ready and fully initialized.
49  /// </summary>
50  public int WarmUpPeriod => 1 + Period;
51 
52  /// <summary>
53  /// Computes the next value for this indicator from the given state.
54  /// </summary>
55  /// <param name="window">The window of data held in this indicator</param>
56  /// <param name="input">The input value to this indicator on this time step</param>
57  /// <returns>A new value for this indicator</returns>
59  {
60  if (!IsReady)
61  {
62  // grab the initial value until we're ready
63  return window[window.Count - 1].Value;
64  }
65 
66  return window.MostRecentlyRemoved.Value;
67  }
68  }
69 }