Category Trend  Published on 06/01/2023

TrailingStop Cycles indicator

Description

This indicator is used for positions trailing stop, and is calculated separately for long (green component) and short (red component) trades, based on price cycles.

 



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 mTrailingStopCycles : Indicator
    {
        [Parameter("ATR Periods (20)", DefaultValue = 20)]
        public int inpPeriods { get; set; }
        [Parameter("ShortCycle Bars (3)", DefaultValue = 3)]
        public int inpCycleShort { get; set; }
        [Parameter("LongCycle Bars (10)", DefaultValue = 10)]
        public int inpCycleLong { get; set; }

        [Output("TrailingStop Bullish", LineColor = "Green", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Dots, Thickness = 2)]
        public IndicatorDataSeries outTrailingStopBullish { get; set; }
        [Output("TrailingStop Bearish", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, LineStyle = LineStyle.Dots, Thickness = 2)]
        public IndicatorDataSeries outTrailingStopBearish { get; set; }
        
        private AverageTrueRange _atr;
        private IndicatorDataSeries _rawbullish, _rawbearish, _tsbullish, _tsbearish;
        

        protected override void Initialize()
        {
            _atr = Indicators.AverageTrueRange(inpPeriods, MovingAverageType.Simple);
            _rawbullish = CreateDataSeries();
            _rawbearish = CreateDataSeries();
            _tsbullish = CreateDataSeries();
            _tsbearish = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _rawbullish[i] = (i>inpCycleShort ? Bars.LowPrices.Minimum(inpCycleShort) : Bars.LowPrices[i]) + 1/3 * _atr.Result[i];
            _tsbullish[i] = i>inpCycleLong ? _rawbullish.Maximum(inpCycleLong) : _rawbullish[i];
            _rawbearish[i] = (i>inpCycleShort ? Bars.HighPrices.Maximum(inpCycleShort) : Bars.HighPrices[i]) + 1/3 * _atr.Result[i];
            _tsbearish[i] = i>inpCycleLong ? _rawbearish.Minimum(inpCycleLong) : _rawbearish[i];
            
            outTrailingStopBullish[i] = _tsbullish[i];
            outTrailingStopBearish[i] = _tsbearish[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTrailingStopCycles.algo
  • Rating: 5
  • Installs: 848
  • Modified: 06/01/2023 09:19
Comments
Log in to add a comment.
No comments found.