Situation
You want to know how to create an alert in MultiCharts .NET. In this example we will create a price-based alert for bars that close higher or lower than the previous five bars.

Programming example

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

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

        protected override void CalcBar()
        {
            // Return if the current bar is not the last bar on the chart
            if (!Bars.LastBarOnChart)
                return;

            double fiveBarHigh = Bars.High.Highest(5, 1);
            double fiveBarLow  = Bars.Low.Lowest(5, 1);

            if (Bars.Low[0] < fiveBarLow)
            {
                Alerts.Alert("The current bar low ({0}) is below the five bar low ({1}).",
                    Bars.Low[0], fiveBarLow);
            }
            else if (Bars.High[0] > fiveBarHigh)
            {
                Alerts.Alert("The current bar high ({0}) is above the five bar high ({1}).",
                    Bars.High[0], fiveBarHigh);
            }
        }
    }
}

Output of programming example

Applied to a chart that receives real-time data or is updated through the Data Playback mode, the indicator generates alerts like the following:

Example of alerts in MultiCharts .NET

Alerts in MultiCharts .NET

Alerts are visual, audio and/or e-mail notifications triggered by certain conditions in the indicator or strategy by which special events are brought to the attention of the user (e.g., see MultiCharts, 2013).

In the following circumstances MultiCharts .NET checks if the condition(s) of an alert are triggered (MultiCharts Wiki, 2013):

  • When the workspace is opened;
  • When the data series is switched to another data series of a different symbol;
  • When the last price bar on the chart is closed or updated.

Any alerts that are triggered are kept for reference purposes in the Alerts tab of the Order and Position Tracker.

Alerts work in real-time and in the Data Playback mode. Use the Expert Commentary to generate alert-type messages on historical price bars (MultiCharts, 2013).

Enabling or disabling alerts

Turning alerts on or off is done in the following way:

  • Trading strategies: right-click on the price chart and choose 'Format Signals'. Select 'Properties' to go the 'Strategy Properties' window. Select the 'Alerts' tab to enable or disable the alerts.
  • Indicators: right-click the price chart and choose 'Format Indicators'. Select the indicator and click on 'Format'. In the 'Format Study' window, go to the 'Alerts' tab and enable or disable the alerts for that indicator.

Besides programmatically adding alerts you will also need to manually enable the alert option for the indicator or strategy; otherwise alerts will not be triggered.

See managing alerts in MultiCharts .NET for an example of how to programmatically verify the alert settings.

Programming example - setting attributes

We start off the programming example by adding two attributes (lines 8-9). The SameAsSymbol attribute is set to true to ensure that the indicator is plotted on the price chart and not on a separate sub-chart. And with the UpdateOnEveryTick attribute set to false, the indicator is only updated on the bar close instead of updated on every incoming tick.

Returning control to the calling method

In the CalcBar() override method (lines 14-33) we first verify with the logical not operator (!) whether or not the current bar is the last bar on the chart:

// Return if the current bar is not the last bar on the chart
if (!Bars.LastBarOnChart)
    return;

Should the price bar be a different bar than the last bar on the chart (in which case Bars.LastBarOnChart returns false), the return statement is executed. This statement causes the execution of the method to be terminated and returns control to the calling method (Microsoft Developer Network [MSDN], n.d.).

Since alerts can only be triggered on the last bar of the chart, there is no need to perform calculations for alert generation when the current bar is not the last bar on the chart. By using return a (slight) performance gain is achieved with calculations that are much more complex than this example.

Generating alerts based on price action

In the second part of the CalcBar() method, the actions for the alerts are performed:

double fiveBarHigh = Bars.High.Highest(5, 1);
double fiveBarLow  = Bars.Low.Lowest(5, 1);

if (Bars.Low[0] < fiveBarLow)
{
    Alerts.Alert("The current bar low ({0}) is below the five bar low ({1}).",
        Bars.Low[0], fiveBarLow);
}
else if (Bars.High[0] > fiveBarHigh)
{
    Alerts.Alert("The current bar high ({0}) is above the five bar high ({1}).",
        Bars.High[0], fiveBarHigh);
}

First, the highest and lowest values for the last five bars (with an offset of 1 bar so that the current bar is not included) are stored in two double variables (lines 20-21).

Next, an if statement and else-if statement check if the current low is below the five bar low (lines 23-27) or if the current high is above the five bar high (lines 28-32), respectively. Should either one of these conditional expressions evaluate to true, the Alerts.Alert() method is called and a string with the alert message, formatted with string substitution parameters, is displayed in the alert pop-up.

See managing alerts in MultiCharts .NET for how to cancel already generated alerts.

Key points:

  • Alerts can only be generated in real-time or in Data Playback mode;
  • Alerts are created by calling the Alerts.Alert() method, which (optionally) accepts a string with the alert message;
  • Alerts are only generated when the indicator or strategy settings have alerts enabled.
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

Microsoft Developer Network [MSDN] (n.d.). Return (C# Reference). Retrieved on January 30, 2014, from http://msdn.microsoft.com/en-us/library/1h3swy84.aspx