Old thread Create Custom Index (from specified tickers)

The thread discusses about Create Custom Index (from specified tickers). I need to create Industry Composites with similar calculations, using sym1 = "~" + IndustryID(1)

// the statement below defines sequence of actions 
#pragma sequence(scan,explore) 

Version( 6.40 ); // requires version 6.40.4


if( Status("action") == actionScan ) 
{ 
   AddToComposite( C * V / 100000 , "~MyIndex", "X" ); 
   AddToComposite( 1, "~MyIndex", "V");
   _exit(); // quick exit in scan mode 
} 

CustomCalculation = Foreign( "~MyIndex", "C" );
SymbolCount = Foreign( "~MyIndex", "V" );
CustomIndex = CustomCalculation / SymbolCount; // take the average to create your custom index

///////////////
// Explore
///////////////
Filter = 1;
AddColumn( SymbolCount, "Number of Stocks", 1.0 );
AddColumn( CustomCalculation, "Total Calculation", 1.2 );
AddColumn( CustomIndex, "Custom Index", 1.2 );

My requirement is as per below code example,

#pragma sequence(scan,???) 

Version( 6.40 ); // requires version 6.40.4

if( Status("action") == actionScan ) 
{ 
   sym1 = "~" + IndustryID(1);
  AddToComposite( C * V / 100000 , sym1, "X" ); 
  AddToComposite( 1, "sym1", "V");
   _exit(); // quick exit in scan mode 
} 
    
????

I know above AFL is not correct, the correct AFL if executed with single scan or Run Sequence should save all Industry composites with above calculations in the database.

?

Remove quotes from sym1.

?

Dear fxshrat,

My actual requirement is average of equal weighted Index for Industry Composites, for which your afl discussed below link gives a corect result.

//First put AFL to Analysis, 
//Set Apply to: Filter, Enable Pad&align and hit Scan.
//Then put on chart or create Exploration.
//by fxshrat@gmail.com
procedure EqualWeightIndexScan() {
	/// code source AB forum
	/// @link https://tinyurl.com/yu9ray8y
	local not_null, rc, sum_rc;	
	//is_inIndex = Nz(...);
	rc = ROC(C,1)/100;	
	not_null = NOT IsNull(C) /*AND is_inIndex*/;
	sum_rc = not_null * Nz(rc);
	/// @link https://www.amibroker.com/guide/afl/staticvaradd.html
	if(Status("stocknum") == 0)
		StaticVarRemove("~EWI*");
	StaticVarAdd("~EWI_total", sum_rc);	
	StaticVarAdd("~EWI_count", not_null);
}
function EqualWeightIndex( initial_value ) {
	/// code source AB forum
	/// @link https://tinyurl.com/yu9ray8y
	local cnt, sum_rc, avg_rc, result; 
	sum_rc = Nz(StaticVarGet("~EWI_total"));
	cnt = Nz(StaticVarGet("~EWI_count"));
	avg_rc = SafeDivide(sum_rc,cnt);// SafeDivide - AB 6.35
	result = CumProd(avg_rc+1)*initial_value;// CumProd - AB 6.20 
	return result;
}

Version(6.35);// minimum AB version required (because of SafeDivide)

initial_value = 1000;

if ( Status("action") == actionScan ) {
	EqualWeightIndexScan();
}

ewi = EqualWeightIndex( initial_value );

Plot( ewi, "EWI", colorRed );
PlotGrid(initial_value, colorGold);

The afl is for a Filter in Analysis window. How can I use the afl in an analysis window for a single scan or Run Sequence using 'sym1 = "~" + IndustryID(1);' so that all Industry composites are added to Market253 etc.