staticVarGenerateRank not working properly in Live Trading

I want to replicate the function of positionScore function for live trading via scanner. I want to get only top 5 signals at every candle start. All my buy conditions are based on last candle close. I have a list of 50 symbols. My AFL scans this list and shows top 5 symbols based on their ADX value.
The issue is, the rank generated from the staticVarGenerateRank function is based on the value of previous candle, it only uses the value for this candle once the candle is completed, whereas I need to fire orders on candle open.

Below is my code snippet:


Buy= EMA(Ref(C,-1),5)>EMA(Ref(C,-1),20)



 if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL
{	wlnum = GetOption( "FilterIncludeWatchlist" );
    List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;

    StaticVarRemove( "values*" );
	StaticVarRemove( "rank*" );

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

        values =Ref(ADX(14),-1);
        RestorePriceArrays();
        StaticVarSet (  "values"  +  symbol, values );  
                             
        //_TRACE( symbol );
        
    }

    StaticVarGenerateRanks( "ranks", "values", 0, 1234 );
}

symbol = Name();


values = StaticVarGet ( "values" +  symbol );
ranks = StaticVarGet ( "ranksvalues" +  symbol );

In the final result each symbol is ranked on the basis of Ref(values,-1) instead of values. However, if I run the exploration again at eod it works fine.