Looping for Swing High and Low

Hello,
The code below identifies swing high and low locations. Future bars are used to confirm, in part, whether a bar is a swing high or low. I have written the code for each forward step of the forward count bars, sufficient to count forward 6 bars only.

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

//Swing bars
sb = Param( "Swing Bars", 3, 1, 50, 1 );

// function definitions
function futureHHV( array, periods )
{
    return Ref( HHV( array, periods ), periods );
}
function futureLLV( array, periods )
{
    return Ref( LLV( array, periods ), periods );
}

//Forward Count number of bar(s) after Peak or Trough
fc1 = 1; 
fc2 = 2; 
fc3 = 3; 
fc4 = 4; 
fc5 = 5; 
fc6 = 6; 

//PEAK: Swing bars = 2, Forward Count 1 to 6
pk21 = IIf( Sb == 2 AND H == HHV( H, sb )
            AND futureHHV( H, fc1 ) < H
            AND Ref( L, fc1 ) < Min( IIf( fc1 > 1, futureLLV( L, ( fc1 - 1 ) ), L ), IIf( fc1 < Sb, LLV( L, ( Sb - ( fc1 - 1 ) ) ), L ) ), True, False );

pk22 = IIf( Sb == 2 AND H == HHV( H, Sb )
            AND futureHHV( H, fc2 ) < H
            AND Ref( L, fc2 ) < Min( IIf( fc2 > 1, futureLLV( L, ( fc2 - 1 ) ), L ), IIf( fc2 < Sb, LLV( L, ( Sb - ( fc2 - 1 ) ) ), L ) ), True, False );

pk23 = IIf( Sb == 2 AND H == HHV( H, Sb )
            AND futureHHV( H, fc3 ) < H
            AND Ref( L, fc3 ) < Min( IIf( fc3 > 1, futureLLV( L, ( fc3 - 1 ) ), L ), IIf( fc3 < Sb, LLV( L, ( Sb - ( fc3 - 1 ) ) ), L ) ), True, False );

pk24 = IIf( Sb == 2 AND H == HHV( H, Sb )
            AND futureHHV( H, fc4 ) < H
            AND Ref( L, fc4 ) < Min( IIf( fc4 > 1, futureLLV( L, ( fc4 - 1 ) ), L ), IIf( fc4 < Sb, LLV( L, ( Sb - ( fc4 - 1 ) ) ), L ) ), True, False );

pk25 = IIf( Sb == 2 AND H == HHV( H, Sb )
            AND futureHHV( H, fc5 ) < H
            AND Ref( L, fc5 ) < Min( IIf( fc5 > 1, futureLLV( L, ( fc5 - 1 ) ), L ), IIf( fc5 < Sb, LLV( L, ( Sb - ( fc5 - 1 ) ) ), L ) ), True, False );

pk26 = IIf( Sb == 2 AND H == HHV( H, Sb )
            AND futureHHV( H, fc6 ) < H
            AND Ref( L, fc6 ) < Min( IIf( fc6 > 1, futureLLV( L, ( fc6 - 1 ) ), L ), IIf( fc6 < Sb, LLV( L, ( Sb - ( fc6 - 1 ) ) ), L ) ), True, False );

//PEAK: Swing bars = 3, Forward Count 1 to 6
pk31 = IIf( Sb == 3 AND H == HHV( H, sb )
            AND futureHHV( H, fc1 ) < H
            AND Ref( L, fc1 ) < Min( IIf( fc1 > 1, futureLLV( L, ( fc1 - 1 ) ), L ), IIf( fc1 < Sb, LLV( L, ( Sb - ( fc1 - 1 ) ) ), L ) ), True, False );

pk32 = IIf( Sb == 3 AND H == HHV( H, Sb )
            AND futureHHV( H, fc2 ) < H
            AND Ref( L, fc2 ) < Min( IIf( fc2 > 1, futureLLV( L, ( fc2 - 1 ) ), L ), IIf( fc2 < Sb, LLV( L, ( Sb - ( fc2 - 1 ) ) ), L ) ), True, False );

pk33 = IIf( Sb == 3 AND H == HHV( H, Sb )
            AND futureHHV( H, fc3 ) < H
            AND Ref( L, fc3 ) < Min( IIf( fc3 > 1, futureLLV( L, ( fc3 - 1 ) ), L ), IIf( fc3 < Sb, LLV( L, ( Sb - ( fc3 - 1 ) ) ), L ) ), True, False );

