Drawing an EMA on a custom indicator

Created at 10 Dec 2020, 20:13
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!
AM

amirus.stark

Joined 01.05.2020

Drawing an EMA on a custom indicator
10 Dec 2020, 20:13


Hi,

I'm trying to draw a 100EMA on an custom indicator, it acts weird, but when I take the EMA out of the code and then applly the ema on the indicator drawn on a chart it goes well.

 

the EMA I'm trying to draw is  "volumeup_ema = Indicators.ExponentialMovingAverage(VolUpDS, lengthEMA);"

 

thanks for any help you can come up with!

 

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class VolumeIndy : Indicator
    {

        //parameter
        [Parameter("Volume Retrace - % Backward Limit", DefaultValue = 50)]
        int volBackwardLimit { get; set; }

        [Parameter("Volume Retrace - % Forward Minimum", DefaultValue = 125)]
        int volForwardMinimum { get; set; }

        [Parameter("Volume SPIKE - % Spike Restriction", DefaultValue = 300)]
        int volSPIKERestriction { get; set; }

        [Parameter("Volume SPIKE - EMA Period", DefaultValue = 36)]
        int volSPIKEMA { get; set; }

        [Parameter("Volume EMA Period", DefaultValue = 100)]
        int lengthEMA { get; set; }

        [Parameter("WMA Period 1", DefaultValue = 20)]
        int WMA1 { get; set; }

        [Parameter("WMA Period 2", DefaultValue = 100)]
        int WMA2 { get; set; }

        [Parameter("WMA Period 3", DefaultValue = 400)]
        int WMA3 { get; set; }

        double goldenposition_validation;



        double topwick;
        double bottomwick;
        double body;
        double ohcl4;
        double fractionup;
        double fractiondown;
        double volumeup;
        double volumedown;
        double volumeup_perc;
        double volumedown_perc;
        bool goldenvol_up_check;
        bool goldenvol_up_backcheck;
        bool goldenvol_down_check;
        bool goldenvol_down_backcheck;
        bool goldenposition_up;
        bool goldenposition_down;
        double volSPIKE_up_high1;
        double volSPIKE_down_high1;

        WeightedMovingAverage WMA1p;
        WeightedMovingAverage WMA2p;
        WeightedMovingAverage WMA3p;

        ExponentialMovingAverage volumeup_ema;
        ExponentialMovingAverage volumedown_ema;

        ExponentialMovingAverage volSPIKE_up_ema;
        ExponentialMovingAverage volSPIKE_down_ema;
        [Output("VolUP")]
        public IndicatorDataSeries VolUpDS { get; set; }


        [Output("VolDown")]
        public IndicatorDataSeries VolDownDS { get; set; }

        [Output("VolDown Neg")]
        public IndicatorDataSeries VolDownDSNeg { get; set; }



        [Output("VolUp ema")]
        public IndicatorDataSeries VolUpEma { get; set; }


        [Output("Volumeup histogramme 1", PlotType = PlotType.Histogram, LineColor = "ForestGreen")]
        public IndicatorDataSeries VolUpHisto1 { get; set; }
        [Output("Volumeup histogramme 2", PlotType = PlotType.Histogram, LineColor = "LimeGreen")]
        public IndicatorDataSeries VolUpHisto2 { get; set; }







        protected override void Initialize()
        {


            volumeup_ema = Indicators.ExponentialMovingAverage(VolUpDS, lengthEMA);

            volumedown_ema = Indicators.ExponentialMovingAverage(VolDownDS, lengthEMA);

            volSPIKE_up_ema = Indicators.ExponentialMovingAverage(VolUpDS, volSPIKEMA);
            volSPIKE_down_ema = Indicators.ExponentialMovingAverage(VolDownDSNeg, volSPIKEMA);

            WMA1p = Indicators.WeightedMovingAverage(Bars.ClosePrices, WMA1);
            WMA2p = Indicators.WeightedMovingAverage(Bars.ClosePrices, WMA2);
            WMA3p = Indicators.WeightedMovingAverage(Bars.ClosePrices, WMA3);





        }



        public override void Calculate(int index)
        {


            goldenvol_up_check = false;
            goldenvol_up_backcheck = false;
            goldenvol_down_check = false;
            goldenvol_down_backcheck = false;
            goldenposition_up = false;
            goldenposition_down = false;

            goldenposition_validation = Bars.OpenPrices.LastValue;




            ohcl4 = (Bars.HighPrices.Last(1) + Bars.LowPrices.Last(1) + Bars.OpenPrices.Last(1) + Bars.ClosePrices.Last(1)) / 4;

            if (Bars.OpenPrices.Last(1) < Bars.ClosePrices.Last(1))
            {
                topwick = Bars.HighPrices.Last(1) - Bars.ClosePrices.Last(1);
                bottomwick = Bars.OpenPrices.Last(1) - Bars.LowPrices.Last(1);
                body = Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1);
                fractionup = (topwick + bottomwick + 2 * body) / (2 * topwick + 2 * bottomwick + 2 * body);
                fractiondown = (topwick + bottomwick) / (2 * topwick + 2 * bottomwick + 2 * body);

            }
            if (Bars.OpenPrices.Last(1) > Bars.ClosePrices.Last(1))
            {
                topwick = Bars.HighPrices.Last(1) - Bars.OpenPrices.Last(1);
                bottomwick = Bars.ClosePrices.Last(1) - Bars.LowPrices.Last(1);
                body = Bars.OpenPrices.Last(1) - Bars.ClosePrices.Last(1);
                fractionup = (topwick + bottomwick) / (2 * topwick + 2 * bottomwick + 2 * body);
                fractiondown = (topwick + bottomwick + 2 * body) / (2 * topwick + 2 * bottomwick + 2 * body);
            }



            volumeup = Bars.TickVolumes.LastValue * fractionup * ohcl4;
            VolUpDS[index] = volumeup;
            VolUpEma[index] = volumeup_ema.Result[index];






            volumedown = Bars.TickVolumes.LastValue * fractiondown * ohcl4;



            VolDownDS[index] = volumedown;
            VolDownDSNeg[index] = -volumedown;

            volumeup_perc = (volumeup / volumeup_ema.Result[index]) * 100;
            volumedown_perc = (volumedown / volumedown_ema.Result.LastValue) * 100;


            if ((volForwardMinimum <= volumeup_perc) && (volumeup_perc < volSPIKERestriction))
            {
                goldenvol_up_check = true;
            }

            if (goldenvol_up_check && (volumedown_perc <= volBackwardLimit))
            {

                goldenvol_up_backcheck = true;
            }

            if ((volForwardMinimum <= volumedown_perc) && (volumedown_perc < volSPIKERestriction))
            {
                goldenvol_down_check = true;
            }

            if (goldenvol_down_check && (volumeup_perc <= volBackwardLimit))
            {
                goldenvol_down_backcheck = true;
            }

            if (goldenposition_validation > WMA1p.Result.LastValue && WMA1p.Result.LastValue > WMA2p.Result.LastValue && WMA2p.Result.LastValue > WMA3p.Result.LastValue)
            {
                goldenposition_up = true;
            }

            if (goldenposition_validation < WMA1p.Result.LastValue && WMA1p.Result.LastValue < WMA2p.Result.LastValue && WMA2p.Result.LastValue < WMA3p.Result.LastValue)
            {
                goldenposition_down = true;
            }

            volSPIKE_up_high1 = (volSPIKERestriction / 100) * volSPIKE_up_ema.Result.LastValue;
            volSPIKE_down_high1 = (volSPIKERestriction / 100) * volSPIKE_down_ema.Result.LastValue;

            if (VolUpDS.Last(2) > VolUpDS.Last(1))
            {
                VolUpHisto1[index] = VolUpDS.LastValue;

            }
            if (!(VolUpDS.Last(2) > VolUpDS.Last(1)))
            {
                VolUpHisto2[index] = VolUpDS.LastValue;
            }

        }
    }
}


@amirus.stark
Replies

PanagiotisCharalampous
11 Dec 2020, 08:47

Hi amirus.stark,

You should make your parameters public

        //parameter
        [Parameter("Volume Retrace - % Backward Limit", DefaultValue = 50)]
        public int volBackwardLimit { get; set; }

        [Parameter("Volume Retrace - % Forward Minimum", DefaultValue = 125)]
        public int volForwardMinimum { get; set; }

        [Parameter("Volume SPIKE - % Spike Restriction", DefaultValue = 300)]
        public int volSPIKERestriction { get; set; }

        [Parameter("Volume SPIKE - EMA Period", DefaultValue = 36)]
        public int volSPIKEMA { get; set; }

        [Parameter("Volume EMA Period", DefaultValue = 100)]
        public int lengthEMA { get; set; }

        [Parameter("WMA Period 1", DefaultValue = 20)]
        public int WMA1 { get; set; }

        [Parameter("WMA Period 2", DefaultValue = 100)]
        public int WMA2 { get; set; }

        [Parameter("WMA Period 3", DefaultValue = 400)]
        public int WMA3 { get; set; }

 

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

amirus.stark
11 Dec 2020, 15:15

Thanks a lot sir!

 


@amirus.stark