Category Trend  Published on 06/08/2021

Chandelier Exit

Description

This is a volatility-based indicator which identifies stop loss exit points.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ChandelierExit : Indicator
    {
        [Parameter(DefaultValue = 22)]
        public int Period { get; set; }

        [Parameter(DefaultValue = 3)]
        public double AtrMultiplier { get; set; }

        [Output("Exit", PlotType = PlotType.Line, LineColor = "Red", Thickness = 2)]
        public IndicatorDataSeries Exit { get; set; }


        private AverageTrueRange _atr;
        private IndicatorDataSeries _longExit, _shortExit;
        private int _direction;

        protected override void Initialize()
        {
            _atr = Indicators.AverageTrueRange(Period, MovingAverageType.Exponential);

            _longExit = CreateDataSeries();
            _shortExit = CreateDataSeries();

            _direction = 0;
        }

        public override void Calculate(int index)
        {
            var adjustedAtr = _atr.Result[index] * AtrMultiplier;
            var longExit = Bars.HighPrices.Maximum(Period) - adjustedAtr;
            var shortExit = Bars.LowPrices.Minimum(Period) + adjustedAtr;
            var close = Bars.ClosePrices[index];
            var closePrev = Bars.ClosePrices[index - 1];

            _longExit[index] = double.IsNaN(_longExit[index - 1]) ? longExit : close < _longExit[index - 1] ? longExit : Math.Max(longExit, _longExit[index - 1]);
            _shortExit[index] = double.IsNaN(_shortExit[index - 1]) ? shortExit : close > _shortExit[index - 1] ? shortExit : Math.Min(shortExit, _shortExit[index - 1]);


            var shortSwitch = (close <= _longExit[index - 1] && closePrev > _longExit[index - 1]);
            var longSwtich = (close >= _shortExit[index - 1] && closePrev < _shortExit[index - 1]);

            _direction = _direction <= 0 && longSwtich ? 1 : _direction >= 0 && shortSwitch ? -1 : _direction;

            Exit[index] = _direction > 0 ? _longExit[index] : _shortExit[index];
        }
    }
}


CW
cW22Trader

Joined on 16.03.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Chandelier Exit.algo
  • Rating: 5
  • Installs: 1999
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
AB
abigailmorena · 1 year ago

 We maintain a customer-oriented culture. Everyone in our team is committed to the same goal – providing customers with papers of unmatched quality. By cooperating with us, you can improve your chances of the highest grade https://essays-writers.com/outline-writing-service.html

KE
kenneyy_eur · 3 years ago

Quite good!!! - i altered the code to create a stop loss channel and explicitly reference the channel top or bottom depending on my trade direction.