After drawing a trend line in MultiCharts .NET, we can use the line’s properties and methods to work with the line. Which of these properties and method are there and how do they work?

Accessing a MultiCharts .NET trend line’s properties and methods

We draw a trend line with DrwTrendLine.Create(), a method that returns a reference to the line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). Storing this reference in an ITrendLineObject interface variable is the most common way of accessing the trend line later on.

With such a variable we can access the line’s properties and methods (see MultiCharts, 2014). Properties are used in C# to safely retrieve or modify a variable in a class, and a class is a way to programmatically arrange information and behaviour into a meaningful entity (Liberty & MacDonald, 2009; Sharp, 2013).

A method is a sequence of programming statements grouped together that typically perform a clearly defined action (Liberty & MacDonald, 2009; Sharp, 2013). A method is equivalent to a function, procedure, or subroutine used in other programming languages, but with the difference that a C# method is always part of a class (Sempf, Sphar, & Davis, 2010).

MultiCharts .NET trend lines have the following methods and properties (MultiCharts, 2014; PowerLanguage .NET Help, n.d.):

Member Description
Alert Retrieves or sets the trend line’s alert status. Possible values are EAlertType.Disabled (for disabling alerts), EAlertType.BreakoutIntraBar (for triggering alerts multiple times intra-bar), and EAlertType.BreakoutOnClose to generate alerts on bar close.
Begin This property returns or sets the trend line’s begin coordinates with a ChartPoint struct.
Color A trend line’s colour is retrieved or set with this property.
Delete() This method removes the trend line from the chart.
End With this property we get or set the trend line’s coordinates with a ChartPoint struct.
Exist A read-only property that returns a Boolean value that indicates if the trend line is located on the chart (when true). It returns false when the line has been removed, for example with Delete().
ExtLeft Returns a Boolean value that indicates if the trend line is extended to the left (true) or not (false). We can also enable or disable extension to the left with this property.
ExtRight Sets a line’s extend-to-the-right feature to enabled (true) or disabled (false). This property can also return a Boolean value with the current setting.
ID This property returns an integer with the drawing ID. When a trend line is deleted, its ID is made available for reuse (Henry MultiCharts, 2015). That means trend line IDs aren’t always numbered consecutive and the last line drawn may not have the highest ID number.
Locked This property locks a trend line so it can’t be moved accidentally (when set to true) or unlocks a line again (false).
PriceValue() This method returns the line’s price value for any DateTime value on the chart.
Size Gets or sets the trend line’s size with an integer. This property’s values range from 0 (the default, thinnest line) to 6 (thickest line).
Style This property gets or sets the style of the trend line with a value from the ETLStyle enumeration. Options are a solid (ETLStyle.ToolSolid), dashed (ETLStyle.ToolDashed, ETLStyle.ToolDashed2, and ETLStyle.ToolDashed3), or dotted (ETLStyle.ToolDotted) trend line.

Examples of these properties and methods are given below.

Setting a trend line’s alert status programmatically

A line’s alert status can be set or retrieved with the Alert property (PowerLanguage .NET Help, n.d.). For example:

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

alertLine.Alert = EAlertType.BreakoutIntrabar;

Output.WriteLine("Alert type = {0}", alertLine.Alert);

Here the DrwTrendLine.Create() method first draws a trend line between two ChartPoint structs, which we use to define chart coordinates in MultiCharts .NET (see PowerLanguage .NET Help, n.d.). We set the first to the time and low of 10 bars ago (Bars.Time[10] and Bars.Low[10]), while the second holds the current bar’s time and high. The value returned by DrwTrendLine.Create() is subsequently assigned to the alertLine variable.

We then use that variable to set the line’s Alert property to EAlertType.BreakoutIntrabar. Now each time when the price crosses the trend line an alert is generated. We then use the same property to output the line’s current alert status to the PowerLanguage .NET Editor Output Window with Output.WriteLine(). For a more elaborative example of Alert, see automatically generating trend line alerts.

Retrieving and setting a trend line’s coordinates

A trend line’s Begin and End properties return or set a ChartPoint struct value with the line’s begin and end coordinates (PowerLanguage .NET Help, n.d.). An example of these properties is:

ITrendLineObject activeLine = DrwTrendLine.Active;

if (activeLine != null)
{
    Output.WriteLine("Begin price = {0} and end time = {1}",
        activeLine.Begin.Price,
        activeLine.End.Time);

    activeLine.Begin = new ChartPoint(
        Bars.Time[12], Bars.Close[12]);
}

Here the ITrendLineObject variable (named activeLine) is assigned the value returned by DrwTrendLine.Active, a property that returns the chart’s active trend line (see MultiCharts, 2014; PowerLanguage .NET Help, n.d.).

We then use an if statement to see if activeLine is unequal to (!=) null. Such a value means the reference variable (here activeLine) isn’t associated with an object (Albahari & Albahari, 2012). That happens when DrwTrendLine.Active cannot find an active trend line on the chart (drawn programmatically or by hand). This check is important since trying to access a trend line’s methods or properties when its variable is null triggers a NullReferenceException error (see Stellman & Greene, 2010).

And so when activeLine isn’t null, we can safely access the line’s Begin.Price and End.Time properties to output them with Output.WriteLine(). This first property returns the price value of the line’s begin point while the second returns the DateTime value of its end coordinate. We then change the line’s starting point by assigning its Begin property a new ChartPoint struct set to the time and close of 12 bars ago (Bars.Time[12] and Bars.Close[12]).

Additional examples that use a line’s Begin and End properties are:

Setting a trend line’s colour, size, and style properties

A trend line’s Color, Size, and Style properties can set or retrieve aspects of a trend line’s visual appearance (PowerLanguage .NET Help, n.d.). For instance:

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

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

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

theTrendLine.Color = Color.LimeGreen;
theTrendLine.Size  = 2;
theTrendLine.Style = ETLStyle.ToolDashed;

Here we first create two ChartPoint struct variables named begin and end. The first of these chart coordinates is set to the time and close of 10 bars ago (Bars.Time[10] and Bars.Close[10]) while the second holds the current bar’s time and close.

We subsequently call the DrwTrendLine.Create() method and pass in the begin and end variables to draw a trend line between them. The ITrendLineObject reference returned by this method is then assigned to the theTrendLine variable.

Next we use that variable to set the line’s Color, Size, and Style properties to new values, leading to a dashed, lime green trend line that’s slightly thicker than normal. See changing a trend line’s appearance for more (including an overview of possible Style values).

Extending a trend line to the left/right in MultiCharts .NET

With a line’s ExtLeft and ExtRight properties we can enable or disable a line’s extension to the left or right (PowerLanguage .NET Help, n.d.). This is illustrated below:

ITrendLineObject extendedLine = DrwTrendLine.Create(
    new ChartPoint(Bars.Time[1], Bars.Close[0]),
    new ChartPoint(Bars.Time[0], Bars.Close[0])
    );

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

Output.WriteLine("Trend line extends to the left? {0}",
    extendedLine.ExtLeft);

Here we draw a horizontal trend line with DrwTrendLine.Create() that has its first ChartPoint chart coordinate set to the previous bar’s time and current bar’s close. Its second ChartPoint holds the current bar’s time and close, making a small horizontal line between those two points.

We assign the value returned by this method to the extendedLine variable, which we then use to set the line’s ExtLeft and ExtRight properties to true. As the Output.WriteLine() statement shows, these properties can also retrieve a line’s extension settings.

More examples of ExtLeft and ExtRight are:

Removing a trend line and verifying if a line exists

The Delete() method removes a trend line from the chart (MultiCharts, 2014) while the Exist property returns a Boolean true/false value that indicates if the line is on the chart or not (see PowerLanguage .NET Help, n.d.). For example:

ChartPoint startingPoint = new ChartPoint(
    Bars.Time[5], Bars.Low[5]);

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

ITrendLineObject lineToDelete = 
    DrwTrendLine.Create(startingPoint, endingPoint);

lineToDelete.Delete();

if (!lineToDelete.Exist)
{
    Output.WriteLine("Trend line has been removed from the chart");
}

We create two ChartPoint struct variables here: the first (startingPoint) is set to the time and low of 5 bars ago while the second (endingPoint) holds the coordinates of the current bar’s time and low. Then we call the DrwTrendLine.Create() method to draw a trend line between these two chart coordinates and put this method’s returned value in the lineToDelete variable.

We then remove that trend line with its Delete() method. After that an if statement checks if Exist returns false, which we evaluate here by preceding it with the logical not operator (!). That operator returns true when it’s placed before an expression that’s false and vice versa (Liberty & MacDonald, 2009). Since we’ve just removed the line, Exist returns false and so Output.WriteLine() prints a confirming message to the PowerLanguage .NET Editor.

To learn more about Delete() and Exist, see:

Returning a trend line’s ID and lock/unlock a trend line

The ID property returns a line’s numerical ID while the Locked property locks a trend line to prevent unnecessary user changes (PowerLanguage .NET Help, n.d.). An example of these properties is:

ITrendLineObject theActiveLine = DrwTrendLine.Active;

 if (theActiveLine != null && theActiveLine.ID == 8)
 {
     theActiveLine.Locked = true;
 }

We start by assigning the theActiveLine variable the value returned by the DrwTrendLine.Active property, which fetches the chart’s active trend line (see MultiCharts, 2014).

The if statement then evaluates two conditions. The first checks if the theActiveLine variable is unequal to (!=) null. When that is the case, we know that the DrwTrendLine.Active property has succeeded in retrieve a line’s reference and that it’s safe to access the line’s properties without triggering a NullReferenceException (see Stellman & Greene, 2010). The second condition is whether the line’s ID property is equal to (==) 8.

Because both conditions are combined with the logical and operator (&&), which performs short-circuit evaluation (Albahari & Albahari, 2012), the second condition isn’t evaluated when the first is false. That makes it safe to use the line’s ID property in the second condition, because when the theActiveLine variable is null, this second condition won’t be executed.

When both conditions evaluate to true, we set the line’s Locked property to true so that the line can no longer be accidentally moved.

To learn more about the ID and Locked properties, see:

Retrieving a trend line’s price value in MultiCharts .NET

A trend line’s PriceValue() method returns the line’s price value for any DateTime value on the chart (PowerLanguage .NET Help, n.d.). For instance:

ITrendLineObject priceLine = DrwTrendLine.Create(
    new ChartPoint(Bars.Time[7], Bars.Range(7)),
    new ChartPoint(Bars.Time[3], Bars.Range(3))
    );

Output.WriteLine("Current bar's price value: {0}",
    priceLine.PriceValue(Bars.Time[0]));

Here we draw a trend line with DrwTrendLine.Create(). Its first ChartPoint struct argument is set to the time and range of 7 bars ago (Bars.Time[7] and Bars.Range(7)); its second chart coordinate holds the time and range of 3 bars ago. We assign the value returned by DrwTrendLine.Create() to the priceLine variable.

The Output.WriteLine() method then outputs the line’s price value of the current bar to the PowerLanguage .NET Editor. We do that by calling the line’s PriceValue() method and passing in the DateTime value of the current bar (Bars.Time[0]). Even though the priceLine trend line ended 3 bars ago, PriceValue() still returns a value for the current bar since it extrapolates the trend line’s price values.

More examples of PriceValue() can be found in:

Summary

A line’s ITrendLineObject reference variable provides access to the trend line’s properties and methods. The Alert property enables or disables alerts, while the Color, Size, and Style properties change a trend line’s visual appearance. The Begin and End properties return a ChartPoint struct with the line’s begin and end coordinates, or can assign such a struct to the line’s starting and end point. The ExtLeft and ExtRight properties extend the trend line, whereas the ID property identifies a trend line by its numerical ID. Locked is a property that, when set to true, prevents accidentally moving the trend line. We remove a trend line from the chart with Delete() while Exist indicates if a line is there or not. And with PriceValue() we retrieve a line’s price for any DateTime value on the chart.

Want to learn more about C#, the programming language that drives MultiCharts .NET? Checkout my C# programming tutorials.


References

Albahari, J. & Albahari, B. (2012). C# 5.0 in a Nutshell: The Definitive Reference (5th edition). Sebastopol, CA: O’Reilly Media.

Henry MultiCharts (2015, April 29). TL_new, TL_delete, TL_lock – forum discussion. Retrieved on June 22, 2015, from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=48459#p115296

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

Sempf, B., Sphar, C., & Davis, S.R. (2010). C# 2010 All-In-One for Dummies. Hoboken, NJ: John Wiley & Sons.

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

Stellman, A. & Greene, J. (2010). Head First C#: A Brain-Friendly Guide (2nd edition). Sebastopol, CA: O’Reilly Media.

Visit Kodify for more helpful coding articles.