pk34 = IIf( Sb == 3 AND H == HHV( H, Sb )
            AND futureHHV( H, fc4 ) < H
            AND Ref( L, fc4 ) < Min( IIf( fc4 > 1, futureLLV( L, ( fc4 - 1 ) ), L ), IIf( fc4 < Sb, LLV( L, ( Sb - ( fc4 - 1 ) ) ), L ) ), True, False );

pk35 = IIf( Sb == 3 AND H == HHV( H, Sb )
            AND futureHHV( H, fc5 ) < H
            AND Ref( L, fc5 ) < Min( IIf( fc5 > 1, futureLLV( L, ( fc5 - 1 ) ), L ), IIf( fc5 < Sb, LLV( L, ( Sb - ( fc5 - 1 ) ) ), L ) ), True, False );

pk36 = IIf( Sb == 3 AND H == HHV( H, Sb )
            AND futureHHV( H, fc6 ) < H
            AND Ref( L, fc6 ) < Min( IIf( fc6 > 1, futureLLV( L, ( fc6 - 1 ) ), L ), IIf( fc6 < Sb, LLV( L, ( Sb - ( fc6 - 1 ) ) ), L ) ), True, False );

//PEAK: Swing bars = 4, Forward Count 1 to 6
pk41 = IIf( Sb == 4 AND H == HHV( H, sb )
            AND futureHHV( H, fc1 ) < H
            AND Ref( L, fc1 ) < Min( IIf( fc1 > 1, futureLLV( L, ( fc1 - 1 ) ), L ), IIf( fc1 < Sb, LLV( L, ( Sb - ( fc1 - 1 ) ) ), L ) ), True, False );

pk42 = IIf( Sb == 4 AND H == HHV( H, Sb )
            AND futureHHV( H, fc2 ) < H
            AND Ref( L, fc2 ) < Min( IIf( fc2 > 1, futureLLV( L, ( fc2 - 1 ) ), L ), IIf( fc2 < Sb, LLV( L, ( Sb - ( fc2 - 1 ) ) ), L ) ), True, False );

pk43 = IIf( Sb == 4 AND H == HHV( H, Sb )
            AND futureHHV( H, fc3 ) < H
            AND Ref( L, fc3 ) < Min( IIf( fc3 > 1, futureLLV( L, ( fc3 - 1 ) ), L ), IIf( fc3 < Sb, LLV( L, ( Sb - ( fc3 - 1 ) ) ), L ) ), True, False );

pk44 = IIf( Sb == 4 AND H == HHV( H, Sb )
            AND futureHHV( H, fc4 ) < H
            AND Ref( L, fc4 ) < Min( IIf( fc4 > 1, futureLLV( L, ( fc4 - 1 ) ), L ), IIf( fc4 < Sb, LLV( L, ( Sb - ( fc4 - 1 ) ) ), L ) ), True, False );

pk45 = IIf( Sb == 4 AND H == HHV( H, Sb )
            AND futureHHV( H, fc5 ) < H
            AND Ref( L, fc5 ) < Min( IIf( fc5 > 1, futureLLV( L, ( fc5 - 1 ) ), L ), IIf( fc5 < Sb, LLV( L, ( Sb - ( fc5 - 1 ) ) ), L ) ), True, False );

pk46 = IIf( Sb == 4 AND H == HHV( H, Sb )
            AND futureHHV( H, fc6 ) < H
            AND Ref( L, fc6 ) < Min( IIf( fc6 > 1, futureLLV( L, ( fc6 - 1 ) ), L ), IIf( fc6 < Sb, LLV( L, ( Sb - ( fc6 - 1 ) ) ), L ) ), True, False );

//PEAK: Swing bars = 5, Forward Count 1 to 6
pk51 = IIf( Sb == 5 AND H == HHV( H, sb )
            AND futureHHV( H, fc1 ) < H
            AND Ref( L, fc1 ) < Min( IIf( fc1 > 1, futureLLV( L, ( fc1 - 1 ) ), L ), IIf( fc1 < Sb, LLV( L, ( Sb - ( fc1 - 1 ) ) ), L ) ), True, False );

