MultiCharts .NET trend lines can be extended into the future and even drawn into the future. But how can we get a trend line’s future price values?

Creating MultiCharts .NET trend lines and retrieving future line values

Trend lines are made with the DrwTrendLine.Create() method. This method requires at least two ChartPoint structs (for the line’s begin and end point) and returns an interface reference to the line created (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). Alternatively, we can retrieve the reference to a line by selecting the active trend line or through selecting a specific trend line. Those references, when stored in an ITrendLineObject variable, allow access to the line’s properties and methods.

One trend line method is PriceValue(), which returns the line’s price value that belongs to a specific DateTime value on the chart (MultiCharts, 2014; PowerLanguage .NET Help, n.d.). This method can return both future and historical trend line values, but it doesn’t require that the trend line is actually drawn on those bars. With that feature we can, for example, relocate a trend line beyond its current coordinates.

Tip: Retrieving future trend line values with PriceValue() requires that those DateTime values are actually on the chart (that includes the ‘empty bars’ in the chart’s right margin). When the DateTime value passed into the PriceValue() method is beyond the chart’s x-axis, PriceValue() returns the trend line’s price that belongs to the last DateTime value on the chart.

The ‘Chart Shift’ setting determines how many future bar times are displayed on the chart. To change or review this setting, right-click on the chart and select ‘Format Window’. Then move to the ‘X - Time Scale’ tab and specify how much you want the chart to be shifted, either with a bar amount or percentage (click the ‘Bars’ or ‘%’ button to toggle between these):

MultiCharts .NET chart shift setting

Example: price values of a line extended into the future

The example indicator looks like this when added to an E-mini S&P 500 chart that has its ‘Chart Shift’ set to 20 bars:

Future values of a MultiCharts .NET trend line

The indicator generates an output in the PowerLanguage .NET Editor that’s similar to:

Future bar: 2-3 23:30, trend line value: 2114,6640625
Future bar: 2-3 23:45, trend line value: 2115,078125
Future bar: 3-3 00:00, trend line value: 2115,4921875
Future bar: 3-3 00:15, trend line value: 2115,90625
Future bar: 3-3 00:30, trend line value: 2116,3203125
Future bar: 3-3 00:45, trend line value: 2116,734375
Future bar: 3-3 01:00, trend line value: 2117,1484375
Future bar: 3-3 01:15, trend line value: 2117,5625
Future bar: 3-3 01:30, trend line value: 2117,9765625
Future bar: 3-3 01:45, trend line value: 2118,390625
Future bar: 3-3 02:00, trend line value: 2118,8046875
Future bar: 3-3 02:15, trend line value: 2119,21875
Future bar: 3-3 02:30, trend line value: 2119,6328125
Future bar: 3-3 02:45, trend line value: 2120,046875
Future bar: 3-3 03:00, trend line value: 2120,4609375
Future bar: 3-3 03:15, trend line value: 2120,875
Future bar: 3-3 03:30, trend line value: 2121,2890625
Future bar: 3-3 03:45, trend line value: 2121,703125
Future bar: 3-3 04:00, trend line value: 2122,1171875
Future bar: 3-3 04:15, trend line value: 2122,53125

Retrieving future trend line values in MultiCharts .NET programmatically

The programming code looks as follows:

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

    ITrendLineObject lineIntoFuture = null;

    protected override void CalcBar()
    {
        if (Bars.LastBarOnChart && lineIntoFuture == null)
        {
            // Create and modify trend line
            lineIntoFuture = DrwTrendLine.Create(
                new ChartPoint(Bars.Time[32], Bars.Close[32]),
                new ChartPoint(Bars.Time[0], Bars.Close[0])
                );

            lineIntoFuture.Color    = Color.DarkOrange;
            lineIntoFuture.Size     = 2;
            lineIntoFuture.ExtRight = true;

            // Determine future values of the trend line
            DateTime timeFutureBar = Bars.Time[0];

            for (int i = 1; i <= 20; i++)
            {
                timeFutureBar = timeFutureBar.AddMinutes(15);

                Output.WriteLine("Future bar: {0}, trend line value: {1}",
                    timeFutureBar.ToString("d-M HH:mm"),
                    lineIntoFuture.PriceValue(timeFutureBar));
            }
        }
    }
}

We begin by setting the SameAsSymbol attribute to true to display the indicator on the price chart and not in a separate subchart. Another attribute, RecoverDrawings, is set to false to prevent deletion of intra-bar generated drawings (MultiCharts, 2014).

Then we declare an ITrendLineObject interface reference variable (named lineIntoFuture) and explicitly set it to its default value of null. We’ll use this variable later on to access the trend line’s properties and methods (like PriceValue()).

Creating an extended trend line in MultiCharts .NET

Next is the CalcBar() method. In this method we start with an if statement that evaluates two conditions. The first is whether the current bar is the last on the chart (Bars.LastBarOnChart returns true then), the second condition checks if the lineIntoFuture variable is null. A value of null indicates that the reference variable doesn’t point to an object yet (Albahari & Albahari, 2012). In this context, it means we haven’t associated our trend line variable with an actual trend line object on the chart.

