Trying to make a "manual" way of Advance Decline Line ( ADL ) code AFL


Hi tthere I am new here, I am having trouble to find a solution for static variables or addtocomposite function since debugging it I cant see any values there... someone can help me complete the task of passing to global variables all the tickers of dax index ? Thanks.

nrcotadas = 0;

//upticks = 0;
//downticks = 0;
//StaticVarSet("~globaldownticks", downticks);
//StaticVarSet("~globalupticks", upticks);

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

nrcotadas = i+1;

StaticVarSet("cotadas", nrcotadas );

//t = BarCount;
upticks = Nz( Close - Ref(Close, -1) );
up = upticks>0;
//teste = AdvIssues() ;
StaticVarAdd("~globalupticks", up);
AddToComposite(IIf(C - Ref(C, -1) > 0, 1,0), "~NYSEAdv", "X" );

downticks = Nz ( Ref(Close, -1) - Close );

// IIf (upticks>0, StaticVarAdd("~globalupticks",1),0);
// IIf (downticks>0, StaticVarAdd("~globaldownticks",1),0);

// for( bar = 0; bar < BarCount; bar++ )
// {
// AddToComposite(IIf(Close - Ref(Close, -1) > 0, 1, 0), "~Adv", "X" );
//AddToComposite(IIf(Ref(Close, -1) - Close > 0, 1, 0), "~Dec", "X" );
// }


nrcotadas = StaticVarGet("cotadas");

AA = Foreign("~NYSEAdv","X");
tempo = StaticVarGet("~globalupticks");
tempo2 = StaticVarGet("~globaldownticks");
Plot(AA, "Advances", colorGreen, styleLine|styleThick);

@jpagp3 have you considered the built in AmiBroker ability to create “composites” like advance decline lines from your database?

From @Tomasz on the old forum (copied this from @fxshrat 's board)

"In order to make Advances/Declines work (and TRIN also) first you have to calculate composities in your database:

1) Open Categories window using Symbol->Categories menu item.
2) Select base index for given market in Markets tab and Base indexes for - Composites combo.
For example if you are following NYSE this can by ^DIJ (Dow Jones Average)
(certain symbol must be marked as index in Symbol -> Information and must belong to the same market)

3) Choose “Symbol ->Calculate composites” menu item to open the window shown below and mark:
- Number of advancing/declining issues and
- Apply to: all quotes, All markets

4) Click Calculate . From now on ADLine, AdvVolume() and TRIN and any indicators based on advIssues / DecIssues should be visible.

Best regards
Tomasz Janeczko, "

Also, most decent data vendors provide either the Advance Decline line or at least the daily number of Advancing and Declining Issues for the markets they cover. That would make it easy to create your indicator.

And if that does not work for you then there are probably a few examples of using AddToComposite floating around the internet.

StaticVarAdd being newer (and “better”) probably does not have too many examples available to copy yet. But we can work on it here some day.

Good luck.

BTW, if you have the code writing ability to put together that very attractive chart, I am certain that you can solve this and hope you share your solution with us.

1 Like

I have done it manually, its better this way

nrcotadas = 0;

for( i = 0; ( symbol = StrExtract( lista, i ) ) != “”; i++ )
setForeign( symbol );
//teste = AdvIssues() ;
nrcotadas = i+1;

StaticVarSet("cotadas", nrcotadas );

upticks = Nz( Close - Ref(Close, -1) );
downticks = Nz( Ref(Close, -1) - close );
upcond = upticks>0;
downcond = downticks>0;
unchcond = Nz( Close - Ref(Close, -1) ) == 0;

StaticVarAdd("~globalupticks", upcond);
StaticVarAdd("~globaldownticks", downcond);
StaticVarAdd("~globalunchticks", unchcond);


nrcotadas = StaticVarGet(“cotadas”);

subidas = StaticVarGet("~globalupticks");
descidas = StaticVarGet("~globaldownticks");
inalteradas = StaticVarGet("~globalunchticks");

diferenca = (subidas - descidas)/ (inalteradas + 1);
diferencasqrt = IIf (Diferenca > 0, sqrt (diferenca), - sqrt (-diferenca));
adl = Cum (diferencasqrt);

