# Highest of all variables

#### greco26

##### Active member
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 = ??

#### SleepyZ

##### Well-known member
VIP
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 = ??

Nesting is one way

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);``````

#### greco26

##### Active member
any other way besides nesting by chance? I have that working but was looking for something more performance friendly.

#### Svanoy

##### Active member
@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);

No reason to ever do it this way though....

#### halcyonguy

##### Well-known member
VIP
@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);

No reason to ever do it this way though....
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.

#### Svanoy

##### Active member
@halcyonguy
Defining a count works if you know the number of variables beforehand,
The formula could also be written to accept new variables if needed.

#### greco26

##### Active member
Guys, thank you so much for this. As always, you guys are so amazing!!

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?