Trend lines can be drawn manually as well as with code in MultiCharts .NET. But how do we draw them programmatically?

How to draw trend lines with code in MultiCharts .NET?

Programmatically drawing trend lines consists out of two required and one optional step (see MultiCharts, 2014; PowerLanguage .NET Help, n.d.):

  • First, the line’s begin and end points need to be defined. We do this with a ChartPoint struct that holds the time and price coordinates of a location on the data series or indicator subchart. See MultiCharts .NET’s ChartPoint structs explained for more.
  • We then create the line itself with the DrwTrendLine.Create() method that draws a trend line between two ChartPoint chart coordinates.
  • Optionally, we can assign the value returned by DrwTrendLine.Create() to an ITrendLineObject interface variable. That variable can then be used to work with the trend line, such as changing the line’s location or adjusting its visual appearance.

These three steps are further discussed below.

Step 1) Defining ChartPoint structs for a MultiCharts .NET trend line

A C# structure, or struct for short, groups methods, variables, and other members in a ‘container’ so that they can be stored and manipulated as a group (Liberty & MacDonald, 2009; Stephens, 2014). This makes them suited to hold related information together (see Sharp, 2013).

The ChartPoint struct in MultiCharts .NET is used to define a point on the chart (PowerLanguage .NET Help, n.d.). That makes these structs hold information about a chart location’s price and time coordinate.

Three things are needed to create a struct variable (see Sharp, 2013). First, the type needs to be provided (ChartPoint in this case) followed by an identifier (that is, a for that context unique name like recentLowPoint). Then, to initialise the struct, the new keyword needs to be followed by calling a ChartPoint constructor. So for example:

ChartPoint lineBegin = new ChartPoint(Bars.Time[10], Bars.Close[10]);

Here the lineBegin ChartPoint struct variable is made and set to the time and close of 10 bars ago (Bars.Time[10] and Bars.Close[10]). For more on ChartPoint structs, see what are MultiCharts .NET’s ChartPoint structs.

Step 2) Drawing a trend line between two chart coordinates

Trend lines are made with the DrwTrendLine.Create() method (MultiCharts, 2014). This overloaded method can be used in three ways (PowerLanguage .NET Help, n.d.):

Examples of these different versions of DrwTrendLine.Create() are given below.

Drawing a trend line on the price chart in MultiCharts .NET

The first version of DrwTrendLine.Create() draws a trend line on price data, like so:

ChartPoint begin = new ChartPoint(Bars.Time[5], Bars.Low[5]);
ChartPoint end   = new ChartPoint(Bars.Time[0], Bars.High[0]);

DrwTrendLine.Create(begin, end);

We first create two ChartPoint struct variables: begin holds the coordinates of the low from 5 bars ago while end corresponds to the current bar’s high. Then DrwTrendLine.Create() is called with both variables as arguments to draw a trend line between them.

We can also initialise the ChartPoint structs inside the DrwTrendLine.Create() method without creating variables first, like this:

DrwTrendLine.Create(
    new ChartPoint(Bars.Time[5], Bars.Low[5]),
    new ChartPoint(Bars.Time[0], Bars.High[0])
    );

Once a trend line is made, its coordinates are retrievable with the Begin and End properties that both return a ChartPoint struct (see PowerLanguage .NET Help, n.d.). An example later in this article shows how to do that.

Drawing a trend line in an indicator’s subchart

The second version of DrwTrendLine.Create() draws a line in an indicator’s subchart. For example:

ChartPoint startPoint = new ChartPoint(Bars.Time[20], Bars.Close[20]);
ChartPoint endPoint   = new ChartPoint(Bars.Time[5], Bars.Close[5]);

DrwTrendLine.Create(startPoint, endPoint, true);

These two ChartPoint variables hold the chart locations of 15 and 5 bars ago. We then call the DrwTrendLine.Create() method to draw a trend line between them. That method’s third argument, a Boolean true value, creates a trend line in the indicator’s subchart; otherwise, with a value of false the line would have been drawn on the script’s data series.

Drawing a trend line on a different data series

The third way to use DrwTrendLine.Create() is by drawing a trend line on a specific data series:

ChartPoint startData2 = new ChartPoint(
    BarsOfData(2).Time[20], BarsOfData(2).High[20]);

ChartPoint endData2 = new ChartPoint(
    BarsOfData(2).Time[1], BarsOfData(2).High[1]);

DrwTrendLine.Create(startData2, endData2, 2);

We create two ChartPoint variables here: startData2 is set to the time and high of 20 bars ago while endData2 holds coordinates of the previous bar. With the BarsOfData() method we can access price data from any data series on the chart, and this method works just like the Bars property for the primary data series (MultiCharts, 2014).

Then we call DrwTrendLine.Create() with the two coordinate variables and an integer as arguments. That latter value specifies the data series to draw the line on; with a value of 2 the line is drawn on the second data series.

Tip: The order in which trend lines are drawn on multiple data series matters: with two data series, the trend line on the second data series needs to be created first and the line on the primary data series should be drawn last (MultiCharts Support, personal communication, March 16, 2015).

Step 3) Accessing a trend line through its variable

The third, optional step when drawing a trend line is assigning the value returned by DrwTrendLine.Create() to an ITrendLineObject variable, which then can be used to manipulate the trend line (MultiCharts, 2014). For example:

ITrendLineObject highsLine = DrwTrendLine.Create(
    new ChartPoint(Bars.Time[10], Bars.High[10]),
    new ChartPoint(Bars.Time[0], Bars.High[0])
    );

highsLine.Color = Color.Green;
highsLine.Size  = 2;

Here we declare an ITrendLineObject variable named highsLine which we assign the value returned by DrwTrendLine.Create(). This line between the high 10 bars ago and the current high then has its visual appearance changed by setting new values to the Color and Size properties through the highsLine variable.

In a similar way we can use the highsLine variable to retrieve the line’s coordinates:

double lineBeginPrice = highsLine.Begin.Price;
double lineEndPrice   = highsLine.End.Price;

DateTime lineStartTime = highsLine.Begin.Time;
DateTime lineEndTime   = highsLine.End.Time;

The Begin.Price and End.Price properties return the line’s price coordinates of its starting and ending point (see PowerLanguage .NET Help, n.d.). We store these values here in two double variables. Then the time coordinates of the line’s points, accessible through the Begin.Time and End.Time properties, are assigned to two DateTime variables.

We can also get the ChartPoint chart coordinates through a trend line variable like this:

ChartPoint lineStart = highsLine.Begin;
ChartPoint lineEnd   = highsLine.End;

These Begin and End properties both return a ChartPoint struct corresponding to the chart location of the line’s begin and end point (PowerLanguage .NET Help, n.d.). See working with ChartPoint structs in MultiCharts .NET for more.

Tip: prevent trend lines from being removed automatically

Drawings (trend lines, arrows, and text boxes) made during an intra-bar script calculation are removed by default in MultiCharts .NET, while drawings created on bar close are kept (MultiCharts, 2014). This behaviour can cause trend lines to display for a second or so before disappearing from the chart.

That behaviour can be changed by setting the RecoverDrawings attribute to true, which makes drawings created when the bar hasn’t closed also persistent (MultiCharts, 2014).

Summary

The DrwTrendLine.Create() method can draw trend lines on price data, an indicator’s subchart, or on another data series. This method requires at least two ChartPoint structs that specify the line’s begin and end point. The value returned by DrwTrendLine.Create() can be assigned to an ITrendLineObject variable to alter the line once drawn.


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.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.