Select randomly amongst top ROC

I am currently trying to achieve the following:

Generate a list of the top 10 ranked stocks according to ROC(C, 10) and select random entry from this top 10 list.

I am currently using:

Positionscore = ROC(C, 10);

But i know this will give me the top ranked entry only and thus give unrealistic results. Is there a simple way to achieve what I want to recreate?

Not exactly what you’re after, but one option would be to use ranking as outlined here: https://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/ In other words, limit potential signals to top 10, and then apply random PositionScore.

e.g., something like this:

// we run the code on WatchList 0
List = CategoryGetSymbols( categoryWatchlist, 0 );
SetOption("MaxOpenPositions", 3);

if ( Status("stocknum") == 0 ) // Generate ranking when we are on the very first symbol
{
     StaticVarRemove( "values*" );

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
     {
         SetForeign ( symbol );

         // value used for scoring
         values = ROC(C,10);

         RestorePriceArrays();
         StaticVarSet (  "values"  +  symbol, values );
         _TRACE( symbol );
     }

     StaticVarGenerateRanks( "rank", "values", 0, 1224 );
}

symbol = Name();
values = StaticVarGet ( "values" +  symbol );
rank = StaticVarGet ( "rankvalues" +  symbol );

BuySignal = “enter buy condition here” AND rank <=10; 

// buy on next bar
Buy = Ref(BuySignal,-1);

PositionScore = mtRandom();

Of course this doesn’t guarantee a trade. It could be that top ranked items don’t meet your buy condition.

1 Like

Thanks for the help, will give it a go :slight_smile: