Lean  $LEAN_TAG$
LeastSquaresMovingAverage.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 System.Linq;
18 using MathNet.Numerics;
19 using MathNet.Numerics.LinearAlgebra;
20 
22 {
23  /// <summary>
24  /// The Least Squares Moving Average (LSMA) first calculates a least squares regression line
25  /// over the preceding time periods, and then projects it forward to the current period. In
26  /// essence, it calculates what the value would be if the regression line continued.
27  /// Source: https://rtmath.net/assets/docs/finanalysis/html/b3fab79c-f4b2-40fb-8709-fdba43cdb363.htm
28  /// </summary>
30  {
31  /// <summary>
32  /// Array representing the time.
33  /// </summary>
34  private readonly double[] _t;
35 
36  /// <summary>
37  /// The point where the regression line crosses the y-axis (price-axis)
38  /// </summary>
40 
41  /// <summary>
42  /// The regression line slope
43  /// </summary>
45 
46  /// <summary>
47  /// Required period, in data points, for the indicator to be ready and fully initialized.
48  /// </summary>
49  public int WarmUpPeriod => Period;
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="LeastSquaresMovingAverage"/> class.
53  /// </summary>
54  /// <param name="name">The name of this indicator</param>
55  /// <param name="period">The number of data points to hold in the window</param>
56  public LeastSquaresMovingAverage(string name, int period)
57  : base(name, period)
58  {
59  _t = Vector<double>.Build.Dense(period, i => i + 1).ToArray();
60  Intercept = new Identity(name + "_Intercept");
61  Slope = new Identity(name + "_Slope");
62  }
63 
64  /// <summary>
65  /// Initializes a new instance of the <see cref="LeastSquaresMovingAverage"/> class.
66  /// </summary>
67  /// <param name="period">The number of data points to hold in the window.</param>
68  public LeastSquaresMovingAverage(int period)
69  : this($"LSMA({period})", period)
70  {
71  }
72 
73  /// <summary>
74  /// Computes the next value of this indicator from the given state
75  /// </summary>
76  /// <param name="window"></param>
77  /// <param name="input">The input given to the indicator</param>
78  /// <returns>
79  /// A new value for this indicator
80  /// </returns>
82  {
83  // Until the window is ready, the indicator returns the input value.
84  if (!window.IsReady) return input.Value;
85 
86  // Sort the window by time, convert the observations to double and transform it to an array
87  var series = window
88  .OrderBy(i => i.Time)
89  .Select(i => Convert.ToDouble(i.Value))
90  .ToArray();
91  // Fit OLS
92  var ols = Fit.Line(x: _t, y: series);
93  Intercept.Update(input.Time, (decimal)ols.Item1);
94  Slope.Update(input.Time, (decimal)ols.Item2);
95 
96  // Calculate the fitted value corresponding to the input
97  return Intercept.Current.Value + Slope.Current.Value * Period;
98  }
99 
100  /// <summary>
101  /// Resets this indicator and all sub-indicators (Intercept, Slope)
102  /// </summary>
103  public override void Reset()
104  {
105  Intercept.Reset();
106  Slope.Reset();
107  base.Reset();
108  }
109  }
110 }