pk52 = IIf( Sb == 5 AND H == HHV( H, Sb )
            AND futureHHV( H, fc2 ) < H
            AND Ref( L, fc2 ) < Min( IIf( fc2 > 1, futureLLV( L, ( fc2 - 1 ) ), L ), IIf( fc2 < Sb, LLV( L, ( Sb - ( fc2 - 1 ) ) ), L ) ), True, False );

pk53 = IIf( Sb == 5 AND H == HHV( H, Sb )
            AND futureHHV( H, fc3 ) < H
            AND Ref( L, fc3 ) < Min( IIf( fc3 > 1, futureLLV( L, ( fc3 - 1 ) ), L ), IIf( fc3 < Sb, LLV( L, ( Sb - ( fc3 - 1 ) ) ), L ) ), True, False );

pk54 = IIf( Sb == 5 AND H == HHV( H, Sb )
            AND futureHHV( H, fc4 ) < H
            AND Ref( L, fc4 ) < Min( IIf( fc4 > 1, futureLLV( L, ( fc4 - 1 ) ), L ), IIf( fc4 < Sb, LLV( L, ( Sb - ( fc4 - 1 ) ) ), L ) ), True, False );

pk55 = IIf( Sb == 5 AND H == HHV( H, Sb )
            AND futureHHV( H, fc5 ) < H
            AND Ref( L, fc5 ) < Min( IIf( fc5 > 1, futureLLV( L, ( fc5 - 1 ) ), L ), IIf( fc5 < Sb, LLV( L, ( Sb - ( fc5 - 1 ) ) ), L ) ), True, False );

pk56 = IIf( Sb == 5 AND H == HHV( H, Sb )
            AND futureHHV( H, fc6 ) < H
            AND Ref( L, fc6 ) < Min( IIf( fc6 > 1, futureLLV( L, ( fc6 - 1 ) ), L ), IIf( fc6 < Sb, LLV( L, ( Sb - ( fc6 - 1 ) ) ), L ) ), True, False );

//PEAK: Swing bars = 6, Forward Count 1 to 6
pk61 = IIf( Sb == 6 AND H == HHV( H, sb )
            AND futureHHV( H, fc1 ) < H
            AND Ref( L, fc1 ) < Min( IIf( fc1 > 1, futureLLV( L, ( fc1 - 1 ) ), L ), IIf( fc1 < Sb, LLV( L, ( Sb - ( fc1 - 1 ) ) ), L ) ), True, False );

pk62 = IIf( Sb == 6 AND H == HHV( H, Sb )
            AND futureHHV( H, fc2 ) < H
            AND Ref( L, fc2 ) < Min( IIf( fc2 > 1, futureLLV( L, ( fc2 - 1 ) ), L ), IIf( fc2 < Sb, LLV( L, ( Sb - ( fc2 - 1 ) ) ), L ) ), True, False );

pk63 = IIf( Sb == 6 AND H == HHV( H, Sb )
            AND futureHHV( H, fc3 ) < H
            AND Ref( L, fc3 ) < Min( IIf( fc3 > 1, futureLLV( L, ( fc3 - 1 ) ), L ), IIf( fc3 < Sb, LLV( L, ( Sb - ( fc3 - 1 ) ) ), L ) ), True, False );

pk64 = IIf( Sb == 6 AND H == HHV( H, Sb )
            AND futureHHV( H, fc4 ) < H
            AND Ref( L, fc4 ) < Min( IIf( fc4 > 1, futureLLV( L, ( fc4 - 1 ) ), L ), IIf( fc4 < Sb, LLV( L, ( Sb - ( fc4 - 1 ) ) ), L ) ), True, False );

pk65 = IIf( Sb == 6 AND H == HHV( H, Sb )
            AND futureHHV( H, fc5 ) < H
            AND Ref( L, fc5 ) < Min( IIf( fc5 > 1, futureLLV( L, ( fc5 - 1 ) ), L ), IIf( fc5 < Sb, LLV( L, ( Sb - ( fc5 - 1 ) ) ), L ) ), True, False );

pk66 = IIf( Sb == 6 AND H == HHV( H, Sb )
            AND futureHHV( H, fc6 ) < H
            AND Ref( L, fc6 ) < Min( IIf( fc6 > 1, futureLLV( L, ( fc6 - 1 ) ), L ), IIf( fc6 < Sb, LLV( L, ( Sb - ( fc6 - 1 ) ) ), L ) ), True, False );

