Problems with static variables in the generation of rankings

Hi all,

I am trying to create an action range based on the codes available on the page How to imitate IBD Relative Strength Percentile Ranking of Stocks, but with moving averages. However, both the original and the current rank have generated successive failures. Here is my code:

listNYSE = CategoryGetSymbols(categoryMarket, 1);
listNSDQ = CategoryGetSymbols(categoryMarket, 2);
List    = listNYSE + "," + listNSDQ;     // joint markets
     

ListQty = StrCount(List, ",") + 1; // Determine the quantity of stocks to rank

if( Status( "stocknum" ) == 0 )
{
	// Clear the static vars from last run
	StaticVarRemove( "TRS_*" );
	StaticVarRemove("TRSraw_*");
	StaticVarRemove("TRankTRSraw*_"); 

	// Generate the raw RS score for every stock and store in a static var
	for (n = 0; (Symbol = StrExtract(List, n)) != "";  n++) 
	{ 
		SetForeign (Symbol,0);
		RSraw = 0;
		x = MA(C,20);
		y = Ref(MA(C,20),-20);
		RSraw = x/y;	//compares current moving average and 20-day moving average
		
		
		 if ( Symbol == "NBACR") { //Debug only
		
		_TRACEF ("the value RSraw is NBACR :%g", RSraw[BarCount-1]);
		_TRACEF ("the first value RSraw is NBACR :%g", RSraw[0]);
		_TRACEF ("Bars :%g", BarCount);
		}
		
		RestorePriceArrays(); 
		StaticVarSet("TRSraw_"  +  Symbol, RSraw, False); 
	} 

	// rank the stocks using this extremely useful function!
	StaticVarGenerateRanks("TRank", "TRSraw_", 0, 1224); 

	// Convert the static var ranks generated into a percentile score.
	for (n = 0; (Symbol = StrExtract(List, n))  != "";  n++) 
	{
		Rank  = StaticVarGet ("TRankTRSraw_" +  Symbol); 
		RSpctile = 100 - 100*Rank/ListQty;
		StaticVarSet("TRS_" + Symbol, RSpctile, False);
		
		if ( Symbol == "NBACR") {	//Debug only
		
		_TRACEF ("the value Rank is NBACR :%g", Rank[BarCount-1]);
		}
		
}
}
z = Name (); //Debug only
Valor  = StaticVarGet ("TRSraw_" +  Name()); 
Ranque  = StaticVarGet ("TRS_" +  Name()); 
		
// exploration code for verification
AddColumn(Valor, "Value");
AddColumn(Ranque, "Rank");
Filter = (Ranque >= 95); 

When I run in the explore window it results in the list below (the date in American format is 08/07/2020):
Explore
However, there are several symbols that have wrong values. For example, the last value of RSraw = MA (C, 20) (/ MA (C, 20), - 20) for the symbol "NBACR" is ~ 0.2 / 0.16 = 1.25 and not 9.00:
Graph

When I run the code in debbugger mode, the values are correct, as shown in the screens below:
debu1
debug2

The values of _TRACEF () are in accordance with the values generated in the explore window.
traceF
Finally, how do I delete static variables with persistent = true? Can I delete the PersistVars.bin file? I ask, because I didn't notice this characteristic of the static variables in the first codes and I believe that the saved variables are messing up some code. I deleted the PersistVars.bin file and it doesn't seem to have worked.

I appreciate anyone who can help me!

This line

should be

Not tested

Change
SetForeign (Symbol,0);
to
SetForeign (Symbol,1);

and make sure you set "Pad and align" in Analysis settings

SetForeign (Symbol,1);

@awilson It worked! Thank you very much for take your time to help me. I read dozens of various functions, including SetForeing, but that detail was missed! I even marked the "Pad and align" configuration in Analysis settings, but it had not worked. The statistical variable was also wrong and should be responsible for generating different results sometimes. Obrigado!

It is worth noting that 1 (actually "true" value), is the default value for fixup parameter see http://www.amibroker.com/f?setforeign

The defaults are in place for a reason - you should use them in 99% of cases.

1 Like