GroupID - how to use

Hy everyone,i have a little problem with this simple script: why the GroupID property don’t change with the loop? It print the same group for every symbol of the list !! But this is incorret, the groupId is different !

Thank you

fh = fopen( "c:\\temp\\myfile.txt", "w");

list = CategoryGetSymbols( categoryWatchlist, 4 );

for( i = 0;  (sym =  StrExtract(list , i )) != ""; i++ )
{
 fputs(GroupId(1) +  sym + "\n"  ,fh);
}

fclose( fh );

As for your question:
there are better ways doing what you want to do.

But related your presented code you would have to use SetForeign(). Also I would use a trigger since you want to export just once. Right?

if( ParamTrigger( "Export Groups", "CLICK HERE" ) ) 
{
	fh = fopen( "c:\\temp\\myfile.txt", "w");
	//
	list = CategoryGetSymbols( categoryWatchlist, 4 );
	//
	for( i = 0; (sym = StrExtract(list, i )) != ""; i++ )
	{
		SetForeign( sym );
		group_id = GroupID(1);
		RestorePriceArrays();
		
		fputs(group_id + "," + sym + "\n", fh);
	}
	//
	fclose( fh );
}
4 Likes

One comment that may explain why your code did not work:

GroupID() function like any other function use current symbol. Your code iterated thru symbol list, but did not change current symbol, that is why you got the same group all the time (that was the group of current symbol).

@fxshrat code changes current symbol using SetForeign() call. That is why it works. But SetForeign() is not very efficient.

If you want to export groups it is much faster and easier to use Exploration. Exploration is designed for tabular output other methods not.

SetOption("NoDefaultColumns", True ); // don't want name and date columns 
Filter = Status("lastbarinrange");
// concatenated group and symbol as in original code
AddTextColumn( GroupID(1) + Name(), "Group name + symbol" ); 

It works without SetForeign() because Exploration, by itself, goes thru multiple symbols (use Apply To: Filter)

Generally I observe tendency to abuse SetForeign(). This call should be avoided when other methods exist.

5 Likes

Hi fxshrat

May I ask two questions:

  1. Why are different colours used in the code?
    — and —
  2. Why is the code offset in relation to LH edge of a new line?

Code is automatically colorized by Discourse. See “How to use this site” topic. Offset is automatic too.