staticVarGenerateRank not working properly in Live Trading

#1
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 current candle once the candle is completed, whereas I need to fire orders on candle open.

In the final result each symbol is ranked on the basis of Ref(values,-1) (i.e, previous value) instead of current values. However, if I run the exploration again at eod it works fine, I only face this problem as long as that candle is not complete in live market. Timeframe used is 15 mins.

Any help is appreciated. Thanks!!

Below is my code snippet:

Code:
 if ( Status("stocknum") == 0 ) 
{    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 );


Buy= EMA(Ref(C,-1),5)>EMA(Ref(C,-1),20) and ranks<=5;