Need information about level2 to disqualify level1 above, without using a loop

I've asked something similar before, but this one goes another level down, and now I don't know if I can accomplish this without a loop. This comes from a much more complex piece of code that I've reduced for example's sake here. I'm hoping there is a way to do this without using a loop because it will get real complex if that is required.

I need to disqualify this sequence (denoted by the horiztonal, aqua, small circles) when we either hit the top channel, or Max(X bars after the first aqua circles start, Y bars after the second aqua circles start). So, what is confusing me is that I need information about the second level's barcount to disqualify the first level. But since the second level is built upon the first level, I don't have that information at the top of the code, which is where I need it to disqualify.

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

dt = DateTimeToStr(GetCursorXPosition());
bi = BarIndex();

myMA = MA(close, 22);
myATR = ATR(20);

Level1 = myMA - 1 * myATR;
TopLevel = myMA + 1 * myATR;

Plot(myMA,"myMA",colorWhite,styleLine);
Plot(Level1,"Level1",colorWhite,styleDashed);
Plot(TopLevel,"Level1",colorWhite,styleDashed);

ResetCondition = High >= ref( TopLevel, -1 ); 

Setup1Condition = Low <= Ref(Level1, -1);
Flip1 = Flip( Setup1Condition, ResetCondition );
Level1PotentialPrice = Min(Open, ref( Level1, -1 ));
Level1Price = ValueWhen( Flip1 == 1 AND Ref(Flip1, -1) == 0, Ref(Level1PotentialPrice,0)); 
PlotShapes(IIf(Flip1 == 1, shapeSmallCircle, shapeNone),colorAqua,  0, Level1Price, Offset=0); 

Level2 = Level1Price - myATR * 1;
Setup2Condition = Low <= Ref(Level2, -1);
Flip2 = Flip( Setup2Condition, ResetCondition );
Level2PotentialPrice = Min(Open, ref( Level2, -1 ));
Level2Price = ValueWhen( Flip2 == 1 AND Ref(Flip2, -1) == 0, Ref(Level2PotentialPrice,0)); 
PlotShapes(IIf(Flip2 == 1, shapeSmallCircle, shapeNone),colorAqua,  0, Level2Price, Offset=0); 


Level1BarsSince = BarsSince(Flip1 AND Ref(Flip1, -1) == 0);
Level2BarsSince = BarsSince(Flip2 AND Ref(Flip2, -1) == 0);

PlotShapes(IIf( (Ref(Flip1,-1) OR Ref(Flip2,-1)) AND ResetCondition, shapeCircle, shapeNone),colorWhite,  0, Max(ref( TopLevel, -1 ), Open), Offset=0); 

BarsSinceDisqualifyCondition = ( ( Flip1 AND Ref(Flip1,-1) AND BarsSince(Flip1 AND Ref(flip1,-1) == 0) == 20 ) OR ( Flip2 AND Ref(Flip2,-1) AND Max( BarsSince(Flip1 AND Ref(flip1,-1) == 0) == 20, BarsSince(Flip2 AND Ref(flip2,-1) == 0) == 10 ) ) );
PlotShapes(IIf(BarsSinceDisqualifyCondition, shapeCircle, shapeNone),colorWhite,  0, Close , Offset=0);

So, I need both cyan horizontal dots to stop being drawn, which I am considering that to be the disqualification of the whole sequence, where the hand drawn line blue vertical line is drawn.

image

How do I get the information about Level2 up there to disqualify Level1 without using a loop?

no takers? Do you need more info?

Can you just compute a preliminary Level 1 and Level 2, then go back and truncate them both in a single step, i.e. with an array function?

There are actually many levels, all that build below the previous level(s). Some levels we trade, some we do not. The levels get more and more complex as they stack. So for example's sake here I am only using two levels because if I can get it to work with two, I can adapt it to many.

So, the answer is no.

I'm really thinking this has to be done with a loop, but that will open a world of complication to redo everything I have done so far.

What if you start by defining Level 1 so that it lasts for a maximum of X bars. Then when you calculate Level 2, you define it to last for a maximum of Y bars, and at that time also extend Level 1 to the end of Level 2? After calculating Level N, you would need to revisit Levels 1 through N-1.

