How to determine if a previous bar is LastBarInSession?

Questions about MultiCharts .NET and user contributed studies.
novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

How to determine if a previous bar is LastBarInSession?

Postby novaleaf » 21 May 2015

you can determine if the current bar is the last bar in a session via

Code: Select all

this.Bars.LastBarInSession
how can you determine if a previous bar in your history is the last bar of a session?

I need this because the way MaxBarsBack works, I wish to retroactively do calculations in the StartCalc() method, and my calculations need to know where the session breaks are.

The "easy" way is to look at the changes in date (this.Bars.Time[x]), but that won't work with if a session spans across midnight.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: How to determine if a previous bar is LastBarInSession?

Postby JoshM » 22 May 2015

how can you determine if a previous bar in your history is the last bar of a session?
You can use a `VariableSeries<T>` for this, but it's probably quicker/more convenient to use a Boolean variable that's assigned its value in the last part of the `CalcBar()` method. That way, it will hold the value of the previous calculation when the script calculates again.

For example:

Code: Select all

private bool firstOfSession = false;

protected override void CalcBar()
{
if (firstOfSession)
{
// This bar is the first of the session
// because the previous bar was the
// last of the session
}

if (Bars.Status == EBarState.Close && Bars.LastBarInSession)
firstOfSession = true;
else
firstOfSession = false;
}

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: How to determine if a previous bar is LastBarInSession?

Postby JoshM » 22 May 2015

You can use a `VariableSeries<T>` for this (...)
To expand on my previous post for a more complete answer, here's how to use a `VariableSeries<bool>` to determine if the previous bar was the last in the session:

Code: Select all

public class Snippet_Indicator : IndicatorObject
{
public Snippet_Indicator(object _ctx) : base(_ctx) { }

private VariableSeries<bool> sessionStatus;

protected override void Create()
{
sessionStatus = new VariableSeries<bool>(this);
}

protected override void CalcBar()
{
sessionStatus.Value = Bars.LastBarInSession;

// Check if, on the previous bar, the
// Bars.LastBarInSession property returned true
if (sessionStatus[1] == true)
{
// This is the first bar of the session
}
}
}

novaleaf
Posts: 49
Joined: 17 Apr 2014
Has thanked: 9 times
Been thanked: 4 times

Re: How to determine if a previous bar is LastBarInSession?

Postby novaleaf » 22 May 2015

Hi Josh, but putting your logic in CalcBar() and using Bars.LastBarInSession doesn't work in this case, because CalcBar() is never called for the first "MaxBarsBack" bars.

So I need to walk the past 50 bars in MaxBarsBack, and determine if there are any session breaks there.

It seems there currently isn't a solution for this, and its OK with me I guess, I'll just use midnight as the divider. I just wanted to detect the actual session break for completeness.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: How to determine if a previous bar is LastBarInSession?

Postby JoshM » 22 May 2015

Hi Josh, but putting your logic in CalcBar() and using Bars.LastBarInSession doesn't work in this case, because CalcBar() is never called for the first "MaxBarsBack" bars.

So I need to walk the past 50 bars in MaxBarsBack, and determine if there are any session breaks there.
I didn't realise you wanted to do this in `StartCalc()` for the bars that are also affected by MaxBarsBack.

By the way, if you want to loop over the first 50 bars, you can use the `Bars.FullSymbolData.Time[x]` property to return the `DateTime` value of those bars. Using `Bars.Time[x]`, as you referred to in the openings post, is still affected/limited by MaxBarsBack.


Return to “MultiCharts .NET”