NinjaScript > Language Reference > Data > Bars > Session >

GetNextBeginEnd

Print this Topic Previous pageReturn to chapter overviewNext page

Definition
Returns the date and time representing the next begin/end of a session.

 

Note: Returned values are paired for the same session. If the next DateTime is the session end time, the begin time returned will be the paired begin time corresponding with that session's end time. It is not the next begin time after the end time.
 

Property Value

A DateTime structure.

 

Syntax
Bars.Session.GetNextBeginEnd(DateTime time, out DateTime sessionBegin, out DateTime sessionEnd)

Bars.Session.GetNextBeginEnd(Bars bars, int barsAgo, out DateTime sessionBegin, out DateTime sessionEnd)

 

Examples

/* Note: The two different signatures do not necessarily produce the exact same results. When using the Bars signature, it compares the bar you request versus where that bar belongs within a session template and provides you session begin/end times that way. When using the Time signature, it compares the timestamp of the bar to figure out the session begin/end.

 

When using the Time signature, if you passed in a timestamp with exactly the same timestamp of a session begin/end it compares them as if the session begin/end was an inclusive time frame so it may return values for session begin/end of the prior session despite the bar in question being of a new session. If you want it to match the Bars' session template positioning please use the Bars signature. */

 

// Example #1 - Get session begin/end times in relation to the bar you are processing

private DateTime sessionBegin;

private DateTime sessionEnd;

 

protected override void OnBarUpdate()
{

    // On the start of a new session, get the next begin or end session time and its corresponding paired session begin/end time.
    if (Bars.FirstBarOfSession)
         Bars.Session.GetNextBeginEnd(BarsArray[0], 0, out sessionBegin, out sessionEnd);

 

    Print("Session Start: " + sessionBegin + " Session End: " + sessionEnd);
}

 

 

// Example #2 - Get session begin/end times in relation to a timestamp versus the session template's begin/end times

private DateTime sessionBegin;

private DateTime sessionEnd;

 

protected override void OnBarUpdate()
{

    // At 10AM, get the next begin or end session time and its corresponding paired session begin/end time.
    if (ToTime(Time[0]) == 100000)
         Bars.Session.GetNextBeginEnd(Time[0], out sessionBegin, out sessionEnd);

 

    Print("Session Start: " + sessionBegin + " Session End: " + sessionEnd);
}