With MultiCharts .NET trend lines we can highlight price action, such as an instrument’s opening range or pivot swing levels. But how to generate alerts when prices cross a trend line?

Drawing MultiCharts .NET trend lines and enabling alerts

The DrwTrendLine.Create() method draws trend lines and returns a reference to the line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). One way to change a trend line is by assigning its reference to an ITrendLineObject variable and then accessing the line’s properties and methods through that variable. That way we can do things like relocating, removing, and extending a trend line.

We can also access a line’s Alert property through such variable. This property can enable (or disable) alerts that are triggered when the price moves above or below the trend line (MultiCharts, 2014). These alerts can be a visual notification window, a sound, and/or an e-mail message (MultiCharts Wiki, 2013). We can also use the Alert property to retrieve the line’s current alert status (see PowerLanguage .NET Help, n.d.).

Both the setting and retrieving of a line’s alert settings is done with the EAlertType enumeration, which can take one of three values (PowerLanguage .NET Help, n.d.):

  • EAlertType.Disabled indicates the trend line doesn’t generate alerts (the default value).
  • EAlertType.BreakoutIntrabar is used for alerts that are triggered each time the price crosses the trend line intra-bar.
  • EAlertType.BreakoutOnClose are alerts triggered when the price bar closed above or below the trend line. Such alerts are triggered at most once per bar.

Interestingly, we do not need to enable a script’s alert settings before a programmatically drawn trend line can generate alerts. For an indicator, those settings are found by right-clicking the indicator’s name in the Status Line and choosing ‘Format [indicator name]‘. Then move to the ‘Alerts’ tab.

MultiCharts .NET script - alert disabled

What a trend line’s Alert property does, however, is configuring alerts for individual trend lines. These alert settings can be found (and manually changed) with a right-click on a trend line, choosing ‘Format ‘Trend Line”, and then select the ‘Alerts’ tab:

A MultiCharts .NET trend line's alerts enabled

Example: generating alerts with a programmatically drawn trend line

When the example indicator is applied to a EUR/USD chart, it draws a trend line like the following:

Example of MultiCharts .NET trend line alerts - 1

Then, as soon as the bar’s close crosses the trend line, it generates an alert:

Example of MultiCharts .NET trend line alerts - 2

And these alerts continue whenever the price crosses the trend line again:

Example of MultiCharts .NET trend line alerts - 3

Programmatically generating trend line alerts in MultiCharts .NET

The programming example looks as follows:

[SameAsSymbol(true)]
public class Example_TrendLineAlert : IndicatorObject
{
    public Example_TrendLineAlert(object _ctx) : base(_ctx) { }

    private ITrendLineObject trendLine;

    protected override void StartCalc()
    {
        ExecInfo.MaxBarsBack = 5;
    }

    protected override void CalcBar()
    {
        if (trendLine == null && Bars.LastBarOnChart && 
            Bars.Status == EBarState.Close)
        {
            ChartPoint begin = new ChartPoint(
                Bars.Time[1], Bars.Close[1]);

            ChartPoint end = new ChartPoint(
                Bars.Time[0], Bars.Close[1]);

            trendLine = DrwTrendLine.Create(begin, end);

            // Set the line's appearance
            trendLine.Size     = 2;
            trendLine.Color    = Color.MediumOrchid;
            trendLine.ExtRight = true;

            // Retrieve the trend line's current alert status
            Output.WriteLine("trendLine.Alert = {0}",
                trendLine.Alert);

            //> trendLine.Alert = Disabled

            // Enable the line's alerts
            trendLine.Alert = EAlertType.BreakoutOnClose;
        }
    }
}

We first set the SameAsSymbol attribute to true. That way the indicator is displayed on the data series and not in a separate subchart. Then we declare an ITrendLineObject interface variable named trendLine. We’ll use that variable to hold the trend line reference and to access the line’s properties and methods.

In the StartCalc() method we use the ExecInfo.MaxBarsBack property to set the indicator’s MaxBarsBack value explicitly (see MultiCharts, 2014). MaxBarsBack, short for maximum number of bars a study will reference, is the number of historical bars the script uses in its code (see MultiCharts Wiki, 2014). Setting this value ourselves, and not having it determined automatically, prevents that the script is automatically recalculated when it instead should draw its trend line.

Drawing a MultiCharts .NET trend line programmatically

