How to use StaticVarGenerateRanks correctly?

Hi, big guys

Question about the use of StaticVarGenerateRanks, it's different between 6.00.2 & 6.40.4 versions, and i'm wondering how to use StaticVarGenerateRanks in the right way. Here's the situation:

The version of my previous Amibroker was 6.00.2.
I follwed the guide ( StaticVarGenerateRanks - guide ) and update to 6.40.4 version, but the use of StaticVarGenerateRanks seems to have changed (different from the guide) :

6.00.2 version --> when SetForeign every Symbols which need to be Rank inside the LOOP,
6.40.4 version --> alerm: "Warning 512"


No, it has not changed!

Warning 512 is in regards to inefficient use of SetForeign within loop.
StaticVarGenerateRanks can be used without loop.
Codes have been posted already.

Hi, @fxshrat

This code is correctly ?

Mainlist = GetCategorySymbols(categoryWatchlist, 0);

if (Status( "StockNum" ) == 0 )
	for (i = 0; (Sym = StrExtract(Mainlist, i)) != ""; i++)
		Values =  IIf(IsEmpty(Close), Null, -Close);
		StaticVarSet("ValuesToSort" + Sym, Values);
	StaticVarGenerateRanks("rank", "ValuesToSort", 0, 1224);
Price = StaticVarGet("ValuesToSort" + Name());
Rank = StaticVarGet("rankValuesToSort" + Name());

Filter = 1;
AddColumn(CB_Price, "Close");
AddColumn(Rank, "Price_Rank");

Buy = 0;
Sell = 0;


Re-read 2nd post. Read slowly and try to understand.
You got response and reason for warning already (as well as alternative way without any loop and where to find it... well, forum search).

Please read what the warning is:

Warning 512 says exactly what is all about. It says:

Warning 512. Calling Foreign() or SetForeign() too many times (in a loop or in repeated code) is very inefficient.

It means, that IF ONLY YOU CAN you should avoid calling Foreign lots of times.

If you can't, the code continues to WORK FINE. The warning just indicates that your code is inefficient. Inefficient, but still working.

You can use StaticVarGenerateRanks without Foreign calls at all. In 6.40.4 there is a #pragma sequence command that allows to run StaticVarGenerateRanks in first step of sequence (scan). For examples see this thread:

Generally Foreign-less scheme would look something like that:

Version( 6.40 ); // required version

#pragma sequence( scan, explore )

if( Status( "action" ) == actionScan )
    if( Status( "StockNum" ) == 0 )
        StaticVarRemove( "ValuesToSort*" );

	Values = - Close; // NO NEED TO CALL Foreign !

    StaticVarSet( "ValuesToSort" + Name(), Values );

	_exit(); // we are done - don't proceed further

if( Status("StockNum") == 0 )
  // remove previous rankings if any
  StaticVarRemove( "rank*" );
  // do the ranking 
  StaticVarGenerateRanks("rank", "ValuesToSort", 0, 1224);

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.