//TROUGH: Swing bars = 2, Forward Count 1 to 6
tr21 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc1 ) > L
            AND Ref( H, fc1 ) > Max( IIf( fc1 > 1, futureHHV( H, ( fc1 - 1 ) ), H ), IIf( fc1 < Sb, HHV( H, ( Sb - ( fc1 - 1 ) ) ), H ) ), True, False );

tr22 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc2 ) > L
            AND Ref( H, fc2 ) > Max( IIf( fc2 > 1, futureHHV( H, ( fc2 - 1 ) ), H ), IIf( fc2 < Sb, HHV( H, ( Sb - ( fc2 - 1 ) ) ), H ) ), True, False );

tr23 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc3 ) > L
            AND Ref( H, fc3 ) > Max( IIf( fc3 > 1, futureHHV( H, ( fc3 - 1 ) ), H ), IIf( fc3 < Sb, HHV( H, ( Sb - ( fc3 - 1 ) ) ), H ) ), True, False );

tr24 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc4 ) > L
            AND Ref( H, fc4 ) > Max( IIf( fc4 > 1, futureHHV( H, ( fc4 - 1 ) ), H ), IIf( fc4 < Sb, HHV( H, ( Sb - ( fc4 - 1 ) ) ), H ) ), True, False );

tr25 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc5 ) > L
            AND Ref( H, fc5 ) > Max( IIf( fc5 > 1, futureHHV( H, ( fc5 - 1 ) ), H ), IIf( fc5 < Sb, HHV( H, ( Sb - ( fc5 - 1 ) ) ), H ) ), True, False );

tr26 = IIf( Sb == 2 AND L == LLV( L, sb )
            AND futureLLV( L, fc6 ) > L
            AND Ref( H, fc6 ) > Max( IIf( fc6 > 1, futureHHV( H, ( fc6 - 1 ) ), H ), IIf( fc6 < Sb, HHV( H, ( Sb - ( fc6 - 1 ) ) ), H ) ), True, False );

//TROUGH: Swing bars = 3, Forward Count 1 to 6
tr31 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc1 ) > L
            AND Ref( H, fc1 ) > Max( IIf( fc1 > 1, futureHHV( H, ( fc1 - 1 ) ), H ), IIf( fc1 < Sb, HHV( H, ( Sb - ( fc1 - 1 ) ) ), H ) ), True, False );

tr32 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc2 ) > L
            AND Ref( H, fc2 ) > Max( IIf( fc2 > 1, futureHHV( H, ( fc2 - 1 ) ), H ), IIf( fc2 < Sb, HHV( H, ( Sb - ( fc2 - 1 ) ) ), H ) ), True, False );

tr33 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc3 ) > L
            AND Ref( H, fc3 ) > Max( IIf( fc3 > 1, futureHHV( H, ( fc3 - 1 ) ), H ), IIf( fc3 < Sb, HHV( H, ( Sb - ( fc3 - 1 ) ) ), H ) ), True, False );

tr34 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc4 ) > L
            AND Ref( H, fc4 ) > Max( IIf( fc4 > 1, futureHHV( H, ( fc4 - 1 ) ), H ), IIf( fc4 < Sb, HHV( H, ( Sb - ( fc4 - 1 ) ) ), H ) ), True, False );

tr35 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc5 ) > L
            AND Ref( H, fc5 ) > Max( IIf( fc5 > 1, futureHHV( H, ( fc5 - 1 ) ), H ), IIf( fc5 < Sb, HHV( H, ( Sb - ( fc5 - 1 ) ) ), H ) ), True, False );

tr36 = IIf( Sb == 3 AND L == LLV( L, sb )
            AND futureLLV( L, fc6 ) > L
            AND Ref( H, fc6 ) > Max( IIf( fc6 > 1, futureHHV( H, ( fc6 - 1 ) ), H ), IIf( fc6 < Sb, HHV( H, ( Sb - ( fc6 - 1 ) ) ), H ) ), True, False );

//TROUGH: Swing bars = 4, Forward Count 1 to 6
tr41 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc1 ) > L
            AND Ref( H, fc1 ) > Max( IIf( fc1 > 1, futureHHV( H, ( fc1 - 1 ) ), H ), IIf( fc1 < Sb, HHV( H, ( Sb - ( fc1 - 1 ) ) ), H ) ), True, False );

