MarketData.GetBars retrieves too few bars

Created at 30 Mar 2022, 12:58
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!
TH

thomas.abele

Joined 31.12.2017

MarketData.GetBars retrieves too few bars
30 Mar 2022, 12:58


Hi,

MarketData.GetBars retrieves too few bars.

Using the bars to create moving average doesn't work when more bars are needed.

_ma.Result.LastValue will be NAN in this case.

 

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 MTissue : Robot
  {
    [Parameter("MA TimeFrame", DefaultValue = "Weekly")]
    public TimeFrame MATimeFrame { get; set; }

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

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

    private MovingAverage _ma;

    protected override void OnStart()
    {
      Bars bars = MarketData.GetBars(MATimeFrame);
      while(bars.Count < MAPeriod * 2)
      {
        if(bars.LoadMoreHistory() < 1)
          break;
      }
      _ma = Indicators.MovingAverage(bars.ClosePrices, MAPeriod, MAType);
    }

    protected override void OnTick()
    {

      Print("distance={0}", _ma.Result.LastValue.ToString("f2"));
    }

  }
}

 


@thomas.abele
Replies

amusleh
31 Mar 2022, 10:44

Hi,

I just tested your code on EURUSD and it works fine, there are enough weekly bars for the moving average.

If bars.LoadMoreHistory() doesn't load enough bars then it means there is not enough historical bars for that symbol time frame.

The code I tested:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MTissue : Robot
    {
        [Parameter("MA TimeFrame", DefaultValue = "Weekly")]
        public TimeFrame MATimeFrame { get; set; }

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

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

        private MovingAverage _ma;

        protected override void OnStart()
        {
            var bars = MarketData.GetBars(MATimeFrame);

            while (bars.Count < MAPeriod * 2)
            {
                var numberOfLoadedBars = bars.LoadMoreHistory();

                Print(numberOfLoadedBars);

                if (numberOfLoadedBars < 1) break;
            }

            Print(bars.Count);

            _ma = Indicators.MovingAverage(bars.ClosePrices, MAPeriod, MAType);
        }

        protected override void OnTick()
        {
            Print("distance={0}", _ma.Result.LastValue.ToString("f2"));
        }
    }
}

Result:

31/03/2022 15:12:37.740 | CBot instance [New cBot, EURUSD, h1] started.
31/03/2022 15:12:38.599 | 1596
31/03/2022 15:12:39.662 | distance=1.15
31/03/2022 15:12:39.958 | distance=1.15
31/03/2022 15:12:40.396 | distance=1.15
31/03/2022 15:12:41.162 | distance=1.15
31/03/2022 15:12:41.505 | distance=1.15
31/03/2022 15:12:42.474 | CBot instance [New cBot, EURUSD, h1] stopped by user.

 


@amusleh

thomas.abele
31 Mar 2022, 21:34 ( Updated at: 21 Dec 2023, 09:22 )

RE:

amusleh said:

Hi,

I just tested your code on EURUSD and it works fine, there are enough weekly bars for the moving average.

If bars.LoadMoreHistory() doesn't load enough bars then it means there is not enough historical bars for that symbol time frame.

The code I tested:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MTissue : Robot
    {
        [Parameter("MA TimeFrame", DefaultValue = "Weekly")]
        public TimeFrame MATimeFrame { get; set; }

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

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

        private MovingAverage _ma;

        protected override void OnStart()
        {
            var bars = MarketData.GetBars(MATimeFrame);

            while (bars.Count < MAPeriod * 2)
            {
                var numberOfLoadedBars = bars.LoadMoreHistory();

                Print(numberOfLoadedBars);

                if (numberOfLoadedBars < 1) break;
            }

            Print(bars.Count);

            _ma = Indicators.MovingAverage(bars.ClosePrices, MAPeriod, MAType);
        }

        protected override void OnTick()
        {
            Print("distance={0}", _ma.Result.LastValue.ToString("f2"));
        }
    }
}

Result:

31/03/2022 15:12:37.740 | CBot instance [New cBot, EURUSD, h1] started.
31/03/2022 15:12:38.599 | 1596
31/03/2022 15:12:39.662 | distance=1.15
31/03/2022 15:12:39.958 | distance=1.15
31/03/2022 15:12:40.396 | distance=1.15
31/03/2022 15:12:41.162 | distance=1.15
31/03/2022 15:12:41.505 | distance=1.15
31/03/2022 15:12:42.474 | CBot instance [New cBot, EURUSD, h1] stopped by user.

 

Hi Ahmad,

I tried with cTrader 4.1 ICMarkets and TopFX without success. Bars count is only 99 and then MA result is NAN.

Do you use CTrader 4.2?

Best Regards
Thomas


@thomas.abele

amusleh
04 Apr 2022, 09:50

Hi,

I just tested it on both cTrader 4.1 (IC Markets) and 4.2 (Spotware Beta) and it works fine, it doesn't display any NAN value.


@amusleh