MaxBarsBack is an extremely important, but also relatively simple concept to understand. But it does take up a disproportionate amount of people’s time, purely because they are unaware of its considerable impact.
What is MaxBarsBack?
Every calculation needs a certain number of bars to calculate. For example, to calculate a 10 bar simple moving average, 10 bars are needed to do the calculation. In simple terms, MaxBarsBack is the number of bars more than the current bar required to do a calculation. So for a 10 bar simple moving average, MaxBarsBack will be 9.
MaxBarsBack (or MBB) usually refers to the ‘Maximum number of bars study will reference’ setting which is found in the properties section of all indicators and strategies.
MaxBarsBack is also an EasyLanguage reserved word that can be used to detect (though not set) the MaxBarsBack setting for the current indicator or strategy.
Why do we need MaxBarsBack?
Let’s say we have a 10 bar simple moving average indicator. This needs 10 bars before it can do a calculation. If we apply this indicator to a chart then the calculation starts on the first bar of the chart. The problem is that our indicator needs 10 bars before it can calculate. It can’t ‘look back’ because there are no previous bars on the chart – we are on the first bar. So our 10 bar simple moving average indicator can’t start calculating until it gets to the tenth bar. MaxBarsBack tells the indicator to skip the first 9 bars, and only start calculating on the 10th bar.
Some facts about MaxBarsBack
- During the MaxBarsBack period no calculations take place.
- Indicators have an Auto-Detect option for MaxBarsBack, whereas strategies do not.
- The default MaxBarsBack setting for indicators is Auto-Detect. The default MaxBarsBack setting for strategies is 50.
- The Auto-Detect MaxBarsBack setting in indicators adds 1 to the required number of MaxBarsBack (with one exception). For instance, if you use Close, which requires a MaxBarsBack setting of 0, the Auto-Detect setting will set MaxBarsBack to 1. The one exception is when the MaxBarsBack requirement is 1, then Auto-Detect leaves it at 1 i.e. Close sets MBB to 1, Close sets MBB to 1, Close sets MBB to 3, Close sets MBB to 4 etc.
- MaxBarsBack cannot be set from within EasyLanguage code. But its value can be detected using the MaxBarsBack reserved word.
- On multi-datastream charts, it is necessary for ALL datastreams to meet the MaxBarsBack requirements before calculations start. So, for example, if DATA1 is 1 minute and DATA2 is daily, then a MaxBarsBack of 50 means we have to wait 50 days before calculations start. In this instance, many thousands of 1 minute bars have passed, but no calculations can take place until MaxBarsBack has been met on the daily (DATA2) chart.
- Each indicator applied to a chart has its own MaxBarsBack setting. But all strategies applied to a chart share a single MaxBarsBack setting. For example, if you apply three strategies to a chart – one with a MaxBarsBack setting of 20, one with 40 and one with 200 – then a MaxBarsBack of 200 will be used for all three strategies.
- When indicators are run with the Auto-Detect setting, they will first run through the code with a MaxBarsBack value of 1. This first ‘run’ is used to detect a correct MaxBarsBack value. After this first run, MaxBarsBack is changed and the indicator is started again from scratch. For example, the following code is run in an indicator with a MaxBarsBack setting of Auto-Detect.
Print ("Date: ", date:0:0, " Time: ", time:4:0, " Bar Number: ", currentbar:0:0, " MaxBarsBack: ", MaxBarsBack:0:0); value1 = Close; value2 = Close; Once Print ("hello"); If CurrentBar = 1 then Print ("goodbye");
This code creates the following output in the Print Log
You can see that the code is run on the first bar when Bar Number is 1 and MaxBarsBack is 1. A new value of MaxBarsBack is detected – in this case it will be 11, as there is a reference to 10 bars ago (Close) and Auto-Detect adds 1 to this value. So now TradeStation changes MaxBarsBack and restarts the indicator. So on the second run we can see Bar Number is 1 and MaxBarsBack is 11. You can see that this situation seemingly leads to there being two Bar Number 1’s on the chart. However, the ‘real’ Bar Number 1 is the second one (1070530, 1530).
Why does MaxBarsBack cause so many problems?
- Indicators have an ‘Auto-Detect’ setting for MaxBarsBack, whereas strategies do not and instead have to be manually set.
This means that indicators and strategies start calculating at different points on the chart (e.g. a 10 bar simple moving average calculation in an indicator will have MaxBarsBack auto-detected to 10, but strategies just default the MaxBarsBack to 50). This in itself causes two problems:
- An indicator displays values on your chart and you can ‘see’ where trade signals should be created, but the strategy doesn’t take those signals. This is because the indicator has reached its MaxBarsBack setting first and is now calculating, but the strategy hasn’t so is not calculating, and hence not trading.
- The value of exponential and summed calculations depend on their start bar. The value of an exponential moving average, for instance, will be different on the current bar if you started the calculation 50 bars ago or 60 bars ago. Eventually (usually after several hundred bars), the values will converge, but initially they will be different. Functions that sum values right through the chart will always have different values which depends on their start bar. See Starting Point Dependence.
- MaxBarsBack has to be met on every datastream before calculations will start.
Often people have thousands of x minute bars on their chart, but no trades take place. It may be that MaxBarsBack hasn’t been met on the second (or third, fourth etc) datastreams.
- Poor coding and poor design of indicators/strategies.
It is easy to write code that looks back hundreds or even thousands of bars. But in most cases this is an incorrect approach. Sometimes you just need to rethink your approach to the problem. Other times, you can use Arrays to store historic data. If necessary, post your code in the Support Forum for others to take a look and offer suggestions.
- Indicators with Auto-Detect enabled run twice with Bar Number = 1.
In the majority of cases this is not a problem – EasyLanguage ‘forgets’ that it just ran on Bar Number 1, resets and starts again. However, if you are doing something external to EasyLanguage that won’t ‘forget’ – such as writing to a file – then this can cause problems. The easiest way to resolve this is to manually set MaxBarsBack for your indicator.
Some example functions and their MaxBarsBack requirements:
|Reserved word/function||MaxBarsBack requirement|
|Close||0||Close only refers to the current bar, so no previous bars are required.|
|Close||5||In this instance the  means we need to look 5 bars ago.|
|CurrentBar||0||CurrentBar only refers to the current bar.|
|BarNumber||1||BarNumber is a function that has a reference to the previous bar.|
|Average(Close, 10)||9||You need the current bar and 9 previous bars for a total of 10 bars.|
|Highest(High, 5)||4||The Highest calculation is for the current bar and 4 previous bars|
|Highest(High, 5)||5||The Highest calculation is for the previous bar and 4 bars previous to that.|
|XAverage(Close, 10)||1|| The XAverage (exponential moving average) calculation only requires 2 bars to calculate (the current bar and the previous bar). This is true of all ‘exponential’ calculations. The 10 is a ‘smoothing factor’ but is not related to the number of bars. Note however that ‘exponential’ calculations can take several hundred bars before they ‘settle down’ to a correct value. See Starting Point Dependence.|
MaxBarsBack cannot be set by your EasyLanguage code. It can only be manually set within the indicators/strategy properties.
For an indicator, its default MaxBarsBack can be set whilst editing the EasyLanguage code. Right click in the code editing window and choose ‘Properties…’.
After an indicator has been applied to a chart its active MaxBarsBack can be set by right clicking on the chart and choosing ‘Format Analysis Techniques…’ and then the ‘Format…’ button.
For a strategy, its default MaxBarsBack can be set whilst editing the EasyLanguage code. Right click in the code editing window and choose ‘Properties…’.
After a strategy has been applied to a chart its active MaxBarsBack can be set by right clicking on the chart and choosing ‘Format Strategies…’, and then the ‘Properties for All…’ button. Note that this method sets the MaxBarsBack for all strategies applied to the chart.
Common Questions & Issues:
- When I apply my strategy to a chart I get the following error:
This is a standard MaxBarsBack error. It means your EasyLanguage code has a reference to previous bars, but the MaxBarsBack setting is not set high enough. There could be simple references in your code such as High, or there could be more complex calls to functions such as Average(Close, MALength) where MALength is either set as an input or calculated. There is no easy way to find MaxBarsBack errors except to check your strategy thoroughly and use standard Debugging.
2. The example above says that a 10 bar moving average needs a MaxBarsBack of 9, but when I apply the following code to an indicator it produces a MaxBarsBack of 10, and when applied to a strategy it produces a MaxBarsBack of 50
value1 = Average(Close, 10); print (date:0:0, " , ", time:4:0, " , ", value1, " , ", MaxBarsBack);
The default MaxBarsBack setting for indicators is ‘Auto-Detect’. The Auto-Detect setting adds one extra bar to the required number of MaxBarsBack. So for the 10 bar moving average, the Auto-Detect feature detects that 9 is needed for MaxBarsBack, but then adds 1, giving 10. For strategies, the default MaxBarsBack setting is 50. So strategies will always run with a MaxBarsBack setting of 50 unless you manually change it.
3. My indicator and strategy calculations do not match
There are many reasons for this, but if you have checked your code and believe the calculations are the same then the problem could be to do with MaxBarsBack. Indicators have an Auto-Detect setting, whereas strategies have MaxBarsBack manually set. This often means that the MaxBarsBack value can be different for an indicator and strategy applied to the same chart. Some calculations, such as exponential moving averages, are very sensitive to the number of bars in their calculations. So for instance, if your indicators has been auto-detected to have a MaxBarsBack of 10 and your strategy is still using the default MaxBarsBack of 50 then the indicator will start calculating first. An exponential moving average typically takes about 12 times its length to ‘settle’ to a standard value, so any calculations in the first couple of hundred bars on the chart will be different between the indicator and the strategy. The easiest way to resolve this is to change the MaxBarsBack setting for the indicator to a manual setting, which is the same as the strategy.
4. My strategy doesn’t produce any trades, even though there are clearly places on the chart where signals occur
There are many reasons for this, but if you have checked your code and believe the calculations are the same then the problem could be to do with MaxBarsBack. Indicators have an Auto-Detect setting, whereas strategies have MaxBarsBack manually set. Your indicator may have auto-detected that it only needs a small number of bars for MaxBarsBack, but your strategy is still using the default value of 50. Therefore, your indicator will start calculating and displaying its values on the chart, whereas the strategy has not yet started calculating or trading. The easiest way to resolve this is to change the MaxBarsBack setting for your indicator to a manual setting, which is the same value as the strategy.
5. My strategy runs fine until I add a second strategy to the chart, and then there are no trades
When strategies are applied to a chart they share the same MaxBarsBack setting. This value will be the highest of all the strategies that are applied. For instance, if your first strategy has a MaxBarsBack setting of 10, it will start trading after the first 10 bars on the chart. But if you now apply a second strategy to the chart with a MaxBarsBack of 500, this will change the MaxBarsBack settting for all strategies to 500 and so your first strategy will not start trading until after bar 500.
6. When I add multiple data streams to my chart the strategy no longer produces any trades
MaxBarsBack has to be satisfied on all datastreams before calculations start. So, for example, if DATA1 is 1 minute and DATA2 is daily, then a MaxBarsBack of 50 means we have to wait 50 days before calculations start. In this instance, many thousands of 1 minute bars have passed, but no calculations can take place until MaxBarsBack has been met on the daily (DATA2) chart.
7. When I Print from my indicator I get several BarNumber/CurrentBar = 1 outputs
Indicators that use the default ‘Auto-Detect’ setting for MaxBarsBack can run multiple times. They run once on the chart and use this run to detect any MaxBarsBack errors. If there are no errors then they will run to the end of the chart. But if any errors are encountered the MaxBarsBack setting is reset and the indicators restarts. This leads to any print statements printing out two (or more) times.
8. My indicator/strategy needs a very high MaxBarsBack setting before it will work
Some calculations do require large lookbacks. For instance, if you are calculating a 200 period simple moving average then you will need a lookback (and hence a MaxBarsBack setting) of 200. However, it is an easy mistake (and quite common) to write code that looks back further than is necessary and hence creates huge MaxBarsBack settings. Remember that indicators will not display and strategies will not trade during the MaxBarsBack period so having very large MaxBarsBack values can be very wasteful. If your code needs a large MaxBarsBack period then you should post in the Support Forum for ideas on how to reduce its value. Even if you do need lots of historical data, it is usually possible to reduce the MaxBarsBack requirement by using Arrays
9. My indicator runs very slowly with MaxBarsBack set to ‘Auto-detect’ (but OK with MaxBarsBack set manually)
If you back-reference variable numbers of bars within your code then this can keep causing the Auto-Detect option to restart. For example, if you are using an x bar simple moving average, but x is calculated within your code then the change in x can cause problems. For instance, if on the first bar x is 5, the Auto-Detect setting will detect this and set MaxBarsBack to 5. The it will restart the code with MaxBarsBack. Now, after a certain number of bars, you calculate x to be 10. The Auto-Detect setting will see that the current MaxBarsBack is incorrect, will set it to 10 and restart the indicator. This process can go on indefinately, causing the indicator to keep resetting.
10. When I debug my indicator I can see that it is constantly resetting MaxBarsBack. Could it by the loop in my code?
for x = 10 to 100 begin
myval = close[x] + statusval;
This kind of back-reference within a loop is guaranteed to cause MaxBarsBack problems. The first time this code is called x starts at 10 and hence references close, so MaxBarsBack is set 10 and the code is restarted. On the next run MaxBarsBack is 10, the loop starts with x = 10 and Close, which works OK. But on the next iteration of the loop x is 11 so we get a reference to Close. This causes MaxBarsBack to reset to 11 and the code to restart. The code restarts with MaxBarsBack at 11, the loop starts at 10, which is OK, then iterates to 11, which is OK, then 12 which references Close and causes another MaxBarsBack reset. This process goes on until x is 100.
The quickest way to deal with this is to reverse your loop order i.e.
for x = 100 downto 10 begin
myval = close[x] + statusval;
Then on the first run of the code x is 100, which causes MaxBarsBack to be set to 100, but will not generate errors on futures iterations through the loop. There may also be more efficient ways of dealing with your calculation, so you should also ask in the Support Forum for ideas and suggestions.
11. Can I set the MaxBarsBack value from EasyLanguage?
No. MaxBarsBack can be detected from your EasyLanguage code, but it cannot be set. The only place to set it is in the indicator/strategy properties. It is possible in Indicators (not Strategies) to force a minimum MaxBarsBack setting, if the indicator properties is set to auto-detect. In this case, a single line in your code that says, for instance, ‘value1 = close’ will force the MaxBarsBack to 11.
12. I get different values between my indicators in RadarScreen and Charting. Is this a MaxBarsBack issue?
Probably not. It is most likely to do with the ‘Load additional data for accumulative calculations’ setting. See the RadarScreen section for more details
13. When I optimize my strategy I get the following MaxBarsBack error. What should I do?
This is not an ‘error’; it is a warning. It is warning you that some values you use in your optimization may cause MaxBarsBack errors. Whether it will actually cause an error depends on which values you are optimizing. If you are optimizing the length of a simple moving average then it is likely that you will cause a MaxBarsBack error. However, if you are optimizing the size of a stop then it is unlikely that this will cause a MaxBarsBack error and so you can ignore this warning (click ‘Yes’). There is no way that TradeStation can tell whether the inputs you are optimizing on are used in ‘lookback’ calculations so it warns you about any values that are greater than the current MaxBarsBack setting.
14. When I set MaxBarsBack to 400 then my Strategy Performance Report says my total profit is $10,000 but if I change it to 50 then my total profit is only $100. Can I use the MaxBarsBack of 400 to get better results?
If you set MaxBarsBack to 400 then it means your strategy is excluding the first 400 bars of data on the chart. A MaxBarsBack of 50 means your strategy excludes the first 50 bars of data in the chart. This will change your Strategy Performance Report because it means there will be 350 bars of data and hence 350 bars of potential trades different between the two strategy runs. In this particular instance it means there was a big loss (or several losses) in the 350 bars that you have excluded from the chart. Ignoring these losses is just closing your eyes to potential problems with your strategy.
15. How should I alter MaxBarsBack to get the best/most accurate results from my strategy?
In general you should not ‘play’ with the MaxBarsBack setting. It is designed for a specific issue with calculation lookbacks and should not be altered unless you are getting MaxBarsBack errors.
16. If I set the indicator properties to ‘auto-detect’ is is possible to have a MaxBarsBack of ‘0’ or ‘2’?
No. ‘Auto-detect’ will automatically set the MaxBarsBack to a minimum of ‘1’. If you do any back-referencing that requires an increase in MaxBarsBack then its next value is ‘3’, then ‘4’, ‘5’ etc. But it is not possible to force MaxBarsBack to ‘2’.