/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using QuantConnect.Data.Market;
namespace QuantConnect.Indicators
{
///
/// The McClellan Oscillator is a market breadth indicator which was
/// developed by Sherman and Marian McClellan. It is based on the
/// difference between the number of advancing and declining periods.
///
public class McClellanOscillator : TradeBarIndicator, IIndicatorWarmUpPeriodProvider
{
private readonly IndicatorBase _averageDelta;
///
/// Fast period EMA of advance decline difference
///
public ExponentialMovingAverage EMAFast { get; }
///
/// Slow period EMA of advance decline difference
///
public ExponentialMovingAverage EMASlow { get; }
///
/// The number of advance assets minus the number of decline assets
///
public AdvanceDeclineDifference ADDifference { get; }
///
/// Gets a flag indicating when this indicator is ready and fully initialized
///
public override bool IsReady => EMASlow.IsReady;
///
/// Required period, in data points, for the indicator to be ready and fully initialized.
///
public int WarmUpPeriod => EMASlow.WarmUpPeriod + ADDifference.WarmUpPeriod;
///
/// Initializes a new instance of the class
/// The name of the indicator
/// The fast period of EMA of advance decline difference
/// The slow period of EMA of advance decline difference
///
public McClellanOscillator(string name, int fastPeriod = 19, int slowPeriod = 39) : base(name)
{
if (fastPeriod > slowPeriod)
{
throw new ArgumentException("fastPeriod must be less than slowPeriod.");
}
ADDifference = new AdvanceDeclineDifference("ADD");
EMAFast = ADDifference.EMA(fastPeriod);
EMASlow = ADDifference.EMA(slowPeriod);
_averageDelta = EMAFast.Minus(EMASlow);
}
///
/// Initializes a new instance of the class
/// The fast period of EMA of advance decline difference
/// The slow period of EMA of advance decline difference
///
public McClellanOscillator(int fastPeriod = 19, int slowPeriod = 39)
: this("McClellanOscillator", fastPeriod, slowPeriod)
{
}
///
/// Computes the next value of this indicator from the given state
///
/// The input given to the indicator
/// A new value for this indicator
protected override decimal ComputeNextValue(TradeBar input)
{
ADDifference.Update(input);
return _averageDelta.Current.Value;
}
///
/// Resets this indicator to its initial state
///
public override void Reset()
{
ADDifference.Reset();
EMAFast.Reset();
EMASlow.Reset();
_averageDelta.Reset();
base.Reset();
}
///
/// Add Tracking asset issue
///
/// the tracking asset issue
public void Add(Symbol asset)
{
ADDifference.Add(asset);
}
///
/// Remove Tracking asset issue
///
/// the tracking asset issue
public void Remove(Symbol asset)
{
ADDifference.Remove(asset);
}
}
}