Multi Time Frame EMAs, do not show current value

Created at 18 Oct 2018, 20:12
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!
nh.zadeh's avatar

nh.zadeh

Joined 11.03.2018

Multi Time Frame EMAs, do not show current value
18 Oct 2018, 20:12


Hi everybody

 

I wrote multi time frame EMAs. the indicator do not draw the current H4 value on the chart, please help me to solve this issue. My codes are as below:

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class NasserMFT : Indicator
    {
        [Parameter(DefaultValue = 15)]
        public int Period1 { get; set; }

        [Parameter(DefaultValue = 30)]
        public int Period2 { get; set; }


        [Parameter("EMAs Timeframe1", DefaultValue = "Minute15")]
        public TimeFrame EMATimeframe1 { get; set; }

        [Parameter("EMAs Timeframe2", DefaultValue = "Hour4")]
        public TimeFrame EMATimeframe2 { get; set; }

        [Output("EMA1", Color = Colors.Red)]
        public IndicatorDataSeries EMA1 { get; set; }

        [Output("EMA2", Color = Colors.ForestGreen)]
        public IndicatorDataSeries EMA2 { get; set; }

        [Output("EMA3", Color = Colors.BlueViolet)]
        public IndicatorDataSeries EMA3 { get; set; }

        [Output("EMA4", Color = Colors.Violet)]
        public IndicatorDataSeries EMA4 { get; set; }

        private MarketSeries seriesM15;
        private MarketSeries seriesH4;

        private ExponentialMovingAverage Ema1;
        private ExponentialMovingAverage Ema2;
        private ExponentialMovingAverage Ema3;
        private ExponentialMovingAverage Ema4;

        protected override void Initialize()
        {
            seriesM15 = MarketData.GetSeries(EMATimeframe1);
            seriesH4 = MarketData.GetSeries(EMATimeframe2);

            Ema1 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period1);
            Ema2 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period2);
            Ema3 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period1);
            Ema4 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period2);
        }

        public override void Calculate(int index)
        {

            var index1 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
            if (index1 != -1)
            {
                EMA1[index] = Ema1.Result[index1];
            }

            var index2 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
            if (index2 != -1)
            {
                EMA2[index] = Ema2.Result[index2];
            }

            var index3 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
            if (index3 != -1)
            {
                EMA3[index] = Ema3.Result[index3];
            }

            var index4 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
            if (index4 != -1)
            {
                EMA4[index] = Ema4.Result[index4];
            }



        }


        private int GetIndexByDate(MarketSeries series, DateTime time)
        {


            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}


@nh.zadeh
Replies

PanagiotisCharalampous
19 Oct 2018, 10:49 ( Updated at: 21 Dec 2023, 09:20 )

Ηι nh.zadeh,

Thanks for posting in our forum. Ι have tried your indicator and seems ok to me.

Best Regards,

Panagiotis


@PanagiotisCharalampous

nh.zadeh
19 Oct 2018, 11:22

Dear Panagiotis

Thanks for your prompt reply.

As you see in the photo which your shared, the H4 EMAs current value did not draw, in the H4 candle which is not closed yet, we have EMA value at the current situation, I want the indicator draw the line till now. It is obvious that the value of the EMA could be changed in every tick.

 

Please advise


@nh.zadeh

PanagiotisCharalampous
19 Oct 2018, 14:18

Hi nh.zadeh,

See below

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class NasserMFT : Indicator
    {
        [Parameter(DefaultValue = 15)]
        public int Period1 { get; set; }

        [Parameter(DefaultValue = 30)]
        public int Period2 { get; set; }


        [Parameter("EMAs Timeframe1", DefaultValue = "Minute15")]
        public TimeFrame EMATimeframe1 { get; set; }

        [Parameter("EMAs Timeframe2", DefaultValue = "Hour4")]
        public TimeFrame EMATimeframe2 { get; set; }

        [Output("EMA1", Color = Colors.Red)]
        public IndicatorDataSeries EMA1 { get; set; }

        [Output("EMA2", Color = Colors.ForestGreen)]
        public IndicatorDataSeries EMA2 { get; set; }

        [Output("EMA3", Color = Colors.BlueViolet)]
        public IndicatorDataSeries EMA3 { get; set; }

        [Output("EMA4", Color = Colors.Violet)]
        public IndicatorDataSeries EMA4 { get; set; }

        private MarketSeries seriesM15;
        private MarketSeries seriesH4;

        private ExponentialMovingAverage Ema1;
        private ExponentialMovingAverage Ema2;
        private ExponentialMovingAverage Ema3;
        private ExponentialMovingAverage Ema4;

        protected override void Initialize()
        {
            seriesM15 = MarketData.GetSeries(EMATimeframe1);
            seriesH4 = MarketData.GetSeries(EMATimeframe2);

            Ema1 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period1);
            Ema2 = Indicators.ExponentialMovingAverage(seriesM15.Close, Period2);
            Ema3 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period1);
            Ema4 = Indicators.ExponentialMovingAverage(seriesH4.Close, Period2);
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar)
            {
                var index1 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
                if (index1 != -1)
                {
                    EMA1[index] = Ema1.Result[index1];
                }

                var index2 = GetIndexByDate(seriesM15, MarketSeries.OpenTime[index]);
                if (index2 != -1)
                {
                    EMA2[index] = Ema2.Result[index2];
                }

                var index3 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
                if (index3 != -1)
                {
                    EMA3[index] = Ema3.Result[index3];
                }

                var index4 = GetIndexByDate(seriesH4, MarketSeries.OpenTime[index]);
                if (index4 != -1)
                {
                    EMA4[index] = Ema4.Result[index4];
                }
            }
            else
            {

                EMA1[index] = Ema1.Result.LastValue;

                EMA2[index] = Ema2.Result.LastValue;

                EMA3[index] = Ema3.Result.LastValue;

                EMA4[index] = Ema4.Result.LastValue;
            }



        }


        private int GetIndexByDate(MarketSeries series, DateTime time)
        {


            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

nh.zadeh
19 Oct 2018, 15:29

Dear Panagiotis

 

Many thanks for your help. It is solved

 

Best regards

Nasser


@nh.zadeh

Mastah1238
13 Nov 2018, 18:55

Hello

How I can change one ema for sma?

Best regards.


@Mastah1238

nh.zadeh
14 Nov 2018, 07:52

RE:

Mastah1238 said:

Hello

How I can change one ema for sma?

Best regards.

Hello, you can use SimpleMovingAverage rather than ExponentialMovingAverage in the code.

Best regards


@nh.zadeh