/*
* 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
{
///
/// Represents the Vortex Indicator, which identifies the start and continuation of market trends.
/// It includes components that capture positive (upward) and negative (downward) trend movements.
/// This indicator compares the ranges within the current period to previous periods to calculate
/// upward and downward movement trends.
///
public class Vortex : BarIndicator, IIndicatorWarmUpPeriodProvider
{
private readonly int _period;
private readonly AverageTrueRange _atr;
private readonly Sum _atrSum;
private readonly Sum _plusVMSum;
private readonly Sum _minusVMSum;
private IBaseDataBar _previousInput;
///
/// Gets the Positive Vortex Indicator, which reflects positive trend movements.
///
public IndicatorBase PlusVortex { get; private set; }
///
/// Gets the Negative Vortex Indicator, which reflects negative trend movements.
///
public IndicatorBase MinusVortex { get; private set; }
///
/// Indicates whether this indicator is fully ready and all buffers have been filled.
///
public override bool IsReady => Samples >= _period;
///
/// The minimum number of samples needed for the indicator to be ready and provide reliable values.
///
public int WarmUpPeriod => _period;
///
/// Initializes a new instance of the class using the specified period.
///
/// The number of periods used to construct the Vortex Indicator.
public Vortex(int period)
: this($"VTX({period})", period)
{
}
///
/// Initializes a new instance of the class with a custom name and period.
///
/// The custom name for this instance of the Vortex Indicator.
/// The number of periods used to construct the Vortex Indicator.
public Vortex(string name, int period)
: base(name)
{
_period = period;
_atr = new AverageTrueRange($"{Name}_ATR", 1, MovingAverageType.Simple);
_atrSum = new Sum("ATR_Sum", period).Of(_atr);
_plusVMSum = new Sum("PlusVM_Sum", period);
_minusVMSum = new Sum("MinusVM_Sum", period);
PlusVortex = _plusVMSum.Over(_atrSum);
MinusVortex = _minusVMSum.Over(_atrSum);
}
///
/// Computes the next value of the Vortex Indicator based on the provided input.
///
/// The input data used to compute the indicator value.
/// The computed value of the indicator.
protected override decimal ComputeNextValue(IBaseDataBar input)
{
_atr.Update(input);
if (_previousInput != null)
{
var plusVMValue = Math.Abs(input.High - _previousInput.Low);
var minusVMValue = Math.Abs(input.Low - _previousInput.High);
_plusVMSum.Update(input.EndTime, plusVMValue);
_minusVMSum.Update(input.EndTime, minusVMValue);
}
_previousInput = input;
if (!IsReady)
{
return 0;
}
return (PlusVortex.Current.Value + MinusVortex.Current.Value) / 2;
}
///
/// Resets all indicators and internal state.
///
public override void Reset()
{
base.Reset();
_atr.Reset();
_atrSum.Reset();
_plusVMSum.Reset();
_minusVMSum.Reset();
PlusVortex.Reset();
MinusVortex.Reset();
_previousInput = null;
}
}
}