Daily SMA on a 1hr chart calculates a different number compared to its result on the Daily chart. I think the problem lies in incorrect mapping.

Created at 26 May 2020, 19:20
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!
LU

lucrum

Joined 06.08.2019

Daily SMA on a 1hr chart calculates a different number compared to its result on the Daily chart. I think the problem lies in incorrect mapping.
26 May 2020, 19:20


//Any help greatly appreciated.

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

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

        [Parameter("S colour", DefaultValue = "Red")]
        public string colourS { get; set; }

        [Parameter("R colour", DefaultValue = "Blue")]
        public string colourR { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }

        [Parameter("Slow Period", DefaultValue = 30, MinValue = 1)]
        public int slowPeriod { get; set; }

        [Parameter("Fast Period", DefaultValue = 12, MinValue = 1)]
        public int fastPeriod { get; set; }

        [Parameter("Time Frame MA", DefaultValue = "Daily")]
        public TimeFrame timeFrame { get; set; }

        [Output("30 ma", LineColor = "Green", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries MA30D { get; set; }

        [Output("12 ma", LineColor = "Red", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries MA12D { get; set; }

        public Bars selectedSeries;

        public MovingAverage fastMA;
        public MovingAverage slowMA;

        protected override void Initialize()
        {
            if (timeFrame == Bars.TimeFrame)
            {
                selectedSeries = Bars;

                fastMA = Indicators.MovingAverage(Bars.ClosePrices, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(Bars.ClosePrices, slowPeriod, maType);
            }
            else
            {
                selectedSeries = MarketData.GetBars(timeFrame);
                fastMA = Indicators.MovingAverage(selectedSeries.ClosePrices, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(selectedSeries.ClosePrices, slowPeriod, maType);
            }
        }

        public override void Calculate(int index)
        {

            var selectedIndex = selectedSeries.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            if (selectedIndex == -1)
            {
                return;
            }

            MA12D[index] = fastMA.Result[selectedIndex];
            MA30D[index] = slowMA.Result[selectedIndex];

            Print("slow {0}", MA30D[index - 1]);
        }
    }
}


@lucrum
Replies

PanagiotisCharalampous
27 May 2020, 08:40

Hi lucrum,

Try adding this condition before assigning the values to the indicator

            if (Bars.OpenTimes[index] == selectedSeries.OpenTimes[selectedIndex])
            {
                MA12D[index] = fastMA.Result[selectedIndex];
                MA30D[index] = slowMA.Result[selectedIndex];
            }

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous