Adding symbols to watchlist in specific order

Dear , @beppe I did some trials and It seems that the data holes greatly affect the ranking process, indicated by applying the rank to carefully selected symbols (main market indexes) whom I believe the most complete data I have and the result was perfect even with moderate complex ranking formula but upon complicating the formula more by using some higher order factors (exponential) it get some troubles again may be the approximations play some rule here.

1 Like

Thank you @beppe for sharing the above code,very kind of you as always.

About the aforesaid code:

  1. We have 300 symbols in the Source WatchList.
  2. And to get only Top 10 Ranked Symbols in the Destination WatchList for the second Exploration, slightly modified one line in the following part as follows:
    for( i = 0; i < symCount-290; i++ )
// perform top ranking
            StaticVarGenerateRanks( "top", "ValuesToSort", symCount, 1234 );
            sdt = SelectedValue( DateTime() );  // in AA this is the Last date in range
            // this next function retirns the sorted symbols!
            listTop = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );

            for( i = 0; i < symCount-290; i++ )
            {
                sym = StrExtract( listTop, i );
                CategoryAddSymbol( sym, cat, nDestWatchList );
            }
  1. This fills the Destination WatchList with only the Top 10 Ranked Symbols (Desired Result).
  2. Is this the Right way to do it ? , is there anyway we can do this without the prior knowledge of the total number of Symbols in the Source WatchList ?

Thank you once again @Beppe for your time n patience.

@NowToLook, it is simpler!

Before the main part of the code, define a new variable that will limit the size of the destination watchlist items:

topN = 5; // max number of symbols to include in the destination watchlist - Parametrize it if you like

Then modify the code as follows;

			// perform top ranking
			StaticVarGenerateRanks( "top", "ValuesToSort", topN, 1234 );  // modified line
			sdt = SelectedValue( DateTime() );  // in AA this is the Last date in range
			// this next function returns the sorted symbols!
			listTop = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );
			symCount = StrCount( listTop, "," ) + 1; // line added - get the top list symbols count

			for( i = 0; i < symCount; i++ )
			{
				sym = StrExtract( listTop, i );
				CategoryAddSymbol( sym, cat, nDestWatchList );
			}


By the way, this is the "normal" way to use the "top-N" mode ranking.

My example was using all the symbols since the request was to put them all in the watchlist, but the real advantage of the "top-N" method is to limit the resulting list size to include only the top ranked values!

And if your topN value is greater than the original watchlist symbol count no problem: it will work anyway!

3 Likes
  1. Yes its working, thank you @Beppe .

  2. Completely misread the third argument there in the Function Reference :smile:, thought it was 0
    for normal Mode(Top) and -1 for Bottom Mode, nothing more.

  3. Is it possible to switch to the New Destination watchList for a new/next exploration execution in the same afl without manuaally changing the Applyto filter ?

thank you

@NowToLook re point 3 I think it will NOT work (when you execute the ranking process you are already in the exploration process using the original watchlist).

In my opinion, the proper way to use this stuff is to use a 2-step process: execute a first formula, based on this kind of code, do the ranking (optionally a multiple level ranking as I explained before), save the resulting "ranked" watchlist and then use a second formula to do your standard exploration utilizing the destination watchlist.

The entire process can easily be automated via a batch (.abb) saving the intermediate steps/settings as .apx files.

1 Like

Yes, halfway there ,thanks , God Bless

I was thinking that the code to clear a watchlist and fill it with the exploration results seems a good candidate for a new kind of "batch" operation to add to future versions of AmiBroker.

In practice, I wonder if it could be useful to post in the Feedback Center the request to add to the Batch Windows operations these two items available via a popup menu on the exploration analysis results:

immagine

Does not seems a lot of work for @Tomasz, but only he can tell us!

3 Likes

Hope @Tomasz will consider, fingers crossed.
Thanks @Beppe, have a blasting weekend , take care.

As usual, nice idea @beppe this could be a fundamental solution for the problem even with holes containing data.

@NowToLook , @mohamed.gad, you can also take a look at the (very slightly) modified version of my code from the thread which @Beppe has mentioned in his first reply: Link It is very similar to the above one, but there are some small differences, which potentially might be useful to you:

  1. My code imports the name of the watchlist (selected by the user) in AA Filter settings window and in case of the output watchlist, user provides it's name (not number). I usually use watchlist names, because watchlist numbers might change in some situations.

  2. I added some Filter rules. Only the issues meeting the Filter criteria take part in ranking process, which should make the code execution faster if the criteria are strict.

  3. There are some slight differences in the mechanism of adding and removing issues to/from a watchlist.

Period = 5;
TopX = 10;
MinimumPrice = 20;
OutputWatchlistName = "Test 2"; // Name of the existing output watchlist

if( Status( "stocknum" ) == 0 )
{
    StaticVarRemove( "ValuesToSort*" );

    InputWatchlistNum = GetOption( "FilterIncludeWatchlist" ); // Import watchlist from AA Filter settings
    InputSymList = CategoryGetSymbols( categoryWatchlist, InputWatchlistNum );
   
    OutputWatchlistNum = CategoryFind( OutputWatchlistName, categoryWatchlist );
    OutputSymList = CategoryGetSymbols( categoryWatchlist, OutputWatchlistNum );

    // Erasing the output watchlist
    if( OutputSymList != "" ) for( i = 0; ( symbol = StrExtract( OutputSymList, i ) ) != ""; i++ ) CategoryRemoveSymbol( symbol, categoryWatchlist, OutputWatchlistNum );
   
    for( i = 0; ( symbol = StrExtract( InputSymList, i ) ) != ""; i++ )  // Sorting
    {
        SetForeign( symbol );

        if( LastValue( C ) > MinimumPrice ) // Example Filter
        {
            ValuesToSort = ROC( C, Period ); // Example values to sort
            RestorePriceArrays();
            StaticVarSet( "ValuesToSort_" + symbol, ValuesToSort );
        }

        else RestorePriceArrays();
    }

    StaticVarGenerateRanks( "top", "ValuesToSort_", TopX, 1234 );
    SymList = StaticVarGetRankedSymbols( "top", "ValuesToSort_", LastValue( DateTime() ) );

    // Adding sorted symbols to the output watchlist
    for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ ) CategoryAddSymbol( symbol, categoryWatchlist, OutputWatchlistNum );
}

ValuesToSort = ROC( C, Period ); // // Example values to sort
Filter = LastValue( C )  > MinimumPrice; // Example Filter

AddColumn( ValuesToSort, "ROC " + Period, 1.3, IIf( ValuesToSort > 0, colorGreen, IIf( ValuesToSort < 0, colorRed, colorBlue ) ), colorDefault, 80 );
SetSortColumns( -3 );

It should be used with settings: Apply to: Filter, Range: 1 recent bar

obraz

By the way, if someone doesn't need to view Exploration's output, such solutions, after some modifications (especially implementing conditional execution of the ranking part), can be used in Indicator/chart window instead of AA.


@beppe I agree with you, that adding/replacing watchlists with Exploration's results would be a useful feature in Batch operations. I suggest you report it to the Feedback centre.

8 Likes
  1. Thank you @Milosz, its a previledge for us lesser fortuneds (in the grey matter department) to have multiple choices from the greats like you, @beppe , @fxshrat , @Tomasz , and so many others with so much rich contents.

  2. Ofcourse will grab your aforesaid code too to see which fits best my immiediate needs.

  3. Currently trying (crazy, i know) bottom Ranking with CATEGORYMARKET (instead of CATEGORYWATCHLIST) which will move (not just add) the bottom ranked symbols to New Destination and retain in the SourceList only the top 10 Ranked Symbols on which i intend to run new/next exploration.

  4. Yes an added filter like your code example is definitely welcome.

  5. And , while we are on the subject of filter, this is completely off topic, --> long time ago, somewhere @Tomasz had written in an article/post about not to use (in a Trading System) an additional indicator/filter which uses the same basic computation as in the already used indicator function, and went on to list different indicator functions which use same/diffrent base computation.
    I have been searching for that article/post since morning now , no luck , if anyone provide the link, will be greatful.
    Thank you.God Bless.

1 Like

@NowToLook I recall these Tomasz posts related to custom versus in-built indicators, but I'm not sure if you are looking for these:

3 Likes

No ,not them.But while searching , those repeatedly came up anyway , but what im looking for is a categorised list of indicator functions which use same basic calculation/inputs as in others in the same category , so that i shall avoid using multiple indicators of same category in a Trading System.
Thank you @ Milosz, take care

@NowToLook are you sure it was a post made by Tomasz on this forum? Maybe it was someone else's or on the old Yahoo group? I can't recall a similar post here, but I might be wrong. It's too bad - my mind doesn't resemble a sorted array on which a binary search can be applied :wink:

1 Like

No no not necessarily on this forum , most probably on KB series / yahoo Group.
Hey dont stress that precious mind of yours, we need it for more fruitful jobs :smile:

1 Like

Thank you @Milosz for your code, Keep good working :relaxed:

1 Like

Hallo,
thank you for your code. I try it and everything works well. But there is one little bug. I see top 10 result in watchlist but one symbol is extra. The extra symbol is 1st from analysed group sorted by alphabet. For example:

Top10: IBM,INTC,SIRI,..etc , and last is AAPL. (as 11th. symbol in result.) Can you help me please…
My code is below.

Jan

nSrcWatchList = -1;
// optionally hardcode a watchlist number
// nSrcWatchList = 8; // TO_CHANGE / PARAMETRIZE

nDestWatchList = 2; //  TO_CHANGE / PARAMETRIZE

if( Status( "Action" ) == actionExplore )
{
    cat = categoryWatchlist;

    // if not hard-coded choose a single watchlist in the Analysis filter settings
    if( nSrcWatchList == -1 )
    {
        if( GetOption( "ApplyTo" ) == 2 ) // filter
        {
            nSrcWatchList = GetOption( "FilterIncludeWatchlist" );
        }
    }

    if( nSrcWatchList >= 0 )
    {
        if( Status( "Stocknum" ) == 0 )
        {
            // empty the dest watchlist
            destSymList = CategoryGetSymbols( cat, nDestWatchList );
            destSymCount = StrCount( destSymList, "," ) + 1;

            for( i = destSymCount - 1; i >= 0; i-- )
            {
                sym = StrExtract( destSymList, i );
                CategoryRemoveSymbol( sym, cat, nDestWatchList );
            }

            // delete static variables - DO NOT forget the asterisk (wildcard) at the end
            StaticVarRemove( "ValuesToSort*" );
            StaticVarRemove( "topValuesToSort*" );

            symlist = CategoryGetSymbols( cat, nSrcWatchList );
            symCount = StrCount( symList, "," ) + 1;

            // fill input static arrays
            for( i = 0; i < symCount; i++ )
            {
                sym = StrExtract( symlist, i );
                SetForeign( sym );
                values = ROC(Close, 10); // TO_CHANGE HERE AND BELOW IN EXPLORATION
                StaticVarSet( "ValuesToSort" + sym, values );
                RestorePriceArrays();
            }
topN = 10;
            // perform top ranking
            StaticVarGenerateRanks( "top", "ValuesToSort", topN, 1234 );
            sdt = SelectedValue( DateTime() );  // in AA this is the Last date in range
            // this next function retirns the sorted symbols!
            listTop = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );

            for( i = 0; i < symCount; i++ )
            {
                sym = StrExtract( listTop, i );
                CategoryAddSymbol( sym, cat, nDestWatchList );
            }

			// relase static var memory
			StaticVarRemove( "ValuesToSort*" );
			StaticVarRemove( "topValuesToSort*" );
        }

        // EXPLORATION USED ONLY TO VERIFY THE RANKING
        // if hard-coded use the same nSrcWatchList as a filter in AA for the
        // exploration to verify grid results vs. the ranked watchlist

        // display only last bar from the Analysis range
        Filter = ROC(Close, 10);//Status( "lastbarinrange" );
        AddColumn( ROC( C, 10 ), "ROC(10)" ); // Show the same array as used in ranking (values)
        SetSortColumns( -3 );
        AddRankColumn();
    }
    else
        Error( "Please select a single watchlist to rank" );
}

@honzasek, I'm not in front of AmiBroker, but a quick look at the code you posted shows me that you missed a line that was added in the "top" variation, where I reassigned the symCount variable based on the resulting topList:

            listTop = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );
	    
////// ADD THIS LINE	
      	    symCount = StrCount( listTop, "," ) + 1; // line added - get the top list symbols count

			for( i = 0; i < symCount; i++ )
			{
				sym = StrExtract( listTop, i );
				CategoryAddSymbol( sym, cat, nDestWatchList );
			}

Hope this will fix it.

1 Like

I looks very good. Thank you very much!

Nice to see that the suggested idea to improve the Batch functionality (regarding the watchlists) has been implemented in version 6.31.

1 Like