The visual appearance and location of MultiCharts .NET trend lines can be changed programmatically. But how about creating a trend line that extends indefinitely into one or both directions?

Creating trend lines and extending them in MultiCharts .NET

The DrwTrendLine.Create() method creates a trend line and returns a reference to the line created (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). By assigning that reference to an ITrendLineObject interface variable the trend line’s properties and methods are accessible through that variable.

A trend line can be extended in two ways (MultiCharts, 2014; MultiCharts Wiki, 2014):

  • An extended trend line stretches itself to the left and right. This requires that the line’s ExtRight and ExtLeft properties are both set to true.
  • A ray line (also called beam line) is a trend line that extends itself into one direction. A line that continues endlessly to the left has its ExtLeft property set to true with the ExtRight property set to the default false. Likewise, a line that ‘beams’ to the right has ExtRight set to true with ExtLeft set to false.

Note that extending a trend line with the ExtLeft and ExtRight properties doesn’t change the line’s begin and end coordinates; all it does is adding an extension to the line (see MultiCharts Wiki, 2012; PowerLanguage .NET Help, n.d.). See changing a trend line’s location for how to change the line’s starting and ending point.

Creating a ray and an extended line in MultiCharts .NET

This article’s example creates a ray line and an extended line. When the indicator is added to a chart, like this EUR/USD chart, it first creates two regular trend lines:

Extend a MultiCharts .NET trend line - before

Then, after a few price updates, the orange line becomes a ray line and the blue line an extended line:

Extending a MultiCharts .NET trend line - after

Extending trend lines programmatically in MultiCharts .NET

The indicator’s programming code is the following:

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

    private ITrendLineObject rayLine, extendedLine;

    int counter = 0;

    protected override void CalcBar()
    {
        if (Bars.LastBarOnChart)
        {
            if (counter == 0)
            {
                // Creating trend lines and setting Size and Color
                rayLine = DrwTrendLine.Create(
                    new ChartPoint(Bars.Time[30], 
                        Bars.Close[30] - 0.0010),
                    new ChartPoint(Bars.Time[0], Bars.Close[0])
                    );

                extendedLine = DrwTrendLine.Create(
                    new ChartPoint(Bars.Time[30], 
                        Bars.Close[30] + 0.0010),
                    new ChartPoint(Bars.Time[0], Bars.Close[0])
                    );

                // Change the line's appearance
                rayLine.Size      = 2;
                extendedLine.Size = 2;

                rayLine.Color      = Color.Orange;
                extendedLine.Color = Color.Navy;
            }
            else if (counter == 30)
            {
                // Extending the trend lines
                rayLine.ExtLeft  = false;
                rayLine.ExtRight = true;

                extendedLine.ExtLeft  = true;
                extendedLine.ExtRight = true;
            }

            counter++;
        }
    }
}

We first set the SameAsSymbol attribute to true so that the indicator is plotted on the data series (and not in a subchart). With RecoverDrawings set to false we prevent intra-bar generated drawings from being removed (MultiCharts, 2014).

Then two ITrendLineObject interface variables are declared: rayLine and extendedLine. These are used later on to access and change the trend line properties. We also declare and initialise the counter variable, and we’ll use that integer to determine when to extend the lines.

Creating trend lines programmatically in MultiCharts .NET

The example’s next part is the CalcBar() method. In it an if statement evaluates whether Bars.LastBarOnChart returns true, which it does when the current bar is the last one (PowerLanguage .NET Help, n.d.). When that’s the case, a nested if/else statement is executed.

That if statement checks whether counter is zero with the equals operator (==). Immediately after the indicator has been added to the chart, this variable is zero and so this if statement’s code is executed. It begins with creating two trend lines with DrwTrendLine.Create() and this method’s returned value is stored in the rayLine and extendedLine variables.

Both lines have their starting point set to the time and close of 30 bars ago. By adding or subtracting an arbitrary value to that bar’s close the lines are placed some distance above or below the bar. The end coordinate of both lines is set to the current bar’s time (Bars.Time[0]) and price (Bars.Close[0]).

After creating the lines, we change their visual appearance by setting their Size and Color properties to different values.

Creating a ray or extended trend line in MultiCharts .NET

The else if portion of the if/else statement checks whether counter equals 30. When it does, its code block changes the line extension properties of both trend lines. The ray line (rayLine) has ExtLeft set to false with ExtRight set to true to have the line continue indefinitely to the right. For the extended line (extendedLine) both ExtLeft and ExtRight properties are set to true.

Below the if/else statement the counter variable is incremented with the postfix increment operator (++), which is just a shorthand way to write counter = counter + 1 (see Dorman, 2010). Since indicators are updated on every real-time tick by default, this variable is increased with 1 with each tick. This allows us to see how, after 30 price updates, both trend lines are extended by the code.

See removing a trend line and drawing a trend line into the future to learn more about modifying trend lines. Extended trend lines are also used for horizontal trend lines, vertical trend lines, and vertical trend lines that extend to the subchart.

Summary

The DrwTrendLine.Create() method draws a trend line between two chart coordinates and returns a reference that’s needed to further modify the line. When that reference is stored in an ITrendLineObject variable, the line’s properties and methods are accessible through that variable. Setting a line’s ExtLeft and ExtRight properties to true extend the trend line indefinitely in both directions, while setting one of these properties to true creates a line that continues endlessly in one direction.

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_ExtendTrendLine : IndicatorObject
    {
        public Example_ExtendTrendLine(object _ctx) : base(_ctx) { }

        private ITrendLineObject rayLine, extendedLine;

        int counter = 0;

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart)
            {
                if (counter == 0)
                {
                    // Creating trend lines and setting Size and Color
                    rayLine = DrwTrendLine.Create(
                        new ChartPoint(Bars.Time[30], 
                            Bars.Close[30] - 0.0010),
                        new ChartPoint(Bars.Time[0], Bars.Close[0])
                        );

                    extendedLine = DrwTrendLine.Create(
                        new ChartPoint(Bars.Time[30], 
                            Bars.Close[30] + 0.0010),
                        new ChartPoint(Bars.Time[0], Bars.Close[0])
                        );

                    // Change the line's appearance
                    rayLine.Size      = 2;
                    extendedLine.Size = 2;

                    rayLine.Color      = Color.Orange;
                    extendedLine.Color = Color.Navy;
                }
                else if (counter == 30)
                {
                    // Extending the trend lines
                    rayLine.ExtLeft  = false;
                    rayLine.ExtRight = true;

                    extendedLine.ExtLeft  = true;
                    extendedLine.ExtRight = true;
                }

                counter++;
            }
        }
    }
}

References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN: Sams/Pearson Education.

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

MultiCharts Wiki (2012, January 26). TL_SetExtLeft. Retrieved on February 16, 2015, from http://www.multicharts.com/trading-software/index.php/TL_SetExtLeft

MultiCharts Wiki (2014, October 1). Trend Line. Retrieved on February 16, 2015, from http://www.multicharts.com/trading-software/index.php/Trend_Line

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