Bars, Last(0), Last(1) and LastBar Clarification

Created at 11 Jul 2021, 10:02
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!
CT

ctid3999979

Joined 05.07.2021

Bars, Last(0), Last(1) and LastBar Clarification
11 Jul 2021, 10:02


Hi

I'm coming from a Python background and as the title says, was hoping for some clarification.

 

Lets say I have the following:

protected override void OnStart()
        {

            // Define TimeFrames for indicators
            data1Min = MarketData.GetBars(TimeFrame.Minute);

So this runs when the cBot starts. How many previous bars does this get? When new bars are formed as time moves on, do these bars get added to the data1Min variable or is the data gathers during the OnStart function all the data that will ever be in there? I'm assuming the former.

Now lets say I want to have code such as the following:

protected override void OnTick()
        {
            // If MA1 is above MA2 and MA2 is below MA3 (trending up)
            if (ma1MovingAverage.Result.Last(1) > ma2MovingAverage.Result.Last(1) && ma2MovingAverage.Result.Last(1) > ma3MovingAverage.Result.Last(1))
            {
                // Enter code here...
            }

Can I get confirmation one how the indexing works for the Last(i) property please?

Last(0) = current bar
Last(1) = previous bar
LastBar = Last(1) = previous bar

If Last(0) = current bar, then it will have values for Open, Low and High (Low and High could change until the bar closes) but it won't have a Close value until the bar closes?


@ctid3999979
Replies

PanagiotisCharalampous
13 Jul 2021, 10:19

Hi ctid3999979,

See below the replies to your questions

How many previous bars does this get?

There is no specific number. It depends at which time the method is called.

do these bars get added to the data1Min variable

Yes

Can I get confirmation one how the indexing works for the Last(i) property please?

Last(0) = current bar
Last(1) = previous bar

This is correct

LastBar = Last(1) = previous bar

No LastBar = Last(0)

If Last(0) = current bar, then it will have values for Open, Low and High (Low and High could change until the bar closes) but it won't have a Close value until the bar closes?

Close price for open bars is always equal to the bid price.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous