Is there a way to get the highest of all variables or is the only way to use nested Max()?
def var1 = 1;
def var2 = 2;
def var3 = 3;
def var4 = ...... and so on....
#I want the next def to give me the highest value of all of these variables
def highestvar = ??
Ruby:def var1 = 1; def var2 = 3; def var3 = 2; def var4 = -1; def var5 = 0; def var6 = max(var1,max(var2,max(var3,max(var4,var5)))); def var7 = min(var1,min(var2,min(var3,min(var4,var5)))); addlabel(1,"max: " + var6 + " min: " + var7, color.yellow);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
declare lower;
def var1 = 5;
def var2 = 8;
def var3 = 2;
def var4 = 9;
def var5 = 7;
def line = if IsNaN(line[1]) then var1 else if line[1]==var1 then var2 else if line[1]==var2 then var3
else if line[1]==var3 then var4 else if line[1]==var4 then var5 else if line[1]==var5 then var1
else Double.nan;
def highest = HighestAll(line);
def lowest = LowestAll(line);
addlabel(yes,highest,color.white);
addlabel(yes,lowest,color.white);
that is a clever formula.@greco26
Def the variables under the same variable and use highestall() or lowestall().
Code:declare lower; def var1 = 5; def var2 = 8; def var3 = 2; def var4 = 9; def var5 = 7; def line = if IsNaN(line[1]) then var1 else if line[1]==var1 then var2 else if line[1]==var2 then var3 else if line[1]==var3 then var4 else if line[1]==var4 then var5 else if line[1]==var5 then var1 else Double.nan; def highest = HighestAll(line); def lowest = LowestAll(line); addlabel(yes,highest,color.white); addlabel(yes,lowest,color.white);
No reason to ever do it this way though....
Perhaps set to start counting backwards to barnumber = 1 or total bars for a day, hour, 30m etc.?that is a clever formula.
since the pattern of data is repeated, you only need to look at the last 5 values.
you could add a variable for the count of variables ,
def cnt = 5;
then use that as length in,
= lowest( line, cnt)
= highest( line, cnt)
and not have to use highestall, lowestall
.......
once you have a set of different variables copied to 1 variable, it becomes possible to apply some functions to the list.
i use a different method, that i will talk about one of these days.
maybe, but like svanoy said, you have to know the quantity of data beforehand.Perhaps set to start counting backwards to barnumber = 1 or total bars for a day, hour, 30m etc.?
Always the trick: I assume 390 bars per day on 1m times X number of days. One can set up conditionals based upon GetAggregation() for the number of bars to use, up to 30m or so (not sure 1h can agg correctly from daystart).maybe, but like svanoy said, you have to know the quantity of data beforehand.
just depends on what you are trying to do and on which bar you need to use the data.
side note, i use a reverse counter during inside bar code, to draw horizontal lines
https://usethinkscript.com/threads/gann-3-day-low-or-high.18875/#post-142298
any other way besides nesting by chance? I have that working but was looking for something more performance friendly.
if x > y then x else y
, and if thinkscript couldn't handle code that simple, we'd all be screwed. Given the example code provided, I see no reason to avoid the nested method, nor do I see how any alternative method could be an improvement. Any performance hit you might be experiencing is very likely elsewhere in the script. Could you tell us more about the script that you're actually working on?Unless you have like 20 variables huh?@greco26
Def the variables under the same variable and use highestall() or lowestall().
Code:declare lower; def var1 = 5; def var2 = 8; def var3 = 2; def var4 = 9; def var5 = 7; def line = if IsNaN(line[1]) then var1 else if line[1]==var1 then var2 else if line[1]==var2 then var3 else if line[1]==var3 then var4 else if line[1]==var4 then var5 else if line[1]==var5 then var1 else Double.nan; def highest = HighestAll(line); def lowest = LowestAll(line); addlabel(yes,highest,color.white); addlabel(yes,lowest,color.white);
No reason to ever do it this way though....
why not?Unless you have like 20 variables huh?
No reason actually. I tried it out and it seems like a pretty easy nesting method tbh.why not?
i used max with 40 varibles in this, all in one formula.
sometimes i will break it out into several smaller max formulas.
https://usethinkscript.com/threads/tradingview-poor-mans-volume-profile.14460/#post-119999
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
H | Future bars - find Highest price and lowest price | Questions | 4 | |
![]() |
Highest High N Lowest Low with the corresponding bar number | Questions | 1 | |
C | Cumulative Highest High Calculation | Questions | 2 | |
T | Previous High is the highest bar within 5 bars ago | Questions | 2 | |
J | Highest Price Volume Line within a Candle? | Questions | 1 |
Start a new thread and receive assistance from our community.
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.
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.