/*
* 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 QuantConnect.Data.Market;
namespace QuantConnect.Indicators
{
///
/// The Acceleration Bands created by Price Headley plots upper and lower envelope bands around a moving average.
///
///
public class AccelerationBands : IndicatorBase, IIndicatorWarmUpPeriodProvider
{
private readonly decimal _width;
///
/// Gets the type of moving average
///
public MovingAverageType MovingAverageType { get; }
///
/// Gets the middle acceleration band (moving average)
///
public IndicatorBase MiddleBand { get; }
///
/// Gets the upper acceleration band (High * ( 1 + Width * (High - Low) / (High + Low)))
///
public IndicatorBase UpperBand { get; }
///
/// Gets the lower acceleration band (Low * (1 - Width * (High - Low)/ (High + Low)))
///
public IndicatorBase LowerBand { get; }
///
/// Initializes a new instance of the class.
///
/// The name of this indicator.
/// The period of the three moving average (middle, upper and lower band).
/// A coefficient specifying the distance between the middle band and upper or lower bands.
/// Type of the moving average.
public AccelerationBands(string name, int period, decimal width,
MovingAverageType movingAverageType = MovingAverageType.Simple)
: base(name)
{
WarmUpPeriod = period;
_width = width;
MovingAverageType = movingAverageType;
MiddleBand = movingAverageType.AsIndicator(name + "_MiddleBand", period);
LowerBand = movingAverageType.AsIndicator(name + "_LowerBand", period);
UpperBand = movingAverageType.AsIndicator(name + "_UpperBand", period);
}
///
/// Initializes a new instance of the class.
///
/// The period of the three moving average (middle, upper and lower band).
/// A coefficient specifying the distance between the middle band and upper or lower bands.
public AccelerationBands(int period, decimal width)
: this($"ABANDS({period},{width})", period, width)
{
}
///
/// Initializes a new instance of the class.
///
/// The period of the three moving average (middle, upper and lower band).
public AccelerationBands(int period)
: this(period, 4)
{
}
///
/// Gets a flag indicating when this indicator is ready and fully initialized
///
public override bool IsReady => MiddleBand.IsReady && LowerBand.IsReady && UpperBand.IsReady;
///
/// Required period, in data points, for the indicator to be ready and fully initialized.
///
public int WarmUpPeriod { get; }
///
/// Resets this indicator to its initial state
///
public override void Reset()
{
base.Reset();
MiddleBand.Reset();
LowerBand.Reset();
UpperBand.Reset();
}
///
/// 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(IBaseDataBar input)
{
var coefficient = _width * (input.High - input.Low).SafeDivision(input.High + input.Low);
LowerBand.Update(input.EndTime, input.Low * (1 - coefficient));
UpperBand.Update(input.EndTime, input.High * (1 + coefficient));
MiddleBand.Update(input.EndTime, input.Close);
return MiddleBand.Current.Value;
}
}
}