Category Trend  Published on 17/02/2023

ATR Stop and Reverse indicator

Description

The ATR Stop and Reverse, is a very simple trading system used for scalping. The indicators used in this trading system basically focus on the stop and reverse concept, as in the parabolic SAR indicator .

In this version you can definate to use ATR or Fix for defination reverse Pips Zone.


using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mATRLevelStopReverse : Indicator
    {
        [Parameter("UseATRMode (yes)", DefaultValue = true)]
        public bool inpUseATRMode { get; set; }
        [Parameter("NonATRStopPips (40)",DefaultValue = 40)]
        public int inpNonATRStopPips { get; set; }
        [Parameter("ATRPeriod (9)", DefaultValue = 9)]
        public int inpATRPeriod { get; set; }
        [Parameter("ATRMultiplier (3.0)", DefaultValue = 3.0)]
        public int inpATRMultiplier { get; set; }
        [Parameter("ATRType (sma)", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType inpATRType { get; set; }
        [Parameter("ATRSmoothPeriod (1)", DefaultValue = 1)]
        public int inpATRSmoothPeriod { get; set; }
        [Parameter("ATRSmoothType (sma)", DefaultValue = MovingAverageType.WilderSmoothing)]
        public MovingAverageType inpATRSmoothType { get; set; }

        [Output("ATR Level Stop-Reverse", LineColor = "Magenta", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Lines, Thickness = 1)]
        public IndicatorDataSeries outLSR { get; set; }
        [Output("OpenLong trigger", LineColor = "Green", PlotType = PlotType.Points, LineStyle = LineStyle.Dots, Thickness = 5)]
        public IndicatorDataSeries outLongOpen { get; set; }
        [Output("OpenShort trigger", LineColor = "Red", PlotType = PlotType.Points, LineStyle = LineStyle.Solid, Thickness = 5)]
        public IndicatorDataSeries outShortOpen { get; set; }
        
        private AverageTrueRange _atr;
        private MovingAverage _atrma;
        private IndicatorDataSeries _deltastop, _stoplevel, _arrowup, _arrowdown;
        

        protected override void Initialize()
        {
            _atr = Indicators.AverageTrueRange(inpATRPeriod, inpATRType);
            _atrma = Indicators.MovingAverage(_atr.Result, inpATRSmoothPeriod, inpATRSmoothType);
            _deltastop = CreateDataSeries();
            _stoplevel = CreateDataSeries();
            _arrowup = CreateDataSeries();
            _arrowdown = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _deltastop[i] = inpUseATRMode == true 
                    ? Math.Round(_atrma.Result[i] * inpATRMultiplier, Symbol.Digits) 
                    : Math.Round(inpNonATRStopPips * Symbol.PipSize, Symbol.Digits);

            _stoplevel[i] = Bars.ClosePrices[i];
            _arrowup[i] = double.NaN;
            _arrowdown[i] = double.NaN;
            
            if(i>1 && Bars.ClosePrices[i] == _stoplevel[i-1]) 
                _stoplevel[i] = _stoplevel[i-1];
            else 
            {
                if(i>1 && Bars.ClosePrices[i-1] <= _stoplevel[i-1] && Bars.ClosePrices[i] < _stoplevel[i-1])
                    _stoplevel[i] = Math.Min(_stoplevel[i-1], (Bars.ClosePrices[i] + _deltastop[i]));
                else 
                {
                    if(i>1 && Bars.ClosePrices[i-1] >= _stoplevel[i-1] && Bars.ClosePrices[i] > _stoplevel[i-1])
                        _stoplevel[i] = Math.Max(_stoplevel[i-1], (Bars.ClosePrices[i] - _deltastop[i]));
                    else 
                    {                        
                        if(i>1 && Bars.ClosePrices[i] > _stoplevel[i-1])
                        {
                            _stoplevel[i] = Bars.ClosePrices[i] - _deltastop[i];
                            _arrowup[i] = Bars.LowPrices[i] - (5 * Symbol.PipSize);
                        }
                        else 
                        {
                            _stoplevel[i] = Bars.ClosePrices[i] + _deltastop[i];
                            _arrowdown[i] = Bars.HighPrices[i] + (5 * Symbol.PipSize);
                        }
                    }
                }
            }
            
            outLSR[i] = _stoplevel[i];
            outLongOpen[i] = _arrowup[i];
            outShortOpen[i] = _arrowdown[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mATRLevelStopReverse.algo
  • Rating: 5
  • Installs: 2046
  • Modified: 17/02/2023 17:26
Comments
Log in to add a comment.
vanderlei-martinss's avatar
vanderlei-martinss · 8 months ago

PARABENS MUITO BONS SEUS INDICADORES, POREIA CRIAR ALTOMATIZAÇÃO PELO MENOS DOS STOPS AUTOMÁTICOS? 

vanderlei-martinss's avatar
vanderlei-martinss · 8 months ago

acharia bem legal se desse para automatizar o stop, ele sendo automático conduzido pela linha rosa.

VI
VietNam · 1 year ago

That would be great if there is a Cbot version available for testing. 
That's fantastic! I'm glad you found the information helpful. To add a pop-up notification for buy or sell signals on both the cAlgo platform and the cTrader app, you will need to modify the code to incorporate the notification feature

EL
elainaorne · 1 year ago

If you enjoy the version of you can play the full version here.

 

codefinger Five Nights at Freddy's,@ - February 20, 2023 @ 20:43

Thanks for this indi but how you was able to compile it?

This line:

_atrma = Indicators.CommodityChannelIndex(_atr.Result, inpATRSmoothPeriod, inpATRSmoothType);

Returns an error:

mATRLevelStopReverse.cs(42, 33): [CS1501] No overload for method 'CommodityChannelIndex' takes 3 arguments

NG

What happen?

mfejza's avatar
mfejza · 1 year ago

please anybody block this account (nguyenphongotjtiqatabto43): https://ctrader.com/users/profile/82116

NG

Looks like your line 40-42 is wrong.

CO
codefinger · 1 year ago

yeap, sorry for this mess. Probably by mistake, I've overridden it in my IDE and haven't noticed.

mfejza's avatar
mfejza · 1 year ago

@codefinger

mate, you are trying to replace smoothing ATR result, from MovingAverrage to CCI, and you can not, because CCI is calculated from 4 array components (bars data: open, high, low, close); ATR indicator return only a array of data

CO
codefinger · 1 year ago

Thanks for this indi but how you was able to compile it?

This line:

_atrma = Indicators.CommodityChannelIndex(_atr.Result, inpATRSmoothPeriod, inpATRSmoothType);

Returns an error:

mATRLevelStopReverse.cs(42, 33): [CS1501] No overload for method 'CommodityChannelIndex' takes 3 arguments

 

VE
VEI5S6C4OUNT0 · 1 year ago

Nice indicator. I should get you to fix my codes ha ha ha . keep up the great work.

Very Much appreciated.