Once we’ve drawn trend lines, we for example might want to change their location or visual appearance. But how do we retrieve all those trend lines from the chart?

Retrieving a collection of MultiCharts .NET trend lines from the chart

We draw trend lines in MultiCharts .NET programmatically with the DrwTrendLine.Create() method, which returns a reference to the line made (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). When that reference is stored in an ITrendLineObject interface variable, we can access the line’s properties and methods through the variable. That way we can remove, move, extend the trend line and more. But this also means that, without a reference to the trend line object on the chart, we cannot programmatically work with a trend line.

Fortunately, we can retrieve references to a group of trend lines with the DrwTrendLine.GetTrendLineObjects() method (see PowerLanguage .NET Help, n.d.). This method returns a collection object that implements the IEnumerable<T> interface (see PowerLanguage .NET Help, n.d.), which is a type-safe collection interface that makes enumeration (that is, looping) with the foreach loop possible (Liberty & MacDonald, 2009; Stellman & Greene, 2010).

This T in IEnumerable<T> is a placeholder for a particular data type and indicates it’s generic: the T will be replaced with whichever type the collection should hold (Sempf, Sphar, & Davis, 2010; Stellman & Greene, 2010). In the case of MultiCharts .NET trend lines, the DrwTrendLine.GetTrendLineObjects() method returns an IEnumerable<ITrendLineObject> collection (PowerLanguage .NET Help, n.d.).

Note that, since the IEnumerable<T> interface only contains the GetEnumerator() method that returns the next object to loop over (Sharp, 2013), we cannot remove elements from a collection that solely implements this interface. What we can do, however, is converting the collection returned by DrwTrendLine.GetTrendLineObjects() to another collection type that does allow adding and removing elements. An example of this is given in looping over a list of trend lines in MultiCharts .NET.

Specifying which MultiCharts .NET trend lines to retrieve from the chart

The DrwTrendLine.GetTrendLineObjects() method has one required argument: a value from the EDrawingSource enumeration that specifies the drawing source of the trend lines in the collection (PowerLanguage .NET Help, n.d.). The values of that enumeration have the following effect when used with DrwTrendLine.GetTrendLineObjects() (PowerLanguage .NET Help, n.d.):

  • With EDrawingSource.CurrentTech a collection of trend lines that’s created by the current script is returned.
  • EDrawingSource.NotCurrentTechOrManual gives a collection of trend lines that are not created by the current script nor manually (so it only includes trend lines created by another script).
  • EDrawingSource.AnyTechOrManual specifies that a collection of trend lines made by any script or manually should be returned.
  • With EDrawingSource.CurrentTechOrManual a collection of trend lines that are either created by the current script or manually is returned.
  • EDrawingSource.NotCurrentTech returns all trend lines that were not created by the current script.
  • EDrawingSource.AnyTech returns a collection of trend lines created by any script.
  • And EDrawingSource.Manual returns all trend lines that were drawn manually.

Let’s look at a few examples to see how DrwTrendLine.GetTrendLineObjects() can be used.

Example: retrieving all manual and programmatically drawn trend lines

Getting a collection with DrwTrendLine.GetTrendLineObjects() that contains all the chart’s trend lines is done as follows:

IEnumerable<ITrendLineObject> allTrendLines = 
    DrwTrendLine.GetTrendLineObjects(EDrawingSource.AnyTechOrManual);

Here we pass the AnyTechOrManual value from the EDrawingSource enumeration in the DrwTrendLine.GetTrendLineObjects() method, causing it to return a collection with all trend lines made by the script or manually. This method’s returned value is assigned to the IEnumerable<ITrendLineObject> collection object named allTrendLines, which we for example can use to loop over a trend line collection.

Example: getting all trend lines drawn by the current script

DrwTrendLine.GetTrendLineObjects() can also return all trend lines made by the current script, like so:

IEnumerable<ITrendLineObject> scriptTrendLines = 
    DrwTrendLine.GetTrendLineObjects(EDrawingSource.CurrentTech);

Or the same method can get all manually drawn trend lines from the chart with the EDrawingSource.Manual argument:

IEnumerable<ITrendLineObject> manualTrendLines = 
    DrwTrendLine.GetTrendLineObjects(EDrawingSource.Manual);

Adding IEnumerable’s namespace to the script’s source code

The IEnumerable<T> interface originates from the System.Collections.Generic namespace (Microsoft Developer Network, n.d.). When we assign the value returned by DrwTrendLine.GetTrendLineObjects() to a collection object that implements IEnumerable<T>, as we did in the previous examples, it’s helpful to have this namespace added to the script. That way we don’t need to qualify the interface with the namespace it belongs to each time we use it (Sharp, 2013).

In other words, with the namespace added to the script’s code we can suffice with typing IEnumerable<ITrendLineObject>; without the namespace, we need to qualify the interface with the namespace like this: System.Collections.Generic.IEnumerable<ITrendLineObject>.

Adding the System.Collections.Generic namespace is done with a using directive in the top of the script’s source code:

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.Collections.Generic;       // Added for IEnumerable

Working with trend line collections in MultiCharts .NET

The collection returned by DrwTrendLine.GetTrendLineObjects() can be used in several ways. Programming examples using that method include:

Working with a collection of trend lines often means operating on each trend line in turn. The most convenient way to do that is by looping over a trend line collection.

Summary

Trend lines are drawn with DrwTrendLine.Create(), a method that returns a trend line reference. With that reference we can access the trend line’s properties and methods to work with the line programmatically. The DrwTrendLine.GetTrendLineObjects() method returns all trend lines or a subset of them. The collection object this method returns implements the IEnumerable<T> interface (from the System.Collections.Generic namespace), and its argument requires a value from the EDrawingSource enumeration that specifies the type of trend lines to return.


References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

Microsoft Developer Network (n.d.). IEnumerable<T> Interface. Retrieved on June 3, 2015, from https://msdn.microsoft.com/en-us/library/9eekhta0%28v=vs.110%29.aspx

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.