Can you use MIN/MAX for 3 variables or more?

rottentrade

Member
Say I have 4 moving averages on the chart, eg. ma10, ma20, ma50, ma100.

For each day, I would like thinkscript to tell me which is the highest of the four. But instead of numerical value of the moving average, can i have it tell me "ma20" or "ma50" instead?
 
Solution
But then again, I see a bug. Why are the circled values both MA20?

RUnFy4f.jpg

Here is a quick fix that seems to work. I added an input offset to move the bubbles where you might want them.

Capture.jpg
Ruby:
#Sorting_MovingAverages_Max_Min

plot ma10  = Average(close, 10);
plot ma20  = Average(close, 20);
plot ma50  = Average(close, 50);
plot ma100 = Average(close, 100);

def maxavg = Max(ma10, Max(ma20, Max(ma50, ma100)));
def minavg = Min(ma10, Min(ma20, Min(ma50, ma100)));

def h  = if ma10 == maxavg
          then Max(ma20, Max(ma50, ma100)) else
         if ma20 == maxavg
          then Max(ma10, Max(ma50, ma100)) else
         if ma50 == maxavg
          then Max(ma10, Max(ma20, ma100))
         else Max(ma10, Max(ma20...
Is there a way to find the maximum or minimum value among 3 or more variables? (Yes, I know you can divide it up into 2 groups, but it's not so straightforward when using convoluted strategy)

plot maximum = max(open,high,low,close);
plot minimum = min(open,high,low,close);
-------------
error msg:
2 params expected but 4 found while calling max at 1:16
2 params expected but 4 found while calling min at 2:16
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Is there a way to find the maximum or minimum value among 3 or more variables? (Yes, I know you can divide it up into 2 groups, but it's not so straightforward when using convoluted strategy)

plot maximum = max(open,high,low,close);
plot minimum = min(open,high,low,close);
-------------
error msg:
2 params expected but 4 found while calling max at 1:16
2 params expected but 4 found while calling min at 2:16

This is one way to stack them:

Ruby:
plot maximum = max(open,max(high,max(low,close)));
plot minimum = min(open,min(high,min(low,close)));
addlabel(1,maximum + " " + minimum);
 
Say I have 4 moving averages on the chart, eg. ma10, ma20, ma50, ma100.

For each day, I would like thinkscript to tell me which is the highest of the four. But instead of numerical value of the moving average, can i have it tell me "ma20" or "ma50" instead?

Similar to your other max/min question, here is the max portion.

Ruby:
plot ma10 = average(close, 10);
plot ma20 = average(close, 20);
plot ma50 = average(close, 50);
plot ma100 = average(close, 100);

def maxavg = max(ma10,max(ma20,max(ma50,ma100)));
addlabel(1, if maxavg==ma10 then "ma10" else   
            if maxavg==ma20 then "ma20" else   
            if maxavg==ma50 then "ma50"
            else "ma100");
 
Thanks as always. I have another one to ask.

Now that we know the high (max) and the low (min), is there a way to SORT the four variables, from highest to the lowest?

Here are two sorting scripts for the 2 questions you posed. Both seem to work so far.

First is the hlco of a bar. Since high and low were respective maximum/minimum, this was a simpler process.

Capture.jpg
Ruby:
#Sorting_HLCO_Bar_Max_Min
def  maximum = Max(open, Max(high, Max(low, close)));
def  minimum = Min(open, Min(high, Min(low, close)));

def midhigh = Max(open, close);
def midlow  = Min(open, close);

AddChartBubble(IsNaN(close[-1]), low, "Sorted\n" + maximum + "\n" + midhigh + "\n" + midlow + "\n" + minimum, Color.WHITE, no);
AddChartBubble(IsNaN(close[-1]), low, "UnSorted\n" + high + "\n" + close + "\n" + open + "\n" + low, Color.YELLOW, no);

The Second one was 4 moving averages stacked Max to Min
Ruby:
#Sorting_MovingAverages_Max_Min
plot ma10  = Average(close, 10);
plot ma20  = Average(close, 20);
plot ma50  = Average(close, 50);
plot ma100 = Average(close, 100);

def maxavg = Max(ma10, Max(ma20, Max(ma50, ma100)));
def minavg = Min(ma10, Min(ma20, Min(ma50, ma100)));

def h  = if ma10 == maxavg
          then Max(ma20, Max(ma50, ma100)) else
         if ma20 == maxavg
          then Max(ma10, Max(ma50, ma100)) else
         if ma50 == maxavg
          then Max(ma10, Max(ma20, ma100))
         else Max(ma10, Max(ma20, ma50));
def h1 = if ma10 == maxavg and ma20 == maxavg
          then ma20
         else Max(ma50, ma100);
def h2 = Max(ma20, minavg);

AddLabel(1, if maxavg == ma10 then "ma10" else  
            if maxavg == ma20 then "ma20" else  
            if maxavg == ma50 then "ma50"
            else "ma100", Color.YELLOW);
AddLabel(1, if h == ma10 then "ma10" else  
            if h == ma20 then "ma20" else  
            if h == ma50 then "ma50"
            else "ma100", Color.YELLOW);
AddLabel(1, if Min(h1, h2) == ma10 then "ma10" else  
            if Min(h1, h2) == ma20 then "ma20" else  
            if Min(h1, h2) == ma50 then "ma50"
            else "ma100", Color.YELLOW);
AddLabel(1, if minavg == ma10 then "ma10" else  
            if minavg == ma20 then "ma20" else  
            if minavg == ma50 then "ma50"
            else "ma100", Color.YELLOW);



input bubblemover = 6;
def bm = bubblemover;
def bm1 = bm + 1;
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma10[bm1], "10:  " + ma10[bm1], ma10.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma20[bm1], "20:  " + ma20[bm1], ma20.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma50[bm1], "50:  " + ma50[bm1], ma50.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), ma100[bm1], "100:  " + ma100[bm1], ma100.TakeValueColor());

#Debug
Input debug = no;
AddChartBubble(debug and IsNaN(close[-1]), low, +maxavg + "\n" + h + "\n" + Min(h1, h2) + "\n" + minavg, Color.WHITE, no);
AddChartBubble(debug and IsNaN(close[-1]), low, ma10 + "\n" + ma20 + "\n" + ma50 + "\n" + ma100, Color.YELLOW, no);
;
 
But then again, I see a bug. Why are the circled values both MA20?

RUnFy4f.jpg

Here is a quick fix that seems to work. I added an input offset to move the bubbles where you might want them.

Capture.jpg
Ruby:
#Sorting_MovingAverages_Max_Min

plot ma10  = Average(close, 10);
plot ma20  = Average(close, 20);
plot ma50  = Average(close, 50);
plot ma100 = Average(close, 100);

def maxavg = Max(ma10, Max(ma20, Max(ma50, ma100)));
def minavg = Min(ma10, Min(ma20, Min(ma50, ma100)));

def h  = if ma10 == maxavg
          then Max(ma20, Max(ma50, ma100)) else
         if ma20 == maxavg
          then Max(ma10, Max(ma50, ma100)) else
         if ma50 == maxavg
          then Max(ma10, Max(ma20, ma100))
         else Max(ma10, Max(ma20, ma50));
def h1 = if ma10 == maxavg and ma20 == h
          then Max(ma50, ma100) else
         if ma10 == maxavg and ma50 == h   
          then Max(ma20, ma100) else
         if ma10 == maxavg and ma100 == h
          then Max(ma20, ma50) else
         if ma20 == maxavg and ma10 == h
          then Max(ma50, ma100) else
         if ma20 == maxavg and ma50 == h   
          then Max(ma10, ma100) else
         if ma20 == maxavg and ma100 == h
          then Max(ma20, ma50) else
         if ma50 == maxavg and ma10 == h
          then Max(ma20, ma100) else
         if ma50 == maxavg and ma20 == h   
          then Max(ma10, ma100) else
         if ma50 == maxavg and ma100 == h
          then Max(ma10, ma20) else
         if ma100 == maxavg and ma10 == h
          then Max(ma20, ma50) else
         if ma100 == maxavg and ma20 == h   
          then Max(ma10, ma50)
         else Max(ma10, ma20);

ma10.SetDefaultColor(Color.YELLOW);
ma10.SetStyle(Curve.MEDIUM_DASH);
ma10.SetLineWeight(2);
ma20.SetDefaultColor(Color.ORANGE);
ma20.SetStyle(Curve.MEDIUM_DASH);
ma20.SetLineWeight(2);
ma50.SetDefaultColor(Color.VIOLET);
ma50.SetStyle(Curve.MEDIUM_DASH);
ma50.SetLineWeight(2);
ma100.SetDefaultColor(Color.MAGENTA);
ma100.SetStyle(Curve.MEDIUM_DASH);
ma100.SetLineWeight(2);

input offset = .05;
AddChartBubble(1, high + offset, if minavg == ma10 then "ma10" else
            if minavg == ma20 then "ma20" else
            if minavg == ma50 then "ma50"
            else "ma100", if minavg == ma10 then Color.YELLOW else if minavg == ma20 then Color.ORANGE else if minavg == ma50 then Color.VIOLET else Color.MAGENTA
);
AddChartBubble(1, high + offset, if h1 == ma10 then "ma10" else
            if h1 == ma20 then "ma20" else
            if h1 == ma50 then "ma50"
            else "ma100", if h1 == ma10 then Color.YELLOW else if h1 == ma20 then Color.ORANGE else if h1 == ma50 then Color.VIOLET else Color.MAGENTA
);
AddChartBubble(1, high + offset, if h == ma10 then "ma10" else
            if h == ma20 then "ma20" else
            if h == ma50 then "ma50"
            else "ma100", if h == ma10 then Color.YELLOW else if h == ma20 then Color.ORANGE else if h == ma50 then Color.VIOLET else Color.MAGENTA
);
AddChartBubble(1, high + offset, if maxavg == ma10 then "ma10" else
            if maxavg == ma20 then "ma20" else
            if maxavg == ma50 then "ma50"
            else "ma100", if maxavg == ma10 then Color.YELLOW else if maxavg == ma20 then Color.ORANGE else if maxavg == ma50 then Color.VIOLET else Color.MAGENTA
);
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
338 Online
Create Post

Similar threads

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.
Back
Top