Whats wrong with this code? The value of High1 is zero?

Ray503

New member
input length = 5;

def Close0 = close[0];
def Close1 = close[1];
def Close2 = close[2];
def Close3 = close[3];
def Close4 = close[4];
def Close5 = close[5];
def Close6 = close[6];
def Close7 = close[7];

def inc0 = Close0 - Close1;
def inc1 = Close1 - Close2;
def inc2 = Close2 - Close3;
def inc3 = Close3 - Close4;
def inc4 = Close4 - Close5;
def inc5 = Close5 - Close6;
def inc6 = Close6 - Close7;

def High1 = if inc1 > inc2 then High1 == inc1 else High1 == inc2;

plot UpperLevel = High1;
 

Pensar

Well-known member
VIP
Lifetime
@Ray503 Could you please post what you're want to do with this code? Just glancing at it, there looks to be a few things wrong, including that there is a length input that is not being used anywhere in the code.
 

Pensar

Well-known member
VIP
Lifetime
@Ray503 I put this code together. I'm not 100% certain that its what you're looking for, but it might give you an idea or two.

Code:
declare lower;

input n = 10;

def l = low(period = "day");
def h = high(period = "day");
def lo = lowest(l, n);
def hi = highest(h, n);

plot dif = hi - lo;
 

Ray503

New member
Thank you but no thats not what I'm looking for I looking to take the closes of the last 10 days. Subtract the current day close I previous day close do this for the last 10 days then get the highest Range (differance) and the lowest.
 
You will need to use code similar to whats below to get Arrows to paint at the close.


Code:
def vCloseClose = absValue(close-close[1]);

def x = Highest(vcloseclose, 10);
def a = lowest(vCloseClose,10);

def z = if(absValue(close - close[1])) ==a then 1 else double.nan;
def y = if (absvalue(close - close[1])) == x then 1 else double.nan;

#plots

plot py = y;
plot pz = z;

#Plot Styles

py.setpaintingstrategy(paintingStrategy.BOOLEAN_ARROW_UP);
py.setlineWeight(3);

pz.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
pz.setlineweight(3);
 

Ray503

New member
Thanks but that's not even close to what I'm trying to do. I'm trying to Subtract yesterday close from todays close, for the last 10 days. Then get the highest of those results and the lowest.
 
This code draws horizontal lines in a lower study. Red is lowest value and Green is highest value.

Code:
Declare lower;

# Definitions

def vCloseClose = absValue(close-close[1]);

def x = Highest(vcloseclose, 10);
def a = lowest(vCloseClose,10);



#plots

plot Plowest = a;
plot PHighest = x;

#Plot Styles

plowest.setpaintingStrategy(paintingStrategy.HORIZONTAL);
Phighest.setpaintingStrategy(paintingstrategy.horIZONTAL);

Plowest.setdefaultColor(color.red);
phighest.setdefaultColor(color.green);
 

Ray503

New member
Like try to make this simple

def High1 = if inc1 > inc2 then High1 == inc1 else High1 == inc2

plot UpperLevel = High1

When I run this code High1 is 0 or blank. Yes I double checked inc1 and inc2 has the correct data. Is there a problem with my code? or is there a bug in think or swim which is what I read. Is there another way to do this as a work around?
 

OBW

Member
This line of code is wrong:
Ruby:
 def High1 = if inc1 > inc2 then High1 == inc1 else High1 == inc2

The double equal sign "==" is not for assigning a value to a variable. It is used as a comparison. It means, "Is the value on the left of == the same as the value on the right?" So, here's what your code is really doing.

If inc1 is greater than inc2 then check to see if the value of High1 is the same as the value of inc1 if it is then assign a value of 1 (true). If it isn't the same then assign a value of 0 (false). Otherwise (if inc1 is not greater than inc2) then check to see if the value of High1 is the same as the value of inc2 if it is then assign a value of 1 (true). If it isn't the same then assign a value of 0 (false).

Since High1 doesn't have a value yet (it is just in process of being defined) then it will never be the same as inc1 or inc2 and will always return as 0 (false).

It should be re-written to:
Ruby:
def High1 = if inc1 > inc2 then inc1 else inc2;

Now, changing that line of code only fixes the semantics of that line. It still doesn't accomplish what you say you are trying to do which is find the highest / lowest change in price over the past 10 days. Here's how you would accomplish that.

Ruby:
input length = 10;
def net_change = close - close[1];
def highest_change = highest(net_change, length);
def lowest_change = lowest(net_change, length);

nxA4DLw.png
 
Last edited:
R

RickAns

Guest
Thank you OBW for such a great explanation on that bit of code and explaining how it works in everyday English. I wish more 'guides' on thinkscript were as easy to understand as what you have just written. If the Thinkorswim Learning Center was written more this way then I believe I would have a much better grasp on coding. Would be helpful for those of us beginners learning to program our thinkscripts. (y)
 
  • Like
Reactions: OBW

Ray503

New member
def TP = 100;
def TP2 = 50;
def TP3 = 25;

plot UpperLevel = highest_change;

plot LowerLevel = lowest_change;

plot rangeBands = highest_change - lowest_change;

// Not sure how to do this statement, I just want to add 100 to the close then if rangeBands are < 500 but > 200 TP2+Close but if rangeBands are < 200 then TP3+Close

if rangeBands > 500 then TP = TP+Close else
 

Ray503

New member
This line of code is wrong:
Ruby:
 def High1 = if inc1 > inc2 then High1 == inc1 else High1 == inc2

The double equal sign "==" is not for assigning a value to a variable. It is used as a comparison. It means, "Is the value on the left of == the same as the value on the right?" So, here's what your code is really doing.

If inc1 is greater than inc2 then check to see if the value of High1 is the same as the value of inc1 if it is then assign a value of 1 (true). If it isn't the same then assign a value of 0 (false). Otherwise (if inc1 is not greater than inc2) then check to see if the value of High1 is the same as the value of inc2 if it is then assign a value of 1 (true). If it isn't the same then assign a value of 0 (false).

Since High1 doesn't have a value yet (it is just in process of being defined) then it will never be the same as inc1 or inc2 and will always return as 0 (false).

It should be re-written to:
Ruby:
def High1 = if inc1 > inc2 then inc1 else inc2;

Now, changing that line of code only fixes the semantics of that line. It still doesn't accomplish what you say you are trying to do which is find the highest / lowest change in price over the past 10 days. Here's how you would accomplish that.

Ruby:
input length = 10;
def net_change = close - close[1];
def highest_change = highest(net_change, length);
def lowest_change = lowest(net_change, length);

nxA4DLw.png
def TP = 100;
def TP2 = 50;
def TP3 = 25;

plot UpperLevel = highest_change;

plot LowerLevel = lowest_change;

plot rangeBands = highest_change - lowest_change;

// Not sure how to do this statement, I just want to add 100 to the close then if rangeBands are < 500 but > 200 TP2+Close but if rangeBands are < 200 then TP3+Close

if rangeBands > 500 then TP = TP+Close else
 

Ray503

New member
How do I do this?

if rangeBands > 500 then TP = TP+Close

if rangeBands < 500 then TP = TP1+Close

if rangeBands <500 and rangeBands > 200 then TP = TP2+Close
 

Ray503

New member
Ruby

Can you please help?

How do I do this?

if rangeBands > 500 then TP = TP+Close

if rangeBands < 500 then TP = TP1+Close

if rangeBands <500 and rangeBands > 200 then TP = TP2+Close
 

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top