Situation
You want to know how to retrieve the time at which trading started for a certain weekday and the time at which trading ended in the current chart symbol.

Programming example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_SessionInformation : IndicatorObject
    {
        public Example_SessionInformation(object _ctx) : base(_ctx) { }

        protected override void StartCalc()
        {
            Output.Clear();

            // Retrieve information for all sessions
            AllSessionsInfo();
        }

        protected override void CalcBar()
        {
            // Output session information for the current bar
            if ((Bars.Status == EBarState.Close) && (Bars.LastBarOnChart))
            {
                SessionInfo(Bars.TimeValue.DayOfWeek);
            }
        }

        // Output all session information
        private void AllSessionsInfo()
        {
            // Give a note if the user has applied a different
            // session than the session specified in the QuoteManager settings
            if (Bars.Info.SessionName != "Default")
            {
                Output.WriteLine("Note: using a non-standard session template ({0}).",
                    Bars.Info.SessionName);
            }

            // Output session start and end times
            for (int i = 0; i < Bars.Sessions.Count; i++)
            {
                Output.WriteLine("{0}, start time: {1}, End time: {2}",
                    Bars.Sessions[i].StartDay,
                    Bars.Sessions[i].StartTime,
                    Bars.Sessions[i].EndTime);
            }
        }

        // Retrieve the session information for a specific day
        private void SessionInfo(DayOfWeek weekday)
        {
            // Calculate the trading day
            int tradingDay = Math.Max(0, (int)weekday - 1);

            Output.WriteLine("{0}'s trading session started at {1} and ended at {2}",
                weekday,
                Bars.Sessions[tradingDay].StartTime,
                Bars.Sessions[tradingDay].EndTime);
        }
    }
}

Output of the programming example

Applied to a chart, the indicator outputs the following information to the PowerLanguage .NET Editor Output Window:

Sunday, start time: 17:00:00, End time: 16:15:00
Monday, start time: 17:00:00, End time: 16:15:00
Tuesday, start time: 17:00:00, End time: 16:15:00
Wednesday, start time: 17:00:00, End time: 16:15:00
Thursday, start time: 17:00:00, End time: 16:15:00
Friday's trading session started at 17:00:00 and ended at 16:15:00

And for another symbol with a different session template:

Note: using a non-standard session template (24/5).
Monday, start time: 00:00:00, End time: 23:59:00
Tuesday, start time: 00:00:00, End time: 23:59:00
Wednesday, start time: 00:00:00, End time: 23:59:00
Thursday, start time: 00:00:00, End time: 23:59:00
Friday, start time: 00:00:00, End time: 23:59:00
Friday's trading session started at 00:00:00 and ended at 23:59:00

Using session information in MultiCharts .NET

Session information can be retrieved with the SessionObject (Henry MultiCharts, 2014), which can be accessed in one of two ways: (a) by creating a session object or, more conveniently, (b) through the ‘Bars.Sessions’ instrument property.

The returned session data is dependent on both the time zone and session settings of the symbol, which are specified in the Format Instrument window. The Default session template returns the session data as specified for the symbol in the QuoteManager. Other session templates take precedence over the default QuoteManager settings.

MultiCharts .NET programming example

In the programming example we first set the SameAsSymbol attribute to true (line 8), which forces the indicator to be plotted on the price chart and not in a separate sub-chart.

In the StartCalc() method we clear the Output Window by calling Output.Clear(), after which the AllSessionsInfo() method is called:

protected override void StartCalc()
{
    Output.Clear();

    // Retrieve information for all sessions
    AllSessionsInfo();
}

Outputting information from all sessions

The AllSessionsInfo() method is implemented as follows:

// Output all session information
private void AllSessionsInfo()
{
    // Give a note if the user has applied a different
    // session than the session specified in the QuoteManager settings
    if (Bars.Info.SessionName != "Default")
    {
        Output.WriteLine("Note: using a non-standard session template ({0}).",
            Bars.Info.SessionName);
    }

    // Output session start and end times
    for (int i = 0; i < Bars.Sessions.Count; i++)
    {
        Output.WriteLine("{0}, start time: {1}, End time: {2}",
            Bars.Sessions[i].StartDay,
            Bars.Sessions[i].StartTime,
            Bars.Sessions[i].EndTime);
    }
}

The method starts by verifying if the name of the current session template, a string returned by the Bars.Info.SessionName property, is different from the default template name ("Default"). Should that be the case, a notification is outputted to let the user know non-standard session times can be expected.

Then we subsequently loop through all Bars.Sessions[index]:

// Output session start and end times
for (int i = 0; i < Bars.Sessions.Count; i++)
{
    Output.WriteLine("{0}, start time: {1}, End time: {2}",
        Bars.Sessions[i].StartDay,
        Bars.Sessions[i].StartTime,
        Bars.Sessions[i].EndTime);
}

Since Bars.Sessions is zero-based, we start looping at 0 for weekday 1 and end the loop at Bars.Sessions.Count - 1. Failing to account for this will trigger the "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection." error message.

For every day, we output the start time of the first session (returned by Bars.Sessions[index].StartTime), the day on which this first session started (Bars.Sessions[index].StartDay), and the time at which the last session of the day ended (Bars.Sessions[index].EndTime).

Retrieving session information for a specific day

In the CalcBar() override method we call the SessionInfo() method when the last bar on the chart has closed. In this method we pass the DayOfWeek parameter:

protected override void CalcBar()
{
    // Output session information for the current bar
    if ((Bars.Status == EBarState.Close) && (Bars.LastBarOnChart))
    {
        SessionInfo(Bars.TimeValue.DayOfWeek);
    }
}

To retrieve the weekday of the current bar, the Bars.TimeValue.DayOfWeek property is used (this is the equivalent of Bars.Time[0].DayOfWeek).

This SessionInfo() method is implemented as follows:

// Retrieve the session information for a specific day
private void SessionInfo(DayOfWeek weekday)
{
    // Calculate the trading day
    int tradingDay = Math.Max(0, (int)weekday - 1);

    Output.WriteLine("{0}'s trading session started at {1} and ended at {2}",
        weekday,
        Bars.Sessions[tradingDay].StartTime,
        Bars.Sessions[tradingDay].EndTime);
}

This method starts with calculating the value of the tradingDay variable. Because enumerations are a set of named constants with a numeric value (cf. e.g., Liberty & MacDonald, 2009), we can explicitly cast the DayOfWeek enumeration to an integer value (meaning, (int)weekday).

Since the DayOfWeek enumeration is not zero-based while Bars.Sessions is, we need to subtract 1. But for some symbols, like ES that starts trading on Sunday, this would give a negative index value. By subsequently using the Max() method of the math class, we ensure that the lowest value is at least zero, so that calculated value also works for symbols that start trading in the weekend.

Then, finally, we use this tradingDay variable for outputting the session information for specific days with the use of string substitution parameters (lines 57-60).

References

Henry MultiCharts (2014). Retrieve Trading Sessions Date & Time forum discussion. Retrieved on February 15, 2014, from http://www.multicharts.com/discussion/viewtopic.php?f=19&t=46121&p=102386#p102238.

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.