ATR on Chart free

by ctrader.guru in category Volatility at 12/03/2023
Description
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader

/*
 
    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;
            
        }

    }

}
Comments
0