Not getting the latest bar back

Created at 17 Aug 2022, 19:51
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!
JA

jaydcrowe1989

Joined 11.08.2022

Not getting the latest bar back
17 Aug 2022, 19:51


Hi,

Within the OnBar method I am looping around the instruments I am interested in trading and getting the latest bars back using:

Bars currencyData = MarketData.GetBars(TimeFrame.Minute5, instrumentName);

But this isnt always returning the latest Bar. For example I can run it in US500 and the code kicks in at 17:50, I would expect the latest Bar open time to be 17:45 but sometimes I am getting the 17:40 bar back which isnt the latest bar.

Is there anyway to guarantee that I always get the latest bar back?

Regards,

Jay


@jaydcrowe1989
Replies

PanagiotisCharalampous
18 Aug 2022, 08:15

Hi Jay,

Can you share your souce code so that we can give it a try?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

jaydcrowe1989
18 Aug 2022, 11:04

RE:

PanagiotisCharalampous said:

Hi Jay,

Can you share your souce code so that we can give it a try?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Yeah sure...so on bar I am running the following:
 

protected override void OnBar()
{
    base.OnBar();

    DateTime serverTime = Server.Time;

    // Gets me the time now minus 5 minutes ago
    serverTime = serverTime.AddMinutes(-5).AddMinutes(-(serverTime.Minute % 5)).AddSeconds(-serverTime.Second).AddTicks(-(serverTime.Ticks % TimeSpan.TicksPerSecond));

    foreach (string instrument in _instruments)
    {
        Bars currencyData = MarketData.GetBars(TimeFrame.Minute5, instrument);
        Bar latestBar = currencyData.Last(1);
        DateTime latestCandle = latestBar.OpenTime;
        if (latestCandle == serverTime)
        {
            Print("Instrument " + instrument + " BarTime " + latestCandle + " LastHigh: " + latestBar.High + " LastLow: " + latestBar.Low);
        } 
        else
        {
            Print($"Didn't get the correct candle back...server time: {serverTime}...latest candle: {latestCandle}");
        }
    }
}


@jaydcrowe1989

jaydcrowe1989
18 Aug 2022, 17:16

RE:

PanagiotisCharalampous said:

Hi Jay,

Can you share your souce code so that we can give it a try?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Have you managed to replicate the problem? It is driving me crazy haha


@jaydcrowe1989

PanagiotisCharalampous
19 Aug 2022, 08:13

Hi Jay,

Your code uses variables defined outside OnBar. Please share the complete cBot code so that I do not need to make guesses.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous