How to find empty (not yet) existing Tickers within Watchlist

Hello dear all.

I am having a hard time understanding my issue. within a EOD Backtest i am trying to find the percentage of stocks that Closed Green for the current day within an Watchlist. For this i use the following code

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );
NoM = StrCount( List, "," );
       
    Up = 0;
    Total = 0;
    
    for ( i = 0; ( symbol = StrExtract( List, i ) )  != "";  i++ )
    {
        SetForeign ( symbol );

        Up = IIf( Close / Open > 1, Up ++, Up );
        RestorePriceArrays();      
        
    }

PercentWatchlistUp = round ( 100 * ( Up / NoM ) );

Plot( PercentWatchlistUp, "UP", ColorRed, StyleLine );

It does give me the wanted results, but starts plotting the Value's to late. Within my Watchlist (S&P 500 stocks) it starts giving me usefull
Data in June this Year (see Image Below).

I Presume this is because that day will be the very first day that Data is available for
ALL stocks together at the same time ?
And most likely this part of the code is checking if the Ticker is not empty and makes sure it does not set Foreign to a empty Ticker ?

( symbol = StrExtract( List, i ) )  **!= ""**

What i would like is to include the (not yet) existing stocks in the Watchlist and keep track of the amount "Useable" stocks and ignore the empty ones (Array Total reserved for this in script) and include them in the calculation.

For example if Stock X and Y where the only ones available in the year 1985, i would like to
have that number (2 for 2 stocks) even the next Stocks data would start 3 Years later.

Is this doable, if so could someone be so kind and push me in the right direction ?

AA

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

Up = 0;
Total = 0;
NoM = 0;
for( i = 0; ( symbol = StrExtract( List, i ) )  != "";  i++ )
{
    SetForeign( symbol, 0 );
		Up += Nz(Close / Open > 1);
		NoM += NOT IsNull(C);
    RestorePriceArrays();
}

PercentWatchlistUp = round ( 100 * ( Up / NoM  ) );
Plot( PercentWatchlistUp, "UP", ColorRed, styleHistogram );

BUT, instead of using SetForeign within loop you should rather use StaticVarAdd() function via scan!
(Since you want to ignore data holes you should disable pad&align in that case)

2 Likes

Fantastic. This will do and give so much opportunities. One would be able to calculate within the Backtest Percentual how many Stocks would face the same Criterium and use this information as an Filter. A big thank you. At times i wonder if there is anything Amibroker can not do in the right hands :relaxed:

1 Like

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