Hi,
I'm trying to compute and display retracement levels after a peak has formed.  The code I have developed works fine only once after I apply the indicator. Clicking anywhere on the graph causes the computed levels to disappear!  Obviously, I am doing something wrong, only can't figure 
out what.  Would be grateful for any helpful hints.
Attached is a screenshot showing how the graph looks immediately after the first application of the indicator.
Cheers,
Lal
 
_SECTION_BEGIN("Retracements");
Level = Param("Pivot Level", 9, 2, 100, 1);
Plot(C, "Retracements", colorBlack, styleBar);
My_Peak[0] = 0;
My_Trough[0] = 0;
for( i = Level+1; i <
 BarCount-Level; i++ ) 
  { 
	// There shouldn't be a higher high for the next few bars
	R_Higher_high[0] = 0;		// Higher High found on the right of current bar
	L_Higher_high[0] = 0;		// Higher High found on the left of current bar
	R_Lower_Low[0] = 0;		// Lower Low found on the right of current bar
	L_Lower_Low[0] = 0;		// Lower Low found on the left of current bar
	//    | | | | C | | | |
	//	               ---->	
	for (j = i; j <= i+Level; j++)	
		{
		if(High[j] > High[i])	
			{
			R_Higher_high[0] = R_Higher_high[0] + 1;
		}
		if(Low[j] < Low[i])	
			{
			R_Lower_Low[0] = R_Lower_Low[0] + 1;
		}
	}
	// There should be no  higher high for previous few bars
	//    | | | | C | | | |
	//	         <----	
	for(k = i;  k >= (i-Level);  k--)	
	{
		if(High[k] > High[i])
		{
			L_Higher_high[0] = L_higher_high[0] + 1;
		}
		if(Low[k] < Low[i])
	
	{
			L_Lower_Low[0] = L_Lower_Low[0] + 1;
		}
	}
	// If we've detected a higher high on either 
	// side of current bar,
	// we do NOT have a valid peak
	if(NOT L_Higher_High[0] AND NOT r_Higher_High[0])
	{
		My_peak[i] = 1;	// Mark current bar as a valid pivot/peak
		PlotText("P", i, H[i] + 20, colorRed);
	}
	// If we've detected a lower low on either 
	// side of current bar,
	// we do NOT have a valid trough
	if(NOT L_Lower_Low[0] AND NOT R_Lower_Low[0])
	{
		My_Trough[i] = 1;	// Mark current bar as a valid pivot/peak
		PlotText("T", i, L[i] - 30, colorBlue);
	}
	L_higher_high[0] = 0; R_Higher_High[0] = 0;
	L_Lower_Low[0] = 0;  R_Lower_Low[0] = 0;
} 
// Find Co-ordinates to draw lines 
// Peak to Peak AND Trough to Trough
Y0 = ValueWhen(My_Peak, H, 2);
X0 = ValueWhen(My_Peak, BarIndex(), 2);
Y1 =  ValueWhen(My_Peak, H, 1);
X1 = ValueWhen(My_Peak, BarIndex(),
 1);
NullY0 = IsNull(y0);
NullY1 = IsNull(y1);
TY0 = ValueWhen(My_Trough, L, 2);
TX0 = ValueWhen(My_Trough, BarIndex(), 2);
TY1 =  ValueWhen(My_Trough, L, 1);
TX1 = ValueWhen(My_Trough, BarIndex(), 1);
NullTY0 = IsNull(Ty0);
NullTY1 = IsNull(Ty1);
// Work out retracement levels after a peak
P1 		=  ValueWhen(My_Peak, H, 1);
P1_B 	= ValueWhen(My_Peak, BarIndex(), 1);
T1 		= ValueWhen(My_Trough, L, 2);
T1_B 	= ValueWhen(My_Trough, BarIndex(), 2);
T2 		=  ValueWhen(My_Trough, L, 1);
T2_B 	= ValueWhen(My_Trough, BarIndex(), 1);
Cond1 = P1_B > T1_B AND P1_B < T2_B;
T1P1_Swing = P1 - T1;
P1T2_Swing = P1 - T2;
Swing_Extent = P1T2_Swing / T1P1_Swing;
PlotNow 	= ValueWhen(Cond1, BarIndex());
Ext			= ValueWhen(Cond1, Swing_Extent, 1);
// Test End
for( i = BarCount-1; i > 10; i--)
{
	if ( !Nully0[i])
	{
		// Line to conncect peaks
		Line1 =
 LineArray( x0[i], y0[i], x1[i], y1[i], 0, True);
		Plot(Line1, "", colorWhite, styleDashed+styleNoRescale+styleNoLabel);
	}	
	if ( !NullTy0[i])
	{
		// Line to connect troughs
		Line2 = LineArray( tx0[i], ty0[i], tx1[i], ty1[i], 0, True);
		Plot(Line2, "", colorWhite, styleLine+styleNoRescale+styleNoLabel);
	}	
	if (PlotNow[i])
	{
		// Show retracement extent after a peak
		PlotText("" + WriteVal(Ext[i], 1.2), T2_B[i], T2[i]-60, colorBlack);
	}
}
_SECTION_END();
 
		 
What kind of emailer are you? Find out today - get a free analysis of your email personality. Take the quiz at the 
.