currentbar and MaxBarsBack

Questions about MultiCharts and user contributed studies.
Squib
Posts: 17
Joined: 25 Nov 2011
Has thanked: 19 times
Been thanked: 6 times

currentbar and MaxBarsBack

Postby Squib » 06 Dec 2011

Hi,

my understanding of MaxBarsBack is, that it is fix for a study, once I have set a value in the "Format Study" dialog.
Now I'm debugging an Indicator and I discovered followig behaviour:
At the beginning of the code I placed a debug print instruction

Code: Select all

Print("CB: ",currentbar," MB: ", MaxBarsBack);

I get following output:
CB starts at "1" and counts up until "20". During this time MB is "20".
But then something strange happens. CB restarts at "1" and MB goes to "33". Then after CB reaches "33", MB goes to "55" and so on.
Does anybody know how to explain this?

Thanks!

EDIT: I have set MaxBarsBack to 20
Attachments
MaxBarsBack.jpg
MaxBarsBack.jpg (240.93 KiB) Viewed 257 times

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: currentbar and MaxBarsBack

Postby JoshM » 08 Dec 2011

Squib wrote:Does anybody know how to explain this?

I don't know, it runs correctly here (MC7.0) with an indicator with a user specified MaxBarsBack of 20:

Code: Select all

CB:    1.00 MB:   20.00
CB:    2.00 MB:   20.00
CB:    3.00 MB:   20.00
CB:    4.00 MB:   20.00
CB:    5.00 MB:   20.00
CB:    6.00 MB:   20.00
CB:    7.00 MB:   20.00
CB:    8.00 MB:   20.00
CB:    9.00 MB:   20.00
CB:   10.00 MB:   20.00
CB:   11.00 MB:   20.00
CB:   12.00 MB:   20.00
CB:   13.00 MB:   20.00
CB:   14.00 MB:   20.00
CB:   15.00 MB:   20.00
CB:   16.00 MB:   20.00
CB:   17.00 MB:   20.00
CB:   18.00 MB:   20.00
CB:   19.00 MB:   20.00
CB:   20.00 MB:   20.00
CB:   21.00 MB:   20.00
CB:   22.00 MB:   20.00
CB:   23.00 MB:   20.00
CB:   24.00 MB:   20.00
CB:   25.00 MB:   20.00
CB:   26.00 MB:   20.00
CB:   27.00 MB:   20.00
CB:   28.00 MB:   20.00
CB:   29.00 MB:   20.00
CB:   30.00 MB:   20.00
CB:   31.00 MB:   20.00
CB:   32.00 MB:   20.00
CB:   33.00 MB:   20.00
CB:   34.00 MB:   20.00
CB:   35.00 MB:   20.00
CB:   36.00 MB:   20.00
CB:   37.00 MB:   20.00
CB:   38.00 MB:   20.00
CB:   39.00 MB:   20.00
CB:   40.00 MB:   20.00
CB:   41.00 MB:   20.00
CB:   42.00 MB:   20.00
CB:   43.00 MB:   20.00
CB:   44.00 MB:   20.00
CB:   45.00 MB:   20.00
CB:   46.00 MB:   20.00
CB:   47.00 MB:   20.00
CB:   48.00 MB:   20.00
CB:   49.00 MB:   20.00
CB:   50.00 MB:   20.00
CB:   51.00 MB:   20.00
CB:   52.00 MB:   20.00
CB:   53.00 MB:   20.00
CB:   54.00 MB:   20.00
CB:   55.00 MB:   20.00
CB:   56.00 MB:   20.00
CB:   57.00 MB:   20.00
CB:   58.00 MB:   20.00
CB:   59.00 MB:   20.00
CB:   60.00 MB:   20.00
CB:   61.00 MB:   20.00
CB:   62.00 MB:   20.00
CB:   63.00 MB:   20.00
CB:   64.00 MB:   20.00
CB:   65.00 MB:   20.00
CB:   66.00 MB:   20.00
CB:   67.00 MB:   20.00
CB:   68.00 MB:   20.00
CB:   69.00 MB:   20.00
CB:   70.00 MB:   20.00
CB:   71.00 MB:   20.00
CB:   72.00 MB:   20.00
CB:   73.00 MB:   20.00
CB:   74.00 MB:   20.00
CB:   75.00 MB:   20.00
CB:   76.00 MB:   20.00
CB:   77.00 MB:   20.00
CB:   78.00 MB:   20.00
CB:   79.00 MB:   20.00
CB:   80.00 MB:   20.00
CB:   81.00 MB:   20.00
CB:   82.00 MB:   20.00
CB:   83.00 MB:   20.00
CB:   84.00 MB:   20.00
CB:   85.00 MB:   20.00
CB:   86.00 MB:   20.00
CB:   87.00 MB:   20.00
CB:   88.00 MB:   20.00
CB:   89.00 MB:   20.00
CB:   90.00 MB:   20.00
CB:   91.00 MB:   20.00
CB:   92.00 MB:   20.00
CB:   93.00 MB:   20.00
CB:   94.00 MB:   20.00
CB:   95.00 MB:   20.00
CB:   96.00 MB:   20.00
CB:   97.00 MB:   20.00
CB:   98.00 MB:   20.00
CB:   99.00 MB:   20.00
CB:  100.00 MB:   20.00
CB:  101.00 MB:   20.00
CB:  102.00 MB:   20.00
CB:  103.00 MB:   20.00
CB:  104.00 MB:   20.00
CB:  105.00 MB:   20.00
CB:  106.00 MB:   20.00
CB:  107.00 MB:   20.00
CB:  108.00 MB:   20.00
CB:  109.00 MB:   20.00
CB:  110.00 MB:   20.00
CB:  111.00 MB:   20.00
CB:  112.00 MB:   20.00
CB:  113.00 MB:   20.00
CB:  114.00 MB:   20.00
CB:  115.00 MB:   20.00
CB:  116.00 MB:   20.00
CB:  117.00 MB:   20.00
CB:  118.00 MB:   20.00
CB:  119.00 MB:   20.00
CB:  120.00 MB:   20.00
CB:  121.00 MB:   20.00
CB:  122.00 MB:   20.00
CB:  123.00 MB:   20.00
CB:  124.00 MB:   20.00
CB:  125.00 MB:   20.00
CB:  126.00 MB:   20.00
CB:  127.00 MB:   20.00
CB:  128.00 MB:   20.00
CB:  129.00 MB:   20.00
CB:  130.00 MB:   20.00


