Page 1 of 1

MaxBarsBack - Help needed

Posted: Oct 20 2019
by Mydesign
Hello,

I am struggeling with this error: "tried to reference back more bars then allowed" and would like to avoid the case in my code. I have read evreything I could about this but sadly can't get a grip on it.

Strangely this exact same code will never give an error as an indicator but will do as a signal, unless I increasing the MaxBarsBack manually to over needed value. Sadly, one cannot set the MaxBarsBack in a signal. I just can't find a proper way to systematically avoid the problem ( = referencing back less bars than allowed).

Code: Select all

inputs: Strength( 20 ) ;

Vars: HSwing(False), LSwing(False), LastSwingHighBN(0), LastSwingLowBN(0), TLRef(0) ;

HSwing = PivotHighVSBar( 1, High, Strength, Strength, Strength + 1 ) <> -1 ;
LSwing = PivotLowVSBar( 1, Low, Strength, Strength, Strength + 1 ) <> -1 ;

if HSwing then
begin
LastSwingHighBN = BarNumber[Strength] ;
TLRef = TL_New_BN( LastSwingLowBN, Close[CurrentBar-LastSwingLowBN], LastSwingHighBN, Close[CurrentBar-LastSwingHighBN] ) ;
end;

if LSwing then
begin
LastSwingLowBN = BarNumber[Strength] ;
TLRef = TL_New_BN( LastSwingHighBN, Close[CurrentBar-LastSwingHighBN], LastSwingLowBN, Close[CurrentBar-LastSwingLowBN] ) ;
end;
Restriction such as "If CurrentBar > MaxBarsBack + Strength" does not seem to help...

Your help is most welcome. Thanks !

Re: MaxBarsBack - Help needed

Posted: Oct 20 2019
by TJ
What is the instrument?
What is the chart resolution?
How much back data do you have? (ie many bars on the chart?)
How many data series?

Please take a screenshot of the error message.

Re: MaxBarsBack - Help needed

Posted: Oct 20 2019
by Mydesign
Thank you TJ for your time. This is for instance on a single data LMAX USTech100 115 points range bar chart, from 1/1/2017 till now, which is about 50 000 bars loaded.

If I set the MaxBarsBack to 100, the error says I tried to reference 106 bars. If I set the MaxBarsBack to 200, the error says I tried to reference 206 bars... but if I et the MaxBarsBack to 150, the error says I tried to reference 201 bars ! This is driving me nuts :cry: :roll:

MaxBarsBack error.jpg
(104.89 KiB) Downloaded 296 times

Re: MaxBarsBack - Help needed

Posted: Oct 20 2019
by TJ
What kind of calculations do you have in your strategy/study?

Re: MaxBarsBack - Help needed

Posted: Oct 21 2019
by Mydesign
TJ, I posted the part of the code that produces the error... Other calculations are indeed made based on that but that's not the issue. Basically I need to reference back the last 2 High-Low pivot prices.

Anyway I need it as a signal, not as an indicator (which is working fine as stated).

Re: MaxBarsBack - Help needed

Posted: Oct 22 2019
by MAZINGUER
Hello,
On the chart with the signal, display the menu on the SA tab and select strategy properties.
Then, in the table of the strategy properties, indicate in the table the minimum bars that the strategy needs to make the calculations.
I attached an image

Re: MaxBarsBack - Help needed

Posted: Oct 22 2019
by Mydesign
Thank you Mazinguer, I am aware of that setting, which I use to set it manually once I get the error. But my question was: how to make sure to avoid the error - within the code -, thus how to avoid to reference back more bars than allowed ?

One very annoyning thing is that every time the error appears, MC switches off every single signal applied to the chart.
I usually have several signals, which I switch on or off for testing purposes. When everything is automatically switched off by such error, I have no way to recall the previous state: I have to remember/guess my previous setup. A pain...

Nevermind...

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by MAZINGUER
Hello,
Looking at your code there is something I don't understand ... you want to calculate the pivots considering "strength" bars on the left and "strength" bars on the right of the considered pivot.
However, just consider "strength + 1" bars for the calculation so I don't think the code can find any pivot because if you want it to have "strength" bars on the left and "strength" bars on the right, you should consider at least (2 * strength) +1 bars.
I mean
HSwing = PivotHighVSBar (1, High, Strength, Strength, (2 * Strength) + 1) <> -1;
LSwing = PivotLowVSBar (1, Low, Strength, Strength, (2 * Strength) + 1) <> -1;
and then to refer to the pivot bar number, I think it would be more correct to write
LastSwingHighBN = BarNumber [Strength + 1]

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by ABC
Mydesign,

from glancing at your code the problem is likely coming from the variable lookback you introduce here:

Code: Select all

if HSwing then
begin
LastSwingHighBN = BarNumber[Strength] ;
TLRef = TL_New_BN( LastSwingLowBN, Close[CurrentBar-LastSwingLowBN], LastSwingHighBN, Close[CurrentBar-LastSwingHighBN] ) ;
end;

