learning REC & ADDCHARTBUBBLE


Category:
0
0

Hi Pete;

First of all, How are you feeling, I heard you were ill. I hope you are OK now.

I am still new to this coding & testing environment. I am trying to test 2 new concepts I never used before, learning on the fly, little by little.

I tried to find my answers on the internet, but I am still confused.

I am trying to replace an arrow with a number in a bubble. Plus I am trying to utilize the REC code to make this thing work.

Enclosed below is the code I am trying to utilize and an attachment to illustrate what I am trying to accomplish.

The arrows are suppose to point to a valley, I am trying to use the bubble with a successive number as a replacement.

Marked as spam
Posted by (Questions: 4, Answers: 5)
Asked on August 5, 2019 2:33 pm
641 views
0
Private answer

Yes, I am doing much better. Thanks for asking.

Here is the code you provided in the attachment:

def Rec= 0;
def Valley = low < Lowest(low[1], 3) and low < Lowest(low[-3], 3)
rec = rec[1] + 1
else
double.NaN ;
plot y = if rec <= 100 then
AddChartBubble(Valley, low, rec, if rec > 0 then Color.white
else double.NaN ;

Review

Let me review this for you since you stated that you are trying to learn.

First of all, please be sure not to use any reserved words as variable names. "rec" is a reserved word used to define recursive variables. Details here:

http://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/rec.html

So instead of using

def Rec = 0;

You should be using something like this:

rec myRecursiveVariable = 0;

Recursive Variable Declarations

Notice from the link I provided above this type of variable assignment has been obsoleted. However it is still supported and is very useful for making your code easier to read. When you define a recursive variable it helps make it more readable to make that assignment using rec instead of def.

Back to Your Code

Next thing I will mention is that none of this code you provided will work. There are many things wrong with this code and some of those problems are syntax errors. It looks like you tried to employ an if/then/else statement but you are missing the if and the then portions of that structure.

I am taking a wild guess. But it seems you wanted to create a recursive variable and each time the variable named "Valley" was true you wanted to add a 1 to the current value of the recursive variable. If so, then I would suggest the following:

rec myRecursiveVariable;
def Valley = low < Lowest(low[1], 3) and low < Lowest(low[-3], 3);
if Valley {
myRecursiveVariable = myRecursiveVariable[1] + 1;
} else {
myRecursiveVariable = myRecursiveVariable[1];
}

Search for Recursion

There are many examples of recursive variables in this forum. So if you want to learn how to use this then just enter "recursion" in the search box at the top of the topic.

This link shows the results of searching the "Chart Studies" topic:

https://www.hahn-tech.com/ans/cat/studies/?question_type=all&search=recursion

The Chart Bubble

Now let's take a look at the section of code where you are trying to create those chart bubbles.

plot y = if rec <= 100 then
AddChartBubble(Valley, low, rec, if rec > 0 then Color.white
else double.NaN ;

It is important to note that you can never insert the AddChartBubble() function into the middle of an if/then/else statement. So this section of code will also fail.

Since the AddChartBubble() function always stands along we need to apply it just like the example shown in the Thinkorswim language reference:

http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddChartBubble.html

Before I provide a line of code to replace this, I need to mention one other mistake. You see where you have added an if/then/else statement to set the color of the chart bubble? Well the else portion of that statement must be a color and can never be "Double.NaN".

Properly Structure Chart Bubble

Here is what you might use for the chart bubble:

AddChartBubble(Valley, low, myRecursiveVariable, if myRecursiveVariable > 0 then Color.WHITE else Color.CURRENT);

This statement checks if Valley is true. And if it is it will plot a chart bubble at the low of the bar and set the text value of that chart bubble to the value of myRecursiveVariable. The if/then/else statement is properly structured, however it is accomplishing absolutely nothing because the very first occurrence of Valley will set that recursive variable to 1. It will always be greater than zero.

Summary

I have gone through all of this without checking any of this code in the platform. So this is just going from memory and my understanding of how the Thinkorswim language works. If you find any of my suggestions do not work just leave me a note in the comments section and I will make any required adjustments.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on August 7, 2019 3:25 pm
0
Pete, thank you for taking the time, to eloquently explain the 2 functions. To me the explanations by thinkorswim were lacking. I wish there examples were more thought out like yours. thanks again, I appreciate it.
( at August 8, 2019 10:26 am)