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:
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.