Having trouble finding the error for extract numbers from list... Market Capitalization of DAX30 plot chart

Hi there I just like to plot the total market cap of DAX 30 in billions of euros, the code is below, but I having a bit time stress to grab the nr of shares of each dax stock constituent :s ... thanks.

_SECTION_BEGIN("CAPTESTING");
_N( lista = ParamStr( "Tickers", "BAYN.DE,CON.DE,MUV2.DE,IFX.DE,EOAN.DE,LIN.DE,PSM.DE,DAI.DE,SAP.DE,DPW.DE,FME.DE,CBK.DE,BMW.DE,BAS.DE,BEI.DE,VOW3.DE,DTE.DE,DBK.DE,VNA.DE,SIE.DE,HEN3.DE,HEI.DE,ALV.DE,ADS.DE,DB1.DE,MRK.DE,RWE.DE,FRE.DE,LHA.DE,TKA.DE" ));
_N( nrshares = ParamStr( "Nrshares", "857947808,200005983,145770000,1136450679,2167149433,185638071,228949482,1069837447,1193405243,1229573069,306461371,1252357634,657600600,918478694,226818984,501295263,4742941596,2066402041,511100826,815518880,434278198,198416477,438879929,203893455,186610158,434777878,614745499,554710473,471259644,622531741" ));
_N(Title = StrFormat( " Date : {{DATE}} - {{VALUES}}" ) + "\n" );
SetChartOptions(0,chartShowArrows|chartShowDates);

Buy=Sell=0;
nrcotadas = 0;

StaticVarRemove("~shares*");
StaticVarRemove("~dayvalue");
nshares = 0;

for( j = 0; (nrshares = StrExtract( nrshares, j )) != "" ; j++ )
{

nshares = StrToNum(nrshares);

StaticVarSet("~shares"+j, nshares);

}

for( i = 0; (symbol = StrExtract( lista, i )) != "" ; i++ )
{
setForeign(symbol);

shares = StaticVarGet("~shares"+i);

marketcap =  (close*shares);//1000000000 ;

StaticVarAdd("~dayvalue", marketcap);

}

total1 = StaticVarGet("~dayvalue");

Plot( total1, "DAX 30 Market Cap in Billions of Euros", colorOrange, styleLine);

_SECTION_END();

Seems that cannot grab the numbers for the number of shares in order do multiply with close ticker price... its weird.. can someone help me with this please. Thanks.

@jpagp3

why are you reassigning nrshares in the first loop initializer?
After the first iteration that variable it is no longer the string with the list of numbers of shares and so the loop is immediately over!

Check this snippet with the debugger:

nrshares = "857947808,200005983,145770000,1136450679,2167149433,185638071,228949482,1069837447,1193405243,1229573069,306461371,1252357634,657600600,918478694,226818984,501295263,4742941596,2066402041,511100826,815518880,434278198,198416477,438879929,203893455,186610158,434777878,614745499,554710473,471259644,622531741";
nshares = 0;
for( j = 0; ( nrshares = StrExtract( nrshares, j ) ) != "" ; j++ )
{
    nshares = StrToNum( nrshares );
    _TRACE("" + j + ") Nr shares: " + nrshares);
}

Anyway, I will do it all in a simpler way:

_SECTION_BEGIN( "CAPTESTING" );
symbolList = "BAYN.DE,CON.DE,MUV2.DE,IFX.DE,EOAN.DE,LIN.DE,PSM.DE,DAI.DE,SAP.DE,DPW.DE,FME.DE,CBK.DE,BMW.DE,BAS.DE,BEI.DE,VOW3.DE,DTE.DE,DBK.DE,VNA.DE,SIE.DE,HEN3.DE,HEI.DE,ALV.DE,ADS.DE,DB1.DE,MRK.DE,RWE.DE,FRE.DE,LHA.DE,TKA.DE";
sharesList = "857947808,200005983,145770000,1136450679,2167149433,185638071,228949482,1069837447,1193405243,1229573069,306461371,1252357634,657600600,918478694,226818984,501295263,4742941596,2066402041,511100826,815518880,434278198,198416477,438879929,203893455,186610158,434777878,614745499,554710473,471259644,622531741";
_N( Title = StrFormat( " Date : {{DATE}} - {{VALUES}}" ));
SetChartOptions( 0, chartShowArrows | chartShowDates );
marketcap = 0; // will be an array since we will add foreign Close arrays to it 
for( i = 0; ( symbol = StrExtract( symbolList, i ) ) != "" ; i++ )
{
    // SetForeign( symbol );
    fgnClose = Foreign( symbol, "C");
    sshares = StrExtract( sharesList, i );
    nshares = StrToNum( sshares) ;
    marketcap = marketcap + ( fgnClose * nshares ); 
    // _TRACE(symbol + " - Shares: " + sshares);
}
Plot( marketCap, "DAX 30 Market Cap in Billions of Euros", colorOrange, styleLine );
_SECTION_END();

