In MultiCharts .NET we can draw trend lines programmatically and also create horizontal trend lines. But how to draw a vertical trend line that extends to the chart’s top and bottom?

Drawing trend lines in MultiCharts .NET programmatically

Drawing MultiCharts .NET trend lines is done with the DrwTrendLine.Create() method that at least requires two ChartPoint values to define the line’s begin and end point (see PowerLanguage .NET Help, n.d.). This method returns a reference to the line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.), which, when assigned to an ITrendLineObject variable, provides access to the line’s methods and properties. With that we can do things like relocating a trend line or changing a line’s visual appearance.

Creating a vertical trend line works just like creating a regular trend line. The only difference is that, instead of creating two ChartPoint values with different DateTime values, both chart coordinates have the same date and time (but different prices).

A trend line that expands to the top and bottom of the chart can be made in several ways. One way is setting the line’s begin and end points beyond the visible chart area. But when new price bars are created and the price scale is adjusted, we can end up seeing where the vertical trend lines end.

A better alternative is to draw a small vertical line and then extend the trend line in either direction, which is done by setting the line’s ExtRight and ExtLeft properties to true (MultiCharts, 2014). That causes the line to continue indefinitely up and down, and we don’t need to complicate things by determining the coordinates of the currently visible chart area. Let’s consider the programming example to see how.

Example: drawing extended vertical trend lines

When the indicator is added to an E-Mini Dow Jones chart, it draws vertical trend lines like the following:

Vertical lines in MultiCharts .NET

If we drag-‘n-drop the chart’s price scale with the mouse up and down, we can see how the lines continue beyond the original chart area:

Vertical trend lines in MultiCharts .NET - extended price scale

Programmatically drawing vertical lines in MultiCharts .NET

This example’s programming code looks as follows:

[SameAsSymbol(true), RecoverDrawings(false)]
public class Example_TrendLinesVertical : IndicatorObject
{
    public Example_TrendLinesVertical(object _ctx) : base(_ctx) { }

    private bool linesAlreadyDrawn;

    protected override void StartCalc()
    {
        linesAlreadyDrawn = false;
    }

    protected override void CalcBar()
    {
        if (Bars.LastBarOnChart && !linesAlreadyDrawn)
        {
            // Draw the first vertical line 20 bars ago
            ChartPoint begin = new ChartPoint(
                Bars.Time[20], Bars.High[20]);

            ChartPoint end = new ChartPoint(
                Bars.Time[20], Bars.Low[20]);

            ITrendLineObject verticalLine = DrwTrendLine.Create(begin, end);

            // Extend the line so it encompassed the whole chart
            verticalLine.ExtLeft  = true;
            verticalLine.ExtRight = true;

            // Change the line's appearance
            verticalLine.Size  = 4;
            verticalLine.Color = Color.DarkOrange;
            verticalLine.Style = ETLStyle.ToolDashed;

            // Draw the second trend line 5 bars ago
            begin = new ChartPoint(Bars.Time[5], Bars.High[5]);
            end   = new ChartPoint(Bars.Time[5], Bars.Low[5]);

            ITrendLineObject anotherVerticalLine = 
                DrwTrendLine.Create(begin, end);

            // Extend the line in both directions
            anotherVerticalLine.ExtLeft  = true;
            anotherVerticalLine.ExtRight = true;

            // Change the line's appearance
            anotherVerticalLine.Size  = 2;
            anotherVerticalLine.Color = Color.ForestGreen;

            linesAlreadyDrawn = true;
        }
    }
}

We start with setting the SameAsSymbol attribute to true to display the indicator on the data series. And with RecoverDrawings set to false we prevent intra-bar generated drawings from being removed automatically (MultiCharts, 2014).

Then we declare the linesAlreadyDrawn Boolean variable that’s used later on to prevent the lines from being drawn repeatedly. This variable is initialised to false in the StartCalc() method, which is executed at the start of a full script calculation on all price bars (MultiCharts, 2014). By setting this variable in the StartCalc() method, we reset it so that with each script calculation the trend lines can be drawn.

Drawing a dashed vertical trend line in MultiCharts .NET

