Problems coding counters based on conditions

Hi to everybody!
I am having an easy problem but I can't find where is the mistake.
I have coded an indicator which show us New lows of the NYSE (I have used esignal as a data source). The esignal ticker is $YRLO.NY-ST.
Everything works properly (plotting New Lows Histogram), but I don't get the expected value in the Title (counting the days with new lows above the limit in a row). This value came from the loop.
I know it is very simple doubt, but I can't see where I was wrong.
I'll show you the code so you can take a look at it.

// New Low 
// Coding by Santiago Vázquez marzo 2015
// Basado en las indicaciones de Oscar Cagigas

SetChartOptions(0,chartShowDates|chartWrapTitle);

_SECTION_BEGIN("New lows");

SetForeign("$YRLO.NY-ST");
Limit = Param("Limit", 40, 30, 60, 10);
days = Param("Days above alert", 5, 3, 10, 1);
alert = Sum(C > Limit, days) >= days;
color = IIf(alert, colorRed, colorLightGrey);

cont = 0;
for(i = 1; i< BarCount; i++)
{
if (C[i] > Limit)
	cont++;
else
	cont = 0;	
}

Plot(C, "Close", color, styleHistogram, Null, Null, Null, Null, 5);
Plot(Limit, "Limit", colorRed, styleLine+styleThick);

Title = "New Lows = " + C 
	    + EncodeColor(colorRed) + " Alert = " + EncodeColor(colorDefault) + Limit
	    + EncodeColor(colorRed) + " New lows above limit counter = " + EncodeColor(colorDefault) + cont; 	

_SECTION_END();

If anyone could give me a clue

1 Like

@svaztej, probably, all you need is only:

totCounter = Cum( C > limit );
// or
totCounterAlert = Cum( alert ); 

and a corresponding SelectedValue() or LastValue() in the title, but, to make it more generic, I decided to expand your code sample.

I added an exploration and revised a bit the first part to separate the different condition/rules used both for the plot and the exploration.
(I also change the symbol to SPY since I do not have your feed; change it back as needed).

I removed the loop, but since I'm not sure that you truly wanted to count ALL the bars that have a Close price above the limit (as the loop was supposed to do) I added, in the title, some different counters (see the exploration to better understand how they are calculated)

I changed also the plot to show (in Yellow) when the C > Limit condition is True but the Alert is not yet triggered (based on the nDays value).

// New Low 
// Coding by Santiago Vázquez marzo 2015
// Basado en las indicaciones de Oscar Cagigas

Version (6.28); // Tested version - Min version 6.10 required to use SumSince
SetBarsRequired( sbrAll, sbrAll );

SetChartOptions( 0, chartShowDates | chartWrapTitle );

_SECTION_BEGIN( "New lows" );

// SetForeign("$YRLO.NY-ST");
// Limit = Param("Limit", 40, 30, 60, 10);
// nDays = Param("Days above limit", 5, 3, 10, 1);

SetForeign( "SPY" );
limit = Param( "Limit", 250, 50, 300, 5 );
nDays = Param( "Days above limit", 3, 1, 100, 1 );

condition = C > limit;
condCounter = Sum( condition, nDays );
alert = condCounter >= nDays;
countFromHere = condCounter == (nDays - 1);
counter = SumSince( countFromHere, alert );

totCounter = Cum( condition );
totCounterAlert = Cum( alert );

color = IIf( alert, colorRed, IIf( condition, colorYellow, colorLightGrey ) );
Plot( C, "Close", color, styleHistogram, Null, Null, Null, Null, 5 );
Plot( limit, "Limit", colorLightGrey, styleDashed | styleThick, 0, 0, 0, 1 );

Title = "New Lows: " + C 
	    +  " - Limit: " + EncodeColor(colorLightGrey) + limit
	    + EncodeColor(colorRed) + " - Current range Alert counter: " + EncodeColor(colorDefault) + SelectedValue(counter)  
	    + EncodeColor(colorYellow) + WriteIf(Alert AND (nDays > 1), " (+ " + (nDays-1) + ") ", "")  
	    + EncodeColor(colorRed) + " - Cumulative Alerts counter: " + EncodeColor(colorDefault) + SelectedValue(totCounterAlert)
	    + EncodeColor(colorOrange) + " - Cumulative Above Limit counter: " + EncodeColor(colorDefault) + SelectedValue(totCounter) 
	    + EncodeColor(colorRed) + " - Total Alerts counter: " + EncodeColor(colorDefault) + LastValue(totCounterAlert)
	    + EncodeColor(colorOrange) + " - Total Above Limit counter: " + EncodeColor(colorDefault) + LastValue(totcounter);

_SECTION_END();

Filter = 1;
AddColumn( Close, "Close", 1.2 );
AddColumn( limit, "Limit", 1.2 );
AddColumn( IIf( condition, condition, Null ), "C >" + Limit, 1 );
AddColumn( condCounter, "Cond. counter", 1 );
AddColumn( IIf( alert, alert, Null ), "Alert (C>Limit) since " + nDays + " days", 1 );
AddColumn( IIf(countFromHere, countFromHere, Null), "Count from here", 1 );
AddColumn( counter, "Range Counter", 1 );
AddColumn( totCounterAlert, "Tot. Alert Counter", 1 );
AddColumn( totCounter, "Tot. Counter", 1 );
SetSortColumns( -2 );

If you use the exploration, remember to change its "Parameters" from the Analysis toolbar (click on the icon with a small mixer).

6 Likes

Beppe, thank you very much for your dedication to solve the problem. It is true I didn't need everything you gave me, so I really apreciate this. I will use your code...
The most important thing it is I can find out where I made the mistake...
Thank you very much again!!

1 Like

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