Thanks ver much beppe :slight_smile: you made a very clean code, I was really confused... But I have learned working better with strExtract... My main purpose of this test was just to see some divergence between dax index and dax market cap index... not many, but some occasions made some divergence... Err.. the data of the number of shares was grabbed by hand from website investing.com following the ticker de30 index.. I think that might not be very well updated so I came to an "hammer " solution

_SECTION_BEGIN( "CAPTESTING" );
symbolList = "BAYN.DE,CON.DE,MUV2.DE,IFX.DE,EOAN.DE,LIN.DE,PSM.DE,DAI.DE,SAP.DE,DPW.DE,FME.DE,CBK.DE,BMW.DE,BAS.DE,BEI.DE,VOW3.DE,DTE.DE,DBK.DE,VNA.DE,SIE.DE,HEN3.DE,HEI.DE,ALV.DE,ADS.DE,DB1.DE,MRK.DE,RWE.DE,FRE.DE,LHA.DE,TKA.DE";
sharesList = "857947808,200005983,145770000,1136450679,2167149433,185638071,228949482,1069837447,1193405243,1229573069,306461371,1252357634,657600600,918478694,226818984,501295263,4742941596,2066402041,511100826,815518880,434278198,198416477,438879929,203893455,186610158,434777878,614745499,554710473,471259644,622531741";
_N( Title = StrFormat( " Date : {{DATE}} - {{VALUES}}" ));
SetChartOptions( 0, chartShowArrows | chartShowDates );
marketcap = 0; // will be an array since we will add foreign Close arrays to it
for( i = 0; ( symbol = StrExtract( symbolList, i ) ) != "" ; i++ )
{
// SetForeign( symbol );
fgnClose = Foreign( symbol, "C");
sshares = StrExtract( sharesList, i );
nshares = StrToNum( sshares) ;
marketcap = marketcap + ( fgnClose * nshares );
// _TRACE(symbol + " - Shares: " + sshares);
}

bi = Barindex();
fvb = FirstVisiblevalue( bi );
marketcapb = marketcap/1000000000;

marketcapbperf = (Nz( ( marketcapb - marketcapb [ fvb ] ) / marketcapb [ fvb ] ) + 1)*100;

Plot( marketcapbperf, "DAX 30 % Market Cap", colorOrange, styleLine );
_SECTION_END();

mcap

Thanks.

markcap2

Can anyone confirm this? Market Cap top January of 2018 below 2015 market cap top?

@jpagp3 in order to produce an accurate chart (and the relative comparisons) instead of a single "list" of the number of shares for each ticker you should have this number for each bar (storing the data, for instance, in one of the Aux fields), to do the correct calculation based on the number of shares available at a certain date (that may change over time).
But I imagine that getting these numbers in an automated way may not be easy (and/or free).
Moreover, ideally, the calculation should also take in account index composition changes. This past thread discuss how to address this kind of issues using AmiBroker.

P.S. When sharing code, please, learn how to past it properly using the </> code button in the forum message editor toolbar (and ideally, use the "Edit/Prettify selection" in the formula editor, to reformat/indent your code before pasting it here).

I suggest you to review the "Enter the AFL code properly" section in this other thread.

@beppe is absolutely correct. Shares outstanding figures are typically published quarterly and can change drastically over time.

I would also like to add that you need to use clean unadjusted price data to calculate market cap. Any historical price adjustment (dividends, spinoffs, splits, etc.) will ruin your calculations.

It can take a bit of work to resolve events that occur between reporting periods, but this should be manageable for 30 stocks. Many adjustments that are commonly applied to prices are not related to changes in the number of shares outstanding.

ok, thank you very much :slight_smile: Yes its not easy or free :s ... ok yes I was aware of that... aux field, historical composition changed quarterly or year end... but what do you think in this case (dax30) timeframe 2015 to 2018(now) ? (the chart above)
It wasnt much change to the tickers, and I dont think that were capital increase (maybe just DBK), is the chart above from 2015 to 2018 very deformed from the truth?

Thanks both for the answers.

Well I thinking building a chart kind of stacked chart, with the market cap of dax tickers in stacked up layers... But if this for a 2 year or 3 year timeframe isnt accurate from the truth then this simple method is not useful..

I’m sorry but I don’t have the details for those stocks.

I see on Wikipedia that the DAX is not equally weighted. It is capitalization-weighted, which also changes over time. That is something else to consider depending on what you are trying to measure.

Hi there again, after just a small research I find a site that helps seeking shares outstanding through the years...
http://financials.morningstar.com/income-statement/is.html?t=0P0000CKP6&culture=en-US&platform=sal

3150846195190711001

I just ask a friend for the market cap data from bloomberg terminal, and in fact doesn't match the numbers from investing.com number of shares :s, by comparison the trend is the same, but the numbers are a bit disproportionate different as a result...