One summary total vs. detail list

I rarely need/use any AFL coding skills (obvious here). I wanted a simple explore to make a count of how many of each of 3 conditions (close > ma1, ma2 ma3) in a watch list.

My code below delivers each symbol in watch list with results of 3 conditions but I simply want just a one line total of how many of each condition. The concept here is "when a very high % of stocks in an index are trading above sma (x), history shows a likely pullback. thanks in advance...

// explore count # of stocks above certain MA levels... theory is look for extremes when % very high or low
maShort = 10;
maMedium = 15;
maLong = 20;

_SECTION_BEGIN("Count");
cond1 = Close > MA(Close,maShort);
cond2 = Close > MA(Close,maMedium);
cond3 = Close > MA(Close,maLong);
cntr1 = sum(cond1,1);
cntr2 = sum(cond2,1);
cntr3 = sum(cond3,1);
Filter = cond1 OR cond2 OR cond3;
_SECTION_END();

numcolumns = 3;
column0name = "10ma";
column1name = "15ma";
column2name = "20ma";
column0 = cntr1;
column1 = cntr2;
column2 = cntr3;

1 Like

@BD_TX similar topic in the past,

Review and experiment with StaticVarAdd
https://www.amibroker.com/guide/afl/staticvaradd.html

I've taken a quick stab at modifying that but you will need to double check its accuracy (it's late and my focus is elsewhere)

// Using StaticVarAdd and run EXPLORE
// pick watchlist in AA window

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

maShort = 10;
maMedium = 15;
maLong = 20;

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs
    StaticVarRemove( "~SymbolCount*" );
    StaticVarRemove( "~above*" );
	StaticVarRemove("~cond*"); // should take care of all 3 "cond"
	StaticVarRemove( "~AboveALL*" );

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );
		cond1 = Close > MA(Close, maShort);
		cond2 = Close > MA(Close, maMedium);
		cond3 = Close > MA(Close, maLong);
        AboveALL = cond1 AND cond2  AND cond3 ;
        StaticVarAdd( "~SymbolCount", 1 );
        StaticVarAdd( "~cond1", cond1 );
		StaticVarAdd( "~cond2", cond2 );
        StaticVarAdd( "~cond3", cond3 );
        StaticVarAdd( "~AboveALL", AboveALL );

        RestorePriceArrays();
    }
}

svCOND1 = StaticVarGet( "~cond1" );
svCOND2 = StaticVarGet( "~cond2" );
svCOND3 = StaticVarGet( "~cond3" );
svABOVEALL = StaticVarGet( "~AboveALL" );

svSymbolCount = StaticVarGet( "~SymbolCount" );

PercentAbove = ( svABOVEALL / svSymbolCount ) * 100; // Percent Above all 3 moving averages

///////////////
// Explore
///////////////

Filter = Status( "stocknum" ) == 0 ;
SetOption( "NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );
AddColumn( svSymbolCount, "Total Number of Stocks", 1.0 );
AddColumn( svCOND1, "Number > ma(" + maShort + ")", 1.0 );
AddColumn( svCOND2, "Number > ma(" + maMedium + ")", 1.0 );
AddColumn( svCOND3, "Number > ma(" + maLong + ")", 1.0 );
AddColumn( PercentAbove, "Percentage > all 3 m.a.", 1.2 );

///////////////
// Chart
///////////////
Plot( PercentAbove, "Percentage > all 3 m.a.", colorRed, styleThick );

Produces an output like this,

image

Or a chart like this,

image

4 Likes

Thank you very much for the quick and helpful response. This is exactly what I was hoping to achieve, It seems the 'Total # of Stocks" counter has a problem. I ran it against the Nasdaq 100 watch list and it shows 248 total stocks... the MA totals look OK..

I'll try to figure out the code, perhaps I'll learn something!

Thanks again.
Bill D

@BD_TX look at your WL, make sure you don't have 248 symbols in the WL.

My current Nasdaq 100 WL has 103 symbols and the code works on it like this,
image

And on the known DJIA (30 stocks)
image

Also, if the filter has multiple WL or markets, the count would run on sum total of all the unique symbols filtered.

@BD_TX Click the filter icon and see how many symbols are included in the total.

image

Thanks all for the help. What I did was close Amibroker and re-opened it and the code worked perfectly. Prior to that, I had copy and pasted the new code into an existing file, and updated via the "Apply" button. Then running the explore, gave me the inaccurate results.... all is good now

Thanks again.