Situation
You've come across articles about setting the MaxBarsBack programmatically or about MaxBarsBack causing the code to execute twice, but now would like to know what these ‘MaxBarsBack’ are and how they work with indicators and strategies.

What are ‘MaxBarsBack’ in MultiCharts .NET?

An indicator or strategy can refer to values of previous bars so that these can be used in the evaluation of the current bar. But when a value of N bars ago is referenced in an indicator or strategy, the script can only start calculating at the N + 1 bar since it needs the other previous bars to base its calculations on.

For example, a moving average with a length of 20 periods begins plotting its line for the first time at the 21st bar of the chart, since it needs the prior 20 bars for calculating the value for the current bar (bar number 21).

This number of previous bars that must be available for a study in order to perform its calculations are called the maximum number of bars a study will reference, abbreviated to MaxBarsBack (MultiCharts, n.d.; MultiCharts Wiki, 2012).

This MaxBarsBack setting ensures that the strategy or indicator has enough price data available to perform all of its calculations. So, if a moving average is set to a length of 20, the MaxBarsBack setting will need to be at least 20 to ensure that the moving average can be calculated.

The MaxBarsBack setting is also the minimum amount of bars after which an indicator or strategy will perform its calculations. For example, if you have a 20 period moving average with a MaxBarsBack setting of 50, it will start plotting its line at bar 51, even though it will not need 50 price bars to calculate a 20 period moving average.

Comparison: MaxBarsBack for indicators and strategies

The table below compares the differences between MaxBarsBack for indicators and strategies (Andrew MultiCharts, 2012; Henry MultiCharts, 2013; MultiCharts, n.d.; MultiCharts Wiki, 2013a, 2013b):

Indicators Strategies
Definition of MaxBarsBack The number of bars that your indicator will reference for the calculation on the current bar. The number of bars that your strategy will reference for the calculation on the current bar.
What does that mean? With a MaxBarsBack setting of N, your indicator will start plotting at bar number N + 1. With a MaxBarsBack setting of N, your strategy can generate the first signal at bar number N + 1 at the soonest.
Which value of MaxBarsBack should I use? At least the amount of price bars your indicator references in its calculations. So, a moving average with a length of 20 periods will need a MaxBarsBack setting of at least 20 bars.

In general, a MaxBarsBack setting of 50 is enough for most indicators.
At least the amount of price bars that your strategy references in its calculations. So, if your strategy uses multiple moving averages with the longest having a length of 75, your MaxBarsBack setting will need to be at least 75 bars.

In general, a MaxBarsBack setting of 100 is enough for most trading strategies.
Which values can the MaxBarsBack setting be? The MaxBarsBack can not be smaller than 1 or greater than the actual number of bars on the chart. The MaxBarsBack can not be smaller than 1 or greater than the actual number of bars on the chart.
Can the MaxBarsBack value be determined automatically? Yes, with the use of the 'Auto-Detect' option in the 'Format Study' window, found in the 'Properties' tab. No, the MaxBarsBack of a strategy can not be set to auto-detect.
How is the MaxBarsBack value determined automatically by MultiCharts .NET? Initially, MaxBarsBack will be set to the largest data offset in the indicator. If this initial value proves to be too small, it is increased by a value of 5 or by a factor of 1.618, whichever yields the highest value.

This process is repeated until the MaxBarsBack value is high enough to calculate the indicator. Consequently, the resulting MaxBarsBack value can be much higher than it needs to be.
The MaxBarsBack of a strategy can not be set automatically.
How do I set the MaxBarsBack value manually? This can be done in two ways:

On the price chart:
1) Right-click on the price chart and select 'Format Indicators'.
2) Select the indicator and click on 'Format'.
3) In the 'Format Study' window, click on the 'Properties' tab.
4) Below the heading of 'Max number of bars study will reference' the MaxBarsBack can either be set to 'Auto-detect' or 'User Specified'.

In the MultiCharts .NET PowerLanguage Editor:
1) With the source code of the indicator shown, right-click on the code and select 'Properties'.
2) Below the heading of 'Max number of bars study will reference' the MaxBarsBack can either be set to 'Auto-detect' or 'User Specified'.
1) Right-click on the price chart and select 'Format Signals'.
2) Select the strategy and click on 'Properties'.
3) In the 'Strategy Properties' window, look for the option 'Maximum number of bars study will reference' in the 'Properties' tab.
What happens if my initial MaxBarsBack value is set too low? The MaxBarsBack value is set automatically to a higher value and your indicator will be recalculated. Your strategy will generate an error message like the following: "Error in study [study name]: Tried to reference back more bars (x) than allowed by the current MaxBarsBack setting. Please increase the MaxBarsBack setting."
How can I retrieve the current MaxBarsBack setting? The ExecInfo.MaxBarsBack property returns an integer that holds the MaxBarsBack setting. The ExecInfo.MaxBarsBack property returns an integer that holds the MaxBarsBack setting.
How do I set the MaxBarsBack programmatically? By assigning an integer value to the ExecInfo.MaxBarsBack property. See for an example the article how to set the MaxBarsBack of an indicator programmatically.

Note: a MaxBarsBack value set programmatically takes precedence over the value specified in the indicator properties.
The MaxBarsBack can not be set programmatically for a trading strategy.

Should you want to compare studies, make sure that all have the same MaxBarsBack setting. For example, a 14 period RSI indicator with a MaxBarsBack setting of 50 will plot slightly different than a 14 period RSI indicator with a MaxBarsBack setting of 20. The same applies to visually confirming signals from a trading strategy: make sure both the strategy and indicator have the same MaxBarsBack setting.

Key points:

  • MaxBarsBack are the maximum number of bars a study will reference in order to perform its calculations;
  • An indicator or trading strategy will start its calculations at the MaxBarsBack + 1 bar number;
  • The MaxBarsBack for a trading strategy can be set in the 'Strategy Properties' window;
  • The MaxBarsBack for an indicator can be set (a) automatically with 'Auto-Detect, (b) manually in the 'Format Study' window, or (c) programmatically with ExecInfo.MaxBarsBack;
  • If the MaxBarsBack for an indicator is set too low, the indicator will be recalculated;
  • If the MaxBarsBack for a strategy is set too low, the strategy will trigger an error message.
References

Andrew MultiCharts (2012, August 30). Extremes, highest/lowest functions affecting maxbarsback forum discussion, retrieved on January 16, 2014 from http://www.multicharts.com/discussion/viewtopic.php?f=1&t=10853&view=unread#p53601

Henry MultiCharts (2013, September 25). How to set the MaxBarsBack in the .Net Editor forum discussion, retrieved from http://www.multicharts.com/discussion/viewtopic.php?f=19&p=98323#p98323

MultiCharts (n.d.). MultiCharts user manual, retrieved on January 16, 2014, from https://dl.dropboxusercontent.com/u/95112551/MultiCharts%20User%20Manual.pdf

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

MultiCharts Wiki (2013a). Strategy Properties - Setting Maximum Bars Back, retrieved on January 16, 2014, from http://www.multicharts.com/trading-software/index.php/Strategy_Properties#Setting_Maximum_Bars_Back

MultiCharts Wiki (2013b). Indicator Settings - Setting Maximum Bars Back, retrieved on January 16, 2014, from http://www.multicharts.com/trading-software/index.php/Indicator_Settings#Setting_Maximum_Bars_Back