Situation
You want to know how to access any historical price bar in MultiCharts .NET regardless of the MaxBarsBack setting.

Programming example

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

namespace PowerLanguage.Indicator
{
    [UpdateOnEveryTick(false), SameAsSymbol(true)]
    public class Example_AccessAnyHistoricalBar : IndicatorObject
    {
        public Example_AccessAnyHistoricalBar(object _ctx) : base(_ctx) { }
        
        protected override void StartCalc()
        {
            ExecInfo.MaxBarsBack = 10;
            Output.Clear();
        }
        
        protected override void CalcBar()
        {
            Output.WriteLine("Bar #{0} - Average closing price till now: {1}.",
                Bars.CurrentBar + ExecInfo.MaxBarsBack,
                AverageToCurrentBar());
        }

        // The AverageToCurrentBar() method calculates the average
        // closing price from the first bar till the current.
        private double AverageToCurrentBar()
        {
            double sumOfPrices = 0;
            int barNumber      = Bars.CurrentBar + ExecInfo.MaxBarsBack;

            for (int i = 0; i < barNumber; i++)
            {
                sumOfPrices += Bars.FullSymbolData.Close[i];
            }

            return (sumOfPrices / barNumber);
        }
    }
}

Output of the programming example

The indicator generates the following text to the PowerLanguage .NET Editor Output Window:

Bar #11 - Average closing price till now: 1,35886272727273.
Bar #12 - Average closing price till now: 1,3588825.
Bar #13 - Average closing price till now: 1,35890384615385.
Bar #14 - Average closing price till now: 1,35890785714286.
Bar #15 - Average closing price till now: 1,358918.
Bar #16 - Average closing price till now: 1,358938125.
Bar #17 - Average closing price till now: 1,35895235294118.
Bar #18 - Average closing price till now: 1,35895277777778.
Bar #19 - Average closing price till now: 1,35907789473684.
Bar #20 - Average closing price till now: 1,3592415.
Bar #21 - Average closing price till now: 1,35936619047619.
Bar #22 - Average closing price till now: 1,35952227272727.
Bar #23 - Average closing price till now: 1,35970739130435.
Bar #24 - Average closing price till now: 1,35988791666667.
Bar #25 - Average closing price till now: 1,3600268.
...
Bar #293 - Average closing price till now: 1,36077208191126.
Bar #294 - Average closing price till now: 1,36077299319728.
Bar #295 - Average closing price till now: 1,36077484745763.
Bar #296 - Average closing price till now: 1,36077844594595.
Bar #297 - Average closing price till now: 1,36078195286195.
Bar #298 - Average closing price till now: 1,36078516778524.
Bar #299 - Average closing price till now: 1,36078889632107.
Bar #300 - Average closing price till now: 1,36079413333333.

Accessing historical price bars in MultiCharts .NET: the MaxBarsBack limitation

Previous bars’ price and time data and volume data are accessible through the Bars property (for the main data series) or the BarsOfData() method (when working with multiple data series) (MultiCharts, 2013). Each of these, however, is limited by the MaxBarsBack setting, which is the maximum number of bars a script can reference (MultiCharts Wiki, 2012).

If a trading strategy or an indicator has a MaxBarsBack setting of 20, it can at most reference price data of 20 bars ago (e.g., Bars.Close[20] or Bars.High[20]). But what if you wanted to access price data from much more bars ago?

Increasing the MaxBarsBack indicator or strategy setting is not always desirable because the higher this setting, the less data remains to actually calculate the MultiCharts .NET script on. Luckily, the MaxBarsBack limitation can be circumvented with the Bars.FullSymbolData property.

Accessing any price historical price bar in MultiCharts .NET

The Bars.FullSymbolData property provides access to any price bar of the primary data series (MultiCharts, 2013; PowerLanguage .NET Help, n.d.). This property can be accessed in the StartCalc() and CalcBar() override methods.

Because Bars.FullSymbolData references the same IInstrument interface as the Bars property (see MultiCharts, 2013; PowerLanguage .NET Help, n.d.), the Bars.FullSymbolData property works just like the Bars property in accessing time and price data and working with volume data.

For example, both Bars.Close[1] and Bars.FullSymbolData.Close[1] return the closing price of the previous bar. Likewise, Bars.High[10] and Bars.FullSymbolData.High[10] return the high price of 10 bars ago.

A difference occurs when more historical bars than the MaxBarsBack setting are referenced: Bars will then not work, while Bars.FullSymbolData does (provided it does not reference more price bars than available on the chart).

Programmig example: attribute and MaxBarsBack setting

The example begins with two MultiCharts .NET attributes:

[UpdateOnEveryTick(false), SameAsSymbol(true)]

The UpdateOnEveryTick attribute set to false lets the indicator only calculate on bar close, while the SameAsSymbol attribute set to false prevents that the indicator plots its own sub-chart.

The next part is the StartCalc() method:

protected override void StartCalc()
{
    ExecInfo.MaxBarsBack = 10;
    Output.Clear();
}

Here the MaxBarsBack are programmatically set to 10 bars. By making this value explicit we can verify in the output (see above) that the script begins its calculation at bar number 11 and that Bars.FullSymbolData is not limited by this setting.

The Output.Clear() method is also called in the StartCalc() method: this way the PowerLanguage .NET Editor Output Window is cleared on every calculation cycle to prevent cluttering.

Outputting price information in MultiCharts .NET

Next is the CalcBar() override method:

protected override void CalcBar()
{
    Output.WriteLine("Bar #{0} - Average closing price till now: {1}.",
        Bars.CurrentBar + ExecInfo.MaxBarsBack,
        AverageToCurrentBar());
}

The Output.WriteLine() method outputs two values by using string substitution parameters: the current bar number (returned by adding the Bars.CurrentBar and ExecInfo.MaxBarsBack properties together) and the value returned by the AverageToCurrentBar() method.

Accessing historical price bars regardless of the MaxBarsBack setting

This AverageToCurrentBar() method is implemented as follows:

// The AverageToCurrentBar() method calculates the average
// closing price from the first bar till the current.
private double AverageToCurrentBar()
{
    double sumOfPrices = 0;
    int barNumber      = Bars.CurrentBar + ExecInfo.MaxBarsBack;

    for (int i = 0; i < barNumber; i++)
    {
        sumOfPrices += Bars.FullSymbolData.Close[i];
    }

    return (sumOfPrices / barNumber);
}

The method begins with declaring and initialising the sumOfPrices double and barNumber integer variables. The first will be used to calculate the average closing price, while the latter holds the bar number on which the calculation is performed.

The next part is a for loop that loops over historical price bars. The loops begins at 0 and continues as long as the counter variable is less than the bar number. On each iteration of the looping statement, the bar’s close (returned by Bars.FullSymbolData.Close[i]) is added to the sumOfPrices variable.

The last statement (line 38) returns the average closing price by dividing the summed prices (sumOfPrices) by the number of price bars till the current bar (barNumber).

Key points:

  • The Bars property and BarsOfData() method are limited by the MaxBarsBack setting in how many price bars can be referenced;
  • The Bars.FullSymbolData property does not have this limitation and can access any historical and future price bar of the data series;
  • Bars.FullSymbolData has the same properties as Bars: for instance, Bars.Close[1] and Bars.FullSymbolData.Close[1] are the same, just as Bars.Upticks[5] and Bars.FullSymbolData.UpTicks[5].
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 June 29, 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