When a trend line is drawn programmatically, it’s placed on the chart with standard colour, thickness, and style settings. How can we change a line’s visual appearance?

Changing the formatting of a trend line in MultiCharts .NET

Trend lines in MultiCharts .NET are created with the DrwTrendLine.Create() method, which returns a reference to the trend line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). When that reference is assigned to an ITrendLineObject interface variable, we can use that variable to access the trend line’s properties and methods.

Three properties change a trend line’s look (MultiCharts, 2014; PowerLanguage .NET Help, n.d.):

  • With the Color property we can set the line’s colour.
  • The Size property changes the size of a trend line, which can range from 0 (thinnest line) to 6 (thickest line).
  • And with the Style the line’s style is changed. This property is set with an ETLStyle enumerated value, which can be any of the following:
Formatting a trend line's style in MultiCharts .NET

Trend lines can also be changed manually by right-clicking on a trend line and selecting ‘Format Trend Line’. The above three properties correspond to the manual options as follows:

Modify a trend line manually in MultiCharts .NET

Example: changing a trend line’s look in MultiCharts .NET

When the example indicator is added to a 30-minute EuroStoxx 50 future chart, it draws a green, dashed trend line:

Example of changing a MultiCharts .NET's trend lines look

Programmatically changing a trend line’s appearance

The example’s programming code is the following:

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

    private bool isAlreadyCreated;

    protected override void StartCalc()
    {
        // Reset isAlreadyCreated each time 
        // the script calculates fully
        isAlreadyCreated = false;
    }

    protected override void CalcBar()
    {
        if (Bars.LastBarOnChart && isAlreadyCreated == false)
        {
            // Create the begin and end points
            // and draw the trend line
            ChartPoint beginPoint = new ChartPoint(
                Bars.FullSymbolData.Time[55],
                Bars.FullSymbolData.High[55]);

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

            ITrendLineObject myTrendLine =
                DrwTrendLine.Create(beginPoint, endPoint);

            // Adjust the trend line
            myTrendLine.Color = Color.ForestGreen;
            myTrendLine.Size  = 3;
            myTrendLine.Style = ETLStyle.ToolDashed;

            isAlreadyCreated = true;
        }
    }
}

We first set the SameAsSymbol attribute to true so that the indicator is plotted on the data series and not in a subchart. Setting UpdateOnEveryTick to false ensures the script is calculated on bar close only. That also prevents the script from making intra-bar generated drawings, which are removed by default during the next script calculation (MultiCharts, 2014).

The Boolean variable isAlreadyCreated is then declared and assigned false in the StartCalc() method. This resets the variable at the beginning of every full script calculation so that enabling/disabling the indicator manually redraws the trend line. isAlreadyCreated is used later to prevent multiple trend lines from being drawn.

Creating a trend line programmatically in MultiCharts .NET

The CalcBar() method contains an if statement that checks whether the current bar is the last (in which case Bars.LastBarOnChart returns true; PowerLanguage .NET Help, n.d.) and if isAlreadyCreated is false. When both conditions satisfy, the if statement’s code is executed.

That code begins with creating two ChartPoint structs for the line’s begin and end points. For the starting point we use the Bars.FullSymbolData property to access any bar of the primary data series (see PowerLanguage .NET Help, n.d.). That way we access historical bar data from 55 bars ago.

The line’s ending point (endPoint) is set to the current bar’s time (Bars.Time[0]) and close (Bars.Close[0]). Now that both chart points are defined, we create the trend line with the DrwTrendLine.Create() method. The ITrendLineObject reference returned by this method is assigned to the myTrendLine variable so that we can change the line afterwards.

Adjusting the line’s visual appearance

That myTrendLine variable is then used to change the trend line’s appearance. We do that by setting the Color property to Color.ForestGreen, the Size property to 3, and the line’s Style property is set to the ETLStyle.ToolDashed enum value.

In the example’s last statement the isAlreadyCreated Boolean variable is set to true. Since that invalidates the CalcBar()’s if statement, the trend line is only drawn once.

For more on changing trend lines, see modifying a trend line’s location and extending trend lines. Other things we can do with trend lines are removing them from the chart and trigger trend line alerts.

Summary

Trend lines are made with DrwTrendLine.Create(). This method returns a reference to the created trend line. When that reference is stored in an ITrendLineObject variable, that variable can be used to modify the trend line. Changing a line’s visual appearance is done by setting its Color, Size, and Style properties.

Complete MultiCharts .NET indicator example

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

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

        private bool isAlreadyCreated;

        protected override void StartCalc()
        {
            // Reset isAlreadyCreated each time 
            // the script calculates fully
            isAlreadyCreated = false;
        }

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart && isAlreadyCreated == false)
            {
                // Create the begin and end points
                // and draw the trend line
                ChartPoint beginPoint = new ChartPoint(
                    Bars.FullSymbolData.Time[55],
                    Bars.FullSymbolData.High[55]);

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

                ITrendLineObject myTrendLine =
                    DrwTrendLine.Create(beginPoint, endPoint);

                // Adjust the trend line
                myTrendLine.Color = Color.ForestGreen;
                myTrendLine.Size  = 3;
                myTrendLine.Style = ETLStyle.ToolDashed;

                isAlreadyCreated = true;
            }
        }
    }
}

References

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