So when both conditions are true, we draw a trend line with the DrwTrendLine.Create() method and two ChartPoint structs passed in. The first ChartPoint for the line’s starting point is set to 32 bars ago (that corresponds with the low in the image above); the second ChartPoint for the end coordinate is set to the current bar’s time (Bars.Time[0]) and close (Bars.Close[0]).

Since we’ve assigned the value returned by DrwTrendLine.Create() to the lineIntoFuture variable, we can use this variable to work with the trend line. We do so by assigning new values to the Color and Size properties to change the line’s visual appearance. And by setting the ExtRight property to true we create an extended trend line into the future.

Retrieving a trend line’s future price values

The next part of the if statement located in the CalcBar() method gets the trend line’s future price values. For that we first declare a DateTime variable named timeFutureBar and initialise it to the current bar’s time (Bars.Time[0]). That variable is then used to determine the future DateTime values.

We do that in a for loop, which starts at 1 and continues as long as the i loop variable is less than or equal to (<=) 20. During each iteration (that is, loop cycle) we assign the timeFutureBar variable the value that’s returned by calling its AddMinutes() method with an argument of 15. This method adds or subtracts the specified number of minutes and returns a new DateTime value instead of modifying the existing one (Dorman, 2010). Since we do this on every iteration, the timeFutureBar DateTime value is incremented with 15 minutes each time – which is also the duration of one price bar on the 15-minute E-mini S&P 500 chart.

The for loop’s other part executes the Output.WriteLine() method to output data to the Output Window. The first value outputted is timeFutureBar formatted to a string with the custom format specifiers of d-M HH:mm. Here d stands for the day of the month (1 to 31), M for the month of the year (1 through 12), HH displays the hour with a 24-hour clock (0 to 23), and mm stands for the minutes from 00 to 59 (Dorman, 2010).

The other value outputted by Output.WriteLine() is the trend line’s future price value. For this we use the trend line’s variable (lineIntoFuture) to access its PriceValue() method. In that method we pass the DateTime value which we’ve just calculated and that holds a future date and time (timeFutureBar). And so when the loop is done, we’ve outputted the trend line’s price for each of the 20 future price bars.

For more examples that use a trend line’s PriceValue() method, see how to get a trend line’s price values for historical bars, colouring price bars above or below a trend line, and submitting market orders with a trend line.

Summary

Trend lines are made with the DrwTrendLine.Create() method that returns a reference to the line just created. When assigned to an ITrendLineObject variable, we can access a trend line’s PriceValue() method through that variable. With that method we can determine a line’s price value for any given future DateTime value that’s visible on the chart.

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

        ITrendLineObject lineIntoFuture = null;

        protected override void CalcBar()
        {
            if (Bars.LastBarOnChart && lineIntoFuture == null)
            {
                // Create and modify trend line
                lineIntoFuture = DrwTrendLine.Create(
                    new ChartPoint(Bars.Time[32], Bars.Close[32]),
                    new ChartPoint(Bars.Time[0], Bars.Close[0])
                    );

                lineIntoFuture.Color    = Color.DarkOrange;
                lineIntoFuture.Size     = 2;
                lineIntoFuture.ExtRight = true;

                // Determine future values of the trend line
                DateTime timeFutureBar = Bars.Time[0];

                for (int i = 1; i <= 20; i++)
                {
                    timeFutureBar = timeFutureBar.AddMinutes(15);

                    Output.WriteLine("Future bar: {0}, trend line value: {1}",
                        timeFutureBar.ToString("d-M HH:mm"),
                        lineIntoFuture.PriceValue(timeFutureBar));
                }
            }

            //> Future bar: 2-3 23:30, trend line value: 2114,6640625
            //> Future bar: 2-3 23:45, trend line value: 2115,078125
            //> Future bar: 3-3 00:00, trend line value: 2115,4921875
            //> Future bar: 3-3 00:15, trend line value: 2115,90625
            //> Future bar: 3-3 00:30, trend line value: 2116,3203125
            //> Future bar: 3-3 00:45, trend line value: 2116,734375
            //> Future bar: 3-3 01:00, trend line value: 2117,1484375
            //> Future bar: 3-3 01:15, trend line value: 2117,5625
            //> Future bar: 3-3 01:30, trend line value: 2117,9765625
            //> Future bar: 3-3 01:45, trend line value: 2118,390625
            //> Future bar: 3-3 02:00, trend line value: 2118,8046875
            //> Future bar: 3-3 02:15, trend line value: 2119,21875
            //> Future bar: 3-3 02:30, trend line value: 2119,6328125
            //> Future bar: 3-3 02:45, trend line value: 2120,046875
            //> Future bar: 3-3 03:00, trend line value: 2120,4609375
            //> Future bar: 3-3 03:15, trend line value: 2120,875
            //> Future bar: 3-3 03:30, trend line value: 2121,2890625
            //> Future bar: 3-3 03:45, trend line value: 2121,703125
            //> Future bar: 3-3 04:00, trend line value: 2122,1171875
            //> Future bar: 3-3 04:15, trend line value: 2122,53125
        }
    }
}

References

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

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

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