Assigning values to an Array within a loop

Assiging values to an array within a loop. When it works and when it doesn’t…
I found the following code on Traders.com under June 2013 Traders’ Tips code. The code creates and plots a ZigZag indicator. To understand how the code works, I added a section at the bottom, beginning with “filter = 1;”, so that I could look at the values of the various variables using Explore. When I look at the values for LINE, which is the zigzag line that is plotted, all makes sense. When I look at the input values to LINE, which are Llb, Ll, Lhb and Hh, it no longer makes sense to me. The array LINE seems to have retained the values assigned during the looping in the code above. The arrays Llb, Ll, Lhb and Hh did not. Why did LINE retain the values assigned during looping but the other variables did not retain the values assigned during looping? It looks to me like the other variables (Llb, Ll, Lhb and Hh) only retained what was assigned at or near the end of the loop and then that value was the value for the entire array. Any explanations would be greatly appreciated. Thank you.

Code:
ZZPercent = Param("ZZPercent", 5 );
ATRPeriod = Param("ATRPeriod", 5 );
ATRFactor = Param("ATRFactor", 1.5, 0, 5 );

HLPivot = ZZPercent * 0.01 + ATRFactor * ATR( ATRPeriod )/Close;

Ll = Low[ 0 ];
Hh = High[ 0 ];
Llb = Lhb = 0;

if( High[ 1 ] >= Hh )
{
Hh = High[ 1 ];
Lhb = trend = 1;
}
else
{
Ll = Low[ 1 ];
Llb = 1;
trend = -1;
}

Line = Null;

for( i = 2; i < BarCount; i++ )
{
if( trend > 0 )
{
if( High[ i ] >= Hh )
{
Hh = High[ i ];
Lhb = i;
Curline = LineArray( Llb, Ll, Lhb, Hh );
Line = IIf( IsNull( CurLine ), Line, CurLine );
}
else
if( Low[ i ] < Hh - Hh * HLPivot[ i ] )
{
Ll = Low[ i ];
Llb = i;
trend = -1;
CurLine = LineArray( Lhb, Hh, Llb, Ll );
Line = IIf( IsNull( CurLine ), Line, CurLine );
}
}
else
{
if( Low[ i ] <= Ll )
{
Ll = Low[ i ];
Llb = i;
CurLine = LineArray( Lhb, Hh, Llb, Ll );
Line = IIf( IsNull( CurLine ), Line, CurLine );
}
else
if( High[ i ] > Ll + Ll * HLPivot[ i ] )
{
Hh = High[ i ];
lhb = i;
trend = 1;
CurLine = LineArray( Llb, Ll, Lhb, Hh );
Line = IIf( IsNull( CurLine ), Line, CurLine );
}
}
}

Plot( Line, "", colorBlueGrey, styleThick );
Plot( Close, Date()+ " Close", colorDefault, styleCandle );
Filter=1;
AddColumn(High,"High");
AddColumn(Low,"Low");
AddColumn(trend,"Trend");
AddColumn(Ll,"LL");
AddColumn(Hh,"HH");
AddColumn(Llb,"LLB");
AddColumn(lhb,"LHB");

AddColumn(CurLine,"CURLINE");
AddColumn(Line,"LINE");

Explorer results:

  1. List item

Look again. The variables Ll, Llb, Lhb, and Hh are not arrays, they are scalars.

That explains it. Thanks!

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Thank you and will do.

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