Issue with Simple Moving Average on MultiTimeFrame

Created at 29 May 2020, 03:23
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!
RA

RayAdam

Joined 02.06.2019

Issue with Simple Moving Average on MultiTimeFrame
29 May 2020, 03:23


Hi Support,

Having a very strange issue here. Need 200 Simple Moving Average calculated value. When used the below code I get NaN  for first 74 Bars before getting first SMA value.

If I change the inputSMA to 126 I get Calculated value on 1st Bar. No NaN

If I change the inputSMA to 127 I get Calculated value on 2nd Bar., NaN on 1st Bars

If I change the inputSMA to 200 I get Calculated value on 75th Bar, NaN on 1-74 Bars

 

Also If I change the SMA to EMA I get value on the first Bar for 200 EMA  - No NaN values

Could you please advise how I can get value on first bar for 200 SMA?

Thanks

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TbotMay2020_Support : Robot
    {
        [Parameter("Other TimeFrame", DefaultValue = "Hour")]
        public TimeFrame otherTF { get; set; }
        
        [Parameter("SMA", DefaultValue = 200)]
        public int inputSMA { get; set; }
                
        public IndicatorDataSeries SMA;
        private SimpleMovingAverage _SMA;

        private Bars Series;      

        protected override void OnStart()
        {            
            Series = MarketData.GetBars(otherTF);

            SMA = CreateDataSeries();            
            _SMA = Indicators.SimpleMovingAverage(Series.ClosePrices, inputSMA);           
        }

        protected override void OnBar()
        {
            int index = Bars.Count - 1;

            var index1 = GetIndexByDate(Series, Bars.OpenTimes[index]);
            if (index1 != -1)
            {
                SMA[index] = _SMA.Result[index1];
                Print("SMA("+ inputSMA.ToString() + ") : {0}", SMA[index].ToString());
            }
        }

        protected override void OnStop()
        {            
        }

        private int GetIndexByDate(Bars Bars, DateTime time)
        {
            for (int i = Bars.Count - 1; i > 0; i--)
            {
                if (time == Bars.OpenTimes[i])
                    return i;
            }
            return -1;
        }
    }
}

 

 


@RayAdam
Replies

PanagiotisCharalampous
29 May 2020, 08:21

Hi RayAdam,

This happens because Bars does not have enough history to calculate the SMA. Try adding the below in your OnStart() method

            while (Bars.Count < inputSMA)
                Bars.LoadMoreHistory();

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

RayAdam
29 May 2020, 16:08 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Thanks Panagiotis for prompt reply,

I have update the OnStart() method below as advised but system goes into an infinite loop and had to stop it. For some reason LoadMoreHistory() is not functioning as expected please see the logs below.

protected override void OnStart()
        {            
            Series = MarketData.GetBars(otherTF);

            SMA = CreateDataSeries();            
            _SMA = Indicators.SimpleMovingAverage(Series.ClosePrices, inputSMA);
            
            int SeriesBarsCountBeforeLoadMoreHistory = Series.Count;

            while (Series.Count < inputSMA)
            {
                Series.LoadMoreHistory();
                Print("Series BarsCount Before LoadMoreHistory: {0}, Series BarsCount After LoadMoreHistory: {1}", SeriesBarsCountBeforeLoadMoreHistory, Series.Count);
            }
        }

Logs

I even tried LoadMoreHistory() with Chart Time Frame (Bars) but encountered similar issue.

Restarted cTrader but same issue.

Could you please see and advise

Thanks


@RayAdam

PanagiotisCharalampous
02 Jun 2020, 08:33

Hi RayAdam,

Unfortunately this method doesn't work on backtesting at the moment. The solution will work only for forward execution of the cBot.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous