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):
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:
When I run the code in debbugger mode, the values are correct, as shown in the screens below:
The values of _TRACEF () are in accordance with the values generated in the explore window.
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!