# Average of equal weighted Index for Industry(1)

https://forum.amibroker.com/t/create-custom-index-from-specified-tickers/5625/2
With reference to the above thread, I need to a single scan to build an average of equal weighted Index for Industry(1) lists and save to local database with ~ prefix.

``````sym1 = "~" + IndustryID(1);
Lrc = ROC(L,1);
Orc = ROC(O,1);
Hrc = ROC(H,1);
Crc = ROC(C,1);
Vrc = ROC(V,1);

AddToComposite( 1, sym1,"I");// quantity of stocks
``````

The do understand, the above code can't produce averages only sum of data. This can't be done in a single scan, probably two scans required. First scan to create a symbolcount and then use it to produce average of equal weighted index.
An AFL posted by portfoliobuilder and Tomasz in the above mentioned thread uses StaticVarAdd and run EXPLORE to create an Average of equal weighted Index for a watchlist. How to do for a Industry(1) list? I prefer AddToComposite as its simple and able to access from Group 253 or Market 253. I am open to the idea of using StaticVarAdd and run EXPLORE also.
I paste the AFL code below for reference purposes, which was discussed in the above mentioned thread.

Moderator comment: Again, this is example HOW NOT to write formulas.
DO NOT USE SetForeign() inside loop

You should not use ROC() because ROC may get negative and you probably don't want to have your index going into negative area.

For equal weighted index you have to choose point in time when your index "starts". Then you need to normalize each security price to that point. For example

``````
InitialDate = DateNum() <= 1000101; // start from 2000-01-01

NormalizedPrice = Close / ValueWhen( InitialDate, Close );

``````

At this time you have Normalized price that starts from 1 for every symbol. Now you can add up normalized prices for many symbols to create equal weighted index.

And for Gods sake DO NOT use Foreign()/SetForeign when it is NOT needed.

Read the manual and use the code from the manual.

``````InitialDate = DateNum() <= 1000101; // start from 2000-01-01
NormalizedPrice = Close / ValueWhen( InitialDate, Close );

// this is for SCAN (one time)
if( status("stocknum") == 0 )
{
// remove any earier composite values
StaticVarRemove("~Composite");
}

``````InitialDate = DateNum() <= 1000101; // start from 2000-01-01