Situation
You want to know how to set the MaxBarsBack setting of an indicator programmatically.

Programming example

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

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_SetMaxBarsBack : IndicatorObject
    {
        public Example_SetMaxBarsBack(object _ctx) : base(_ctx) { }

        protected override void StartCalc()
        {
            // Output information about the MaxBarsBack prior to 
            // setting the value programmatically
            OutputMaxBarsBack("StartCalc");

            ExecInfo.MaxBarsBack = 100;
        }

        protected override void CalcBar()
        {
            // On the last bar of the chart, output the MaxBarsBack value
            if (Bars.LastBarOnChart)
            {
                OutputMaxBarsBack("CalcBar");
            }
        }

        // This method outputs information about the MaxBarsBack setting
        private void OutputMaxBarsBack(string sourceOfCalculation)
        {
            Output.WriteLine("{0} - {1} - BarNumber: {2} - MaxBarsBack: {3}",
                sourceOfCalculation,
                Bars.Time[0].ToString("d-M HH:mm:ss"),
                Bars.CurrentBarAbsolute() + 1,
                ExecInfo.MaxBarsBack);
        }
    }
}

Output of the programming example

Applied to a chart with the MaxBarsBack set to Auto-Detect the example generates the following output:

StartCalc - 19-12 00:30:00 - BarNumber: 1 - MaxBarsBack: 1
StartCalc - 23-12 03:30:00 - BarNumber: 101 - MaxBarsBack: 100
CalcBar - 13-1 09:00:00 - BarNumber: 730 - MaxBarsBack: 100

Analysis of the output

The output shows that, on the first calculation, the MaxBarsBack value is set to 1. When this value is changed programmatically to 100, the indicator is recalculated a second time. This recalculation is evident by the fact that the StartCalc() override method is executed twice.

Finally, when the indicator calculation arrives at the last bar on the chart in the CalcBar() method, the bar number and the MaxBarsBack value are printed.

When you reference more price bars in the CalcBar() method than the programmatically set MaxBarsBack setting, the calculation thread will be stuck in an infinite recalculation.

See the article What are 'MaxBarsBack' and how do they work with indicators and signals? for more on working with the MaxBarsBack. This article will focus on setting the MaxBarsBack value programmatically for indicators.

Setting the MaxBarsBack programmatically

We started the example with setting the SameAsSymbol attribute to true (line 8) so that the indicator is not plotted on a separate subchart. Then in the StartCalc() method the value of the MaxBarsBack is set (line 19):

protected override void StartCalc()
{
    // Output information about the MaxBarsBack prior to 
    // setting the value programmatically
    OutputMaxBarsBack("StartCalc");

    ExecInfo.MaxBarsBack = 100;
}

To change the MaxBarsBack setting of an indicator we use the ExecInfo.MaxBarsBack property (MultiCharts, 2013), which we set here to a value of 100.

If you try to set the ExecInfo.MaxBarsBack property in the Create() method, you will generate an "Unaccessible property (method): ExecInfo. Initialize or execute." error message. Instead, use this property in the StartCalc(), CalcBar(), or StopCalc() override methods (MultiCharts, 2013).

Before we set the MaxBarsBack value, we first output some information to the PowerLanguage Editor Output tab (line 15-17) with the OutputMaxBarsBack() method. That way we can see how changing the MaxBarsBack value impacts the indicator.

Outputting the MaxBarsBack value

With the OutputMaxBarsBack() method we can easily output information multiple times by simply calling this method (which we do in lines 17 and 27). It is implemented as follows:

// This method outputs information about the MaxBarsBack setting
private void OutputMaxBarsBack(string sourceOfCalculation)
{
    Output.WriteLine("{0} - {1} - BarNumber: {2} - MaxBarsBack: {3}",
        sourceOfCalculation,
        Bars.Time[0].ToString("d-M HH:mm:ss"),
        Bars.CurrentBarAbsolute() + 1,
        ExecInfo.MaxBarsBack);
}

This method output four different things with the help of string substitution parameters. First, the string parameter sourceOfCalculation is printed so that we can track which method called the OutputMaxBarsBack() method.

Second, the time of the current bar is outputted by converting the DateTime value to a string (line 36).

By the way, with MaxBarsBack set to greater than 1, the current bar number can also be retrieved with Bars.CurrentBar + ExecInfo.MaxBarsBack.

Third, the current bar number (as it is displayed on the chart) is outputted by calling the Bars.CurrentBarAbsolute() method, which is zero-based (hence the + 1 in line 37 to arrive at the correct bar number).

Fourth, the current MaxBarsBack setting is retrieved with the ExecInfo.MaxBarsBack property. This shows that this property can be used for both retrieving the current MaxBarsBack setting and for setting the MaxBarsBack value in an indicator.

MultiCharts .NET’s CalcBar() method

So, to output information about the MaxBarsBack setting, we only need to call the OutputMaxBarsBack() method on the last bar of the chart:

// On the last bar of the chart, output the MaxBarsBack value
if (Bars.LastBarOnChart)
{
    OutputMaxBarsBack("CalcBar");
}

Key points:

  • The ExecInfo.MaxBarsBack property can be used to set the number of MaxBarsBack (e.g., ExecInfo.MaxBarsBack = 30;) or to retrieve the current setting (e.g., int myBarsBack = ExecInfo.MaxBarsBack;);
  • If the MaxBarsBack setting is changed, either manually through the indicator options or programmatically, the indicator is recalculated.
References

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