Situation
You want to know how to terminate the calculation of an indicator or trading strategy in MultiCharts .NET and, by doing so, turning off the script programmatically.

Programming example

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

namespace PowerLanguage.Indicator
{
    [UpdateOnEveryTick(false), SameAsSymbol(true)]
    public class Example_AbortScriptCalculation : IndicatorObject
    {
        public Example_AbortScriptCalculation(object _ctx) : base(_ctx) { }
        
        protected override void CalcBar()
        {
            // Generate error message only with real-time data
            if (Environment.IsRealTimeCalc)
            {
                bool higherClose = Bars.Close[0] > Bars.Open[0];

                Output.WriteLine("{0} - Bar with a higher close? {1}",
                    Bars.Time[0].ToString("d-M-y HH:mm:ss"),
                    higherClose);

                if (higherClose)
                    ExecControl.Abort();
                else
                    ExecControl.Abort("Close ({0}) not higher than open ({1}).",
                        Bars.Close[0],
                        Bars.Open[0]);
            }
        }
    }
}

Output of the programming example

The indicator can generate the following error messages:

Example of aborting a MultiCharts .NET script Example of aborting a MultiCharts .NET script

Depending on how often the indicator needs to be reactivated both messages have been displayed, the output in the Output Window is something like the following:

7-7-14 15:35:00 - Bar with a higher close? True
7-7-14 15:40:00 - Bar with a higher close? True
7-7-14 15:41:00 - Bar with a higher close? True
7-7-14 15:43:00 - Bar with a higher close? True
7-7-14 15:44:00 - Bar with a higher close? False

Controlling the calculation of a MultiCharts .NET script

Controlling script calculation is one of the ways to control MultiCharts .NET programmatically, and this is done with methods accessible through the ExecControl property (PowerLanguage .NET Help, n.d.). Examples of this are recalculation after a time period with ExecControl.RecalcLastBarAfter() and immediate recalculation with ExecControl.Recalculate().

But stopping an indicator or trading strategy completely can also be helpful. For instance, turning off a strategy when there are no real-time price updates anymore prevents erratic behaviour when the data feed continues with a price gap.

Aborting script calculation in MultiCharts .NET

A MultiCharts .NET script can be stopped with the ExecControl.Abort() method that generates a run-time error with an alert-type message (PowerLanguage .NET Help, n.d.). This method can be used to stop calculation in either the StartCalc() or CalcBar() override methods.

By the way, the ExecControl.Abort() method will not only abort script calculation: it will also turn off the indicator or strategy completely.

ExecControl.Abort() is an overloaded method (PowerLanguage .NET Help, n.d.), meaning that the method can be used in different ways (see Liberty & MacDonald, 2009). One way to use ExecControl.Abort() is to generate a blank error message (see the first image above); the second way is using string substitution parameters to specify the error message (see second image).

MultiCharts .NET programming example

Two attributes are set at the start of the example:

[UpdateOnEveryTick(false), SameAsSymbol(true)]

With setting UpdateOnEveryTick to false the indicator is only calculated on bar close (meaning that the CalcBar() method is only called once per bar, at the closing tick). The SameAsSymbol attribute set to true plots the indicator on the main price chart.

Only calculate with real-time price data in MultiCharts .NET

The next part of the example is the CalcBar() override method:

protected override void CalcBar()
{
    // Generate error message only with real-time data
    if (Environment.IsRealTimeCalc)
    {
        bool higherClose = Bars.Close[0] > Bars.Open[0];

        Output.WriteLine("{0} - Bar with a higher close? {1}",
            Bars.Time[0].ToString("d-M-y HH:mm:ss"),
            higherClose);

        if (higherClose)
            ExecControl.Abort();
        else
            ExecControl.Abort("Close ({0}) not higher than open ({1}).",
                Bars.Close[0],
                Bars.Open[0]);
    }
}

The if statement in line 16 verifies if the script is calculated on real-time data, in which case the Environment.IsRealTimeCalc property returns true.

Inside the body of the if statement the Boolean higherClose variable is declared and set to a relational comparison of the close and open price data from the primary data series. The Output.WriteLine() method (lines 20-22) is subsequently called to output the bar’s DateTime formatted to a string and the value of the higherClose variable, both formatted into a string with string substitution parameters.

Generating error messages in MultiCharts .NET

The last part of the CalcBar() method, the nested if statement, aborts the calculation of the indicator:

if (higherClose)
    ExecControl.Abort();
else
    ExecControl.Abort("Close ({0}) not higher than open ({1}).",
        Bars.Close[0],
        Bars.Open[0]);

When the higherClose variable evaluates to true (so when the close is higher than the open), the ExecControl.Abort() method is called and a blank error message is generated (line 25).

In all other cases, due to the else keyword, the ExecControl.Abort() method is called with a string and string substitution parameters as arguments (lines 27-29). This generates an error message highlighting why the script was stopped.

Key points:

  • The calculation of an indicator or trading strategy can be terminated with the ExecControl.Abort() method;
  • The ExecControl.Abort() method can be used in two ways: with no arguments to generate a blank error message or with a string and substitution parameters to specify a message;
  • Calling the ExecControl.Abort() method will not only stop script calculation; it will also turn off the script completely.
References

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

PowerLanguage .NET Help (n.d.). Retrieved on May 16, 2014, from http://www.multicharts.com/downloads/PowerLanguage.NET.chm