Wrong rank of 1 during indicator load time?

I am simply trying to rank four symbols. AAPL, ABBV, INTC, MSFT

ABBV's data starts in 2013, where the rest of the symbols have decades of data. At the very beginning of ABBV's data, where it is collecting enough bars to build its score, I give it a temp score of -1000 as to simulate it being in last position score-wise. I do that with this:
values = Nz(values, -1000);

But during this time period, AB is giving it a rank of 1, which to me means in the first position. How can I tell it to give this symbol a rank of last during this time period? Something is not working right with this:
values = Nz(values, -1000);

Here is a graphic showing it:
Capture

And here is the score/rank generator script:

WatchlistNumber = 1;  //64
myScoreSymbol = Name();	

EnableTextOutput(0);
mySymList = CategoryGetSymbols( categoryWatchlist, WatchlistNumber, 0 ); 
EnableTextOutput(1);

SymbolCount = StrCount(mySymList, ",") + 1;
printf("Symbol SymbolCount: %g\n",  SymbolCount ); 

FirstSymbol = Status("stocknum") == 0;

if( FirstSymbol )  // this must have data all the way back
{
	// delete static variables 
	StaticVarRemove( "ItemScore*" ); 

	// fill input static arrays 
	for ( i = 0; ( sym = StrExtract( mySymList, i ) ) != ""; i++ ) 
	{ 
		SetForeign( sym );
		
		TimeFrameSet( inDaily ); // switch to daily time frame 		
		
		values = ROC(Close, 20);		
		values = Nz(values, -1000);
		
		TimeFrameRestore();	
		
		score = values;

		// write ranked values to a static variable
		StaticVarSet( "ItemScore" + sym, score );
	} 
	
	// perform ranking 
	StaticVarGenerateRanks( "Rank", "ItemScore", 0, 1224 ); // normal rank mode 

}

ThisScore = StaticVarGet("ItemScore" + myScoreSymbol ); 
Plot( ThisScore, myScoreSymbol + "_score", colorYellow ); 
printf( "ThisScore: " + NumToStr( ThisScore, 1.4 ) + "\n" );

And here is the rank-reader script:

WatchlistNumber = 1;  //64
myScoreSymbol = Name();	

EnableTextOutput(0);
mySymList = CategoryGetSymbols( categoryWatchlist, WatchlistNumber, 0 ); 
EnableTextOutput(1);

SymbolCount = StrCount(mySymList, ",") + 1;
printf("Symbol SymbolCount: %g\n",  SymbolCount ); 

SymbolCount = SymbolCount -1;

// read ranking
ThisRank = StaticVarGet("RankItemScore" + myScoreSymbol ) ; 
Plot( ThisRank , myScoreSymbol + "_rank", colorWhite ); 

When I do an exploration, no ranks are generated before ABBV's data starts. I have Pad and Align checked, aligning to AAPL, so I don't understand what's going on here. Ideally we would want ranks to be generated on the other three stocks that have history before ABBV, and ABBV to be in last place during this time and also during the time that it is collecting enough bars to calculate its ROC. Here is a snapshot of the exploration:
Capture2

And when the indicators are collecting enough data to calculate the indicator, it returns a 1 for all symbols during this time, which is also not good for us.

We have a strategy that we are trying to test and would only like trades to go through when ranks are high. So this is letting trades through when it should not.

I have over simplified the situation for this forum post. It will be run on hundreds and thousands of stocks in the end with stocks having starting data in different spots.

Here is a snapshot showing all stocks returning a 1 (first place rank) during the indicator's load time:
Capture

@vjsworld sorry but i can't reproduce your error (though I only am using daily database). With a slightly simplified version of your ranking and the same watch list I get this output
image

wlnum = GetOption( "FilterIncludeWatchlist" ); 
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ; 

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

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        values = ROC( C, 20 ); 
        RestorePriceArrays(); 
        StaticVarSet (  "values"  +  symbol, values ); 
    } 

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

symbol = Name(); 

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

Filter = 1; 
AddColumn ( values, "values" ); 
AddColumn ( rank, "rank", 1.0); 
SetSortColumns( 2, 4 );

And when ABBV starts trading,
image

1 Like

Very good Larry!

Using your code, the exploration now looks good on my end. I will dissect the differences between the code sets, but I am certain this will get me to the next step.

Much appreciated!

Here is a snapshot of my exploration with your code:
Capture

1 Like

And adding this to push the score to a large negative if the symbol has no data yet:
values = Nz(values, -1000);

That works for your ranking, but FYI the "Null" value appears to be -1e+010, just recently discussed on the forum,

So it's already a large negative number then.....

It does not work properly because you have not read my former post from here of same topic of yours.

You simply do NOT expand via TimeFrameExpand after TimeFrameRestore in first post of this "new" thread here!

BTW, why starting n-threads for same topic?

2 Likes

I do not see me doing a TimeFrameExpand after a TimeFrameRestore here.......

WatchlistNumber = GetOption( "FilterIncludeWatchlist" ); 
mySymList = CategoryGetSymbols( categoryWatchlist, WatchlistNumber ) ; 

myScoreSymbol = Name();	

SymbolCount = StrCount(mySymList, ",") + 1;
printf("Symbol SymbolCount: %g\n",  SymbolCount ); 

FirstSymbol = Status("stocknum") == 0;

if( FirstSymbol )  // this must have data all the way back
{
	// delete static variables 
	StaticVarRemove( "ItemScore*" ); 

	// fill input static arrays 
	for ( i = 0; ( sym = StrExtract( mySymList, i ) ) != ""; i++ ) 
	{ 
		SetForeign( sym );
		
		TimeFrameSet( inDaily ); // switch to daily time frame 		
		
		values = ROC(Close, 20);		
		//values = Nz(values, -1000);
		TimeFrameRestore();	
		
		values = TimeFrameExpand( values, inDaily, expandLast);
		
		score = values;

		// write ranked values to a static variable
		StaticVarSet( "ItemScore" + sym, score );
	} 
	
	// perform ranking 
	StaticVarGenerateRanks( "Rank", "ItemScore", 0, 1224 ); // normal rank mode 

}

The solution IS to do TimeFrameExpand after TimeFrameRestore as the above code now works.

2 Likes