Next is the CalcBar() method with an if statement that evaluates three conditions. The first is whether the trendLine variable equals (==) null. That happens when it hasn’t been assigned a value yet given that null is the default value for reference variables (Sempf, Sphar, & Davis, 2010). What this value of null means is that the variable doesn’t point to an object yet (Albahari & Albahari, 2012); so our trendLine variable hasn’t been associated with a trend line object on the chart. That also signals that our trendLine drawing isn’t made yet; otherwise, this variable will hold the value it’s assigned in the if statement’s code block.

The if statement’s second condition is whether the Bars.LastBarOnChart property returns true, which it does when the current bar is the last of the data series (see PowerLanguage .NET Help, n.d.). The third and last condition checks if the current script calculation is performed on bar close, in which case Bars.Status equals EBarState.Close (see MultiCharts, 2014).

When all three conditions evaluate to true, the if statement’s code is executed. That begins with creating two ChartPoint struct variables. The first, begin, is set to the previous bar’s time and close (Bars.Time[1] and Bars.Close[1]). The second, end, has its time set to the current bar (Bars.Time[0]) but its price set to the previous bar’s close (Bars.Close[1]). With these coordinates we can draw a small, horizontal trend line that starts at the previous and ends at the current bar.

With both ChartPoint coordinates defined, we call the DrwTrendLine.Create() method to draw a trend line and pass in the begin and end variables to define the line’s start and end point. We assign the value returned by this method to the trendLine variable.

Enabling trend line alerts programmatically

With that trendLine variable we can now adjust the trend line. To do so, we first change the line’s appearance by setting its Size and Color properties to new values. Then we extend the trend line indefinitely to the right by assigning its ExtRight property a value of true.

With the Output.WriteLine() method we then print a string and the current value of the line’s Alert property to the PowerLanguage .NET Editor Output Window. This shows how that property can be used to retrieve a line’s alert status.

Then the line’s Alert property is set to EAlertType.BreakoutOnClose to generate alerts when the last bar on the chart closes above or below the trend line. Other values we could have set the line’s Alert property to are EAlertType.Disabled (to turn alerts off) or EAlertType.BreakoutIntrabar to generate intra-bar alerts (see PowerLanguage .NET Help, n.d.).

There are two downsides to having trend line alerts generate automatically by MultiCharts .NET. First, alert settings (like the duration of the visual notification window) need to be set for every trend line manually. Second, the content of the alert message (“Trend Line triggered”; see images above) cannot be changed. We address both drawbacks in generate custom trend line alerts.

Summary

Trend lines are drawn with DrwTrendLine.Create(), a method that returns a reference to the line created. With that reference stored in an ITrendLineObject variable, we can access a line’s properties and methods through that variable. These include the Alert property that returns or sets the line’s alert state with one of three EAlertType values: Disabled (no alerts), BreakoutIntrabar (alerts generated with intra-bar price crosses), and BreakoutOnClose (alerts triggered by closing above or below the trend line).

Complete MultiCharts .NET indicator example

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

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

        private ITrendLineObject trendLine;

        protected override void StartCalc()
        {
            ExecInfo.MaxBarsBack = 5;
        }

        protected override void CalcBar()
        {
            if (trendLine == null && Bars.LastBarOnChart && 
                Bars.Status == EBarState.Close)
            {
                ChartPoint begin = new ChartPoint(
                    Bars.Time[1], Bars.Close[1]);

                ChartPoint end = new ChartPoint(
                    Bars.Time[0], Bars.Close[1]);

                trendLine = DrwTrendLine.Create(begin, end);

                // Set the line's appearance
                trendLine.Size     = 2;
                trendLine.Color    = Color.MediumOrchid;
                trendLine.ExtRight = true;

                // Retrieve the trend line's current alert status
                Output.WriteLine("trendLine.Alert = {0}",
                    trendLine.Alert);

                //> trendLine.Alert = Disabled

                // Enable the line's alerts
                trendLine.Alert = EAlertType.BreakoutOnClose;
            }
        }
    }
}

References

Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

MultiCharts (2014). MultiCharts .NET Programming Guide (version 1.1). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.1.pdf

MultiCharts Wiki (2013, May 10). Using Alerts. Retrieved on April 15, 2015, from http://www.multicharts.com/trading-software/index.php/Using_Alerts

MultiCharts Wiki (2014, September 15). Indicator Settings. Retrieved on April 15, 2015, from https://www.multicharts.com/trading-software/index.php/Indicator_Settings

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

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.