tr42 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc2 ) > L
            AND Ref( H, fc2 ) > Max( IIf( fc2 > 1, futureHHV( H, ( fc2 - 1 ) ), H ), IIf( fc2 < Sb, HHV( H, ( Sb - ( fc2 - 1 ) ) ), H ) ), True, False );

tr43 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc3 ) > L
            AND Ref( H, fc3 ) > Max( IIf( fc3 > 1, futureHHV( H, ( fc3 - 1 ) ), H ), IIf( fc3 < Sb, HHV( H, ( Sb - ( fc3 - 1 ) ) ), H ) ), True, False );

tr44 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc4 ) > L
            AND Ref( H, fc4 ) > Max( IIf( fc4 > 1, futureHHV( H, ( fc4 - 1 ) ), H ), IIf( fc4 < Sb, HHV( H, ( Sb - ( fc4 - 1 ) ) ), H ) ), True, False );

tr45 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc5 ) > L
            AND Ref( H, fc5 ) > Max( IIf( fc5 > 1, futureHHV( H, ( fc5 - 1 ) ), H ), IIf( fc5 < Sb, HHV( H, ( Sb - ( fc5 - 1 ) ) ), H ) ), True, False );

tr46 = IIf( Sb == 4 AND L == LLV( L, sb )
            AND futureLLV( L, fc6 ) > L
            AND Ref( H, fc6 ) > Max( IIf( fc6 > 1, futureHHV( H, ( fc6 - 1 ) ), H ), IIf( fc6 < Sb, HHV( H, ( Sb - ( fc6 - 1 ) ) ), H ) ), True, False );

//TROUGH: Swing bars = 5, Forward Count 1 to 6
tr51 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc1 ) > L
            AND Ref( H, fc1 ) > Max( IIf( fc1 > 1, futureHHV( H, ( fc1 - 1 ) ), H ), IIf( fc1 < Sb, HHV( H, ( Sb - ( fc1 - 1 ) ) ), H ) ), True, False );

tr52 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc2 ) > L
            AND Ref( H, fc2 ) > Max( IIf( fc2 > 1, futureHHV( H, ( fc2 - 1 ) ), H ), IIf( fc2 < Sb, HHV( H, ( Sb - ( fc2 - 1 ) ) ), H ) ), True, False );

tr53 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc3 ) > L
            AND Ref( H, fc3 ) > Max( IIf( fc3 > 1, futureHHV( H, ( fc3 - 1 ) ), H ), IIf( fc3 < Sb, HHV( H, ( Sb - ( fc3 - 1 ) ) ), H ) ), True, False );

tr54 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc4 ) > L
            AND Ref( H, fc4 ) > Max( IIf( fc4 > 1, futureHHV( H, ( fc4 - 1 ) ), H ), IIf( fc4 < Sb, HHV( H, ( Sb - ( fc4 - 1 ) ) ), H ) ), True, False );

tr55 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc5 ) > L
            AND Ref( H, fc5 ) > Max( IIf( fc5 > 1, futureHHV( H, ( fc5 - 1 ) ), H ), IIf( fc5 < Sb, HHV( H, ( Sb - ( fc5 - 1 ) ) ), H ) ), True, False );

tr56 = IIf( Sb == 5 AND L == LLV( L, sb )
            AND futureLLV( L, fc6 ) > L
            AND Ref( H, fc6 ) > Max( IIf( fc6 > 1, futureHHV( H, ( fc6 - 1 ) ), H ), IIf( fc6 < Sb, HHV( H, ( Sb - ( fc6 - 1 ) ) ), H ) ), True, False );

//TROUGH: Swing bars = 6, Forward Count 1 to 6
tr61 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc1 ) > L
            AND Ref( H, fc1 ) > Max( IIf( fc1 > 1, futureHHV( H, ( fc1 - 1 ) ), H ), IIf( fc1 < Sb, HHV( H, ( Sb - ( fc1 - 1 ) ) ), H ) ), True, False );

tr62 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc2 ) > L
            AND Ref( H, fc2 ) > Max( IIf( fc2 > 1, futureHHV( H, ( fc2 - 1 ) ), H ), IIf( fc2 < Sb, HHV( H, ( Sb - ( fc2 - 1 ) ) ), H ) ), True, False );

