Category Volatility  Published on 11/10/2013

ATR Stops

Description

ATR Stops provides trend following stops using average true range. 

Signals are used for entries/exits:

  • Exit a long position / enter short when price crosses below the stop line.
  • Exit your short position / enter long when price crosses above the stop line.

 


u

LI
LisaCarter

Joined on 10.10.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ATR Stops.algo
  • Rating: 0
  • Installs: 8863
Comments
Log in to add a comment.
o6h58zs's avatar
o6h58zs · 1 year ago

There appears to be an issue with the reporting of the values from the indicator. See my post [here]. Would you be able to help? ☺

SE
septumrar · 3 years ago

Please help me make a multi-timeframe. I tried like this. But it didn't work out.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
    [Indicator("ATR Trailing Stop", AutoRescale = false, IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ATRStops : Indicator
    {

        [Parameter("Timeframe", DefaultValue = "Ticks10")]
        public TimeFrame Timeframe { get; set; }

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

        [Parameter("Period", DefaultValue = 15, MinValue = 2, MaxValue = 50000)]
        public int Period { get; set; }

        [Parameter("Weight", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

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

        private AverageTrueRange_Cust_lag _atr;
        private bool _isLong;

        private MarketSeries _marketSeries;

        //private AverageTrueRange_Cust_lag[,] stop_ind = new ATRStops[perCount, weightCount];

        protected override void Initialize()
        {
            Print("1");
            // Result = CreateDataSeries();
            //ATR = CreateDataSeries();
            _marketSeries = MarketData.GetSeries(Timeframe);
            _atr = Indicators.GetIndicator<AverageTrueRange_Cust_lag>(_marketSeries, Period);
            // _atr = Indicators.AverageTrueRange(Period, MaType);
            Print("_marketSeries : {0}", _marketSeries);
            Print("_atr : {0}", _atr);


        }

        public override void Calculate(int index)
        {
            Print("2 Market: {0}", MarketSeries.OpenTime[index]);
            var barIndex = 0;


            if (_marketSeries.OpenTime[0] < MarketSeries.OpenTime[index])
                return;
            else

                barIndex = _marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);



            var currentAtr = Weight * _atr.Result[barIndex];

            if (double.IsNaN(currentAtr))
                return;
            // Print("IsNaN {0}", double.IsNaN(Result[barIndex - 1]));
            Print("3");

            if (double.IsNaN(Result[barIndex - 1]) && !double.IsNaN(_atr.Result[barIndex - 1]))
            {
                Print("4");
                var previousATR = Weight * _atr.Result[barIndex - 1];

                _isLong = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices.IsRising();

                var previous = UseHighAndLow ? (_isLong ? MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex - 1] : MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex - 1]) : MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex - 1];

                Result[barIndex] = _isLong ? previous - previousATR : previous + previousATR;
            }


            else
            {
                Print("5");
                //double a = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices.LastValue;
                //  double b = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices.LastValue;
                var current = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex];
                //   var current = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex];

                if (_isLong)
                {
                    Print("6");
                    if (current >= Result[barIndex - 1])
                    {
                        Print("7");
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex];
                        Print("8");
                        Result[barIndex] = Math.Max(Result[barIndex - 1], current - currentAtr);
                    }

                    else
                    {
                        Print("9");
                        _isLong = false;
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex];
                        Print("10");
                        Result[barIndex] = current + currentAtr;
                    }
                }
                else
                {
                    if (current <= Result[barIndex - 1])
                    {
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex];
                        Result[barIndex] = Math.Min(Result[barIndex - 1], current + currentAtr);
                    }
                    else
                    {
                        _isLong = true;
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex];
                        Result[barIndex] = current - currentAtr;
                    }
                }
            }
        }
    }
}

 

3xfx's avatar
3xfx · 8 years ago

I like to us eyor indicator to place my sl. is it possible to also add line of plus 2atr. Im assuming the other is minus atr. plz correct me if im wrong.