Compound Value error – self-assigned a non initialized variable


Category:
0
0

The following was supposed to count the number of bars when close for a day was greater than previous day’s close and assign it to a variable called “SumCl.  But when I tried to plot the result on a chart, I get just blank space with no plots.  If I use it in a scan to scan (for items where the result is greater than 31 on a daily chart for  3 months), I get an error that I was “trying to self-assign a non initialized rec SumCl”

I am not sure why the last parameter 0 is not enough to initialize the variable and am not able to figure out what the error is. I tried different alternatives to take care of initialization at both ends f the bars by using two other additional conditions (see below), but I get the same message.

#Rec SumCl = compoundvalue(1, if close >=close[1] then SumCl+1 else SumcCl,0);

#Rec SumCl = compoundvalue(1,if isNan(close) then SumCl else if close >=close[1] then SumCl+1 else Sumcl,0);

Rec SumCl = compoundvalue(1,if isNan(close) then SumCl else if barnumber()==1 then 0 else (if close >=close[1] then SumCl+1 else Sumcl) ,0);

plot result= sumC

addLabel(Yes,Concat(SumCl,””), color = Color.DARK_ORANGE);

Can you pls point out where my error is and how to correct it?

Thanks

Arun

 

Marked as spam
Posted by (Questions: 11, Answers: 16)
Asked on May 17, 2018 5:29 am
1070 views
0
Private answer

Yes, and I knew what the issue was before I even clicked on the post. I’ve done this myself often and there is only one cause when that error pops up. I’m sure I haven’t done it for the last time either…. lol.

The Basics

Here are the basics for recursive variables (the wrong way):

rec variableName = variableName + 1;

Here it is again (the right way):

rec variableName = variableName[1] + 1;

Self assignment of recursive variables happens when our statement tries to include the current bar’s value of the recursive variable. So the only proper way to write recursive variables is to use a previous bar’s value. Actually, when you look up the definition of recursion, you will find exactly this. It is the very nature of recursion.

Not Done Yet

Now you probably think you have enough information to go in and fix the problem. However there is a typo in the very next line:

plot result= sumC

You are missing a letter and the closing ‘;’. It should be:

plot result= sumCl;

Style Guide

In closing, permit me to explain some style elements. One of the goals for writing source code should be to make it readable (easy on the eyes). I’ll just be pointing out two style elements from your example.

Capitalization:

In most C-based languages, the recommendation is that all variable names should begin with a lower case letter. “Camelcase” should be used to differentiate each word in a variable. First-letter caps is reserved for Class Objects, and system methods. Let’s take a look at the CompoundValue() method: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue.html

Notice in the examples given in the language reference, the CompoundValue() has it’s first letter capitalized. Individual words are capitalized (camelcase) in order to differentiate each word. Same thing with the AddLabel() method: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look—Feel/AddLabel.html

Also, ‘def’ and ‘rec’ always start with lower case.

In reviewing you code you will find you had the rules for capitalization reversed, and inconsistently applied.

Variable Names:

It is perhaps one of my pet-peeves. Excessive use of abbreviations in variable names. Just spell it out. By all means make it easier to read. There is nothing gained by using a fewer number of characters. Instead of using sumCl, spell it out: sumClose. Better yet, spell it out so that it describes exactly what it is: sumOfHigherCloses.

Spaces and Operators

Oh, last nit-picky thing. Never use operators such as <, >, /, *, +, =, without placing a space between the variable name and the operators. I have no idea why so many folks do this. But it makes for very poor readability.

if barnumber()==1 Should be: if BarNumber() == 1

if close >=close[1]  Should be: if close >= close[1]

Be sure to review the examples in this post: http://tlc.thinkorswim.com/center/reference/thinkScript/Operators/Comparison.html

Chart Study Exceptions

Ok, last thing. Promise. I felt it was very important to point this out so I hope you kept reading through to this part. You explain in your question that when plotted on a chart you got a blank subgraph. But there was a clue you missed. See attached screenshot. You need to be looking for these when development your code.

Ok, that’s all I have for now. Have fun writing your code.

 

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on May 17, 2018 8:22 am