|
| 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 QuantConnect.Data.Market; |
| 20 | + |
| 21 | +namespace QuantConnect.Indicators |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// The Least Squares Moving Average (LSMA) of a target in relation with a reference fits a least |
| 25 | + /// squares regression line of the target close prices on the reference close prices over the given |
| 26 | + /// period, instead of on the time index used by <see cref="LeastSquaresMovingAverage"/>. It then |
| 27 | + /// returns the value the regression line takes for the most recent reference price, which is the |
| 28 | + /// price the target is expected to have given where the reference is trading. |
| 29 | + /// |
| 30 | + /// It is common practice to use the SPX index as the reference, so that the indicator describes |
| 31 | + /// the target price in terms of the overall market level. |
| 32 | + /// |
| 33 | + /// The indicator only updates when both assets have a price for a time step. When a bar is missing |
| 34 | + /// for one of the assets, the indicator value fills forward to improve the accuracy of the indicator. |
| 35 | + /// </summary> |
| 36 | + public class LeastSquaresMovingAverageWithReference : DualSymbolIndicator<IBaseDataBar> |
| 37 | + { |
| 38 | + /// <summary> |
| 39 | + /// The point where the regression line crosses the y-axis (target price axis) |
| 40 | + /// </summary> |
| 41 | + public IndicatorBase<IndicatorDataPoint> Intercept { get; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The regression line slope, the target price change per unit of reference price change |
| 45 | + /// </summary> |
| 46 | + public IndicatorBase<IndicatorDataPoint> Slope { get; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Creates a new LeastSquaresMovingAverageWithReference indicator with the specified name, |
| 50 | + /// target, reference and period values |
| 51 | + /// </summary> |
| 52 | + /// <param name="name">The name of this indicator</param> |
| 53 | + /// <param name="targetSymbol">The target symbol of this indicator</param> |
| 54 | + /// <param name="referenceSymbol">The reference symbol of this indicator</param> |
| 55 | + /// <param name="period">The period of this indicator</param> |
| 56 | + public LeastSquaresMovingAverageWithReference(string name, Symbol targetSymbol, Symbol referenceSymbol, int period) |
| 57 | + : base(name, targetSymbol, referenceSymbol, period) |
| 58 | + { |
| 59 | + // Assert the period is greater than one, otherwise the regression line can not be fitted |
| 60 | + if (period < 2) |
| 61 | + { |
| 62 | + throw new ArgumentException($"Period parameter for LeastSquaresMovingAverageWithReference indicator must be greater than 1 but was {period}."); |
| 63 | + } |
| 64 | + |
| 65 | + Intercept = new Identity(name + "_Intercept"); |
| 66 | + Slope = new Identity(name + "_Slope"); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Creates a new LeastSquaresMovingAverageWithReference indicator with the specified target, |
| 71 | + /// reference and period values |
| 72 | + /// </summary> |
| 73 | + /// <param name="targetSymbol">The target symbol of this indicator</param> |
| 74 | + /// <param name="referenceSymbol">The reference symbol of this indicator</param> |
| 75 | + /// <param name="period">The period of this indicator</param> |
| 76 | + public LeastSquaresMovingAverageWithReference(Symbol targetSymbol, Symbol referenceSymbol, int period) |
| 77 | + : this($"LSMA({period})", targetSymbol, referenceSymbol, period) |
| 78 | + { |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Computes the value the regression line of the target on the reference takes for the |
| 83 | + /// most recent reference price |
| 84 | + /// </summary> |
| 85 | + protected override decimal ComputeIndicator() |
| 86 | + { |
| 87 | + // Until both windows are full, the indicator returns the target price, like the LSMA does |
| 88 | + if (!IsReady) |
| 89 | + { |
| 90 | + return TargetDataPoints[0].Close; |
| 91 | + } |
| 92 | + |
| 93 | + // Both windows only hold the data points of the time steps both symbols have a price for, |
| 94 | + // so the target and the reference prices pair up by index |
| 95 | + var referencePrices = ReferenceDataPoints.Select(x => (double)x.Close).ToArray(); |
| 96 | + var targetPrices = TargetDataPoints.Select(x => (double)x.Close).ToArray(); |
| 97 | + var (intercept, slope) = Fit.Line(x: referencePrices, y: targetPrices); |
| 98 | + |
| 99 | + // The regression line is undefined when the reference price does not change over the period |
| 100 | + if (intercept.IsNaNOrInfinity() || slope.IsNaNOrInfinity()) |
| 101 | + { |
| 102 | + return TargetDataPoints[0].Close; |
| 103 | + } |
| 104 | + |
| 105 | + var endTime = TargetDataPoints[0].EndTime; |
| 106 | + Intercept.Update(endTime, intercept.SafeDecimalCast()); |
| 107 | + Slope.Update(endTime, slope.SafeDecimalCast()); |
| 108 | + |
| 109 | + return Intercept.Current.Value + Slope.Current.Value * ReferenceDataPoints[0].Close; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Resets this indicator and all sub-indicators (Intercept, Slope) |
| 114 | + /// </summary> |
| 115 | + public override void Reset() |
| 116 | + { |
| 117 | + Intercept.Reset(); |
| 118 | + Slope.Reset(); |
| 119 | + base.Reset(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments