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(Lrc, sym1,"L");
AddToComposite( Orc, sym1,"O");
AddToComposite( Hrc, sym1,"H");
AddToComposite( Crc, sym1,"C");
AddToComposite( Vrc, sym1,"V");
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
Copy-pasted BAD CODE from the other thread was DELETED

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.
http://www.amibroker.com/guide/afl/staticvaradd.html

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"); 
} 

StaticVarAdd( "~Composite", NormalizedPrice ); 
Buy = 0;

Alternatively you can use AddToComposite

InitialDate = DateNum() <= 1000101; // start from 2000-01-01
NormalizedPrice = Close / ValueWhen( InitialDate, Close );
// one line is needed to create composite index
AddToComposite( NormalizedPrice, "~Composite", "X" );

And no, generally you don't do High/Low, because highs and lows are hit at DIFFERENT times in different symbols therefore you are getting meaningless values if you add those.

2 Likes