Trading "day" studies need to be smarter

Questions about MultiCharts .NET and user contributed studies.
MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Trading "day" studies need to be smarter

Postby MidKnight » 12 Aug 2012

Hello,

The built-in studies that try to find a trading day boundary are all inflexible to meet the needs of global markets. For example, floor trader pivots, OHLC Yesterday, to name a few. They make the assumption that the end of day is midnight rather than inspecting the symbols defined session and using the defined start and end time.

For example, it currently looks like this in the code:

Code: Select all

if (Bars.Time[0].Date != Bars.Time[1].Date)
But should be something like this instead (pseudo code, I don't know how to get the symbol session object):

Code: Select all

if ((Bars.Time[0] > Bars.SessionStartTime) && (Bars.Time[1] <= Bars.SessionEndTime))
With kind regards,
MK

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

Re: Trading "day" studies need to be smarter

Postby JoshM » 13 Aug 2012

But should be something like this instead (pseudo code, I don't know how to get the symbol session object):

Code: Select all

if ((Bars.Time[0] > Bars.SessionStartTime) && (Bars.Time[1] <= Bars.SessionEndTime))
Good question - can someone share an example of the SessionObject? (Or better worded, an example of how to get the SessionBegin and SessionEnd times)

According to the documentation, to instantiate a SessionObject, one needs to know the following:
Initializes a new instance of the SessionObject.

SessionObject(
DayOfWeek startDay,
DayOfWeek endDay,
TimeSpan startTime,
TimeSpan endTime
);
Parameters
startDay
Start session day.
endDay
End session day.
startTime
Start session time.
endTime
End session time.
But if one knows these parameters already, why is the SessionObject needed? I must be missing something. :)

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Trading "day" studies need to be smarter

Postby MidKnight » 13 Aug 2012

**EDIT: Please ignore this post. I got confused in the console output text order. The LastBarInSession is performing as expected it should. It's amazing what sleep can do for the mind! :)



I saw something JoshM said in another thread that prompted some more exploration. There is the Bars.LastBarInSession flag that I thought may do nicely for marking the end of a session in these trading "day" studies. However it appears to me that the flag is set one bar too late. The flag is set to true on the first bar of the new session. What am I missing here?

With thanks in advance,
MK
Last edited by MidKnight on 14 Aug 2012, edited 1 time in total.

Dru
Posts: 107
Joined: 28 Aug 2007
Has thanked: 4 times
Been thanked: 171 times

Re: Trading "day" studies need to be smarter

Postby Dru » 14 Aug 2012

Simple helper methods for access to session start & session end by DateTime :

Code: Select all

namespace PowerLanguage
{
public static class Extensions
{
public static DateTime StartOfSession(this SessionObject _sess, DateTime _dt)
{
if (_sess.StartDay == _dt.DayOfWeek)
return _dt.Date + _sess.StartTime;
if (_sess.EndDay == _dt.DayOfWeek)
{
var _date = _dt.Date;
if (_sess.StartDay != _sess.EndDay)
_date = _date.AddDays(-1);
return _date + _sess.StartTime;
}

throw new ApplicationException("StartOfSession: Wrong session for date");
}

public static DateTime EndOfSession(this SessionObject _sess, DateTime _dt)
{
if (_sess.EndDay == _dt.DayOfWeek)
return _dt.Date + _sess.EndTime;

if (_sess.StartDay == _dt.DayOfWeek)
{
var _date = _dt.Date;
if (_sess.StartDay != _sess.EndDay)
_date = _date.AddDays(1);
return _date + _sess.EndTime;
}

throw new ApplicationException("EndOfSession: Wrong session for date");
}

public static SessionObject SessionForDate(this IInstrument _this, DateTime _dt)
{
var _sess = _this.Sessions;
for (var i = 0; i < _sess.Count; i++)
{
var session = _sess[i];
if (session.StartDay == _dt.DayOfWeek || session.EndDay == _dt.DayOfWeek)
if (session.StartOfSession(_dt) <= _dt && session.EndOfSession(_dt) >= _dt)
return session;
}
return null;
}

public static DateTime BeginOfSession(this IInstrument _this, DateTime _dt)
{
var _sess = _this.SessionForDate(_dt);
if (null != _sess)
return _sess.StartOfSession(_dt);
return _dt - _this.Info.Resolution.Duration();
}

public static DateTime EndOfSession(this IInstrument _this, DateTime _dt)
{
var _sess = _this.SessionForDate(_dt);
if (null != _sess)
return _sess.EndOfSession(_dt);
return _dt - _this.Info.Resolution.Duration();
}
}
How to use:

Code: Select all

Output.WriteLine("Begin of session for bar: {0}", Bars.BeginOfSession(Bars.TimeValue));
Output.WriteLine("End of session for bar: {0}", Bars.EndOfSession(Bars.TimeValue));
I use this helpers for request ticks per sessions.

MidKnight
Posts: 343
Joined: 12 Aug 2012
Has thanked: 123 times
Been thanked: 56 times

Re: Trading "day" studies need to be smarter

Postby MidKnight » 14 Aug 2012

Thanks a lot Dru - that is some really good code for me to study. Very much appreciated.

With thanks,
MK

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

Re: Trading "day" studies need to be smarter

Postby JoshM » 14 Aug 2012

Thanks Dru, that's quite impressive and thanks for sharing! :)

riverTrader
Posts: 64
Joined: 15 Aug 2011
Has thanked: 3 times
Been thanked: 50 times

Re: Trading "day" studies need to be smarter

Postby riverTrader » 15 Aug 2012

Simple helper methods for access to session start & session end by DateTime :

I use this helpers for request ticks per sessions.

Dru. Really helpful, thank you. Please send along any other helpful extensions you might develop and I'll do the same.


To .Net beginners -- notice carefully here how Dru is using the .net 'extensions' capability to add static methods to the class itself rather than writing 'wrapper' methods. And not that he is writing extension methods for a class he did not create and does not own. (it does mean that in the future if the methods are added by mc.net directly to the class he will have a conflict -- but I'd rather resolve a name conflict than have a bunch of obsolete wrapper methods littering my code in 3 years)

Very cool, very powerful .net functionality.


Return to “MultiCharts .NET”