Situation
You want to know how to use historical price data from the primary data series in MultiCharts .NET.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true), UpdateOnEveryTick(false)]
    public class Example_HistoricalPriceBars : IndicatorObject
    {
        public Example_HistoricalPriceBars(object _ctx) : base(_ctx) { }

        protected override void StartCalc()
        {
            // Set the MaxBarsBack programmatically to 10
            ExecInfo.MaxBarsBack = 10;
        }
        
        protected override void CalcBar()
        {
            // Output data on the last bar of the chart
            if (Bars.LastBarOnChart)
            {
                Output.WriteLine("Previous bar time: {0}, previous bar high: {1} and low {2}.",
                    Bars.Time[1].ToString("d-M-y HH:mm:ss"),
                    Bars.High[1],
                    Bars.Low[1]);

                Output.WriteLine("Close five bars ago: {0}, with a volume of {1}.",
                    Bars.Close[5],
                    Bars.Ticks[5]);
            }
        }
    }
}

Output of the programming example

Applied to a chart, the indicator generates an output similar to:

Previous bar time: 23-5-14 17:00:00, previous bar high: 3200,27 and low 3194,76.
Close five bars ago: 3190,48, with a volume of 0.

Using historical price data in MultiCharts .NET

Price data are visually represented in MultiCharts .NET with bars, either visible (such as with a price chart) or implicitly (like with the real-time market scanner). As discussed in the accessing price data for the primary data series article, price bars can be accessed through the Bars property, which contains a reference to the IInstrument interface (MultiCharts, 2013).

To reference historical price bars, the Bars property is used in the CalcBar() override method with an index greater than zero (see MultiCharts, 2013). In this context, the index is the number of bars back to reference: the previous bar has an index of 1 (e.g., Bars.Close[1]), the bar preceding the previous bar an index of 2 (e.g., Bars.Close[2]), and so on.

Referencing historical price data in MultiCharts .NET

The table below shows how to use price and volume data from historical bars, where x can be replaced with the preferred number of bars back (e.g., PowerLanguage .NET Help, n.d.):

Property Description Example
Bars.Close[x] Returns the closing price for the specified bar. Bars.Close[1]: returns the close of the previous bar.
Bars.DownTicks[x] Returns the down ticks volume (or count) for the specified bar. Bars.DownTicks[0]: returns the down ticks volume (or down ticks count) for the current bar.
Bars.High[x] Returns the high of the specified bar. Bars.High[5]: returns the high for the bar five bars ago.
Bars.Low[x] Returns the low of the specified bar. Bars.Low[2]: returns the low of the bar preceding the previous bar.
Bars.Open[x] Returns the open of the specified bar. Bars.Open[1]: returns the open price of the previous bar.
Bars.OpenInt[x] Returns the open interest (or down volume) for the specified bar. Bars.OpenInt[0]: returns the open interest (or down volume) for the current bar.
Bars.Ticks[x] Returns the total volume (or total tick count) for the specified bar. Bars.Ticks[10]: returns the total volume (or total tick count) for the bar 10 bars ago.
Bars.Time[x] Returns the time, in DateTime format, of the specified bar. Bars.Time[1]: returns the DateTime value of the previous bar.
Bars.UpTicks[x] Returns the up tick volume (or tick count) for the specified bar. Bars.UpTicks[3]: returns the up ticks volume (or up ticks count) for three bars ago.
Bars.Volume[x] Returns the up tick volume, up tick count, or total volume for the specified bar. Bars.Volume[1]: returns the volume, up ticks count, or total volume for the previous bar.

By the way, the values returned by the volume properties (for example Bars.UpTicks[x] and Bars.Volume[x]) are influenced by the chart resolution, available data, and the ‘Build Volume On’ setting. See using volume data and up and down ticks explained to learn more.

The number of historical bars that an indicator or trading strategy can use depends on the MaxBarsBack setting, which is the maximum number of bars that a script can reference (MultiCharts Wiki, 2012). See what are ‘MaxBarsBack’? to learn more.

During script calculation, price bars are processed sequentially starting with the out-most left bar up to the most recent bar (see MultiCharts, 2013). This means that, regardless of which bar is processed, the current bar on which the script is calculated on always has an index of 0, the previous bar an index of 1, and so on.

Visual explanation of using historical price bars in MultiCharts .NET

The image below visually highlights some features for accessing historical price bars in MultiCharts .NET:

Example of referencing historical price bars

Let’s now take a look at the programming example.

Programming example: attribute setting

The example begins with setting two attributes:

[SameAsSymbol(true), UpdateOnEveryTick(false)]

Setting the SameAsSymbol attribute to true plots the indicator on the main price chart instead of being plotted in a separate sub-chart. The UpdateOnEveryTick attribute set to false makes the indicator to be calculated on bar close only.

The next part sets the MaxBarsBack programmatically:

protected override void StartCalc()
{
    // Set the MaxBarsBack programmatically to 10
    ExecInfo.MaxBarsBack = 10;
}

Here the ExecInfo.MaxBarsBack property sets the MaxBarsBack setting to 10 in the StartCalc() override method, a method that is called once at the beginning of each script calculation (see MultiCharts, 2013).

This MaxBarsBack setting determines how many historical bars the indicator can reference, and in this case the indicator can reference 10 bars as a maximum.

Referencing previous price bars

The last part of the programming example is the CalcBar() method:

protected override void CalcBar()
{
    // Output data on the last bar of the chart
    if (Bars.LastBarOnChart)
    {
        Output.WriteLine("Previous bar time: {0}, previous bar high: {1} and low {2}.",
            Bars.Time[1].ToString("d-M-y HH:mm:ss"),
            Bars.High[1],
            Bars.Low[1]);

        Output.WriteLine("Close five bars ago: {0}, with a volume of {1}.",
            Bars.Close[5],
            Bars.Ticks[5]);
    }
}

This method begins with an if statement to see if the current bar is the last bar on the chart. When that is the case, the Bars.LastBarOnChart property will evaluate to true and two statements are outputted to the PowerLanguage .NET Editor.

First, in lines 24-27, a string is created with the help of string substitution parameters. It contains the time of the previous bar (Bars.Time[1]) formatted to a string, the previous bar high (Bars.High[1]) and the low of the previous bar (Bars.Low[1]).

Second, in lines 29-31, the close of five bars ago (Bars.Close[5]) is outputted together with the volume of the price bar from 5 bars ago (Bars.Ticks[5]). If there is no volume data available, such as with the index in this example, a zero will be printed to the Output Window.

Key points:

  • Historical price bars can be accessed through the any of the Bars properties with an index greater than zero;
  • The current bar has an index of zero (e.g., Bars.Close[0]), the previous bar an index of 1 (e.g., Bars.Close[1]), and so on;
  • The amount of historical bars that can be referenced depends on the MaxBarsBack setting.
References

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

MultiCharts Wiki (2012). How Scripts Work. Retrieved on May 28, 2014, from http://www.multicharts.com/trading-software/index.php/How_Scripts_Work

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