Problems with ranking

I have found in the help code for ranking from a watchlist, which also works in principle. However, I need a ranking of my watchlist combined (AND) with my setup criteria. In the current code, the ranking of the entire Watchlist is created.
In the AFL code I marked the place.
Can someone give me a hint how this works?
I'm grateful for any help, because here I just can not go on.

SetOption( "initialequity", 10000 );

SetTradeDelays (0,0,0,0);  

risk = -6; 
riskstock = Ref (H,-1) - Ref (L,-1);   
weight = ( risk / riskstock )* Ref( Low, -1); 
PositionSize = Min(weight,-30);
Crit1 = Ref(C,-1) > 10;     
Crit2 = Ref(ROC(C,120),-1)>20;
Setup = Crit1 AND Crit2; 

//---	Rank 
category = categoryWatchlist; 
Tickerlist = CategoryGetSymbols( category, wlnumber ); 

//is something like this possible ?
// tickerlist = tickerlist AND Setup;
if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL 
    StaticVarRemove( "values*" ); 
    for( n = 0; ( Symbol = StrExtract( Tickerlist, n ) )  != "";  n++ ) 
        SetForeign ( symbol ); 
        values = Ref(RSI(12),-1); 
        StaticVarSet (  "values"  +  symbol, values ); 
        _TRACE( symbol ); 
    StaticVarGenerateRanks( "rank", "values", 0, 1224 ); 
symbol = Name(); 
values = StaticVarGet ( "values" +  symbol ); 
rank = StaticVarGet ( "rankvalues" +  symbol ); 

NewHigh = H > Ref(H,-1);

lv_Rank = rank <=10;
Buy = NewHigh AND lv_Rank AND Setup;

BuyPrice = Ref(H,-1);

Sell = Ref (Buy,-2);

@FrankR I'm not sure to fully understand your request, but you can try something along the following lines.

In the ranking block you may store a zero value on stocks that DO NOT FIT your criteria:

    for( n = 0; ( Symbol = StrExtract( Tickerlist, n ) )  != "";  n++ ) 
        SetForeign ( symbol ); 
        fCrit1 = Ref(C,-1) > 10;     
		fCrit2 = Ref(ROC(C,120),-1)>20;
		fSetup = fCrit1 AND fCrit2;         
        values = IIf(fSetup, Ref(RSI(12),-1), 0);

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

Then add an extra condition to your BUY line:

Buy = NewHigh AND lv_Rank AND Setup AND (rank > 0);

The ranking will happen anyway on all the stocks in the watchlist, but the ones that do not fit your rules will be ranked at the bottom and their values are set to zero so you can exclude them from your Buy and still use the first 10 ranked in case many will pass the criteria.

For sure, using an exploration that will track all the values and rules you are defining in your formula will help to verify if everything is like you expect!

I will be happy to see additional answers to your request with alternative/better approaches.

1 Like

A correction.

The Buy line should be:

Buy = NewHigh AND lv_Rank AND Setup AND (values > 0);

(values NOT rank....)

1 Like

Thanks, Guiseppe, it could be a step in the right direction. When I run it in the backtest I get the message "fsetup" used without been initialized and the backtest only works with the first share in my watchlist.
If I make the following extension to fix the initialization error:

fSetup = Nz( StaticVarGet("mystaticarray" ) ); at the beginning
StaticVarSet("mystaticarray", fSetup );  at the end

then there is the error that despite fSetup = 0, the values are not set to = 0.
See attachementexampleForError
if I have described my problem too briefly:
I would like to go into the market with a stop order. The "fSetup" conditions are my rules and must be met.
The next step is to create a ranking for all fSetup = True (with the RSI (12))
Which orders are executed the next day, I do not know "today", for the backtest there is the condition "NewHigh = H> Ref (H, -1) 1".
The purpose of the ranking is to find out how many orders I have to put in order to get an adequate exposure on average.
I think many people who work with stop-orders and positons-score have this problem, but so far I have not been able to find any code.