Using eTables with Amibroker

GOT IT!!!! Step 2 complete....

Well Im pretty excited! I made it through Step 4 and was able to get everything to work. The only issue is that in the list it doesnt seem to actually rank them. There is a column for "Score" which I assume would be how they would be ranked but it is unpopulated. Thanks again @rocketPower! Besides that scoring, this is exactly what I have been looking for!

I hope it isnt too annoying that with every thought I come across trying to get this right that I post...LOL. It just helps to think out loud! Anyway I think I know why it isnt scoring but its going to take me quite a bit of digging and reading. Unless I am wrong I think it has something to do with the StaticVarSet(Name() portion and the other StaticVarGet lines....Ill dig into these and see if I cant find it.

No worries, it's fun to watch you work your way through it lol.

You don't need static variables for the exploration itself, I save the score to the static variables for each stock so that I can call them back up in the chart code for the interpretation window.

If you check the code, it sums all y## parameters as the variable Score for all the negotiable criteria using the etables data and EOD data, then adds the score to a column, then sorts the rows accordingly. I've pulled all the relevant code to get what you want. You can always sort later in the exploration table, too:

// determine parameters (may be more than you need here for the Score)
SMA200 		= MA(C, 200);
SMA150 		= MA(C, 150);
SMA050 		= MA(C,  50);
High52pct 	= HHV(C, 253);
Low52pct   	= LLV(C, 253);
SMAV50		= MA(V,50);
RS                   = StaticVarGet(Name() + "_RS");
CR			= StaticVarGet(Name() + "_CR");
SF			= GetFnData("SharesFloat");
RSL			= C/Foreign("$SPX", "C");
EPS 		= StaticVarGet(Name() + "_EPS");
LQEPS		= StaticVarGet(Name() 	+ "_LQEPS");
LQsales		= StaticVarGet(Name() 	+ "_LQSales");
PTM		= StaticVarGet(Name() 	+ "_PTMargin");

// Negotiable criteria
y1  = IIf(C   		> 10, 1, 0);
y2  = IIf(RS  		> 90, 1, 0);
y3  = IIf(SF  		< 50e6, 1, 0);
y4  = IIf(RSL 		> Ref(RSL, -30), 1, 0);
y5  = IIf(EPS 		> 85, 1, 0);
y6  = IIf(LQEPS		> 20, 1, 0);
y7  = IIf(LQsales 	> 10, 1, 0);
y8  = IIf(BarCount 	< 10*253, 1, 0);
y9  = IIf(PTM		> 10, 1, 0);
y10 = IIf(CR 		> 94, 1, 0);

// Sum all negotiable criteria into a score
Score = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10;

// add the Score column to the exploration analysis window
AddColumn(Score, "Score", 1.0);

// I sort primarily on the score (in this case only on score)
SetSortColumns(-3);

Hope this helps so you can focus on the exact code you need to study.

Mike

OK @rocketPower, maybe this is a mistake but I am trying to read the code to some extent as if it is a book trying to understand why I am not getting an output in the "Score" column of the table. So my question is, am I reading this portion correctly?

y1 = IIf(C > 10, 1, 0);

"y1 is equal to 1 if C is greater than 10 therefore output 1"

Score = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10;

"The Score is equal to the output of y1 plus the output of y2 plus......"

StaticVarSet(Name() + "_score", Score, True);

This line admittedly baffles me even after reading through the AFL literature....LOL. That said at this point, since you didn't list it in this last response, I am assuming it is used to pull the score into the interpretation.

AddColumn(Score, "Score", 1.0);

"In the column labeled "Score" input the sum of which score equals"

Unless I am reading these wrong, it seems to me that I should be getting the output but Im not.

02%20PM

Hello,

So I'm assuming you're not familiar with static variables, which the command staticvarget, staticvargettext retrieve, and staticvarset and staticvarsettext create.

When you run a program you coded, all the variables you set are stored in volatile memory (ie RAM). Once the program has ended that memory is freed up for whatever resource needs arise in your computer and the parameters you set are essentially lost.

Now what if you want to keep these variables and their values/strings so that they can be used in other programs (e.g. your interpretation window)? This is where static variables come in. When you set a static variable, you are actually saving it to your hard drive (see: Backup Persistent Static Variables). Now Amibroker allows you to also allow them to be persistent, so when you close Amibroker, the static variables are not cleared as well.

So what I do is any parameter I want to keep for other programs/analyses/explorations (within limitations of the above thread link), and in my case persistently between Amibroker sessions, I save as a static variable.

So in the case of Score, you don't need the static variable set for the exploration itself, I only save it as one to display in my interpretation window, as you surmised.

The reason it's showing up blank, I can only guess is linked to your lack of knowledge of static variables. When you took my script to scrape the eTables text files, are you saving them to static variables? Because when you calculate Score, the only reason I can think of that would cause them to be blank is that the static variables you are calling for Composite Rating, EPS Rating, RS Rating, SMR Rating, etc, are not actually stored as a static variable at the time of retrieval.

I could be wrong but without seeing all your code, that's my basic suspicion.

Good luck sir!
Mike

2 Likes

Thank you for the explanation and direction...again! I'll pour through these and try and familiarize myself with them more and see what I can come up with.

Well after 10-12 hours across the last 3 nights, I have crawled down every hole you've pointed out and while I have come up with something I am not real sure what to do with it. What I have figured out seems quite elementary and probably makes me look like the fool here...lol. That said, here it is. Something I have noticed is that everywhere you use one of the StaticVar commands, each of them ends with "(Name()". So based on what little I comprehend of what I have read, it seems that this would be the variable name and what you have in there is something generic for a code example. For this to work for me, I need to identify these variable, My guess is that StaticVarSet(Name() is what identifies the variable, StaticVarGet(Name() retrieves the variable. Is this correct? I know there are other StaticVar commands but I haven't quite figured out what they do but I will.

So If I am correct with what I have stated, and that is a BIG IF, I need to identify the (Name() of the StaticVar. Until that is done, the StaticVar commands won't really do what you have them intended for, I think. I am basing this on what you said about "I save them as a static variable". Which brings the question, how do you determine what to call them and is this the action that allows them to be saved? I have reviewed several pieces of code, not just yours, trying to figure this out.

I am sorry for all of the questions but I am deadset on figuring this out one way or another...LOL. Thank you for baring with me.

Hello

Name() retrieves the currently selected ticker.

When creating the static variable name, I use the ticker as a prefix, so I can recall the parameters for a given ticker.

So, given: StaticVarSet(Name() + "_score", Score, True) , current ticker is "NVDA"

... a static variable is created called "NVDA_score" and set that value to the variable Score. Since the persist flag = true, this static variable won't be cleared after you shut down Amibroker.

Hope this helps and enjoy the ride

Mike

OK that actually does make sense. Thanks for the explanation. I tried something else to help me try and sort out the issue. At the beginning of this post in your Step 3 IBD Table, I copied it and then in an adjacent window I copied that Modified IBD Table which sorts out the Negotiable and Non-negotiable. I went through each line by line comparing them and while there are slight differences, they are basically written the same. So then I ran the 1st version in an exploration and it actually appeared to work fine for me, and did display the "Score" in the score column. I tried the same thing again with the modified version of IDB Table and it still isn't displaying the score. So I think what I am going to do is keep comparing the two versions. Since one displays the score and the other doesn't, yet are written nearly identical, I should be able to narrow down the issue in the modified version I would think.

Feel free to send me your code, I don't mind

While I still haven't been able to figure out why I can't get the score column to populate, I thought I would take a little break from that, and instead see if I couldn't make some progress in another way...still more to do but it is something! Thought I would share.....added a little color. Simple I know but we all have to start somewhere right!

12%20PM

1 Like

I tried something else which I think actually get me closer. You mentioned that the whole point of the StaticVar in regard to the "Score" had to do with recalling it for use in your interpretation window (as an example). So I went into the script for the interpretation window (what I copied from yours above) and added a line to pull the score into the interpretation window. My thought behind this was to see if the fact that I cant get the score column to populate in the exploration would hold true to the interpretation window. What I found was that in the interpretation window, it would return "0". The screener is just blank. Regardless, it seems to me that it isn't storing the score information as a Static Variable for some reason. Everything seems to work perfectly except for the score. I am going to keep playing with it but here is it just as I am working on it:

// Generate IBD Table



// determine parameters
SMA200 		= MA(C, 200);
SMA150 		= MA(C, 150);
SMA050 		= MA(C,  50);
High52pct 	= HHV(C, 253);
Low52pct	= LLV(C, 253);
SMAV50		= MA(V,50);
RS          = StaticVarGet(Name() + "_RS");
CR			= StaticVarGet(Name() + "_CR");
SF			= GetFnData("SharesFloat");
RSL			= C/Foreign("$SPX", "C");
EPS 		= StaticVarGet(Name() + "_EPS");
LQEPS		= StaticVarGet(Name() 	+ "_LQEPS");
LQsales		= StaticVarGet(Name() 	+ "_LQSales");
PTM			= StaticVarGet(Name() 	+ "_PTMargin");

// determine trend template status

// Non nego MTP2019 p52
x1a = C  	 > SMA150 OR IsEmpty(SMA150);
x1b = C 	 > SMA200 OR IsEmpty(SMA200);
x2  = SMA150 > SMA200 OR IsEmpty(SMA200);
x3  = SMA200 > Ref(SMA200, -20) OR IsEmpty(SMA200);
x4a = SMA050 > SMA150 OR IsEmpty(SMA150);
x4b = SMA050 > SMA200 OR IsEmpty(SMA200);
x5  = C   	 > Low52pct*1.25;
x6  = C      > High52pct*0.75;
x7  = RS     > 70;

Filter 	= x1a AND x1b AND x2 AND x3 AND x4a AND x4b AND x5 AND x6 AND x7;
StaticVarSet(Name() + "_Stage2", Filter, True);

// Nego
y1  = IIf(C   		> 10, 1, 0);
y1_color=IIf(y1,colorGreen,colorGold);
y2  = IIf(RS  		> 90, 1, 0);
y2_color=IIf(y2,colorGreen,colorRed);
y3  = IIf(SF  		< 50e6, 1, 0);
y3_color=IIf(y3,colorGreen,colorRed);
y4  = IIf(RSL 		> Ref(RSL, -30), 1, 0);
y5  = IIf(EPS 		> 85, 1, 0);
y5_color=IIf(y5,colorGreen,colorRed);
y6  = IIf(LQEPS		> 20, 1, 0);
y6_color=IIf(y6,colorGreen,colorRed);
y7  = IIf(LQsales 	> 10, 1, 0);
y7_color=IIf(y7,colorGreen,colorRed);
y8  = IIf(BarCount 	< 10*253, 1, 0);
y9  = IIf(PTM		> 10, 1, 0);
y9_color=IIf(y9,colorGreen,colorRed);
y10 = IIf(CR 		> 94, 1, 0);
y10_color=IIf(y10,colorGreen,colorRed);

Score = y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10;
StaticVarSet(Name() + "_score", Score, True);

AddColumn(Score, "Score", 1.2,colorblack);
AddColumn(C, "Price",1.2,colorblack,y1_color);
AddColumn((C/Ref(C,-1)-1)*100, 				"% $ Ch",      1.1);
AddColumn((V/SMAV50-1)*100,  				"% Vol ch",    1.0);
AddColumn(High52pct,           				"% 52 WkHigh", 1.1);
AddColumn(SF, "Float",1,colorblack,y3_color);
AddColumn(CR,								"CR",			 1,colorBlack,y10_color);
AddColumn(StaticVarGet(Name() + "_EPS"), 	"EPS", 			 1,colorBlack,y5_color); 
AddColumn(RS, 					            "RS", 			 1,colorBlack,y2_color);

AddTextColumn(StaticVarGetText(Name() 	+ "_IGRS"), 	"IndGrp RS", 	1);
AddTextColumn(StaticVarGetText(Name()	+ "_SMR"), 		"SMR", 			1);
AddTextColumn(StaticVarGetText(Name() 	+ "_AD"), 		"A/D", 			1);
AddTextColumn(StaticVarGetText(Name() 	+ "_Spon"), 	"Spon", 		1);

AddColumn(StaticVarGet(Name() 	+ "_PE"), 		"P/E Ratio", 			1);
AddColumn(StaticVarGet(Name()	+ "_LQEPS"), 	"Last Qtr EPS %", 		1,colorBlack,y6_color);
AddColumn(StaticVarGet(Name() 	+ "_CQEPS15"), 	"Consec Qtr EPS>15%", 	1);
AddColumn(StaticVarGet(Name() 	+ "_CYEPSEst"), "Curr Yr EPS %", 		1);
AddColumn(StaticVarGet(Name() 	+ "_LQSales"),	"Last Qtr Sales %",	 	1,colorBlack,y7_color);
AddColumn(StaticVarGet(Name() 	+ "_PTMargin"),	"Pretax Margin",	 	1,colorBlack,y9_color);
AddColumn(StaticVarGet(Name() 	+ "_ROE"),		"ROE",		 			1);
AddColumn(StaticVarGet(Name() 	+ "_IBD50Rank"),"IBD50 Rank", 			1);

AddTextColumn(StaticVarGetText(Name() 	+ "_inIBD8585"), 	"IBD8585", 		1);
AddTextColumn(StaticVarGetText(Name() 	+ "_inIBDNA"),	 	"IBD New Am",	1);

SetSortColumns(-3, -11, -10, -13);//```

Progress again!!! tThis time I thought well what if it just isnt adding them up for some reason? So what I did was had it JUST do:

Score = y1 + y2;

Without all of the others, it did populate the score column...so now I just need to add them one at a time to figure out which is causing the problem!

I narrowed it down to y4. y4 contains REF which I am assuming has to do with the Relative Strength Line. My guess is that I do not have it to reference. Going to jump down that hole for a little bit...see when I get back!

Thanks again for all your help!!! I went down that hole and found the problem! I now have the score populating the exploration as well as the Interpretation window. y4 was causing the issue because I didnt have anything set up for RS. Found during the process that I don't have data for ^SPX. I set it to ^DJI to get it to work for now. Have reached out to my data provider (Tiingo) to see if they have that data or what my options are. So excited to have this working. You have done an amazing job!

Well I have had good luck so far working with this and making my own modifications but I have come across another issue with a pice of code that I have added to draw more information into the Interpretation Window. It is actually crashing Amibroker. The code I suspect is:

printf("------------IBD Stock Brief--------------" + "\n\n");

brief = (StaticVarGet(Name() + "_Brief") + "\n\n");
brief = StrReplace(brief, "%", "%%");
printf(brief);

I have tried to enter the last three lines based on one of the above comments in an exchange between @rocketPower and @beppe. After loading it, it will work with some of the charts but occasionally will give an error on other charts:

20%20PM

When I attempt to open and edit the formula, that is when Amibroker crashes. Any suggestions?

I can't comment on why AB is crashing, but as a temporary workaround you could open the .afl in a text editor, make your edits, save and close, then reopen AB and have another attempt?

2 Likes

First: you need to post more than just few lines because no-one knows what you are WRITING to static variables. The source of problem is the way you initialize (or the way you do NOT initialize) static variables.

You are getting error because you are trying to get the value of NON-EXISTING static variable and you don't check it. StaticVarGet() would give you NULL if static variable does not exist. And then your code is trying to add (concatenating) Null to string which won't work.

If you want to get static variable that is supposed to be string, you have to call StaticVarGetText() instead.

On non-existing variable it will give you empty string instead of Null.

@MCassICT - this is important - use CODE TAGS, not "quotes"

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Thanks for the response yet again Tomasz! I will remember that next time which I am sure there will be one. I appreciate all the time and effort you put into this for everyone by the way! I will go through the code based on what you have offered and see what I can come up with.