cAlgo Backtesting in Visual Mode - got bars from other TimeFrame earlier than from chartBars

Created at 28 Feb 2025, 13:28
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!
AN

andi21

Joined 14.12.2016

cAlgo Backtesting in Visual Mode - got bars from other TimeFrame earlier than from chartBars
28 Feb 2025, 13:28


Dear Community,

cTrader Desktop 5.1.14

Broker: IC Markets

 

following 2 test-codes are given - first for a test indicator and the second one for a test bot - both have the same logic:

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class Test : Indicator
    {        
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private Bars bigBars;
        int lastCalc;

        protected override void Initialize()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
            bigBars = MarketData.GetBars(TimeFrame.Hour6, SymbolName);
            lastCalc = -1;
        }
        
        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 
            if (IsLastBar)
            {
                if (bigBars.Count != lastCalc)
                {
                    lastCalc = bigBars.Count;
                    Print("BigBar Changed OpenTime Last is now: " + bigBars.OpenTimes[bigBars.OpenTimes.Count - 1]);
                    Print("And Chart Bar OpenTime Last is now: " + Bars.OpenTimes[Bars.OpenTimes.Count - 1]);
                }
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class DoNothingBot : Robot
    {
        private Bars bigBars;
        int lastCalc;

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            //Print(Message);
            bigBars = MarketData.GetBars(TimeFrame.Hour6, SymbolName);
            lastCalc = -1;
        }

        protected override void OnTick()
        {
            // Handle price updates here
            if (bigBars.Count != lastCalc)
            {
                lastCalc = bigBars.Count;
                Print("BigBar Changed OpenTime Last is now: " + bigBars.OpenTimes[bigBars.OpenTimes.Count - 1]);
                Print("And Chart Bar OpenTime Last is now: " + Bars.OpenTimes[Bars.OpenTimes.Count - 1]);
            }
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

 

The produces results will be (Chart-TimeFrame is set to h1):

In Backtesting (15/01/2025-15/01/2025 and with tick-Data):
Test-Indicator (incorrect):
 15.01.2025 10:00:00.640 | Info | BigBar Changed OpenTime Last is now: 15.01.2025 10:00:00
 15.01.2025 10:00:00.640 | Info | And Chart Bar OpenTime Last is now: 15.01.2025 09:00:00

Test-DoNothingBot (correct):
 15.01.2025 10:00:00.640 | Info | BigBar Changed OpenTime Last is now: 15.01.2025 10:00:00
 15.01.2025 10:00:00.640 | Info | And Chart Bar OpenTime Last is now: 15.01.2025 10:00:00

In Live-Chart:
Test-Indicator (correct):
 28.02.2025 12:57:52.266 | Info | BigBar Changed OpenTime Last is now: 28.02.2025 10:00:00
 28.02.2025 12:57:52.266 | Info | And Chart Bar OpenTime Last is now: 28.02.2025 10:00:00

 

So in Backtesting in visual mode the test indicator gets the h6-Bar with OpenTime 10:00:00, but the current last chart-Bar (h1) is still the one with OpenTime 09:00:00. I debugged the code and saw that the chart-Bar has already a tickVolume of 7827 etc. so it seems to be the last tick of the h1-ChartBar of 09:00:00, but it already gets the h6-Bar from 10:00:00.

Thanks in advance for your help.

Best regards,

andi21


@andi21