MarketData.GetBars(...) Maximum Length of Bars

Created at 08 Feb 2022, 07:25
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!
YS

ys2310

Joined 03.12.2021

MarketData.GetBars(...) Maximum Length of Bars
08 Feb 2022, 07:25


I'm trying to use OpenPrices.Last(n) data in Live mode and seems 10080 and 20160 is too past that no data feed comes in because

the Print() outputs are:

115.221

114.93

114.37

NaN

NaN

 Print(MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(1260));
 Print(MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(2520));
 Print(MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(5040));
 Print(MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(10080));
 Print(MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(20160));

 

In the Backtesting mode, it seems no problem I can get the data of 

MarketData.GetBars(TimeFrame.Minute, Symbol.Name).OpenPrices.Last(10080)

but at Live mode, I can't. 

Is there any fix or workaround for this problem??

Best regards, 


@ys2310
Replies

amusleh
08 Feb 2022, 08:30 ( Updated at: 08 Feb 2022, 08:32 )

Hi,

You can access the values inside Bars data series like OpenPrices by index either from beginning or end, but I'm not sure what exactly you are trying to do?

The indices aren't same, they change based on available data on your chart or loaded data from Bars.

One index might give you a value in one chart but another on another chart or environment.

The NAN is retuned when you try to access a value on a data series by index that aren't there yet.

You can try to load more data for bars if you need more data, call Bars LoadMoreHistory method to load more data.


@amusleh

ys2310
09 Feb 2022, 00:52

Hello amusleh, LoadMoreHistory method solved my problem.

though, I see there are small discrepancies in data obtained from backtesting and data obtained from Live.

Is there any method for this problem? backtesing results are different from Live results because of this data gap.

Best regards,

 


@ys2310

amusleh
09 Feb 2022, 08:14

RE:

ys2310 said:

Hello amusleh, LoadMoreHistory method solved my problem.

though, I see there are small discrepancies in data obtained from backtesting and data obtained from Live.

Is there any method for this problem? backtesing results are different from Live results because of this data gap.

Best regards,

 

Hi,

You get the same data, if you are back testing and running your cBot on same broker then the data you get must be same.

And you should back test your cBot on tick data to get the most accurate results.


@amusleh

ys2310
09 Feb 2022, 09:06

CADJPY, GBPJPY data seems exactly the same between backtesting and Live data.

but SP, CL and EURUSD, volatile symbols seem different in decimal between backtesting and Live data.

Can you confirm this on your side? I see difference on my end.

Best regards,


@ys2310

amusleh
09 Feb 2022, 09:38 ( Updated at: 10 Feb 2022, 08:26 )

RE:

ys2310 said:

CADJPY, GBPJPY data seems exactly the same between backtesting and Live data.

but SP, CL and EURUSD, volatile symbols seem different in decimal between backtesting and Live data.

Can you confirm this on your side? I see difference on my end.

Best regards,

Hi,

Please tell us which broker you are using and the start/end date for data or back test.

I will compare the normal chart data with back test data and I will let you know if there was any difference.


@amusleh

ys2310
10 Feb 2022, 01:37

RE: RE:

I'm seeing this data discrepancies with broker called Axiory. For example, the date between 2022/Feb/06 to 2022/Feb/09.

I can see a bit large differences with symbols like SP, CL, and a bit more small differences with EURUSD, USDJPY. 

Best regards, 

 


@ys2310

amusleh
10 Feb 2022, 08:27

RE: RE: RE:

ys2310 said:

I'm seeing this data discrepancies with broker called Axiory. For example, the date between 2022/Feb/06 to 2022/Feb/09.

I can see a bit large differences with symbols like SP, CL, and a bit more small differences with EURUSD, USDJPY. 

Best regards, 

 

Hi,

Can you tell us on which time frame data you found this issue?


@amusleh

ys2310
10 Feb 2022, 09:27

RE: RE: RE: RE:

Hello amusleh

I'm seeing this with one minute bar as,

MarketData.GetBars(TimeFrame.Minute, Symbol.Name)

Best regards,


@ys2310

amusleh
11 Feb 2022, 09:15

Hi,

I tested the SP symbol data 1 minute bars from 6th to 9th of February 2022, and real time data was identical to back test data.

To test you can use this cBot code:

using System;
using cAlgo.API;
using System.Text;
using System.IO;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class DataTester : Robot
    {
        protected override void OnStop()
        {
            var startTime = new DateTime(2022, 2, 6, 0, 0, 0, DateTimeKind.Utc);
            var endTime = new DateTime(2022, 2, 9, 0, 0, 0, DateTimeKind.Utc);

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("Time,Open,High,Low,Close,Volume");

            foreach (var bar in Bars)
            {
                if (bar.OpenTime < startTime || bar.OpenTime > endTime)
                {
                    continue;
                }

                stringBuilder.AppendFormat("{0:o},{1},{2},{3},{4},{5}", bar.OpenTime, bar.Open, bar.High, bar.Low, bar.Close, bar.TickVolume);
                stringBuilder.AppendLine();
            }

            var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), string.Format("data_{0}.csv", RunningMode));

            using (var stream = File.CreateText(filePath))
            {
                stream.Write(stringBuilder.ToString());
            }
        }
    }
}

