How to use AddSummaryRows values for further calculation?

Hi,

Below is the code to calculate and find the number of stocks at various Zones (Fib levels in my case) from a few watchlists.

//Calculate Number of Stocks at different Fib Levels

Filter = 1;

//Calculate Annual Fibonacci Levels
Fib_100 = HHV (H, 52);// 52 week (Annual) HHV value as 100 Level
Fib_0 = LLV (L, 52);// 52 week (Annual) LLV value as 0 Level
Fib_786 = Fib_0 + ((Fib_100 - Fib_0)*0.786);// 52 week (Annual) 78.6 Fib Level
Fib_618 = Fib_0 + ((Fib_100 - Fib_0)*0.618);// 52 week (Annual) 61.8 Fib Level
Fib_50 = Fib_0 + ((Fib_100 - Fib_0)*0.5);// 52 week (Annual) 50 Fib Level
Fib_382 = Fib_0 + ((Fib_100 - Fib_0)*0.382);// 52 week (Annual) 38.2 Fib Level
Fib_236 = Fib_0 + ((Fib_100 - Fib_0)*0.236);// 52 week (Annual) 23.6 Fib Level

//If condition of Price above/between/below Fib Zone met, assign value 1 to  conveniently calculate Summary Rows
CMPAbove786 = IIf ( C > Fib_786, 1, 0 );
CMPBetn786_618 = IIf( C >= Fib_618 AND C <= Fib_786, 1, 0 );
CMPBetn618_50 = IIf( C >= Fib_50 AND C < Fib_618, 1, 0 );
CMPBetn50_382 = IIf( C >= Fib_382 AND C < Fib_50, 1, 0 );
CMPBetn382_236 = IIf( C >= Fib_236 AND C < Fib_382, 1, 0 );
CMPBelow236 = IIf( C < Fib_236, 1, 0 );

//AddColumns
AddColumn (CMPAbove786,">78.6",1.0);
AddColumn (CMPBetn786_618,"<=78.6 & >61.8",1.0);
AddColumn (CMPBetn618_50,"<=61.8 & >50",1.0);
AddColumn (CMPBetn50_382,"<=50 & >38.2",1.0);
AddColumn (CMPBetn382_236,"<=38.2 & >=23.6",1.0);
AddColumn (CMPBelow236,"<23.6",1.0);

//AddSummaryRows to get total number of stocks meeting condition
AddSummaryRows(1,1.0);
AddSummaryRows(16,1.0);

It performs as expected.
Below is the snapshot of the result:

Screenshot 2024-11-20 184900

From reading a few old posts on the forum like:
Possible to do computation with rows displayed by AddSummaryRows?
I understand that to make use of the Total and Count values derived from AddSummaryRows, we can create SEQUENCE and StaticVarAdd.

I would like to use values from Total and Count to get percentages of the same in one row.

How would I go about doing that?

Use StaticVarAdd to calculate Total and Count values and add them to a Static variable, followed by Creating a custom Ticker of sorts (By using AddToComposite?)

Is there any source (preferably video) which explains the following:
1] How #pragma sequence() works and the syntax surrounding it.
(Are there limitations to the number of sequences? Can you have different filters for different sequences? Why use buy = 0 in the scan part? what does it do?)
2] How StaticVarAdd works, what are the common misconceptions surrounding it and the mistakes to avoid while using the same.
(From reading a few old posts, it seems there are many ways to not do it right)

I'm an amateur, but not looking to be spoon-fed. Leading me to the appropriate sources of knowledge would be sufficient and most appreciated!

I am keen on understanding how these codes work.

I tried fiddling around to see what values show up and below is an early attempt (amateurish):

//Test StaticVarAdd

#pragma sequence(scan,explore)

Filter = 1;
Fib_100 = HHV (H, 52);
Fib_0 = LLV (L, 52);
Fib_786 = Fib_0 + ((Fib_100 - Fib_0)*0.786);

//Condition to calculate the total number of stocks trading above 78.6 Fib Level
conditionvalue = IIf (C > Fib_786,1,0);

if (Status("action") == actionScan)
{
if (Status("stocknum") == 0)
{
StaticVarRemove("~condition");
StaticVarRemove("~count");

}
StaticVarAdd("~condition",conditionvalue);//Calculate number of stocks meeting the condition of trading above 78.6 Fib Level
StaticVarAdd("~count",1);//Calculate the total number of stocks scanned

Buy = 0;//Why is this being used? What does it do?
_exit();
}

AddColumn(StaticVarGet("~condition"), "Condition value",1.0);
AddColumn(StaticVarGet("~count"), "Count",1.0);
AddColumn(C,"Price",1.2);//Cross Verify results to see if codes run as expected by comparing with Fib Level
AddColumn(Fib_786,"Fib 78.6",1.2);//Cross Verify results to see if codes run as expected by comparing with Price

Before figuring out, how to get the values for all zones, I just wanted to check for stocks trading above 78.6 Fib Level.

Below was the result:

I understand that entire "Count" column will have same value. Why is entire "Condition value" column not having the same value?

