Average True Range

Post your indicators here
Post Reply
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Average True Range

Post by babalu4u »

This indicator calculates average true range and must be installed to work properly the TMA Slope indicator.
You do not have the required permissions to view the files attached to this post.
User avatar
babalu4u
Trader
Posts: 173
Joined: Fri Apr 13, 2012 6:52 pm
Location: Slovenia - EU

Re: Average True Range

Post by babalu4u »

And code:

Code: Select all

// -------------------------------------------------------------------------------
//
//    Average True Range
//
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, ScalePrecision = 5)]
    public class AverageTrueRange : Indicator
    {
        [Parameter("Period", DefaultValue = 14)]
        public int Period { get; set; }


        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private ExponentialMovingAverage _ema;
        private IndicatorDataSeries _tempBuffer;

        protected override void Initialize()
        {
            _tempBuffer = CreateDataSeries();
            _ema = Indicators.ExponentialMovingAverage(_tempBuffer, Period);
        }

        public override void Calculate(int index)
        {            
            double high = MarketSeries.High[index];
            double low = MarketSeries.Low[index];
            
            if (index == 0)
            {
                _tempBuffer[index] = high - low;
            }
            else
            {
                double prevClose = MarketSeries.Close[index - 1];
                _tempBuffer[index] = Math.Max(high, prevClose) - Math.Min(low, prevClose);
            }

            Result[index] = _ema.Result[index];
        }
    }
}
Caal
Trader
Posts: 10
Joined: Wed Dec 07, 2011 8:41 pm

Re: Average True Range

Post by Caal »

babalu4u wrote:And code:

Code: Select all

// -------------------------------------------------------------------------------
//
//    Average True Range
//
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, ScalePrecision = 5)]
    public class AverageTrueRange : Indicator
    {
        [Parameter("Period", DefaultValue = 14)]
        public int Period { get; set; }


        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private ExponentialMovingAverage _ema;
        private IndicatorDataSeries _tempBuffer;

        protected override void Initialize()
        {
            _tempBuffer = CreateDataSeries();
            _ema = Indicators.ExponentialMovingAverage(_tempBuffer, Period);
        }

        public override void Calculate(int index)
        {            
            double high = MarketSeries.High[index];
            double low = MarketSeries.Low[index];
            
            if (index == 0)
            {
                _tempBuffer[index] = high - low;
            }
            else
            {
                double prevClose = MarketSeries.Close[index - 1];
                _tempBuffer[index] = Math.Max(high, prevClose) - Math.Min(low, prevClose);
            }

            Result[index] = _ema.Result[index];
        }
    }
}
Very nice job! Thanks for pioneering and sharing :)
Post Reply

Return to “Indicators”