Situation
Besides creating alerts in MultiCharts .NET you would like to know how to manage alerts, for example by cancelling already generated alerts and notifying the user of wrong alert settings.

Programming example

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

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

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

            // Check if the user has enabled the alert option
            if (!Alerts.Enabled)
                throw new Exception("Oops! You forgot to enable alerts in the indicator settings!");
        }

        protected override void CalcBar()
        {
            string alertMessage;

            // Check if alerts are enabled and the current bar is the last bar on the chart
            if (Alerts.CheckAlertLastBar)
            {
                // Generate alert if the high is above the previous bar high..
                if (Bars.High[0] > Bars.High[1])
                {
                    alertMessage = string.Format("The current bar high ({0}) is above the previous bar high ({1}).",
                        Bars.High[0], Bars.High[1]);

                    Output.WriteLine("{0} - Generating alert: {1}",
                        Bars.Time[0].ToString("d-M HH:mm:ss"),
                        alertMessage);

                    Alerts.Alert(alertMessage);
                }

                // ..but cancel alert if the close is below the previous bar close
                if (Bars.Close[0] < Bars.Close[1])
                {
                    Output.WriteLine("{0} - Cancelling the alert: Close ({1}) is below previous close ({2}).",
                        Bars.Time[0].ToString("d-M HH:mm:ss"),
                        Bars.Close[0], Bars.Close[1]);

                    Alerts.Cancel();
                }
            }
        }
    }
}

Output of the programming example

Applied to a chart, the indicator generates alerts like the following:

Example of managing alerts in MultiCharts .NET

With the accompanying PowerLanguage .NET Editor output of:

30-1 11:00:00 - Generating alert: The current bar high (3004,9) is above the previous bar high (2995,9).
30-1 11:30:00 - Generating alert: The current bar high (3010,9) is above the previous bar high (3004,9).
30-1 12:00:00 - Generating alert: The current bar high (3011,9) is above the previous bar high (3010,9).
30-1 12:00:00 - Cancelling the alert: Close (3003,9) is below previous close (3008,9).

Here we see that two alerts are generated but that the third alert (line 3 of the output) is cancelled on the same bar.

Should the user forget to enable the alerts for the indicator, the following error message is shown:

Throwing an exception with alerts in MultiCharts .NET

Working with MultiCharts .NET alerts

In this follow-up article to the generating alerts article we will look at three things:

  • Throwing an exception if alerts are disabled;
  • Checking if alerts are enabled and the current bar is the last bar on the chart;
  • Cancelling alerts that are already generated on the current bar.

These are further discussed below. But first we will take a look at setting the attributes of the indicator.

Setting indicator attributes

By setting the SameAsSymbol attribute (line 8) to true, the indicator is plotted on the main price chart and not on a separate sub-chart. And setting the UpdateOnEveryTick attribute (line 9) to false causes the indicator not to be updated on every tick but just on the close of the bar.

Verifying correct settings and throwing an exception

In the StartCalc() method the PowerLanguage .NET Editor output tab is cleared by calling Output.Clear() (line 16) at the start of every calculation. Then we verify whether or not the user has alerts enabled (lines 18-20):

// Check if the user has enabled the alert option
if (!Alerts.Enabled)
    throw new Exception("Oops! You forgot to enable alerts in the indicator settings!");

Alerts can only be generated if the indicator or strategy alert settings are manually enabled (MultiCharts Wiki, 2013), regardless of whether or not the programming code for alerts is added. So if a user accidentally forgets to enable the alert settings, a lot of time can needlessly be spend searching for a programming ‘error’ where there is none to be found.

To prevent that from happening, we first check if the alert settings are enabled with the Alerts.Enabled property by using the logical not operator (!) in the if statement (line 19). Should alerts be disabled, in which case this property returns false, an error exception is thrown to signal to the user that the settings need to be changed.

By the way, see creating alerts for how to enable or disable indicator and trading strategy alert settings.

Checking if alerts are enabled on the last bar of the chart

In the CalcBar() override method, a string variable is declared (alertMessage) followed by the evaluation of a conditional expression in the first if statement:

// Check if alerts are enabled and the current bar is the last bar on the chart
if (Alerts.CheckAlertLastBar)
{
  // ...
}

The Alerts.CheckAlertLastBar property returns true if the current bar is the last bar on the chart and if alerts are enabled in the settings. By using this property, the body of the if statement (lines 30-51) is only executed on the last bar on the chart. Since alerts can only be generated on the last bar (see MultiCharts Wiki, 2013), there is no point in this example to evaluate the alert conditions on the other bars.

The Alerts.Enabled property returns true if alerts are enabled in the indicator or strategy settings and returns false if these are disabled. The Alerts.CheckAlertLastBar property, on the other hand, returns only true if alerts are enabled in the settings and if the current bar is the last bar on the chart. This makes Alerts.CheckAlertLastBar more restrictive and, in other words, the result of the expression Alerts.Enabled && Bars.LastBarOnChart.

Generating an alert in MultiCharts .NET

The first nested if statement evaluates a conditional expression to see if the high of the current bar is above the previous high:

// Generate alert if the high is above the previous bar high..
if (Bars.High[0] > Bars.High[1])
{
    alertMessage = string.Format("The current bar high ({0}) is above the previous bar high ({1}).",
        Bars.High[0], Bars.High[1]);

    Output.WriteLine("{0} - Generating alert: {1}",
        Bars.Time[0].ToString("d-M HH:mm:ss"),
        alertMessage);

    Alerts.Alert(alertMessage);
}

If this condition evaluates to true, a message is generated with the use of string substitution parameters and assigned to the alertMessage string variable (lines 33-34). This string is then passed into the Output.WriteLine() method (lines 36-38) and the Alerts.Alert() method (line 40), where it triggers the alert pop-up box with the specified text.

By the way, see the article generating alerts in MultiCharts .NET for more information on generating alerts.

Cancelling already generated alerts

The second nested if statement checks to see if the current close is below the previous close:

// ..but cancel alert if the close is below the previous bar close
if (Bars.Close[0] < Bars.Close[1])
{
    Output.WriteLine("{0} - Cancelling the alert: Close ({1}) is below previous close ({2}).",
        Bars.Time[0].ToString("d-M HH:mm:ss"),
        Bars.Close[0], Bars.Close[1]);

    Alerts.Cancel();
}

Should this if statement evaluate to true, information to keep track of the indicator is written to the PowerLanguage .NET Editor output log. Then, by calling the Alerts.Cancel() method, alert generation for the current bar is cancelled (see MultiCharts, 2013).

Key points:

  • Use the Alerts.Enabled property to verify if the alert settings are enabled or not;
  • Use the Alerts.CheckAlertLastBar property to check if the alert settings are enabled and if the current bar is the last bar on the chart;
  • Call the Alerts.Cancel() method to cancel an already generated alert on that bar.
References

MultiCharts (April 2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf

MultiCharts Wiki (May 2013). Using Alerts. Retrieved on January 31, 2014, from http://www.multicharts.com/trading-software/index.php/Using_Alerts