Custom Indicator

Created at 04 Sep 2016, 22:51
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
HE

hedgehog

Joined 24.04.2016

Custom Indicator
04 Sep 2016, 22:51


Could you help me please with my custom indicator... I would  like to compile two "buffer" line in one window with own rules for up buffer and down buffer but without success. I dont know how to set up rules or variables. Thanks.

 

RSI with Momentum Osc is Rule ​1, ADX is Rule 2, here is my code:

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

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIMomentum : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("RSI Periods", DefaultValue = 5)]
        public int RSIPeriods { get; set; }

        [Parameter("Momentum Periods", DefaultValue = 10)]
        public int MomentumPeriods { get; set; }

        [Parameter("DMI Periods", DefaultValue = 8)]
        public int DMIPeriods { get; set; }

        [Parameter("Tresh", DefaultValue = 25)]
        public int Tresh { get; set; }

        [Output("RSI", LineStyle = LineStyle.Solid, Thickness = 1, Color = Colors.Orange)]
        public IndicatorDataSeries RSI { get; set; }

        [Output("Momentum", LineStyle = LineStyle.Solid, Thickness = 1, Color = Colors.Green)]
        public IndicatorDataSeries Momentum { get; set; }

        [Output("Momentum Buffer", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Gray)]
        public IndicatorDataSeries MomentumBuffer { get; set; }

        [Output("DMI Buffer", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Gray)]
        public IndicatorDataSeries DMIBuffer { get; set; }

        [Output("Up", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Green)]
        public IndicatorDataSeries Up { get; set; }

        [Output("Down", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Orange)]
        public IndicatorDataSeries Down { get; set; }

        private RelativeStrengthIndex _rsi;
        private MomentumOscillator _momentum;
        private DirectionalMovementSystem _adx;

        protected override void Initialize()
        {
            _rsi = Indicators.RelativeStrengthIndex(Source, RSIPeriods);
            _momentum = Indicators.MomentumOscillator(Source, MomentumPeriods);
            _adx = Indicators.DirectionalMovementSystem(DMIPeriods);
        }

        public override void Calculate(int index)
        {
            RSI[index] = _rsi.Result[index] - 50;
            Momentum[index] = 10 * _momentum.Result[index] - 1000;

            double mb = -75;
            double db = -85;

            MomentumBuffer[index] = mb;
            DMIBuffer[index] = db;

            if ((RSI[index] > 0) && (Momentum[index] > 0))
            {
                Up[index] = mb;
                MomentumBuffer[index] = double.NaN;
            }
            else if ((RSI[index] < 0) && (Momentum[index] < 0))
            {
                Down[index] = mb;
                MomentumBuffer[index] = double.NaN;
            }

            double dmplus = _adx.DIPlus[index];
            double dmminus = _adx.DIMinus[index];
            double dmi = dmplus - dmminus;

            if (_adx.ADX.Last(0) > Tresh)
            {
                if (dmi > 0)
                {
                    Up[index] = db;
                    DMIBuffer[index] = double.NaN;
                }
                else if (dmi < 0)
                {
                    Down[index] = db;
                    DMIBuffer[index] = double.NaN;
                }
            }
        }
    }
}

 


@hedgehog
Replies

hedgehog
05 Sep 2016, 11:12

I created this way:

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

namespace cAlgo
{   [Levels(50)]
    [Indicator(IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIMomentum1 : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("RSI Periods", DefaultValue = 5)]
        public int RSIPeriods { get; set; }

        [Parameter("Momentum Periods", DefaultValue = 10)]
        public int MomentumPeriods { get; set; }

        [Parameter("DMI Periods", DefaultValue = 8)]
        public int DMIPeriods { get; set; }

        [Parameter("Tresh", DefaultValue = 25)]
        public int Tresh { get; set; }

        [Output("RSI", LineStyle = LineStyle.Solid, Thickness = 1, Color = Colors.Orange)]
        public IndicatorDataSeries RSI { get; set; }

        [Output("Momentum", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Gray)]
        public IndicatorDataSeries MomentumBuffer { get; set; }

        [Output("Momentum Up", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Green)]
        public IndicatorDataSeries MomentumUp { get; set; }

        [Output("Momentum Down", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Orange)]
        public IndicatorDataSeries MomentumDown { get; set; }

        [Output("DMI", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Gray)]
        public IndicatorDataSeries DMIBuffer { get; set; }

        [Output("DMI Up", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Green)]
        public IndicatorDataSeries DMIUp { get; set; }

        [Output("DMI Down", PlotType = PlotType.Points, Thickness = 4, Color = Colors.Orange)]
        public IndicatorDataSeries DMIDown { get; set; }

        private RelativeStrengthIndex _rsi;
        private DirectionalMovementSystem _adx;
        private MomentumOscillator _momentum;

        protected override void Initialize()
        {
            _rsi = Indicators.RelativeStrengthIndex(Source, RSIPeriods);
            _adx = Indicators.DirectionalMovementSystem(DMIPeriods);
            _momentum = Indicators.MomentumOscillator(Source, MomentumPeriods);
        }

        public override void Calculate(int index)
        {
            RSI[index] = _rsi.Result[index];
            double momentum = _momentum.Result[index];

            if ((RSI[index] > 50) && (momentum > 100))
            {
                MomentumUp[index] = -5;
                MomentumDown[index] = double.NaN;
                MomentumBuffer[index] = double.NaN;
            }
            else if ((RSI[index] < 50) && (momentum < 100))
            {
                MomentumDown[index] = -5;
                MomentumUp[index] = double.NaN;
                MomentumBuffer[index] = double.NaN;
            }
            else
            {
                MomentumDown[index] = double.NaN;
                MomentumUp[index] = double.NaN;
                MomentumBuffer[index] = -5;
            }

            double dmplus = _adx.DIPlus[index];
            double dmminus = _adx.DIMinus[index];
            double dmi = dmplus - dmminus;

            if (_adx.ADX.Last(0) > Tresh)
            {
                if (dmi > 0)
                {
                    DMIUp[index] = -15;
                }
                else if (dmi < 0)
                {
                    DMIDown[index] = -15;
                }
            }
            else
            {
                DMIBuffer[index] = -15;
            }
        }
    }
}

 


@hedgehog