It should be 27 based on the results of the first code for exploration.

Any help in leading me to the sources which explain the logic/concept of these codes would be most appreciated.

Thank You.

@jairamiyer, I tested your code and it seems to work here:

I only added a column to verify the "condition", so it's not clear to me why it apparently doesn't work for you as you expect.

Hi,

Thank You for your response. Probably for me, the code generates undesirable values under "condition value" column, because it does not capture the intended values.

I have a feeling, it might have to do something with "Pad & Align". I am not sure about this theory, but I have a feeling it has something to do with me hoping the code generates my preferred values, but is actually not.

Let me give a bit of background (hope you find it interesting):
Initially, while trying to fiddle around with StaticVarAdd I tried to use the below code for generating "count"

StaticVarAdd("~count", IIF (C > 0, 1,0));

The results were not good and I faced a similar situation, where undesirable values were being generated.

On reading the manual and posts from the forum and when one thing lead to the other, I came across the below link about AddToComposite (as the concept is similar):

Introduction to the AmiBroker AddToComposite() By Herman van den Bergen

I got the idea of using 1 in the below code:

StaticVarAdd("~count",1);

from the above link.

Since both

StaticVarAdd("~count", IIF (C > 0, 1,0));

and

StaticVarAdd("~condition",conditionvalue);

don't seme to generate the desired values, I think it has to do something with the what I expect the code to do and what it actually does.

I even tried replacing

StaticVarAdd("~condition",conditionvalue);

with

StaticVarAdd("~condition",IIf (C > LLV (L, 52) + ((HHV (H, 52) - LLV (L, 52))*0.786),1,0));

(thereby getting rid of any loop or syntax issues) but the problem persists.

I had a breakthrough today while learning and implementing AddToComposite.

Below is the code to plot the number of stocks trading above 50 EMA from a watchlist (watchlist constituents can be exclusively from any desired index).

Filter = 1;

condition = IIf (C > EMA (C,50),1,0);

AddToComposite( condition, "~TrdAbv50EMA", "X"); // "X" writes OHLC fields
PlotForeign("~TrdAbv50EMA","Abv50EMA",colorDefault,styleLine); // Use the Composite created as an indicator and plot the same
threshold = Param("Threshold",5,0,52,1); // Use for visual inspection and research the critical value of stocks below certain threshold
Plot(threshold,"Threshold",colorRed,styleLine); // modify threshold value to study the critical value

Buy = 0; // needed so Scan does not complain about missing Buy/Sell variables

Visual inspection of the chart, comparing individual watchlist constituents with the plot again resulted in errors, BUT after I checked Pad & Align and input "~TrdAbv50EMA", the chart worked perfectly.

Long story short, I think there's a mismatch between the values I expect my code to generate and the values actually generated by the code due to some issue (like Pad and Align).

I will need to do more research on why it is the case.

Can I ask if you are aware of any good source which explains Pad & Align, StaticVarAdd, AddToComposite, #pragma sequence() etc. to an amateur coder.

Regardless, Thank You for your response and taking the time to test my code and revert with your findings. Most Appreciated!

1 Like

@jairamiyer, re StaticVarAdd() and using the sequence see this post by @Tomasz.
As for AddToComposite, I think the document you linked is more than enough!
Pad&Align has been discussed many times in this forum (use the search function) to read about different scenarios (for example when to use it and when not). Spend time searching and reading: you'll learn a lot!

2 Likes

Excellent links as always. Have bookmarked them and some more for later reference!

Yes, as suggested, I'll spend more time searching and reading. Should definitely be able to get a hang of it soon.

Thank You.

Hope this can be helpful to others:

Code to calculate and find the number of stocks at various Zones (Fib levels in my case) from a few watchlists.

AddSummaryRows can give total number of stocks in each column and total number of stocks scanned in all;
But, to use these values and calculate percentages of stocks in various Zones (Fib levels in my case) we will use StaticVarAdd and StaticVarGet codes instead.

(AddSummaryRows can generate total and count in each column, but not their corresponding percentages)

