Dynamic Universe for backtesting


My first post here :-). Have been using amibroker for few months now and I love it. My question is pertaining to backtesting, I have data for 1700 odd Indian equity stocks from year 2000. Is there a way wherein I can filter top 200 stocks by market cap for backtesting on daily basis and run the script only on those stocks.

Buy = Cross ( EMA (C,9), EMA(C,100));
Sell = Cross ( EMA (C,34), EMA(C,9));
Open_Positions=5;//optimize( "open_positions",5,2,10,1); 
SetOption( "MaxOpenPositions", Open_positions );
SetOption( "WorstRankHeld", Open_positions*2 );
SetPositionSize( 100/Open_positions, spsPercentOfEquity );
PositionScore= ROC(C,20);

Say for example I will check on 1st Jan 2000 for top 200 stocks by market cap and run my scrip, now suppose one trade is closed on 8th Jan and only 4 trades are live. (I am running 5 live positions simultaneously). For adding new trade (5th Live position) on 9th Jan script should check for top 200 socks by market cap on that day (or previous day) and select a trade accordingly.
Hope my query is clear. Also I tried searching for this query on group but couldn’t find anything concrete, if this query has been already answered kindly redirect me to that thread.

Thanks and Regards,

You would need data of historical number of shares outstanding because market capitalization is price of stock multiplied by shares outstanding. Or you would just need historical data of market capitalization.

I have required data with me. But how do I code a script which checks for top 200 stocks by market cap on daily basis (so that for backtesting universe of stocks is dynamic).

Have you searched for ranking?

1 Like

Thanks for the links, I will go through those and see if it can be implemented. Will update on Monday.

So far struggling with coding, I guess it will take some time for me to implement it.

@gbiradar if you post your code (altered versions of the links) we will see what you are working on, and perhaps be able to point you in the right direction when you tell us what you are having difficulty with.

Remember, the more you learn, the easier it will be to do your own. Then you can help others when they have a similar issue. Or even better, they read this thread and see where you struggled and learn how you fixed it, and they learn as well.

So, show us what you have, and tell us what is not working the way you want/expect. We can help educate.

1 Like

Thanks for the encouragement, not working for few days. Will update here soon. Appreciate you guys for helping out newbie like me.

I used following line in ASCII importer to import shares outstanding and market cap

$FORMAT Skip, Ticker, Date_MDY, Open, High, Low, Close, Volume, Skip, MARKET_CAP, SHARES_OUT, Skip

Not sure if I used market_cap correctly but in the fundamental statistics window I can see Shares Outstanding for each and every stock (so assuming either market_cap or shares_out did the trick for me).
Data that I have (supposedly adjusted data, data source Capital Line) has different shares outstanding on different dates (I got the number by dividing market_cap by closing price), sometimes difference in outstanding shares between two dates is upwards of 14%.
After using following lines


I realized that Amibroker stores Shares_Out as single value. I don’t know how to save shares outstanding as an array (assuming that’s correct approach). Any inputs are welcome

How do I get more responses (also quick :slight_smile: ) to my queries?

@gbiradar, Not sure if you are using it correctly either… If you have a source of daily data that includes either Market Cap or Shares Outstanding, I would suggest importing that into the AUX1 or AUX2 field. Then you have it available on a daily basis and will be able to work with the data in your AFL seamlessly.

As to getting more/faster response, that will depend on how many people are interested in your issue, and what you show you are doing.

It looks to me like you are searching the site and finding tidbits of information, but have not showed us your full code.

If you don’t want to show us some “secret sauce” then make your buy signal something simple list a Moving Average cross over, or base it on the position or rank of your 200 highest market cap.

Hope this helps.

Hey Snoopy thanks for the reply. I haven’t reached to top 200 by mkt cap thing yet :slight_smile: . Will post the code the moment it’s ready. Meanwhile will try your Aux suggestion, thanks

@gbiradar: If you’re considering using the Aux1 and Aux2 fields, make sure your data provider isn’t already using them for something else!