Help to finish an indicator, histogram not showing.

Created at 23 Mar 2022, 12:27
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!
SP

Sparkymark123

Joined 23.03.2022

Help to finish an indicator, histogram not showing.
23 Mar 2022, 12:27


I am new to c# and ctrader and have been trying to convert an indicator from prorealtime.

I have managed to create an indicator which shows two seperate emas on a chart and am struggling to add a histogram to the bottom based on the two emas.

 

In Prorealcode:

variables for ema (1); bars (1)

a=ExponentialAverage[ema](MedianPrice)
b=ExponentialAverage[ema](MedianPrice)
 
 
c=(a-b[bars])
 
p=0
 
 
return c as "histogram", p as "zero"

 

 

 

If anybody can give me some pointers or help in the final rearrangement of the coding to display this it would be greatly appreciated. I apologise for bad coding in advance, there are bits which wont make sense in here but it builds fine!!:

 

 

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

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

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

        [Parameter("Source1")]
        public DataSeries Source1 { get; set; }

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

        [Output("periods", LineColor = "Turquoise")]

        public IndicatorDataSeries Result { get; set; }

        [Output("periods1", LineColor = "green")]

        public IndicatorDataSeries Result1 { get; set; }

        [Output("Gradiant", LineColor = "orange", PlotType = PlotType.Histogram)]

        public IndicatorDataSeries Result3 { get; set; }

        private double exp;
        private double exp1;
        private double exp3;

        protected override void Initialize()
        {
            exp = 2.0 / (Periods + 1);
            exp1 = 2.0 / (Periods1 + 1);
            exp3 = 1;
        }

        public override void Calculate(int index)
        {
            var previousValue = Result[index - 1];

            var previousValue1 = Result1[index - 1];

            var Gradiant = Result3[index - 0];


            if (double.IsNaN(previousValue))
            {
                Result[index] = Source[index];

                Result1[index] = Source[index];
                
                Result3[index] = Source[index];
            }
            else
            {
                Result[index] = Source[index] * exp + previousValue * (1 - exp);

                Result1[index] = Source[index] * exp1 + previousValue1 * (1 - exp1);

                Result3[index] = Result[index] - Result1[index] + (1 - exp3);

            }
        }
    }
}


@Sparkymark123
Replies

amusleh
24 Mar 2022, 09:24 ( Updated at: 24 Mar 2022, 09:25 )

Hi,

I'm not familiar with ProRealTime code, but I think the cTrader Automate equivalent of your posted code will look something like this:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None), Levels(0)]
    public class EMAGradient : Indicator
    {
        private ExponentialMovingAverage _ema;

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

        [Parameter("Bars #", DefaultValue = 1)]
        public int BarsNumber { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Main", IsHistogram = true, PlotType = PlotType.Histogram, Thickness = 3)]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            _ema = Indicators.ExponentialMovingAverage(Source, Period);
        }

        public override void Calculate(int index)
        {
            Result[index] = _ema.Result[index] - _ema.Result[index - BarsNumber];
        }
    }
}

 


@amusleh

Sparkymark123
27 Mar 2022, 12:08

Thank you

Many thanks for your help with this.

Do you contract for paid work in creating cbots? I may need something creating once I have the design for it figured properly?

I also have another problem with some code not working correctly - I added a trailing stop and it works with backtest but then doesn`t place any trades...

 

I`ll put a post in the help section for that though.

 

Kind Regards.

 

 

 

 

amusleh said:

Hi,

I'm not familiar with ProRealTime code, but I think the cTrader Automate equivalent of your posted code will look something like this:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None), Levels(0)]
    public class EMAGradient : Indicator
    {
        private ExponentialMovingAverage _ema;

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

        [Parameter("Bars #", DefaultValue = 1)]
        public int BarsNumber { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Main", IsHistogram = true, PlotType = PlotType.Histogram, Thickness = 3)]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            _ema = Indicators.ExponentialMovingAverage(Source, Period);
        }

        public override void Calculate(int index)
        {
            Result[index] = _ema.Result[index] - _ema.Result[index - BarsNumber];
        }
    }
}

 

 


@Sparkymark123

amusleh
28 Mar 2022, 09:31

RE: Thank you

dionysian.apostle said:

Many thanks for your help with this.

Do you contract for paid work in creating cbots? I may need something creating once I have the design for it figured properly?

I also have another problem with some code not working correctly - I added a trailing stop and it works with backtest but then doesn`t place any trades...

 

I`ll put a post in the help section for that though.

 

Kind Regards.

 

 

 

 

amusleh said:

Hi,

I'm not familiar with ProRealTime code, but I think the cTrader Automate equivalent of your posted code will look something like this:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None), Levels(0)]
    public class EMAGradient : Indicator
    {
        private ExponentialMovingAverage _ema;

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

        [Parameter("Bars #", DefaultValue = 1)]
        public int BarsNumber { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Output("Main", IsHistogram = true, PlotType = PlotType.Histogram, Thickness = 3)]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            _ema = Indicators.ExponentialMovingAverage(Source, Period);
        }

        public override void Calculate(int index)
        {
            Result[index] = _ema.Result[index] - _ema.Result[index - BarsNumber];
        }
    }
}

 

 

Hi,

You can post a job request or contact one of our consultants.


@amusleh