Situation
You’re working with the Expert Commentary window but with every click on a bar your indicator or strategy gets recalculated. This poses problems when you, for example, only want to output data once or just makes the overall calculation process sluggish. You’re looking for a way to execute certain code segments only once.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_ExpertCommentary : IndicatorObject
    {
        private bool expertActiveStartCalc = false;
        private bool expertActiveCalcBar   = false;
        private string outputString        = "";

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

        protected override void Create()
        {
            // Clear the output log once when the indicator
            // is created (i.e. added to the chart)
            Output.Clear();
        }

        protected override void StartCalc()
        {
            // Perform the following code block only if the bool is false
            // and the Expert Commentary window is active
            if (!expertActiveStartCalc && ExpertCommentary.Enabled == true)
            {
                Output.WriteLine("StartCalc() - Expert Commentary is enabled");

                // Perform the calculations that 
                // are needed once here

                expertActiveStartCalc = true;
            }
        }

        protected override void CalcBar()
        {
            // Perform the following code block only if the bool is false
            if (!expertActiveCalcBar && ExpertCommentary.Enabled)
            {
                Output.WriteLine("CalcBar() - Expert commentary is enabled");

                // Perform the calculations that 
                // are needed once here

                expertActiveCalcBar = true;
            }

            // Using the Expert Commentary to display some general information
            if (ExpertCommentary.AtCommentaryBar)
            {
                outputString = string.Format("This is bar number {0} with date {1} and time {2}.",
                    Bars.CurrentBar,
                    Bars.Time[0].ToString("dd-MM-yy"),
                    Bars.Time[0].ToString("HH:mm:ss"));

                Output.WriteLine(outputString);
                ExpertCommentary.WriteLine(outputString);
            }
        }
    }
}

MultiCharts .NET’s Expert Commentary

This example begins with a useful MultiCharts .NET attribute (line 8). The SameAsSymbol attribute ensures that this indicator is plotted on the main data series and not in a subchart.

The two boolean variables in lines 11-12 will be used later on to execute our code segments only once. In the Create() method, one of the override methods, Output.Clear() is called when the indicator is created (i.e. added to a chart) so that the output log is kept orderly.

In the StartCalc() method, the following code is used:

// Perform the following code block only if the bool is false
// and the Expert Commentary window is active
if (!expertActiveStartCalc && ExpertCommentary.Enabled == true)
{
    Output.WriteLine("StartCalc() - Expert Commentary is enabled");

    // Perform the calculations that 
    // are needed once here

    expertActiveStartCalc = true;
}

The if statement tests two conditions. First, the boolean expertActiveCalcBar needs to evaluate to false. Note that ! means ‘not’: this boolean needs not to be true; i.e. false. Second, the Expert Commentary window needs to be open. This is checked with the ExpertCommentary.Enabled property, which returns true if this window is opened.

If both of these conditions evaluate to true, the code segment is executed and the output is generated. By setting expertActiveCalcBar to false, we're ensuring that this code block can only be executed once.

Of course, we could do the same in the CalcBar() method that is called on every bar:

// Perform the following code block only if the bool is false
if (!expertActiveCalcBar && ExpertCommentary.Enabled)
{
    Output.WriteLine("CalcBar() - Expert commentary is enabled");

    // Perform the calculations that 
    // are needed once here

    expertActiveCalcBar = true;
}

Here the logic is like we saw with the code used in the StartCalc() method. For background reading, see the article boolean expressions and logical operators in C# for more information about the evaluations made in the if statements in the examples above.

Outputting information in MultiCharts .NET

Finally, in lines 53 till 62 we’re outputting information to both the PowerLanguage Editor output tab and the Expert Commentary window:

// Using the Expert Commentary to display some general information
if (ExpertCommentary.AtCommentaryBar)
{
    outputString = string.Format("This is bar number {0} with date {1} and time {2}.",
        Bars.CurrentBar,
        Bars.Time[0].ToString("dd-MM-yy"),
        Bars.Time[0].ToString("HH:mm:ss"));

    Output.WriteLine(outputString);
    ExpertCommentary.WriteLine(outputString);
}

The property ExpertCommentary.AtCommentaryBar returns true if the bar is clicked on by the user while the Expert Commentary window is active. So, with every click on a bar the above code segment is executed. The value of Bars.Time[0] is formatted in this example using the C# formatting DateTime strings possibilities.

The output in the PowerLanguage Editor output tab looks like:

StartCalc() - Expert Commentary is enabled
CalcBar() - Expert commentary is enabled
This is bar number 2309 with date 03-10-13 and time 02:00:00.
This is bar number 2317 with date 03-10-13 and time 02:40:00.
This is bar number 2325 with date 03-10-13 and time 03:20:00.
This is bar number 2442 with date 03-10-13 and time 13:05:00.
This is bar number 2454 with date 03-10-13 and time 14:05:00.
This is bar number 2469 with date 03-10-13 and time 15:20:00.
This is bar number 2438 with date 03-10-13 and time 12:45:00.
This is bar number 2420 with date 03-10-13 and time 11:15:00.
This is bar number 2391 with date 03-10-13 and time 08:50:00.
This is bar number 2379 with date 03-10-13 and time 07:50:00.
This is bar number 2364 with date 03-10-13 and time 06:35:00.

The first two lines here show our code segments being executed only once, despite the multiple clicks on bars afterwards. Furthermore, should you close the Expert Commentary window and reopen it, the code segments (lines 28-36 and 42-50) will still not execute again - you'll manually need to remove the indicator from the chart for that.

By using a simple boolean variable the above example assures that these code segments do not get needlessly recalculated while using an Expert Commentary window.

See the article Working with the Expert Commentary window to learn more about the Expert Commentary window.