Plot(adl, "Advance Decline Line", colorOrange, styleLine|styleThick);


@jpagp3 you a still have a few minutes to edit your post and properly add code tags (the three ``` at the start and end of your code)

Ups, sorry didnt see it. Thanks

@jpagp3 since you were generous enough to share, and beat me to posting a just made preliminary attempt too. It is not the same as yours as I have not yet included "unchanged issues" (and there are a couple of other differences).

I am too busy to try and compare (and fix it if necessary) for a few days. But thanks for sharing.

// V1.0  a preliminary attempt 
// Building a composite Advance Decline line reading for a WL
// Using StaticVarAdd run EXPLORE

// pick watchlist in AA window
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

if( Status( "stocknum" ) == 0 )
    // cleanup variables created in previous runs (if any)
    StaticVarRemove( "~Advances*" );
    StaticVarRemove( "~Declines*" );
    StaticVarRemove( "~TotalCount*" );

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )

        SetForeign( symbol );

        AdvIss = C > Ref( C, -1 );
        DecIss = C < Ref( C, -1 );

        StaticVarAdd( "~Advances", AdvIss );
        StaticVarAdd( "~Declines", DecIss );
        StaticVarAdd( "~TotalCount", 1 ); // I have not yet included Unchanged Issues




svAdvances = StaticVarGet( "~Advances" );
svDeclines = StaticVarGet( "~Declines" );
AdvDecToday = ( svAdvances - svDeclines );
AdvanceDeclineLine = sum( AdvDecToday, barssince( Status( "firstbarinrange" ) ) );

TotalSymbols = StaticVarGet( "~TotalCount" );

// Explore
Filter = Status( "stocknum" ) == 0;
SetOption( "NoDefaultColumns", True );
AddColumn( DateTime(), "Date", formatDateTime );

AddColumn( svAdvances , "Advances", 1.0 );
AddColumn( svDeclines, "Declines", 1.0 );
AddColumn( AdvDecToday, "AdvDecToday", 1.0 );
AddColumn( AdvanceDeclineLine, "AdvanceDeclineLine", 1.0 );
AddColumn( TotalSymbols, "Total Symbols", 1.0, colorDefault, colorLightYellow );

// Chart
Plot( AdvanceDeclineLine, "AdvanceDeclineLine", colorAqua, styleThick );

I am using specific dates and not my entire data history in this code. It for now it produces an Exploration like this,

And chart like this,


well I guess nowadays germany dax index is in worst shape compared to DIA - Dow Jones Industrial Average no doubt about it, even though like you said you didnt add unchanged tickers issues to your code… it doesnt change the result… but I guess that trump trade must be passed for sell the news and sell the euro for the dax to proceed ATHs… Thanks for the code too.

Can this process be used for making the indicator by using all stocks trading in exchange?

I want to get advance-decline line or ratio for all the stocks.

Is it possible?

@thonkey I think you could use the same code but substitute a couple of changes for getting a "Market" instead of a watch list. Such as,

wlnum = GetOption( "FilterIncludeMarket" ); //FilterIncludeMarket instead of FilterIncludeWatchlist
List = CategoryGetSymbols( categoryMarket, wlnum ); // categoryMarket instead of categoryWatchlist

In your AA Filter setting choose the Market in your data that you want to apply the code to. I haven't tried but it should work.

1 Like

Showing this errorerror

@thonkey the StaticVarAdd() function was added in AmiBroker version 6.0.2:

What version of AB are you using?

1 Like

OT (but related).

Just a curiosity....

I'm not sure about this thing, but to me, it seems that the AFL version that is listed for each function is corresponding to an official AmiBroker version number minus 2 (two).

This means, for example, that each function that is listed as version 4.10 is supported by AmiBroker version 6.10 (AFL 3.5 by AB 5.5, AFL 4.20 by AB 6.20, etc.).

What is the reason for this version numbering difference?

Becase AFL 1.0 was introduced in AmiBroker 3.0. In earlier versions there was no AFL language. Back then there was much much simpler "rule description language".

1 Like

Version 6.00.2

Version 6.00.2