*Very Important *:
(I've made some words bold and in italics as they require special attention.)

About Pad & Align, the manual states:
When this is turned on, all symbols' quotes are padded and aligned to reference symbol. It may .... introduce some slight changes to indicator values when your data has holes and holes are filled with previous bar data. The feature is intended to be used when ... you are creating composites out of unaligned data.
Note: if reference symbol does not exist, data won't be padded.

Explanation of what I understood from this:
(If I have misunderstood this concept, please feel free to correct me!)

Assume you have 3 Tickers (Ref, A and B) and we want to study the last 5 bars of each symbol.
Ref has the following values in each bar: 1 1 1 1 1;
A has the following values in each bar: x x 1 0 1
and
B has the following values in each bar: x x x 1 1,
where x indicates that the values do not exist (have holes).
As we can see, the symbols are not aligned, i.e. while Ref has a value in all 5 bars, A has in the latest 3 bars and B has in the latest 2 bars.
Symbols A and B have data holes.

If "Pad and align all data to the reference symbol:" option in the Analysis settings is not checked and if the "reference symbol" space is not accurately filled, the resulting values generated while using StaticVarAdd and StaticVarGet will be inaccurate!

The below code can be used to calculate the total number of stocks in each Fib Zone and the corresponding percentages

//Calculate Number of Stocks at different Fib Levels and their corresponding percentages

#pragma sequence(scan,explore)

Filter = 1;

if( Status( "action" ) == actionScan )
{
    if( Status( "stocknum" ) == 0 )
    {
        StaticVarRemove( "~Abv786" );
        StaticVarRemove( "~Btwn618_786" );
        StaticVarRemove( "~Btwn50_618" );
        StaticVarRemove( "~Btwn382_50" );
        StaticVarRemove( "~Btwn236_382" );
        StaticVarRemove( "~Blw236" );
        StaticVarRemove( "~Count" );
    }

	//Calculate the number of symbols trading at desired level (Annual Fib Level in this case)
	
    StaticVarAdd( "~Abv786", IIf( C > LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.786 ), 1, 0 ) ); //Calculate number of stocks trading above 78.6 Fib Level

    StaticVarAdd( "~Btwn618_786", IIf( C >= LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.618 ) AND C <= LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.786 ), 1, 0 ) ); //Calculate number of stocks trading between 78.6 and 61.8 Fib Levels

    StaticVarAdd( "~Btwn50_618", IIf( C >= LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.50 ) AND C < LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.618 ), 1, 0 ) ); //Calculate number of stocks trading between 61.8 and 50 Fib Levels

    StaticVarAdd( "~Btwn382_50", IIf( C >= LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.382 ) AND C < LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.50 ), 1, 0 ) ); //Calculate number of stocks trading between 50 and 38.2 Fib Levels

    StaticVarAdd( "~Btwn236_382", IIf( C >= LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.236 ) AND C < LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.382 ), 1, 0 ) ); //Calculate number of stocks trading between 38.2 and 23.6 Fib Levels

    StaticVarAdd( "~Blw236", IIf( C < LLV( L, 52 ) + ( ( HHV( H, 52 ) - LLV( L, 52 ) ) * 0.236 ), 1, 0 ) ); //Calculate number of stocks trading below 23.6 Fib Level

	//Calculate the total number of stocks scanned

    StaticVarAdd( "~Count", 1 ); // We will use this value to calculate percentages later

    Buy = 0; // needed so Scan does not complain about missing Buy/Sell variables

    _exit();
}

// Column will give the total number of stocks trading above 78.6 Fib Level
AddColumn( StaticVarGet( "~Abv786" ), ">78.6", 1.0 ); 

// Column will give the total number of stocks trading above 78.6 Fib Level in Percentages

// We will use StaticVarGet("~.....") / StaticVarGet( "~Count" ) ) * 100 to get percentage of stocks trading at Fib Level

AddColumn( ( StaticVarGet( "~Abv786" ) / StaticVarGet( "~Count" ) ) * 100, ">78.6 %", 1.2 ); 

AddColumn( StaticVarGet( "~Btwn618_786" ), "<=78.6 & >61.8", 1.0 );
AddColumn( ( StaticVarGet( "~Btwn618_786" ) / StaticVarGet( "~Count" ) ) * 100, "<=78.6 & >61.8 %", 1.2 );

AddColumn( StaticVarGet( "~Btwn50_618" ), "<=61.8 & >50", 1.0 );
AddColumn( ( StaticVarGet( "~Btwn50_618" ) / StaticVarGet( "~Count" ) ) * 100, "<=61.8 & >50 %", 1.2 );

AddColumn( StaticVarGet( "~Btwn382_50" ), "<=50 & >38.2", 1.0 );
AddColumn( ( StaticVarGet( "~Btwn382_50" ) / StaticVarGet( "~Count" ) ) * 100, "<=50 & >38.2 %", 1.2 );

AddColumn( StaticVarGet( "~Btwn236_382" ), "<=38.2 & >23.6", 1.0 );
AddColumn( ( StaticVarGet( "~Btwn236_382" ) / StaticVarGet( "~Count" ) ) * 100, "<=38.2 & >23.6 %", 1.2 );

AddColumn( StaticVarGet( "~Blw236" ), "<23.6", 1.0 );
AddColumn( ( StaticVarGet( "~Blw236" ) / StaticVarGet( "~Count" ) ) * 100, "<23.6 %", 1.2 );

AddColumn( StaticVarGet( "~Count" ), "Count", 1.0 );

The above code serves my purpose. Below is the snapshot of the result.

Ideally I would've liked to print out the result in just few rows, rather than the same value being displayed in all rows of all columns.
I would presume that achieving the same would involve:
1] Transferring the values to dummy symbols through AddToComposite.
2] Creating Watchlists through CategoryCreate.
3] Add Symbols to the watchlists created through CategoryAddSymbol and
4] Explore the watchlists created for this purpose.

As implementing these additional steps would only serve presentation purposes, I will not be doing that.