|
| 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.Market; |
| 18 | + |
| 19 | +namespace QuantConnect.Indicators |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Represents the WaveTrend Oscillator (WTO) developed by LazyBear. |
| 23 | + /// |
| 24 | + /// The oscillator uses the typical price (HLC3) and is computed as follows: |
| 25 | + /// ESA = EMA(HLC3, channelPeriod) |
| 26 | + /// D = EMA(|HLC3 - ESA|, channelPeriod) |
| 27 | + /// CI = (HLC3 - ESA) / (0.015 * D) |
| 28 | + /// WT1 = EMA(CI, averagePeriod) |
| 29 | + /// WT2 = SMA(WT1, signalPeriod) |
| 30 | + /// |
| 31 | + /// The indicator's current value is WT1 (the main wave trend line). |
| 32 | + /// The <see cref="Signal"/> property exposes WT2, which is typically plotted as the signal line. |
| 33 | + /// </summary> |
| 34 | + public class WaveTrendOscillator : BarIndicator, IIndicatorWarmUpPeriodProvider |
| 35 | + { |
| 36 | + /// <summary> |
| 37 | + /// Constant used to scale the channel index so most WTO values fall within +/- 100. |
| 38 | + /// </summary> |
| 39 | + private const decimal K = 0.015m; |
| 40 | + |
| 41 | + private readonly ExponentialMovingAverage _channelEma; |
| 42 | + private readonly ExponentialMovingAverage _meanDeviationEma; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the main wave trend line (WT1), computed as an EMA of the channel index. |
| 46 | + /// </summary> |
| 47 | + public ExponentialMovingAverage WaveTrend { get; } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the signal line (WT2), computed as an SMA of <see cref="WaveTrend"/>. |
| 51 | + /// </summary> |
| 52 | + public SimpleMovingAverage Signal { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Initializes a new instance of the <see cref="WaveTrendOscillator"/> class. |
| 56 | + /// </summary> |
| 57 | + /// <param name="name">The name of this indicator</param> |
| 58 | + /// <param name="channelPeriod">The period used to smooth the typical price and its mean deviation (n1)</param> |
| 59 | + /// <param name="averagePeriod">The period used to smooth the channel index into the main wave trend line (n2)</param> |
| 60 | + /// <param name="signalPeriod">The period used to smooth the wave trend line into the signal line</param> |
| 61 | + public WaveTrendOscillator(string name, int channelPeriod, int averagePeriod, int signalPeriod) |
| 62 | + : base(name) |
| 63 | + { |
| 64 | + _channelEma = new ExponentialMovingAverage(name + "_ChannelEMA", channelPeriod); |
| 65 | + _meanDeviationEma = new ExponentialMovingAverage(name + "_MeanDeviationEMA", channelPeriod); |
| 66 | + WaveTrend = new ExponentialMovingAverage(name + "_WaveTrend", averagePeriod); |
| 67 | + Signal = new SimpleMovingAverage(name + "_Signal", signalPeriod); |
| 68 | + |
| 69 | + WarmUpPeriod = 2 * channelPeriod + averagePeriod + signalPeriod - 3; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Initializes a new instance of the <see cref="WaveTrendOscillator"/> class using default parameters (10, 21, 4). |
| 74 | + /// </summary> |
| 75 | + /// <param name="channelPeriod">The period used to smooth the typical price and its mean deviation (n1)</param> |
| 76 | + /// <param name="averagePeriod">The period used to smooth the channel index into the main wave trend line (n2)</param> |
| 77 | + /// <param name="signalPeriod">The period used to smooth the wave trend line into the signal line</param> |
| 78 | + public WaveTrendOscillator(int channelPeriod = 10, int averagePeriod = 21, int signalPeriod = 4) |
| 79 | + : this($"WTO({channelPeriod},{averagePeriod},{signalPeriod})", channelPeriod, averagePeriod, signalPeriod) |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets a flag indicating when this indicator is ready and fully initialized. |
| 85 | + /// </summary> |
| 86 | + public override bool IsReady => Signal.IsReady; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Required period, in data points, for the indicator to be ready and fully initialized. |
| 90 | + /// </summary> |
| 91 | + public int WarmUpPeriod { get; } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Computes the next value of this indicator from the given state. |
| 95 | + /// </summary> |
| 96 | + /// <param name="input">The input given to the indicator</param> |
| 97 | + /// <returns>The current value of the main wave trend line (WT1)</returns> |
| 98 | + protected override decimal ComputeNextValue(IBaseDataBar input) |
| 99 | + { |
| 100 | + var typicalPrice = (input.High + input.Low + input.Close) / 3.0m; |
| 101 | + |
| 102 | + _channelEma.Update(input.EndTime, typicalPrice); |
| 103 | + if (!_channelEma.IsReady) |
| 104 | + { |
| 105 | + return 0m; |
| 106 | + } |
| 107 | + |
| 108 | + var absDeviation = Math.Abs(typicalPrice - _channelEma.Current.Value); |
| 109 | + _meanDeviationEma.Update(input.EndTime, absDeviation); |
| 110 | + if (!_meanDeviationEma.IsReady) |
| 111 | + { |
| 112 | + return 0m; |
| 113 | + } |
| 114 | + |
| 115 | + var weightedMeanDeviation = K * _meanDeviationEma.Current.Value; |
| 116 | + if (weightedMeanDeviation == 0m) |
| 117 | + { |
| 118 | + return WaveTrend.Current.Value; |
| 119 | + } |
| 120 | + |
| 121 | + var channelIndex = (typicalPrice - _channelEma.Current.Value) / weightedMeanDeviation; |
| 122 | + WaveTrend.Update(input.EndTime, channelIndex); |
| 123 | + if (!WaveTrend.IsReady) |
| 124 | + { |
| 125 | + return WaveTrend.Current.Value; |
| 126 | + } |
| 127 | + |
| 128 | + Signal.Update(input.EndTime, WaveTrend.Current.Value); |
| 129 | + return WaveTrend.Current.Value; |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Resets this indicator to its initial state. |
| 134 | + /// </summary> |
| 135 | + public override void Reset() |
| 136 | + { |
| 137 | + _channelEma.Reset(); |
| 138 | + _meanDeviationEma.Reset(); |
| 139 | + WaveTrend.Reset(); |
| 140 | + Signal.Reset(); |
| 141 | + base.Reset(); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments