When we’ve drawn trend lines (by hand or with code) we’ll need to access them in order to further work with them. How do we access trend lines in MultiCharts .NET?

Drawing MultiCharts .NET trend lines and access them programmatically

In MultiCharts .NET, trend lines are either drawn programmatically with DrwTrendLine.Create() (MultiCharts, 2014) or manually with the drawing tools. After drawing a line we’ll often want to do more with it, like extend or remove the trend line, trade with the trend line or enable the trend line’s alerts. All of that requires programmatic access to the line.

We access a trend line through its ITrendLineObject reference variable. This ITrendLineObject interface specifies the different features and capabilities of trend lines, like the Size property for setting or retrieving a trend line’s size and the PriceValue() method for getting a trend line’s price value (PowerLanguage .NET Help, n.d.). A reference variable in C# is a variable that doesn’t represent the data it contains directly, but instead holds a reference to another block of computer memory that contains the actual data (Sharp, 2013; Stephens, 2014).

So let’s say we create an ITrendLineObject variable named myTrendLine and assign it the trend line reference returned by DrwTrendLine.Create(). That makes myTrendLine hold a reference to the trend line, but not the line itself. So when we reset myTrendLine to null (meaning it doesn’t point to an object anymore; Stephens, 2014), the trend line on the chart is unaffected. After all, myTrendLine only holds a reference to the trend line on the chart, just like a browser bookmark is a reference to the website on the internet (but not the actual website itself).

We can get an ITrendLineObject trend line reference in several ways (PowerLanguage .NET Help, n.d.):

Each of these approaches is discussed below.

Drawing a trend line and storing the reference in a variable

The DrwTrendLine.Create() method draws a trend line and returns a reference to the line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). By assigning that returned value in an ITrendLineObject variable we can work with the line afterwards. For example:

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

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

ITrendLineObject trendLine = DrwTrendLine.Create(lineBegin, lineEnd);

trendLine.Style = ETLStyle.ToolDashed;

We first create two ChartPoint struct variables here that hold the line’s coordinates. The first, lineBegin, is set to the time and close of 20 bars ago (Bars.Time[20] and Bars.Close[20]), while the second (lineEnd) holds the current bar’s time and close (Bars.Time[0] and Bars.Close[0]).

Then we call DrwTrendLine.Create() and pass in lineBegin and lineEnd to define the line’s begin and end points. The reference returned by this method is assigned to the ITrendLineObject trendLine variable. We subsequently use that variable to change the line’s appearance by setting its Style property to ETLStyle.ToolDashed (which makes the line dashed).

Examples that use the value returned by DrwTrendLine.Create() are:

Accessing a chart’s active trend line programmatically

Another way to get a trend line reference is with the DrwTrendLine.Active property that returns the ITrendLineObject reference of the chart’s active trend line (see MultiCharts, 2014; PowerLanguage .NET Help, n.d), and does so for lines that are drawn programmatically or manually. A drawing (text box, trend line, or arrow) is considered ‘active’ when it’s created or moved last, or when selected with a mouse click (MultiCharts, 2014).

An example of this property is:

ITrendLineObject activeTrendLine = DrwTrendLine.Active;

if (activeTrendLine != null)
{
    activeTrendLine.Size  = 2;
    activeTrendLine.Color = Color.Indigo;

    activeTrendLine.Alert = EAlertType.BreakoutOnClose;
}

Here we create an ITrendLineObject variable named activeTrendLine and assign it the value returned by the DrwTrendLine.Active property.

Then an if statement checks if activeTrendLine is unequal to (!=) null, which means the reference variable isn’t associated with any object (Stephens, 2014). DrwTrendLine.Active returns null when it can’t find an active trend line, for example when a line hasn’t been drawn yet or when the active trend line has been removed. This if statement check is important since trying to access a trend line’s properties and methods while the reference variable is null results in a NullReferenceException error (see Stellman & Greene, 2010).

When activeTrendLine is different than null, we change the line’s appearance by setting the line’s Size and Color properties to different values. Then the line’s Alert property is set to the EAlertType.BreakoutOnClose enumerated value to enable trend line alerts that are triggered on bar close.

Programming examples that use DrwTrendLine.Active are:

Retrieving a collection of all MultiCharts .NET trend lines

Another way of retrieving ITrendLineObject references is with the DrwTrendLine.GetTrendLineObjects() method, which returns a collection object that implements the IEnumerable<T> interface (see PowerLanguage .NET Help, n.d.). That interface allows the elements to be looped over with the foreach loop (Liberty & MacDonald, 2009; Stellman & Greene, 2010).

An example of looping through all trend lines on the chart is:

foreach (ITrendLineObject line in 
    DrwTrendLine.GetTrendLineObjects(EDrawingSource.AnyTechOrManual))
{
    Output.WriteLine("Trend line begin price: {0}, end price: {1}",
        line.Begin.Price,
        line.End.Price);

    line.Color = Color.GreenYellow;
}

Here we create a foreach loop that has its type set to ITrendLineObject. The collection it loops over is the one returned by DrwTrendLine.GetTrendLineObjects() with the EDrawingSource.AnyTechOrManual enumerated value passed in. With that value this method returns all manually drawn trend lines plus those that are created programmatically (PowerLanguage .NET Help, n.d.).

The foreach loop variable (named line) represents the element that’s currently being processed in the loop (Sharp, 2013) and is updated automatically with the collection’s next element on each subsequent loop cycle (Sempf, Sphar, & Davis, 2010). As such, accessing each individual trend line inside the loop is possible by simply using this line variable.

Inside the loop we print text to the Output Window with the Output.WriteLine() method. The outputted string contains the line’s begin and end price, which are returned by the line’s Begin.Price and End.Price properties (see PowerLanguage .NET Help, n.d.). We insert those values in the string with placeholders (the numbers between { and }) which are replaced by the values following the string when the code executes (Liberty & MacDonald, 2009; Sempf et al., 2010). We then change the line’s visual look by setting its Color property to Color.GreenYellow.

More examples of DrwTrendLine.GetTrendLineObjects() are:

More introductory articles about working with trend lines are essentials of drawing trend lines in MultiCharts .NET, ChartPoint structures explained, and an overview of trend line properties and methods.

Summary

Working with trend lines requires a line’s ITrendLineObject reference variable, which we can get access to in three ways. First, by storing the value returned by the DrwTrendLine.Create() method in an ITrendLineObject variable. Second, the DrwTrendLine.Active property returns a reference to the chart’s active trend line. And third, the DrwTrendLine.GetTrendLineObjects() method returns a collection object with references to all trend lines on the chart or a subset of them.


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

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.

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

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