Have you tried removing the indicator and re-adding it to the chart? (Since changes in the MaxBarsBack setting only seem to come in effect after re-adding as far as I know)

Btw, looking at your screenshot, I wouldn't be surprised if the same indicator is applied to a minimized chart or set to hidden somewhere. Try including something as GetSymbolName, BarInterval, BarType or GetStrategyName in your output to see where the output is being printed from.

Good luck,

Josh
These users thanked the author JoshM for the post:
Squib

Squib
Posts: 17
Joined: 25 Nov 2011
Has thanked: 19 times
Been thanked: 6 times

Re: currentbar and MaxBarsBack

Postby Squib » 08 Dec 2011

Hi Josh,

thanks for your help!
I don't know, it runs correctly here (MC7.0) with an indicator with a user specified MaxBarsBack of 20:

Yes, with a simple indicator, currentbar and MaxBarsBack works like expected at my version (7.4 beta 2). Quite sure I'm doing something wrong here.
Here is the rather simple test code, which generates this strange output:

Code: Select all

inputs:
   extATRPeriod(14); 
variables:
   HighValue(0),
   cnt(0);   
Print("CB:",currentbar," MB:", MaxBarsBack);
if currentbar > extATRPeriod then
   begin
   HighValue=0;
   For cnt=0 to (10+currentbar)
   begin
      if High[cnt]>HighValue then            //High on position cnt higher then previous maximum?
      Begin
         HighValue=High[cnt];                  //Store new Maximum
      end;         
   end;
   Plot1(currentbar);
End;

In this example I have set MaxBarsBack by the properties-dialog of the study to an user specific value of 20.
This code does nothing else then constantly finding the Maximum of all bars except the first 10.
For cnt=0 to (10+currentbar) => MaxBarsBack is 20, so this loop should end at the 10th bar of the chart.
Of course there would be a way to avoid this problem, but I'd like to understand, why MaxBarsBack changes the value while the indicator is active and currentbar restarts at 0 at this time. I could not find in EL documentation, what this behaviour could cause.

I really would appreciate, if somebody could give me a hint.

Thanks!

Squib
Posts: 17
Joined: 25 Nov 2011
Has thanked: 19 times
Been thanked: 6 times

Re: currentbar and MaxBarsBack

Postby Squib » 08 Dec 2011

Btw.
JoshM wrote:Have you tried removing the indicator and re-adding it to the chart? (Since changes in the MaxBarsBack setting only seem to come in effect after re-adding as far as I know)

Btw, looking at your screenshot, I wouldn't be surprised if the same indicator is applied to a minimized chart or set to hidden somewhere. Try including something as GetSymbolName, BarInterval, BarType or GetStrategyName in your output to see where the output is being printed from.

Also removing the indicator doesn't change the behaviour. I also included the test outputs, which you suggested.
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 11.00 MB: 20.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 12.00 MB: 20.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 13.00 MB: 20.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 14.00 MB: 20.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 15.00 MB: 20.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 1.00 MB: 33.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 2.00 MB: 33.00
EUR/USD BarInterval: 1.00 BarType: 1.00 StrategyName:wg_TestFktn_2 CB: 3.00 MB: 33.00