But how do I extend Level 1 at the time of Level 2? Seems to be the same delima of needing info from the bottom at the top...

Not sure I understood what you want but ...

SetChartOptions( 0, chartShowArrows | chartShowDates );
//SetChartBkColor(colorDarkOliveGreen);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

dt = DateTimeToStr( GetCursorXPosition() );
bi = BarIndex();

myMA = MA( close, 22 );
myATR = ATR( 20 );

Level1 = myMA - 1 * myATR;
TopLevel = myMA + 1 * myATR;

ResetCondition = High >= ref( TopLevel, -1 );

Setup1Condition = Low <= Ref( Level1, -1 );
Flip1 = Flip( Setup1Condition, ResetCondition );
Level1PotentialPrice = Min( Open, ref( Level1, -1 ) );
Level1Price = ValueWhen( Flip1 == 1 AND Ref( Flip1, -1 ) == 0, Ref( Level1PotentialPrice, 0 ) );

Level2 = Level1Price - myATR * 1;
Setup2Condition = Low <= Ref( Level2, -1 );
Flip2 = Flip( Setup2Condition, ResetCondition );
Level2PotentialPrice = Min( Open, ref( Level2, -1 ) );
Level2Price = ValueWhen( Flip2 == 1 AND Ref( Flip2, -1 ) == 0, Ref( Level2PotentialPrice, 0 ) );

Level1BarsSince = BarsSince( Flip1 AND Ref( Flip1, -1 ) == 0 );
Level2BarsSince = BarsSince( Flip2 AND Ref( Flip2, -1 ) == 0 );

BarsSinceDisqualifyCondition = ( ( Flip1 AND Ref( Flip1, -1 ) AND BarsSince( Flip1 AND Ref( flip1, -1 ) == 0 ) >= 20 ) OR
	( Flip2 AND Ref( Flip2, -1 ) AND Max( BarsSince( Flip1 AND Ref( flip1, -1 ) == 0 ) >= 20, BarsSince( Flip2 AND Ref( flip2, -1 ) == 0 ) >= 10 ) ) );

Plot( myMA, "myMA", colorWhite, styleLine );
Plot( Level1, "Level1", colorWhite, styleDashed );
Plot( TopLevel, "Level1", colorWhite, styleDashed );
Flip1 = Flip(Flip1, BarsSinceDisqualifyCondition);
Level1Price = IIf( Flip1 == 1, Level1Price, Null);
Flip2 = Flip(Flip2, BarsSinceDisqualifyCondition);
Level2Price = IIf( Flip2 == 1, Level2Price, Null);
Plot( Level1Price, "", colorAqua, styleDots | styleNoLine | styleNoTitle | styleNoLabel);
Plot( Level2Price, "", colorAqua, styleDots | styleNoLine | styleNoTitle | styleNoLabel);
//PlotShapes( IIf( Flip1 == 1, shapeSmallCircle, shapeNone ), colorAqua,  0, Level1Price, Offset = 0 );
//PlotShapes( IIf( Flip2 == 1, shapeSmallCircle, shapeNone ), colorAqua,  0, Level2Price, Offset = 0 );
//PlotShapes( IIf( ( Ref( Flip1, -1 ) OR Ref( Flip2, -1 ) ) AND ResetCondition, shapeCircle, shapeNone ), colorWhite,  0, Max( ref( TopLevel, -1 ), Open ), Offset = 0 );
//PlotShapes( IIf( BarsSinceDisqualifyCondition, shapeCircle, shapeNone ), colorblue,  0, Close , Offset = 0 );


@awilson, Not too sure what this is, and it's not disqualifying all levels simultaneously at the first occurrence of one of the conditions.

i'd say your gap up fill question was pretty clear, but this one is a bit murky :slight_smile:

the gist here is we need both levels to stop being drawn at the first occurrence of either:
1.) X bars after Level1 starts drawing
2.) Y bars after Level2 starts drawing
3.) we hit the top band

So, if you see the graphic above, where I have the hand drawn blue, vertical line, that is the first occurrence of one of the above three, and that is where BOTH aqua lines should stop being drawn. Does this clarify it better?