Ranking Based on equation

Hello,
i am trying to rank some stocks in a watch list based on an equation but the output is not right
here is a snippet of the code

Mainlistnum = GetOption( "FilterIncludeWatchlist" );
Mainlist = CategoryGetSymbols( categoryWatchlist, Mainlistnum );


for ( i = 0; ( sym = StrExtract( Mainlist, i ) ) != ""; i++ ) 	
{ 	SetForeign(sym );
	fv=ValueWhen(DateNum() == startdate, BarIndex(), n = 1);
	AddtextColumn(sym,"start");
	
	fvb=LastValue(fv);
	
	fc = C;
	fv=ValueWhen(DateNum() == startdate, BarIndex(), n = 1);
	Xo = fc[fvb];
	
	for(j=fvb;j<BarCount;j++)
	{
		f[j]=(C[j]/Xo)*100;
	}

	Value = f; 
	
	StaticVarSet( "ValuesToSort" + sym, Value ); 
}
	

// Perform Ranking
StaticVarGenerateRanks( "rank", "ValuesToSort",0,1024 );



todayrank = StaticVarGet("rankValuesToSort" + sym );
AddColumn(todayrank,"rank"); 
  1. Code snippet is not really helpful bcos there's no point going through your code Line-by-line and trying to figure out what Logic you intended to apply.
    Unless there is a syntax error.

  2. Just saying output is not as expected is not useful in any way, post the output and what should be in its place.

  3. If you are posting snippets, make sure its still executable code, not one that will throw half-a-dozen errors.
    Even if I took time out to fix them, how do I know what's the right output?

  4. QuickAfl
    http://www.amibroker.com/kb/2008/07/03/quickafl/

Read here and share more details, are you sure the Required bars are set right(if needed) and quickafl is not actively doing what its supposed to do?
This can easily cause such symptoms.

Maybe you just start with describing in plain English what you want to rank, what is the "equation" you would like to use and what is your "expected" result.

here is the whole code:



//TimeFrameSet(inWeekly);

startdate = DateTime();
enddate = DateTime();

scoreBackdays = param("Scoring lookback days",5,0,50,1);
startdate = ParamDate( "Start date (working day)", "2018-08-25");
bench = ParamStr( "Bench", "-EGX30" );

 

 

xxx=0;
fv=ValueWhen(DateNum() == startdate, BarIndex(), n = 1);



fvb=LastValue(fv);

 

fc = C;

Xo = fc[fvb];

AddColumn(C,"C");


AddColumn(fvb,"fv");

Filter =fc;

fx = Foreign( bench, "C" );


Yo =fx[fvb];


AddColumn(Yo,"C bench");

nrsArray=0;

nrsArrayBench=0;

nrsDiff=0;

 

for(i=fvb;i<BarCount;i++)

{

                nrsArray[i]=(C[i]/Xo)*100;

}

for(i=fvb;i<BarCount;i++)

{

                nrsArrayBench[i]=(fx[i]/Yo)*100;

               

                nrsDiff[i]=nrsArray[i]-nrsArrayBench[i];

}

AddColumn(nrsArray,"nrs");

 

 

 

Mainlistnum = GetOption( "FilterIncludeWatchlist" ); //ok...probably about identify and getting the number of stocks in universe

Mainlist = CategoryGetSymbols( categoryWatchlist,Mainlistnum); //ok..probably about identify and getting list of stocks in universe

StaticVarRemove( "ValuesToSort*" );

                StaticVarRemove( "rankValuesToSort*" );

                // Fill input value (for ranking) into Static arrays

                for ( i = 0; ( sym = StrExtract( Mainlist, i ) ) != ""; i++ )       

                {             

                                                                               

             

                                cFor= Foreign(sym, "C" );
                                fc = cFor;

                                Xo = fc[fvb];


                                for(j=fvb;j<BarCount;j++)

                                {

                                                nrsArray[j]=(cFor[j]/Xo)*100;

                                }

                                if(sym==Name())

                               AddColumn(nrsArray,"NRSARRAAY");

                                Value = nrsArray;

                                StaticVarSet( "ValuesToSort" + sym, Value);

                }

                                                                               

 

                // Perform Ranking

                StaticVarGenerateRanks( "rank", "ValuesToSort",0,1234 ); // normal ranking

RestorePriceArrays();

todayrank = StaticVarGet("rankValuesToSort" + Name());

AddColumn(todayrank,"rank");

 

 



 

 

 

YesterdayRank = Ref(todayrank,-1);

BeforeYesterdayRank = Ref(todayrank,-2);

 

 

AddColumn(YesterdayRank,"YesterdayRank");

AddColumn(BeforeYesterdayRank,"BeforeYesterdayRank");


 

 

 

and here is a snippet of the explore, i rank based on the blue column which is the nrsAraay and the red on is the ranking
and as you can see there are a lot of stocks were there is no ranking even though there is values for that stock

ranking

A couple of things to get you started:

  1. All of your ranking code should only be executed once, instead of once for every symbol. You can do this by putting it inside a statement like this:
if (Status("stocknum") == 0)
{
    // Your ranking code here
}
  1. Make sure you have Pad & Align turned on, as described in this thread: Ranking sometimes not working (when you have holes in data)
1 Like