Bars.Last() doesn't work

Created at 25 Mar 2020, 17:12
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!
ED

edoardo.picazio02

Joined 25.03.2020

Bars.Last() doesn't work
25 Mar 2020, 17:12


I've created a little program to test the OpenedBar event, and every time a new bar gets opened it prints the OpenTime of the previous one.

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 BarOpenedExample : Robot
    {
        protected override void OnStart()
        {
            var bars = MarketData.GetBars(TimeFrame.Minute, "GBPUSD");
            bars.BarOpened += Bars_BarOpened;
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            Print("Open: ", Bars.Last(1).OpenTime);
            Print(" ");
        }
    }
}

It looks very simple, but it doesn't work properly:

The first time is never right, as you can see at 14:50 it printed 14:58 instead of 14:49.

dometimes print the same open time twice, sometime it get it right.

I really have no idea of why this is not working, but i think it is beacuse of the new bar event, i wrote another program that only gives me the previous open time OnStart and it works perfectly.

any ideas? thanks


@edoardo.picazio02
Replies

PanagiotisCharalampous
26 Mar 2020, 09:14

Hi edoardo.picazio02,

To get the open time of the last bar, you need to use Last(0).

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous