Category Trend  Published on 20/03/2023

TrendSignal indicator

Description

TrendSignal trend-following indicator specifies the change of a trend when Close price breaks through the specified range of bars upwards or downwards.

The Risk parameter allows defining the distance from the extreme price for this period, and it is defined it as a percentage of the range for this period.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class mTrendSignal : Indicator
    {
        [Parameter("Period (9)", DefaultValue = 9)]
        public int inpPeriod { get; set; }
        [Parameter("Risk (30)", DefaultValue = 30)]
        public double inpRisk { get; set; }

        [Output("TrendSignal OpenLong trigger", LineColor = "Green", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries outLongOpen { get; set; }
        [Output("TrendSignal OpenShort trigger", LineColor = "Red", PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries outShortOpen { get; set; }
        
        private IndicatorDataSeries _hh, _ll, _min, _max, _trend, _up, _down;
                

        protected override void Initialize()
        {
            _hh = CreateDataSeries();
            _ll = CreateDataSeries();
            _min = CreateDataSeries();
            _max = CreateDataSeries();
            _trend = CreateDataSeries();
            _up = CreateDataSeries();
            _down = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _hh[i] = Bars.HighPrices.Maximum(inpPeriod);
            _ll[i] = Bars.LowPrices.Minimum(inpPeriod);
            _max[i] = _hh[i] - (_hh[i] - _ll[i]) * inpRisk / 100.0;
            _min[i] = _ll[i] + (_hh[i] - _ll[i]) * inpRisk / 100.0;
            _up[i] = _down[i] = double.NaN;
            
            if(i>1 && Bars.ClosePrices[i-1] < _max[i-1] && Bars.ClosePrices[i] > _max[i-1] && _trend[i-1] != +1)
            {
                _trend[i] = +1;
                _up[i] = Bars.OpenPrices[i];
            }
            else
            {
                if(i>1 && Bars.ClosePrices[i-1] > _min[i-1] && Bars.ClosePrices[i] < _min[i-1] && _trend[i-1] != -1)
                {
                    _trend[i] = -1;
                    _down[i] = Bars.OpenPrices[i];
                }
                else
                    _trend[i] = (!double.IsNaN(_trend[i-1]) ? _trend[i-1] : 1);
            }
            
            outLongOpen[i] = !double.IsNaN(_up[i]) ? Bars.HighPrices[i] : _up[i];
            outShortOpen[i] = !double.IsNaN(_down[i]) ? Bars.LowPrices[i] : _down[i];
        }
    }
}

mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mTrendSignal.algo
  • Rating: 5
  • Installs: 863
Comments
Log in to add a comment.
VI
VietNam · 1 year ago

That would be great if there is a Cbot version available for testing. Thank you!

AN
anthonyvolkmann90 · 1 year ago

If you interested creative Lego creations or you collect own favorite Lego series, welcome to our Lego fan blog where you find latest news on Lego and more.

GO
gomezstevens1965408 · 1 year ago

It's a matter of hasty we become what we behold comments. Congratulations it worked.

GM
gmkenneyy · 1 year ago

My bad ....sincere apologies ...it works ...

GM
gmkenneyy · 1 year ago

Hello,

Thanks for all the great indicators (plus code) you uploaded. They've been very useful.

I'm afraid i cant say the same for this very indicator - it shows no signals on the XAUUSD H1 timeframe.

Are you able to look into it?

Regards

K