Category Volatility  Published on 12/03/2023

ATR on Chart




/*
 
    Pine Script: https://www.tradingview.com/script/xx8zsjDW-ATR-on-Chart/
    Translated by cTrader Guru: https://ctrader.guru/
 
 */


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class ATRonChart : Indicator
    {

        [Parameter("Period", DefaultValue = 1)]
        public TimeFrame MyTF { get; set; }

        [Parameter("Period", DefaultValue = 20, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Output("Up", LineColor = "DodgerBlue", LineStyle = LineStyle.Solid)]
        public IndicatorDataSeries ResultUp { get; set; }

        [Output("Dw", LineColor = "Red", LineStyle = LineStyle.Solid)]
        public IndicatorDataSeries ResultDw { get; set; }


        private AverageTrueRange ATR;
        Bars MyBars;

        protected override void Initialize()
        {

            MyBars = MarketData.GetBars(MyTF);
            ATR = Indicators.AverageTrueRange(MyBars, Period, MaType);

        }

        public override void Calculate(int index)
        {
            int MyBarsIndex = MyBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            double a = ATR.Result[MyBarsIndex];

            ResultDw[index] = MyBars.OpenPrices[MyBarsIndex] - a;
            ResultUp[index] = MyBars.OpenPrices[MyBarsIndex] + a;
            
        }

    }

}


ctrader.guru's avatar
ctrader.guru

Joined on 19.07.2018

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Atr On Chart.algo
  • Rating: 5
  • Installs: 1052
Comments
Log in to add a comment.
No comments found.