It creates two CSV files on your desktop, run it both on real time chart and back tester.

Then use a text matching tool to compare the content of them, like this site: Text Compare! - An online diff tool that can find the difference between two text files (text-compare.com)

You will see that both have the same content.


@amusleh

ys2310
12 Feb 2022, 00:38

RE:

Thank you for the code, it seems bar.OpenTime creates identical results and I know this.

Discrepancies happen when I use MarketData.GetBars(TimeFrame.Minute, "SP") as a second symbol for data feed.

Can you also please try this on your side and check the difference?

Best regards,

 


@ys2310

amusleh
14 Feb 2022, 08:55

RE: RE:

ys2310 said:

Thank you for the code, it seems bar.OpenTime creates identical results and I know this.

Discrepancies happen when I use MarketData.GetBars(TimeFrame.Minute, "SP") as a second symbol for data feed.

Can you also please try this on your side and check the difference?

Best regards,

 

Hi,

There is no discrepancies at all. when you load the data with code, or you use the back test data, or the data the is loaded automatically on your chart, all of them came from same source.

The GetBars method loads some number of bars from that symbol, the index values of current symbol data with your loaded bars will not match.

You have to use the Bars.OpenTimes.GetIndexByTime method to get the correct index, for samples check this link: https://help.ctrader.com/ctrader-automate/indicator_code_samples#multiple-timeframes

 


@amusleh

ys2310
14 Feb 2022, 12:05

The following code returns index of two different time stamp. The two index show identical number. Why is this?

index1, index2 are supposed to be different numbers, no?

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            var series5 = MarketData.GetBars(TimeFrame.Minute);
            long index1 = series5.OpenTimes.GetIndexByTime(DateTime.Parse("2022-02-14 18:50:00"));
            long index2 = series5.OpenTimes.GetIndexByTime(DateTime.Parse("2022-02-14 18:54:15"));
            Print("{0} {1}", index1, index2);
        }        
    }
}


@ys2310

amusleh
15 Feb 2022, 08:39 ( Updated at: 21 Dec 2023, 09:22 )

RE:

ys2310 said:

The following code returns index of two different time stamp. The two index show identical number. Why is this?

index1, index2 are supposed to be different numbers, no?

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            var series5 = MarketData.GetBars(TimeFrame.Minute);
            long index1 = series5.OpenTimes.GetIndexByTime(DateTime.Parse("2022-02-14 18:50:00"));
            long index2 = series5.OpenTimes.GetIndexByTime(DateTime.Parse("2022-02-14 18:54:15"));
            Print("{0} {1}", index1, index2);
        }        
    }
}

Hi,

I tested your code and the returned indices aren't identical on my case:

The correct way to use the GetIndexByTime method is to pass another series bar open time and get the index on current series, the GetIndexByTime returns the estimated nearest bar index to your passed time.

If you want to be more precise use the GetIndexByExactTime method, it returns a bar index if an exact time was there, otherwise it returns -1.


@amusleh

... Deleted by UFO ...

ys2310
11 Mar 2022, 05:49

RE: RE:

Hi amusleh,

 

I'm trying the following code and the index is -1

DateTime dt = Server.Time.AddMinutes(-128160);
int index = MarketData.GetBars(TimeFrame.Minute).OpenTimes.GetIndexByTime(dt);
Print("{0} {1}", index, MarketData.GetBars(TimeFrame.Minute).ClosePrices[index]);

if I instead use the code below the index has a value.

DateTime dt = Server.Time.AddMinutes(-1);
int index = MarketData.GetBars(TimeFrame.Minute).OpenTimes.GetIndexByTime(dt);
Print("{0} {1}", index, MarketData.GetBars(TimeFrame.Minute).ClosePrices[index]);

Is this because I'm referring way back in the past and there is no data with it?

How can I work around this problem? I want to get a correct value with the specified date and time.

Best  regards,

 


@ys2310

amusleh
11 Mar 2022, 08:07

RE: RE: RE:

ys2310 said:

Hi amusleh,

 

I'm trying the following code and the index is -1

DateTime dt = Server.Time.AddMinutes(-128160);
int index = MarketData.GetBars(TimeFrame.Minute).OpenTimes.GetIndexByTime(dt);
Print("{0} {1}", index, MarketData.GetBars(TimeFrame.Minute).ClosePrices[index]);

if I instead use the code below the index has a value.

DateTime dt = Server.Time.AddMinutes(-1);
int index = MarketData.GetBars(TimeFrame.Minute).OpenTimes.GetIndexByTime(dt);
Print("{0} {1}", index, MarketData.GetBars(TimeFrame.Minute).ClosePrices[index]);

Is this because I'm referring way back in the past and there is no data with it?

How can I work around this problem? I want to get a correct value with the specified date and time.

Best  regards,

 

It returns -1 if there is no matching bar, to solve this issue you can load more bars by calling Bars LoadMoreHistory method.


@amusleh