Are there good ways to detect first bar of a session?

Questions about MultiCharts .NET and user contributed studies.
christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Are there good ways to detect first bar of a session?

Postby christephan » 15 Sep 2015

Currently, I'm using Bars.LastBarInSession

For back testing it's fine.
But when I put it into live stream, my signal doesn't trigger LastBarInSession on yesterday close of a SGX symbol. I don't know why.
And I must manually stop and restart the strategy every day to make it work properly.

One scenario I can think of is,
for example if I'm running a minute chart, and the session close time is 2 am.
And there is no tick in the 2:00 bar which closes the session.
so the last bar of the day is 1:59 which LastBarInSession will be false in live stream.
And the next bar will be the next session open maybe on 9:00
Thus the first bar detection fails because previous LastBarInSession is false.

Any idea?
Or are there any way to get the symbol session definition that I could calculate it by myself?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Are there good ways to detect first bar of a session?

Postby Henry MultiСharts » 29 Sep 2015

Hello christephan,

Please refer to the SessionObject to access the symbol's sessions.

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: Are there good ways to detect first bar of a session?

Postby christephan » 09 Dec 2015

Henry,

I haven't tried to port my strategies onto MC9.1.

In MC9.0, this code fails to get correct session begin:

Code: Select all

public sealed class FirstBarInSession : FunctionSimple<System.Boolean>
{
public FirstBarInSession(CStudyControl _master) : base(_master) { }
public FirstBarInSession(CStudyControl _master, int _ds) : base(_master, _ds) { }

private VariableObject<bool> prev_session_end;

protected override void Create()
{
prev_session_end = new VariableObject<bool>(this);
}

protected override void StartCalc()
{
prev_session_end.Value = false;
}

protected override System.Boolean CalcBar()
{
bool is_session_end = prev_session_end.Value;
if (Bars.LastBarInSession) {
prev_session_end.Value = true;
} else {
prev_session_end.Value = false;
}
return is_session_end;
}
}
While the following works:

Code: Select all

public sealed class FirstBarInSession : FunctionSimple<System.Boolean>
{
public FirstBarInSession(CStudyControl _master) : base(_master) { }
public FirstBarInSession(CStudyControl _master, int _ds) : base(_master, _ds) { }

private VariableSeries<bool> prev_session_end;

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

protected override void StartCalc()
{
prev_session_end.Value = false;
}

protected override System.Boolean CalcBar()
{
bool is_session_end = prev_session_end[1];
if (Bars.LastBarInSession) {
prev_session_end.Value = true;
} else {
prev_session_end.Value = false;
}
return is_session_end;
}
}
The only difference is VariableObject -> VariableSeries.

And I found sometimes VariableObject doesn't work as my expectation. And it works fine now after I switch all of my code to use VariableSeries.

Do I miss-understand the functionality of VariableObject?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Are there good ways to detect first bar of a session?

Postby Henry MultiСharts » 10 Dec 2015

Hello christephan,

Please see MultiCharts .NET post #14.


Return to “MultiCharts .NET”