Situation
You want to access price, time, and volume data of future price bars in your MultiCharts .NET indicator or trading strategy.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true), UpdateOnEveryTick(false)]
    public class Example_AccessFuturePriceBars : IndicatorObject
    {
        private IPlotObject futurePrice;

        public Example_AccessFuturePriceBars(object _ctx) : base(_ctx) { }
        
        protected override void Create()
        {
            futurePrice = AddPlot(new PlotAttributes("FuturePrice", EPlotShapes.Line, Color.Red));
        }
        
        protected override void CalcBar()
        {
            futurePrice.Set(GetFuturePrice(15));
        }

        // The GetFuturePrice() method returns the closing price of a future bar
        private double GetFuturePrice(int barsForward)
        {
            int requestedBarNum = Bars.FullSymbolData.Current + barsForward;
            int barsAvailable   = Bars.FullSymbolData.Count - 1;

            // If the requested bar is outside the data series, return
            // the close of the second to last bar (i.e., the last closed bar).
            if (requestedBarNum > barsAvailable)
            {
                int i = barsAvailable - Bars.FullSymbolData.Current;
                return Bars.FullSymbolData.Close[-i];
            }

            // ..otherwise, return the closing price of the future price bar
            return Bars.FullSymbolData.Close[-barsForward];
        }
    }
}

Output of the programming example

With a chart style set to ‘Line on Close’ (black line), the indicator (red line) looks as follows:

Example of plotting future bars in MultiCharts .NET

Accessing any price bar in MultiCharts .NET

Any price bar of the primary data series can be accessed with the Bars.FullSymbolData property, which can be used in the StartCalc() and CalcBar() override methods and regardless of the MaxBarsBack setting (MultiCharts, 2013; PowerLanguage .NET Help, n.d.).

See access any historical price bar to learn more about the Bars.FullSymbolData property.

Bars.FullSymbolData provides access to the same properties as the Bars property for accessing time and price data and working with volume data. For example, Bars.FullSymbolData.Close[1] and Bars.Close[1] both return the closing price of the previous bar. Likewise, Bars.FullSymbolData.UpTicks[10] and Bars.UpTicks[10] each return the upticks for 10 bars ago.

Accessing future price bars in MultiCharts .NET

The Bars.FullSymbolData property can also be used to access future price bars of the primary data series since it provides access to any price bar (see MultiCharts, 2013). For referencing future price bars, two characteristics of Bars.FullSymbolData are important (MultiCharts, 2013):

  • Future price bars are accessible with a negative bar index (for example, Bars.FullSymbolData.High[-1] returns the high of the next price bar), as opposed to historical bars that use a positive bar index.
  • Bar indexation is performed in the context of the current bar that is being calculated upon (in other words, the bar on which the CalcBar() override method is called). Bars to the left of the current bar (the historical bars) have positive indices, while those to the right (the future bars) have negative indices.

Both concepts are highlighted in the image below.

Example of full symbol data in MultiCharts .NET

MultiCharts .NET programming example

The example begins with setting two MultiCharts .NET attributes:

[SameAsSymbol(true), UpdateOnEveryTick(false)]

With the SameAsSymbol attribute set to true the indicator plots on the main price chart, while the UpdateOnEveryTick attribute set to false ensures that the indicator is only calculated on bar close.

Creating and setting a line plot in MultiCharts .NET

The next part declares an IPlotObject:

private IPlotObject futurePrice;

This object is subsequently initialised in the Create() override method:

protected override void Create()
{
    futurePrice = AddPlot(new PlotAttributes("FuturePrice", EPlotShapes.Line, Color.Red));
}

Here the futurePrice plot object is created with a name of "FuturePrice" in the MultiCharts .NET Data Window and plotted with a red (Color.Red) line (EPlotShapes.Line).

The next code segment is the CalcBar() method:

protected override void CalcBar()
{
    futurePrice.Set(GetFuturePrice(15));
}

This method, executed on every price bar (see MultiCharts, 2013), sets the futurePrice plot to the value returned by the GetFuturePrice() method with an argument of 15.

Accessing future price data in MultiCharts .NET

This GetFuturePrice() method is implemented as follows:

// The GetFuturePrice() method returns the closing price of a future bar
private double GetFuturePrice(int barsForward)
{
    int requestedBarNum = Bars.FullSymbolData.Current + barsForward;
    int barsAvailable   = Bars.FullSymbolData.Count - 1;

    // If the requested bar is outside the data series, return
    // the close of the second to last bar (i.e., the last closed bar).
    if (requestedBarNum > barsAvailable)
    {
        int i = barsAvailable - Bars.FullSymbolData.Current;
        return Bars.FullSymbolData.Close[-i];
    }

    // ..otherwise, return the closing price of the future price bar
    return Bars.FullSymbolData.Close[-barsForward];
}

This method starts with declaring and initialising two integer variables: the requested bar number (by adding the current bar number, Bars.FullSymbolData.Current, to the barsForward parameter) and the number of bars that are available (Bars.FullSymbolData.Count minus one to arrive at the correct number of bars).

The if statement (line 33) verifies if the requested bar number is beyond the data series range. When that condition evaluates to true, the index of the last available bar is calculated (line 35) and subsequently used to return the closing price of this future price bar (Bars.FullSymbolData.Close[-i]; line 36).

When the condition of the if statement evaluates to false, the return keyword (line 40) is executed to return the closing price of the requested future price bar by using the Bars.FullSymbolData property with the negative -barsForward index.

Key points:

  • The Bars.FullSymbolData property can be used to access any historical price bar or future price bar of the primary data series;
  • Relative to the current bar on which the MultiCharts .NET script is calculated on, historical price bars have a positive index while future price bars have a negative bar index;
  • Referencing more future price bars than there are available triggers an error.
References

MultiCharts (2013). MultiCharts .NET Programming Guide (version 1.0). Retrieved from http://www.multicharts.com/downloads/MultiCharts.NET-ProgrammingGuide-v1.0.pdf

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