tr63 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc3 ) > L
            AND Ref( H, fc3 ) > Max( IIf( fc3 > 1, futureHHV( H, ( fc3 - 1 ) ), H ), IIf( fc3 < Sb, HHV( H, ( Sb - ( fc3 - 1 ) ) ), H ) ), True, False );

tr64 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc4 ) > L
            AND Ref( H, fc4 ) > Max( IIf( fc4 > 1, futureHHV( H, ( fc4 - 1 ) ), H ), IIf( fc4 < Sb, HHV( H, ( Sb - ( fc4 - 1 ) ) ), H ) ), True, False );

tr65 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc5 ) > L
            AND Ref( H, fc5 ) > Max( IIf( fc5 > 1, futureHHV( H, ( fc5 - 1 ) ), H ), IIf( fc5 < Sb, HHV( H, ( Sb - ( fc5 - 1 ) ) ), H ) ), True, False );

tr66 = IIf( Sb == 6 AND L == LLV( L, sb )
            AND futureLLV( L, fc6 ) > L
            AND Ref( H, fc6 ) > Max( IIf( fc6 > 1, futureHHV( H, ( fc6 - 1 ) ), H ), IIf( fc6 < Sb, HHV( H, ( Sb - ( fc6 - 1 ) ) ), H ) ), True, False );

Pk2 = pk21 OR pk22 OR pk23 OR pk24 OR pk25 OR pk26; //Pk2 - The 2 denotes the number of Swing Bars
Pk3 = pk31 OR pk32 OR pk33 OR pk34 OR pk35 OR pk36; //Pk3 - The 3 denotes the number of Swing Bars
Pk4 = pk41 OR pk42 OR pk43 OR pk44 OR pk45 OR pk46;
Pk5 = pk51 OR pk52 OR pk53 OR pk54 OR pk55 OR pk56;
Pk6 = pk61 OR pk62 OR pk63 OR pk64 OR pk65 OR pk66;

Tr2 = tr21 OR tr22 OR tr23 OR tr24 OR tr25 OR tr26; //Tr2 - The 2 denotes the number of Swing Bars
Tr3 = tr31 OR tr32 OR tr33 OR tr34 OR tr35 OR tr36; //Tr3 - The 3 denotes the number of Swing Bars
Tr4 = tr41 OR tr42 OR tr43 OR tr44 OR tr45 OR tr46;
Tr5 = tr51 OR tr52 OR tr53 OR tr54 OR tr55 OR tr56;
Tr6 = tr61 OR tr62 OR tr63 OR tr64 OR tr65 OR tr66;

Pk = Pk2 OR Pk3 OR Pk4 OR Pk5 OR Pk6;
Tr = Tr2 OR Tr3 OR Tr4 OR Tr5 OR Tr6;

Buy = Tr;
Sell = Pk;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

BuyPrice = L;
SellPrice = H;

//Plot the Trend Lines
Sig = Buy OR Sell;
y0 = 0;
y1 = C[0];
FirstVisibleBar = Status( "FirstVisibleBar" );
LastVisibleBar = Status( "LastVisibleBar" );
CombinedColor = colorWhite;
CombinedLine = Null;


for( b = FirstVisibleBar; b <= LastVisibleBar AND b < BarCount; b++ )
{
    if( Buy[b] )
    {
        Co = ColorRGB( 60, 157, 255 );
        TPrice[b] = BuyPrice[b];
    }
    else
        if( Sell[b] )
        {
            Co = ColorRGB( 60, 157, 255 );
            TPrice[b] = SellPrice[b];
        }

    if( Sig[b] )
    {
        x0 = y0;

        x1 = y1;

        y0 = b;

        y1 = TPrice[b];

        La = LineArray( x0, x1, y0, y1 );

        CombinedLine = IIf( IsNull( La ), CombinedLine, La );
        CombinedColor = IIf( IsNull( La ), CombinedColor, Co );
    }
}

Plot( CombinedLine, "", CombinedColor, styleLine, Null, Null, 0, 0, 2 );

GraphXSpace = 30;

It seems to me that this part of the code should be done using a loop but I have been unable to set the loop up correctly. The loop is shown below. I would be very grateful for any assistance with this.

