stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Average True Range
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2449
Page 1 of 1
Author:  babalu4u [ Wed Jun 05, 2013 9:40 pm ]
Post subject:  Average True Range

This indicator calculates average true range and must be installed to work properly the TMA Slope indicator.
Author:  babalu4u [ Wed Jun 05, 2013 9:40 pm ]
Post subject:  Re: Average True Range

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];
        }
    }
}
Author:  Caal [ Sun Jun 09, 2013 10:46 am ]
Post subject:  Re: Average True Range

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 :)
All times are UTC Page 1 of 1