Drawing trend lines by hand is a common way to highlight price action. But how can we make trend line programmatically in MultiCharts .NET?

Creating a trend line in MultiCharts .NET

Drawing a trend line with code in MultiCharts .NET consists out of two steps (MultiCharts, 2014; PowerLanguage .NET Help, n.d.):

  • First we need to define two chart coordinates for the line’s begin and end point. That’s done with a ChartPoint struct, which are lightweight versions of a class (see Sharp, 2013) that hold the price (y-axis) and time (x-axis) coordinate.
  • We then draw the trend line with the DrwTrendLine.Create() method. At a minimum, this method needs two ChartPoint struct values that define where the line needs to begin and where it should end. We can optionally store the value returned by DrwTrendLine.Create() in an ITrendLineObject variable to access the line afterwards.

Let’s turn to the programming example to see how these steps are implemented.

Drawing trend lines with code in MultiCharts .NET

When the indicator is added to an EuroStoxx 50 future chart, it draws a trend line as follows:

Drawing a trend line in MultiCharts .NET

Programmatically drawing a trend line in MultiCharts .NET

The indicator’s programming code is:

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

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

    private bool isAlreadyDrawn = false;

    protected override void CalcBar()
    {
        // Draw the trend line on the close of the chart's last bar,
        // when isAlreadyDrawn indicates we haven't drawn the line already
        if (Bars.LastBarOnChart && Bars.Status == EBarState.Close
            && !isAlreadyDrawn)
        {
            // Create the line's begin and end point
            ChartPoint beginPoint = new ChartPoint(
                Bars.Time[25],
                Bars.Low[25]);

            ChartPoint endPoint = new ChartPoint(
                Bars.Time[0],
                Bars.Low[0]);

            // Draw the trend line
            DrwTrendLine.Create(beginPoint, endPoint);

            isAlreadyDrawn = true;
        }
    }
}

We first set the SameAsSymbol attribute to true so that the indicator is plotted on the data series. Then, in the StartCalc() method, the indicator’s MaxBarsBack value is set to 25 bars with the ExecInfo.MaxBarsBack property. That prevents that the script automatically recalculates itself when the initially guessed MaxBarsBack value is too low.

Then we declare the isAlreadyDrawn Boolean variable and initialise it to false. That variable is used later on to prevent the indicator from drawing the trend line repeatedly.

Drawing a trend line programmatically in MultiCharts .NET

Next is the CalcBar() method which begins with an if statement that checks three requirements. First, the current bar needs to be the last, in which case Bars.LastBarOnChart returns true (see PowerLanguage .NET Help, n.d.). Second, the primary data series’ current bar status (Bars.Status) needs to be closed (EBarState.Close). This way we don’t end up making an intra-bar drawing that’s removed with the next script calculation. The last requirement is that isAlreadyDrawn is false, tested here with the logical not operator (!) that returns true when its value is false (Liberty & MacDonald, 2009).

When all three conditions evaluate to true, we make two ChartPoint structs. The first (beginPoint) is set to the time and low of 25 bars ago and marks the line’s begin point, while the second (endPoint) defines the line’s ending point and is set to the current bar’s time and low.

The trend line is drawn with the DrwTrendLine.Create() method, which has both ChartPoint variables passed in to define the line’s starting and ending point. To prevent drawing this line repeatedly, we set the isAlreadyDrawn variable to true. That invalidates the if statement (which required this variable to be false).

To learn more about trend lines, see adjusting a trend line’s colour, size, and style, extending a trend line, and moving a trend line. The article about MultiCharts .NET’s ChartPoint structures takes a closer look at those chart coordinates.

Summary

The DrwTrendLine.Create() method draws a trend line between a begin and end point. Those chart locations are specified with ChartPoint structs, which consist out of a time (x-axis) and price (y-axis) value.

Complete MultiCharts .NET indicator example

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

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

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

        private bool isAlreadyDrawn = false;

        protected override void CalcBar()
        {
            // Draw the trend line on the close of the chart's last bar,
            // when isAlreadyDrawn indicates we haven't drawn the line already
            if (Bars.LastBarOnChart && Bars.Status == EBarState.Close
                && !isAlreadyDrawn)
            {
                // Create the line's begin and end point
                ChartPoint beginPoint = new ChartPoint(
                    Bars.Time[25],
                    Bars.Low[25]);

                ChartPoint endPoint = new ChartPoint(
                    Bars.Time[0],
                    Bars.Low[0]);

                // Draw the trend line
                DrwTrendLine.Create(beginPoint, endPoint);

                isAlreadyDrawn = true;
            }
        }
    }
}

References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. 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

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

Sharp, J. (2013). Microsoft Visual C# 2013 Step by Step. Microsoft Press.