//Determine the Swing High and Swing Low locations using loop
for( i = 0, j = 1; i < BarCount && j <= 20; i++, j++)
{
	if( Tr[i])
	{
		Tr = 0;
		fc = 0;
		Tr = IIf( Sb == Sb AND L[i] == LLV( L[i], Sb) AND futureLLV( L[i], fc[j]) > L[i] AND Ref( H[i], fc[j]) >
			Max( IIf( fc[j] > 1, futureHHV( H[i], ( fc[j] - 1)), H[i]),
			IIf( fc[j] < Sb, HHV( H[i], ( Sb - ( fc[j] - 1))), H[i])), True, False);
	}
	if( Pk[i])
	{
		Pk = 0;
		fc = 0;
		Pk = IIf( Sb == Sb AND H[i] == HHV( H[i], Sb) AND futureHHV( H[i], fc[j]) < H[i] AND Ref( L[i], fc[j]) <
			Min( IIf( fc[j] > 1, futureLLV( L[i], ( fc[j] - 1)), L[i]),
			IIf( fc[j] < Sb, LLV( L[i], ( Sb - ( fc[j] - 1))), L[i])), True, False);
	}
}

@jordie, welcome to this community.

For the second part of the code you probably should do it in this way:


for( i = 0; i < BarCount; i++)
{
	for( j = 1; j <= 20; j++)
	{
		if...
	}
}

but in that the loop you are doing something strange!
You reassign the Tr and Pk entire arrays in the loop, you compare Sb == Sb ( that is not changed/reassigned in the loop - so it is always True), and assign a zero value to fc (used in the calculation) but that again this variable is not changed/reassigned in the loop.

What should the fc variable contain?

Maybe your intention was to change individual values of pk and tr in the loop.
In such a case it should be (and similarly for the pk branch - assuming that this intricate piece of code is correct):

  // fc = 0; // ???? What is this variable supposed to hold?
  Tr[i] = IIf( Sb == Sb AND L[i] == LLV( L[i], Sb) AND futureLLV( L[i], fc[j]) > L[i] AND Ref( H[i], fc[j]) >
			Max( IIf( fc[j] > 1, futureHHV( H[i], ( fc[j] - 1)), H[i]),
			IIf( fc[j] < Sb, HHV( H[i], ( Sb - ( fc[j] - 1))), H[i])), True, False);

By the way, it seems that the first half could be simplified using some dynamic variables, but this is less important (see this example)

Thank you Beppe for replying, really appreciate it.

I understand the way you have separated the for constucts.

Sb does stay the same inside the loop, so yes, it doesn't need to be in the loop as it has a parameter setting out side the loop.

What I am wanting to happen inside the loop is that at each bar the high is checked as to whether it is a swing high by calculating the formula, first with fc = 1, then fc = 2, etc. up to fc = 20. On the first instance where the formula calculation outcome is true, this confirms that the bar is a swing high (Pk). If the outcome is false after calculating through fc = 1 to fc = 20 the loop then moves onto the next bar.

Hi @Jordie,
With code that has obvious future leaks, and therefore very limited trading usefulness, I regard as purely as programming exercises. In addition to feedback you already have, here is my approach.

I have not addressed the leaks (or the 'unusual' peak/trough algorithm), just the requested loop. I haven't done extensive verification.

fcmax = 6;
pk = tr = 0;
for( fc = 1; fc <= fcmax; fc++ )
{
    pkc = IIf( H == HHV( H, Sb )
               AND futureHHV( H, fc ) < H
               AND Ref( L, fc ) < Min( IIf( fc > 1, futureLLV( L, ( fc - 1 ) ), L ), IIf( fc < Sb, LLV( L, ( Sb - ( fc - 1 ) ) ), L ) ), True, False );
    
    trc = IIf( L == LLV( L, sb )
               AND futureLLV( L, fc ) > L
               AND Ref( H, fc ) > Max( IIf( fc > 1, futureHHV( H, ( fc - 1 ) ), H ), IIf( fc < Sb, HHV( H, ( Sb - ( fc - 1 ) ) ), H ) ), True, False );

    pk = pk OR pkc;
    tr = tr OR trc;
}

Hi @JohnHT,
Thank you. That seems to do what my long winded method does. I was clearly very wide of the mark and now have to study closely as to why. Thank you again.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.