Is there a way to calculate Average True RealBody with Exponential Moving Average, like we calculate ATR? Similar to:
_atr = ATR(_symbol, 8, MovingAverageType.Exponential);
QUANTCONNECT COMMUNITY
Is there a way to calculate Average True RealBody with Exponential Moving Average, like we calculate ATR? Similar to:
_atr = ATR(_symbol, 8, MovingAverageType.Exponential);
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Alexandre Catarino
We can choose the MovingAverageType of the ATR:
_atrDefault = ATR("SPY", 8); _atrExponential = ATR("SPY", 8, MovingAverageType.Exponential);
In the attached project, we have plotted these two ATR to compare:
Boris Sachakov
But the above calculations apply to Average True Range only, where Range is from High to Low, isn't it?
Is there anything similar to that, but instead of Range, it has RealBody (from Open to Close) ?
Thanks
Alexandre Catarino
I misundestood your question, sorry.
There is nothing similar to that. It means you need to create your own indicator.
Please read the tutorial: How Do I Create an Indicator?
As a starting point, you can look at the Average True Range implementation here and adapt the ComputeTrueRange method:
public static decimal ComputeTrueRange( IBaseDataBar previous, IBaseDataBar current) { var range1 = current.High - current.Low; if (previous == null) { return range1; } var range2 = Math.Abs(current.High - previous.Close); var range3 = Math.Abs(current.Low - previous.Close); return Math.Max(range1, Math.Max(range2, range3)); }
with the Average True RealBody formula.
Boris Sachakov
Thanks for your help
Boris Sachakov
@Alexandre,
I followed your recommendation above and modified Average True Range implementation with Average RealBody Range formulas. It looks as following:
using System; using QuantConnect.Data.Market; namespace QuantConnect.Indicators { /// <summary> /// The Average_RB_Range indicator is a measure of volatility introduced by Welles Wilder in his /// book: New Concepts in Technical Trading Systems. This indicator computes the RB_Range and then /// smoothes the RB_Range over a given period. /// /// RB_Range is defined as the maximum of the following: /// (+)CurrentClose - CurrentOpen; /// ABS(CurrentOpen - PreviousClose); /// ABS(CurrentClose - PreviousClose) /// </summary> public class Average_RB_Range : BarIndicator { /// <summary>This indicator is used to smooth the RB_Range computation</summary> /// <remarks>This is not exposed publicly since it is the same value as this indicator, meaning /// that this '_smoother' computers the ARBR directly, so exposing it publicly would be duplication</remarks> private readonly IndicatorBase<IndicatorDataPoint> _smoother; /// <summary> /// Gets the RB range which is the more volatile calculation to be smoothed by this indicator /// </summary> public IndicatorBase<IBaseDataBar> RB_Range { get; private set; } /// <summary> /// Gets a flag indicating when this indicator is ready and fully initialized /// </summary> public override bool IsReady { get { return _smoother.IsReady; } } /// <summary> /// Creates a new Average_RB_Range indicator using the specified period and moving average type /// </summary> /// <param name="name">The name of this indicator</param> /// <param name="period">The smoothing period used to smooth the RB range values</param> /// <param name="movingAverageType">The type of smoothing used to smooth the RB range values</param> public Average_RB_Range(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Wilders) : base(name) { _smoother = movingAverageType.AsIndicator(string.Format("{0}_{1}", name, movingAverageType), period); IBaseDataBar previous = null; RB_Range = new FunctionalIndicator<IBaseDataBar>(name + "_RB_Range", currentBar => { // in our ComputeNextValue function we'll just call the Compute_RB_Range var nextValue = Compute_RB_Range(previous, currentBar); previous = currentBar; return nextValue; } // in our IsReady function we just need at least one sample , RB_RangeIndicator => RB_RangeIndicator.Samples >= 1 ); } /// <summary> /// Creates a new AverageTrueRange indicator using the specified period and moving average type /// </summary> /// <param name="period">The smoothing period used to smooth the true range values</param> /// <param name="movingAverageType">The type of smoothing used to smooth the true range values</param> public Average_RB_Range(int period, MovingAverageType movingAverageType = MovingAverageType.Wilders) : this("ARBR" + period, period, movingAverageType) { } /// <summary> /// Computes the RB_Range from the current and previous trade bars /// /// RB_Range is defined as the maximum of the following: /// Close - Open; /// ABS(Close - PreviousClose); /// ABS(Open - PreviousClose); /// </summary> /// <param name="previous">The previous trade bar</param> /// <param name="current">The current trade bar</param> /// <returns>The RB range</returns> public static decimal Compute_RB_Range(IBaseDataBar previous, IBaseDataBar current) { var range1 = Math.Abs(current.Close - current.Open); if (previous == null) { return range1; } var range2 = Math.Abs(current.Open - previous.Close); var range3 = Math.Abs(current.Close - previous.Close); return Math.Max(range1, Math.Max(range2, range3)); } /// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(IBaseDataBar input) { // compute the true range and then send it to our smoother RB_Range.Update(input); _smoother.Update(input.Time, RB_Range); return _smoother.Current.Value; } /// <summary> /// Resets this indicator to its initial state /// </summary> public override void Reset() { _smoother.Reset(); RB_Range.Reset(); base.Reset(); } } }
However, I could't find a method to use it. ATR is able to be used with the following method:
private AverageTrueRange _atr; _atr = ATR(_symbol, 8, MovingAverageType.Exponential)
I, unfortunately, could not substitute ATR with corresponding acronym. I did rename ATR with ARBR in my version, but it could not be used. One explanation for this problem, I thought,was that ATR is one of QCalgorithm's public properties, and my creation ARBR is obviously not.
Do you think you can help me with this issue? Thanks in advance.
Alexandre Catarino
Method like ATR are API helpers. They basicly create a indicator and register them to receive updates. Alternatively, we would have to call indicator.Update in our code whenever conditions are met.
Here is how it is done for ATR:
public AverageTrueRange ATR( Symbol symbol, int period, MovingAverageType type = MovingAverageType.Simple, Resolution? resolution = null, Func<IBaseData, IBaseDataBar> selector = null) { string name = CreateIndicatorName( symbol, "ATR" + period, resolution); // Created an instance of the Indicator var atr = new AverageTrueRange(name, period, type); // Register the indicator to receive updates RegisterIndicator(symbol, atr, resolution, selector); return atr; }
Boris Sachakov
Thanks for your advice.
Boris Sachakov
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!