Next in the example is CalcBar() with an if statement that checks two conditions. The first is whether the Bars.LastBarOnChart property is true, which is the case when the current bar is the last of the data series (see PowerLanguage .NET Help, n.d.). The second condition uses the logical not operator (!) to see if the linesAlreadyDrawn variable is false. This operator evaluates to the opposite of the value it precedes (Liberty & MacDonald, 2009); so when linesAlreadyDrawn is false, !linesAlreadyDrawn returns true.

When both conditions evaluate to true, the if statement’s code block is executed. That begins with declaring two ChartPoint struct variables: begin and end. The first is set to the time and high of 20 bars ago (Bars.Time[20] and Bars.High[20]), while the second chart coordinate is set to the time and low of the same bar (Bars.Low[20]).

With both ChartPoint variables defined, we call the DrwTrendLine.Create() method and pass in both variables to draw a trend line between them. The value returned by this method is assigned to the verticalLine variable to make the line’s properties and methods easily accessible.

But so far that trend line is only a small line between the high and low of 20 bars ago. To expand it to the top and bottom of the chart we set the line’s ExtLeft and ExtRight properties to true. After that we use the verticalLine variable to adjust the line’s appearance by setting the Size, Color, and Style properties to different values.

Drawing a solid vertical trend line in MultiCharts .NET

The second trend line is drawn in pretty much the same way as the first. With the begin and end ChartPoint variables already declared, we can reuse them instead of creating new variables. By instantiating two ChartPoint structs we assign new values to those variables: the first (begin) set to the time and high of 5 bars ago (Bars.Time[5] and Bars.High[5]) while the second (end) is set to the low of that same bar (Bars.Low[5]).

We then call DrwTrendLine.Create() with both variables passed in to draw a trend line between those chart locations. This method’s returned value is stored in the anotherVerticalLine variable. We extend the trend line in both directions by setting its ExtLeft and ExtRight properties to true, followed by changing its Size and Color properties to end up with a vertical, solid, forest green line.

The example’s last statement sets the linesAlreadyDrawn Boolean variable to true. Since that invalidates CalcBar’s if statement, the trend lines are only drawn once. That prevents the lines from being drawn repeatedly with each real-time tick.

Other examples that use MultiCharts .NET trend lines in a non-typical way are drawing triangles and rectangles with trend lines. We can also draw horizontal trend lines or make vertical trend lines extend to the subchart.

Summary

Trend lines are drawn programmatically with the DrwTrendLine.Create() method. When that method’s returned reference is assigned to an ITrendLineObject variable, a line’s properties and methods become accessible through that variable. Making a vertical line requires two ChartPoint chart locations with the same DateTime value but different price values. Such a line can be extended to the chart’s top and bottom by setting its ExtLeft and ExtRight properties to true.

Complete MultiCharts .NET indicator example

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

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

        private bool linesAlreadyDrawn;

        protected override void StartCalc()
        {
            linesAlreadyDrawn = false;
        }

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart && !linesAlreadyDrawn)
            {
                // Draw the first vertical line 20 bars ago
                ChartPoint begin = new ChartPoint(
                    Bars.Time[20], Bars.High[20]);

                ChartPoint end = new ChartPoint(
                    Bars.Time[20], Bars.Low[20]);

                ITrendLineObject verticalLine = DrwTrendLine.Create(begin, end);

                // Extend the line so it encompassed the whole chart
                verticalLine.ExtLeft  = true;
                verticalLine.ExtRight = true;

                // Change the line's appearance
                verticalLine.Size  = 4;
                verticalLine.Color = Color.DarkOrange;
                verticalLine.Style = ETLStyle.ToolDashed;

                // Draw the second trend line 5 bars ago
                begin = new ChartPoint(Bars.Time[5], Bars.High[5]);
                end   = new ChartPoint(Bars.Time[5], Bars.Low[5]);

                ITrendLineObject anotherVerticalLine = 
                    DrwTrendLine.Create(begin, end);

                // Extend the line in both directions
                anotherVerticalLine.ExtLeft  = true;
                anotherVerticalLine.ExtRight = true;

                // Change the line's appearance
                anotherVerticalLine.Size  = 2;
                anotherVerticalLine.Color = Color.ForestGreen;

                linesAlreadyDrawn = 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