Exploration Filter based dynamic variables

Hi guys,
Could anybody suggest a solution for this, i've been puzzled...

Using Norgate Data base...
PSEUDOCODE

  1. retrieve all stocks from a GICS, say GICS10
  2. run the ranking on them and store in sector-specific staticvar
VarSet("G10",1);//
VarSet("G15",1);//
VarSet("G20",1);//
VarSet("G25",1);//
VarSet("G30",1);//
VarSet("G35",1);//
VarSet("G40",1);//
VarSet("G45",1);//
VarSet("G50",1);//
VarSet("G55",1);//
VarSet("G60",1);//


if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs (if any)
    StaticVarRemove( "ranksec*" );
    StaticVarRemove( "sector*" );
	for( i = 10; i < 30; i+=5 )
	{
	if(VarGet("G"+i)==1)
	{
		List = CategoryGetSymbols( categoryGICS, i ) ;

		for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
		{
			SetForeign( symbol );
			volumeFilter = 			C*MA(V,15)>10000000;
			Pricefilter = 			C > 1;
			
			values = IIf(volumeFilter AND priceFilter ,ROC(C,200) ,0 );
			RestorePriceArrays();

			// write ranked values to a static variable
			StaticVarSet( "sector" + i  + "_" + symbol, values );

		}
		StaticVarGenerateRanks( "ranksec", "sector" + i +"_", 0, 1234 );
    }
    
 	}   
    
	

}


symbol = Name();
for( i = 10; i < 30; i+=5 )
{

values = StaticVarGet( "sector" +i  + "_" + symbol ); // retrieve actual values (ranking is performed in 1 afl, separate ranking for each GCIS [ie, GICS10,GICS15,etc.]
rank = StaticVarGet( "ranksec" + "sector" +i  + "_" + symbol ); // retrieve ranking of each sector
AddColumn( values, "AnnSlope"+i );
AddColumn( rank, "rank"+i );


//AddTextColumn( GICSID( 1 ), "Sector" );
//AddtextColumn( StrLeft( GicsID( 0 ), 2 ), "Sector No", 1 );
}
Filter = rank<100;

Output in Exploration:
image

What I would like to achieve is that the exploration only displays the top x stocks per sector. Say top 50 for GICS10, top 50 for GICS15 etc.,(there will obviously be blanks for stocks not in a specific sector).

If I set the filter like above, I believe it gets overwritten several times, hence the output gets truncated as in the picture (meaning it only shows the rankings of the LAST SECTOR). If I set filter to 1 filter= 1;, I get all stocks, obviously.

Any thoughts on this would be greatly appreciated. Thanks for your time.

1 Like

Variable rank of Filter is outside of loop so only the last rank occurrence (25) gets filtered. So only ranks of last sector are filtered. So that's why the other columns are empty completely.

Rather do like this:

/// Incomplete code snippet!!!
Filt = 0;
symbol = Name();
for( i = 10; i < 30; i += 5 )
{
    values = StaticVarGet( "sector" + i  + "_" + symbol ); // retrieve actual values (ranking is performed in 1 afl, separate ranking for each GCIS [ie, GICS10,GICS15,etc.]
    rank = StaticVarGet( "ranksec" + "sector" + i  + "_" + symbol ); // retrieve ranking of each sector
    Filt ||= rank < 100;
    AddColumn( values, "AnnSlope" + i );
    AddColumn( rank, "rank" + i );
//AddTextColumn( GICSID( 1 ), "Sector" );
//AddtextColumn( StrLeft( GicsID( 0 ), 2 ), "Sector No", 1 );
}
Filter = Filt;
4 Likes

Now let me be honest here:

  1. It works THANK YOU very much :pray: :pray:
  2. I'm glad I asked because I would have never solved it
  3. I wasn't even aware that ||= exists
  4. Which reminds me of about 11 years ago when I asked something in the AMIBROKER YAHOO group and the guy helped me by introducing the %-operator to me :grinning_face_with_smiling_eyes: definitely, today, I learned something new again.

Now lastly, after googling it, I'm still not quite sure how it actually works. @fxshrat , if you'd have a minute to explain to me, I'd really appreciate.

The way I currently see it is like this:
let's start with i=10

values = StaticVarGet( "sector" + "10"  + "_" + symbol ); // gets the ranking criteria value for the 1st stock
rank = StaticVarGet( "ranksec" + "sector" + 10  + "_" + symbol ); // gets the rank of the 1st stock, let's say it's rank = 10
filt ||= rank <100, //evaluates to true (or 1), add to filt ?! for that symbol? Not sure
//addcolum etc...

continue with say i=15

values = StaticVarGet( "sector" + "15"  + "_" + symbol ); // gets the ranking criteria value for the 1st stock
rank = StaticVarGet( "ranksec" + "sector" + "15"  + "_" + symbol ); // gets the rank of the 1st stock, let's say it's rank = 190
filt ||= rank <100, //evaluates to false (or 0), dont add to filt for that symbol? Not sure ?!
//addcolum etc...

thank you.

Dio

1 Like

here is how it looks:
image

and then, sorted by another column, but same exploration:
image

1 Like
  1. It works THANK YOU very much :pray: :pray:
  2. I'm glad I asked because I would have never solved it

Of course it works.
In 90%++ of cases I do not post things not working.
I do no post for the sake of posting or making prank posts. :wink:


  1. I wasn't even aware that ||= exists

Filt ||= rank < 100;

is short form for

Filt = Filt OR rank < 100;

So inside the loop it combines each sector rank via OR operator per iteration step.
To illustrate it more clearly... after last iteration step it would be like this:

Filt = rank10 < 100 OR rank15 < 100 OR rank20 < 100 OR rank25 < 100;

So Filt becomes TRUE if a symbol is part of either sector and if symbol's rank in that sector is lower 100. And if Filt becomes TRUE for a symbol then Filter will become TRUE also and a symbol will be part of exploration result list with value output in either cells 10...15....20...25 depending on which sector it is part of.

4 Likes

Awesome @fxshrat ! I get it now. great explanation. Appreciate it!! :smiley:

thanks again,
Dio

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