Chart seems to be ok...

User avatar
Roman MultiCharts
Posts: 50
Joined: 28 Nov 2011
Has thanked: 21 times
Been thanked: 67 times

Re: currentbar and MaxBarsBack

Postby Roman MultiCharts » 09 Dec 2011

Hi Squib,

We have analyzed your script and found out that the MB output values that you had were caused by the cycle:

Code: Select all

begin
   HighValue=0;
   For cnt=0 to (10+currentbar)
end;


Please substitute the line:

Code: Select all

For cnt=0 to (10+currentbar)


with the line:

Code: Select all

For cnt=0 to MaxBarsBack


It will solve this problem.
These users thanked the author Roman MultiCharts for the post (total 2):
JoshMSquib

Squib
Posts: 17
Joined: 25 Nov 2011
Has thanked: 19 times
Been thanked: 6 times

Re: currentbar and MaxBarsBack

Postby Squib » 11 Dec 2011

Hi Roman,

thanks for helping, but changing the code in this way changes the functionality.

What I want to do is following (just as test-example).

At every bar I want to plot the maximum value from the very left side of the chart until the current bar.
According to my undestanding, following code should do this:

Code: Select all

variables:
   HighValue(0),
   cnt(0);   
Print("CB:",currentbar," MB:", MaxBarsBack);
HighValue=0;
For cnt=currentbar downto 0
begin
   if High[cnt]>HighValue then            //High on position cnt higher then previous maximum?
   Begin
      HighValue=High[cnt];                  //Store new Maximum
   end;         
end;
Plot1(HighValue);

In principal it does, but it only starts from bar 630 and has this strange output of currentbar and MaxBarsBack:

Code: Select all

CB:   1.00 MB:  20.00
CB:   2.00 MB:  20.00
CB:   3.00 MB:  20.00
CB:   4.00 MB:  20.00
CB:   5.00 MB:  20.00
CB:   6.00 MB:  20.00
CB:   7.00 MB:  20.00
CB:   8.00 MB:  20.00
CB:   9.00 MB:  20.00
CB:  10.00 MB:  20.00
CB:  11.00 MB:  20.00
CB:  12.00 MB:  20.00
CB:  13.00 MB:  20.00
CB:  14.00 MB:  20.00
CB:  15.00 MB:  20.00
CB:  16.00 MB:  20.00
CB:  17.00 MB:  20.00
CB:  18.00 MB:  20.00
CB:  19.00 MB:  20.00
CB:  20.00 MB:  20.00
CB:  21.00 MB:  20.00
CB:   1.00 MB:  33.00        <==== step of MaxBarsBack from 20 to 33 and currentbar restarts at 1
CB:   2.00 MB:  33.00
CB:   3.00 MB:  33.00
CB:   4.00 MB:  33.00
CB:   5.00 MB:  33.00
CB:   6.00 MB:  33.00

If I replace currentbar from aboves script with MaxBarsBack, the output works fine (CB is incrementing from 1 on, MB stays at 20) , but the functionality is different, because it only searches the maximum of the last 20 bars.

For me it would be very helpful to get an explanation, why MB has steps and why CB restarts at 1 at every step from MB.
Attachments
MaxBarsBack_1.jpg
MaxBarsBack_1.jpg (692.46 KiB) Viewed 240 times

Squib
Posts: 17
Joined: 25 Nov 2011
Has thanked: 19 times
Been thanked: 6 times

Re: currentbar and MaxBarsBack

Postby Squib » 14 Dec 2011

Meanwhile I found the answer by myself (with help from another forum).

Problem is, that my understanding of MaxBarsBack was wrong. It does not only determine the bar on which currentbar starts to count with 1, but also limits the number of bars, which can be accessed backwards.

So the code in my example can neither work, nor is it programmed in an efficient way, since MC performance breaks down when MaxBarsBack is rising.
It's clear, because the higher MaxBarsBack, the more previous values from all used variables need to be managed.
These users thanked the author Squib for the post (total 3):
TJRoman MultiChartsJoshM

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: currentbar and MaxBarsBack

Postby TJ » 14 Dec 2011

Squib wrote:Meanwhile I found the answer by myself (with help from another forum).

Problem is, that my understanding of MaxBarsBack was wrong. It does not only determine the bar on which currentbar starts to count with 1, but also limits the number of bars, which can be accessed backwards.

So the code in my example can neither work, nor is it programmed in an efficient way, since MC performance breaks down when MaxBarsBack is rising.
It's clear, because the higher MaxBarsBack, the more previous values from all used variables need to be managed.

Thanks for posting an update. The information is much appreciated.
These users thanked the author TJ for the post:
Roman MultiCharts


Return to “MultiCharts”