Situation
You want to explore historical data with the use of the Expert Commentary window instead of working with real-time generated alerts.

MultiCharts .NET programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    [UpdateOnEveryTick(false)]
    public class ExpertCommentary_Example : IndicatorObject
    {
        private IPlotObject highestHighPlot;
        private double highestHigh;

        public ExpertCommentary_Example(object _ctx) : base(_ctx) { }

        protected override void Create()
        {
            highestHighPlot = AddPlot(new PlotAttributes("HighestHigh", EPlotShapes.Line, Color.Yellow));
        }

        protected override void CalcBar()
        {
            // Get the highest high for the last 15 bars
            highestHigh = Bars.High.Highest(15);

            // Plot highest high line
            highestHighPlot.Set(highestHigh);

            // If the Expert Commentary window is not active,
            // return control to the caller
            if (!ExpertCommentary.Enabled)
                return;

            // Create an Expert Commentary text if there's a new high
            if (Bars.High[0] == highestHigh)
            {
                ExpertCommentary.WriteLine("New highest high: {0}.",
                    Bars.High[0]);
            }

            // Generate a default text for every bar that is clicked on
            if (ExpertCommentary.AtCommentaryBar)
            {
                ExpertCommentary.WriteLine("You've clicked on bar number: {0}.",
                    Bars.CurrentBar + ExecInfo.MaxBarsBack);
            }
        }
    }
}

Output of the programming example

In this example an Expert Commentary message is generated for every new 15 bar high. If you click on a bar with a rising plotted line, something like the following is displayed in the Expert Commentary:

Example of the MultiCharts .NET Commentary Window

On the other bars the message will be like this:

Example of the MultiCharts .NET Commentary Window

MultiCharts .NET’s Expert Commentary

In this example three code segments related to the Expert Commentary are used. We started with checking if the Expert Commentary window is active or not:

// If the Expert Commentary window is not active,
// return control to the caller
if (!ExpertCommentary.Enabled)
    return;

If the Expert Commentary window is active, the ExpertCommentary.Enabled property returns true (and false in all other cases). Through the use of the logical not operator (!) this if statement is true when the Expert Commentary window is not active.

In that case, the return statement is executed. This returns the control to the calling method, which means that all remaining programming lines in the CalcBar() method (lines 35 till 47) will not be executed if the Expert Commentary window is not active. This prevents needless calculations since all lines of code in this method after line 33 only need to be executed when the Expert Commentary window is active.

Generating Expert Commentary messages

In the second code section of the example, an Expert Commentary message is generated if the if statement condition is true:

// Create an Expert Commentary text if there's a new high
if (Bars.High[0] == highestHigh)
{
    ExpertCommentary.WriteLine("New highest high: {0}.",
        Bars.High[0]);
}

If the high of the current bar is the highest high of the last 15 bars, this information will be displayed in the Expert Commentary window.

Generating Expert Commentary messages for clicked-on bars

In the last part of the example, a message is generated for every bar that the user clicks on:

// Generate a default text for every bar that is clicked on
if (ExpertCommentary.AtCommentaryBar)
{
    ExpertCommentary.WriteLine("You've clicked on bar number: {0}.",
        Bars.CurrentBar + ExecInfo.MaxBarsBack);
}

The property ExpertCommentary.AtCommentaryBar returns true if the Expert Commentary window is active and the user click on a bar. Here for every bar that is clicked on, the bar number (the current bar plus the MaxBarsBack setting of the indicator) is returned. With this property you can therefore execute certain code depending on which bar is clicked on.

This last part also highlights that you can write multiple lines for one bar in the Expert Commentary window.

See the article How to prevent code recalculation with the Expert Commentary window? to learn more about working with the Expert Commentary window.