AddToComposite results

howto
Tags: #<Tag:0x00007f2d7b742818>

#1

I am trying to develop an indicator (refer below) showing the number of stocks above 20 week moving average on a specific watchlist of 90 stocks. It works fine except for the fact that when I manually count the stocks above 20 week moving average, it shows some discrepancy. I have been trying to figure this out for a number of days and would appreciate if someone can help. Thanks.

MA20 = MA(C,20);
Above20 = Close > MA20;

Buy = 0; 
Filter = 0;

AddToComposite( Above20 , "~Above20", "X", 7);
 
Above20ma = Foreign ("~Above20", "C", True);
Plot(Above20ma,"Above 20",colorBrightGreen,styleLine,0,0,0,0,2);

#2

@applet There are several very informative threads here on the forum about building composite indicators. You should read through some like this one,

If you want a good review of the AddToComposite function you will find this classic document,

If you want to try the StaticVarAdd method here is a start (have not really tested it so go over it carefully),

// pick watchlist in AA window
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

maPeriods = Param( "maPeriods", 20, 5, 35, 1 );


if( Status( "stocknum" ) == 0 )
{
    StaticVarRemove( "~AboveAverage*" );
    StaticVarRemove( "~SymbolCount*" );

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( Symbol );
        
        AboveAverage = Close > MA( C, maPeriods );
        StaticVarAdd( "~AboveAverage", AboveAverage );
        StaticVarAdd( "~SymbolCount", 1 );

        RestorePriceArrays();
    }
}

svAboveAverage = StaticVarGet( "~AboveAverage" );
svSymbolCount = StaticVarGet( "~SymbolCount" );
PercentAbove = ( svAboveAverage / svSymbolCount ) * 100;

///////////////
// Explore
///////////////
Filter = Status( "stocknum" ) == 0;
SetOption("NoDefaultColumns",True);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn( svAboveAverage , "Stocks with C > MA(" + maPeriods + ") ", 1.0 );
AddColumn( svSymbolCount, "Total Number of Stocks", 1.0 );
AddColumn( PercentAbove, "PercentAbove", 1.1, colorWhite, colorDarkGreen );

///////////////
// Chart
///////////////
Plot( PercentAbove, "PercentAbove", colorRed, styleThick );


#3

@portfoliobuilder Thanks a mill for this. I spent a good time with the code understanding it and reading about StaticVarAdd functions. This works perfectly fine and far exceeds my initial requirements. Much appreciated.