if LSwing then
begin
LastSwingLowBN = BarNumber[Strength] ;
TLRef = TL_New_BN( LastSwingHighBN, Close[CurrentBar-LastSwingHighBN], LastSwingLowBN, Close[CurrentBar-LastSwingLowBN] ) ;
end;
When HSwing is true the last time LSwing was true could be several bars ago and vice versa. If CurrentBar - LastSwingLowBN > your max bars back setting at that moment this would trigger the error.
A good approach to deal with that would be to store the trend line prices at the moment you store LastSwingLowBN/LastSwingHighBN, too. This way you have them in a variable and don't have to look back for Close[CurrentBar-LastSwingLowBN] and Close[CurrentBar-LastSwingHighBN] when the opposite swing is detected.

Regards,

ABC

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by MAZINGUER

When HSwing is true the last time LSwing was true could be several bars ago and vice versa. If CurrentBar - LastSwingLowBN > your max bars back setting at that moment this would trigger the error.
A good approach to deal with that would be to store the trend line prices at the moment you store LastSwingLowBN/LastSwingHighBN, too. This way you have them in a variable and don't have to look back for Close[CurrentBar-LastSwingLowBN] and Close[CurrentBar-LastSwingHighBN] when the opposite swing is detected.
Good ABC observation, possibly that is the reason, because indeed the number bar [CurrentBar-LastSwingLowBN] might be larger than the Maxbarsback.
So one question that remains is whether it is necessary that the length used for the calculation of the pivots has to cover the bars to the left and to the right of the pivot ... or just with the length covering the pivot is sufficient?

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by Mydesign
Thank you guys for the feedback !

@Mazinguer: I don't think the Length+1 thing is a problem. I use that in other pivot detection codes without any issue. Actually, this is not my method, I found it in some examples for using this function.

@ABC: You pinpointed perfectly the issue with the look back. I will try your hint of storing the value at detection time instead of trying to retrieve it later on... hopefully it will solve the problem :roll: :)

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by MAZINGUER

@Mazinguer: I don't think the Length+1 thing is a problem. I use that in other pivot detection codes without any issue. Actually, this is not my method, I found it in some examples for using this function.
Fine, but it wasn't the main thing I wanted to say. The main thing I wanted to say is that I think the length should include the bars to the left and to the right of the pivot point, that is, the length of the function: PivotHighVSBar must be at least 2 * strength

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by Mydesign
Mazinguer, you might be right, but the only thing I know is that others just do leave the length as Right strength + 1. I guess it is enough to have it to work properly, and without any maxbackbars issues, as seen in these links:

http://traders.com/Documentation/FEEDbk ... sTips.html
viewtopic.php?t=11922#p58765

This following code detects pivots as seen on the screenshot below, which is what I need and use:

Code: Select all

inputs: LeftStrength( 10 ), RightStrength( 10 ) ;
Vars: HRev(False), LRev(False) ;

HRev = PivotHighVSBar( 1, High, LeftStrength, RightStrength, RightStrength + 1 ) <> -1 ;
LRev = PivotLowVSBar( 1, Low, LeftStrength, RightStrength, RightStrength + 1 ) <> -1 ;

if HRev then Plot1[RightStrength]( High[RightStrength], "PivotHi" )
else NoPlot( 1 ) ;

if LRev then Plot2[RightStrength]( Low[RightStrength], "PivotLo" )
else NoPlot( 2 ) ;
Pivots10.jpg
(26.04 KiB) Downloaded 164 times

On the other hand with this code:

Code: Select all

inputs: LeftStrength( 10 ), RightStrength( 10 ) ;
Vars: HRev(False), LRev(False) ;

HRev = PivotHighVSBar( 1, High, LeftStrength, RightStrength, 2*RightStrength+1) ) <> -1 ;
LRev = PivotLowVSBar( 1, Low, LeftStrength, RightStrength, 2*RightStrength+1) ) <> -1 ;

if HRev then Plot1[RightStrength]( High[RightStrength], "PivotHi" )
else NoPlot( 1 ) ;

if LRev then Plot2[RightStrength]( Low[RightStrength], "PivotLo" )
else NoPlot( 2 ) ;
I get this (which is NOT what I am looking for):
Pivots10x2.jpg
(29.91 KiB) Downloaded 164 times
Don't ask me why...

Re: MaxBarsBack - Help needed

Posted: Oct 23 2019
by MAZINGUER
Well, I don't know how to explain it either but I have a similar indicator but made for TS, using pivots, but from what I see the interpretation is different on both platforms

Re: MaxBarsBack - Help needed

Posted: Nov 05 2019
by wilkinsw
"Close[CurrentBar-LastSwingLowBN]"

You're opening your script up to large lookbacks.

try "Close[minlist(CurrentBar-LastSwingLowBN,maxbarsback)]".

That was just from a quick glance. Have no idea what you are trying to achieve mind ;)

Re: MaxBarsBack - Help needed  [SOLVED]

Posted: Nov 05 2019
by Mydesign
Thank you wilkinsw. I solved it anyway with ABC solution: storing the value at pivot detection time instead of trying to retrieve it later on with lookbacks: much simplier 8)

Basically my goal was to build some kind of zigzag tool for statistical evaluation purposes (Google "Perfect Profit" from R. Pardo and you'll get a hint :wink: )

Re: MaxBarsBack - Help needed

Posted: Nov 05 2019
by wilkinsw
There's lots of Pardo's FF's in this forum, just in case you didn't know.

viewtopic.php?f=5&t=8838&p=42777&hilit= ... fit#p42777

Re: MaxBarsBack - Help needed

Posted: Nov 05 2019
by Mydesign
Thanks. This is well in line with what I am working on...