Category Other  Published on 19/09/2022

TVRVI custom trade trigger indicator

Description

This custom indicator is a combination of TrendValue (https://ctrader.com/algos/indicators/show/3070) indicator and RVI (Relative Vigor Index)

The TrendValue indicator are used to identify trading zones for long and short trades (green and red dots)

the RVI indicator is used to identify the trade StopLoss based in low and high indicator values (+0.2; -0.2)


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, ScalePrecision = 3)]
    public class mTVRVI : Indicator
    {
        [Parameter("Range Period (10)", DefaultValue = 10, MinValue = 1, Group = "Trend")]
        public int RangePeriod { get; set; }
        [Parameter("ATR Period (10)", DefaultValue = 10, MinValue = 1, Group = "Trend")]
        public int ATRPeriod { get; set; }
        [Parameter("ATR Sensitivity (1.618)", DefaultValue = 0.618, MinValue = 0.382, Group = "Trend")]
        public double ATRSensitivity { get; set; }
        [Parameter("Multiplayer (2.618)", DefaultValue = 2.618, MinValue = 0.382, Group = "Trend")]
        public double inpMultiplayer { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple, Group = "Trend")]
        public MovingAverageType MAType { get; set; }
        [Parameter("Periods RVI (1)", DefaultValue = 1, Group = "RVI")]
        public int inpPeriodRVI { get; set; }

        [Output("Trend Value", Thickness = 1, LineColor = "Gold")]
        public IndicatorDataSeries outTV { get; set; }
        [Output("OpenLong trigger", LineColor = "Green", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries outTVlongOpen { get; set; }
        [Output("OpenShort trigger", LineColor = "Red", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries outTVshortOpen { get; set; }
        [Output("OpenLong SL", LineColor = "Black", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries outTVlongSL { get; set; }
        [Output("OpenShort SL", LineColor = "Black", PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries outTVshortSL { get; set; }

        private MovingAverage _smah;
        private MovingAverage _smal;
        private AverageTrueRange _atr;
        private MovingAverage _maatr;
        private IndicatorDataSeries _tv;
        private IndicatorDataSeries _highSensivity;
        private IndicatorDataSeries _lowSensivity;
        private IndicatorDataSeries _buffer;
        private IndicatorDataSeries _openclose, _highlow, _rvi, _longopen, _shortopen, _longSL, _shortSL;


        protected override void Initialize()
        {
            _smah = Indicators.MovingAverage(Bars.HighPrices, RangePeriod, MAType);
            _smal = Indicators.MovingAverage(Bars.LowPrices, RangePeriod, MAType);
            _atr = Indicators.AverageTrueRange(ATRPeriod, MAType);
            _maatr = Indicators.MovingAverage(_atr.Result, 200, MovingAverageType.Simple);
            _tv = CreateDataSeries();
            _highSensivity = CreateDataSeries();
            _lowSensivity = CreateDataSeries();
            _buffer = CreateDataSeries();
            _openclose = CreateDataSeries();
            _highlow = CreateDataSeries();
            _rvi = CreateDataSeries();
            _longopen = CreateDataSeries();
            _shortopen = CreateDataSeries();
            _longSL = CreateDataSeries();
            _shortSL = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _highSensivity[i] = _smah.Result[i] * (1 + 0 / 100) + _atr.Result[i] * ATRSensitivity;
            _lowSensivity[i] = _smal.Result[i] * (1 - 0 / 100) - _atr.Result[i] * ATRSensitivity;

            if (i > 0)
            {
                _buffer[i] = _buffer[i - 1];

                if (Bars.ClosePrices[i] > _highSensivity[i - 1])
                    _buffer[i] = +1;
                if (Bars.ClosePrices[i] < _lowSensivity[i - 1])
                    _buffer[i] = -1;

                if (_buffer[i] > 0)
                {
                    if (_lowSensivity[i] < _lowSensivity[i - 1])
                        _lowSensivity[i] = _lowSensivity[i - 1];
                    _tv[i] = _lowSensivity[i];
                }
                if (_buffer[i] < 0)
                {
                    if (_highSensivity[i] > _highSensivity[i - 1])
                        _highSensivity[i] = _highSensivity[i - 1];
                    _tv[i] = _highSensivity[i];
                }
            }
            else
            {
                _buffer[i] = +1;
                _tv[i] = Bars.ClosePrices[i];
            }

            outTV[i] = _tv[i];
            /************************************************************************************/

            _openclose[i] = ((Bars.ClosePrices[i] - Bars.OpenPrices[i]) + 2 * (Bars.ClosePrices[i - 1] - Bars.OpenPrices[i - 1]) + 2 * (Bars.ClosePrices[i - 2] - Bars.OpenPrices[i - 2]) + (Bars.ClosePrices[i - 3] - Bars.OpenPrices[i - 3])) / 6;
            _highlow[i] = ((Bars.HighPrices[i] - Bars.LowPrices[i]) + 2 * (Bars.HighPrices[i - 1] - Bars.LowPrices[i - 1]) + 2 * (Bars.HighPrices[i - 2] - Bars.LowPrices[i - 2]) + (Bars.HighPrices[i - 3] - Bars.LowPrices[i - 3])) / 6;

            double num = 0;
            double denum = 0;
            for (int j = 0; j < inpPeriodRVI; j++)
            {
                num += _openclose[i - j];
                denum += _highlow[i - j];
            }
            _rvi[i] = num / denum;
            /************************************************************************************/

            _longopen[i] = double.NaN;
            _shortopen[i] = double.NaN;
            _longSL[i] = double.NaN;
            _shortSL[i] = double.NaN;

            if (_tv[i] < Bars.ClosePrices[i] && _rvi[i] > -0.2 && _rvi[i - 1] < -0.2)
            {
                _longopen[i] = _tv[i];

                int result = 0;
                for (int j = 0; j <= 100; j++)
                {
                    if (_rvi[i - j] < -0.2 && _rvi[i - j - 1] > -0.2)
                    {
                        result += 1;
                        break;
                    }
                    result += 1;
                }
                _longSL[i] = Bars.LowPrices.Minimum(result);
            }

            if (_tv[i] > Bars.ClosePrices[i] && _rvi[i] < +0.2 && _rvi[i - 1] > +0.2)
            {
                _shortopen[i] = _tv[i];

                int result = 0;
                for (int j = 0; j <= 100; j++)
                {
                    if (_rvi[i - j] > +0.2 && _rvi[i - j - 1] < +0.2)
                    {
                        result += 1;
                        break;
                    }
                    result += 1;
                }
                _shortSL[i] = Bars.HighPrices.Maximum(result);
            }


            outTVlongOpen[i] = _longopen[i];
            outTVshortOpen[i] = _shortopen[i];

            outTVlongSL[i] = _longSL[i];
            outTVshortSL[i] = _shortSL[i];
        }
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTVRVI.algo
  • Rating: 5
  • Installs: 1444
  • Modified: 19/09/2022 17:58
Comments
Log in to add a comment.
No comments found.