Label to show next support or resistance closest to last price

Raul

New member
Good day. I am trying to create a label showing the closest support price in relation to the last price of the stock. The support prices are based in several studies like Moving Averages 20, 50 periods and lows of 20, 50 periods etc. However, once I have defined the bunch of values, my knowledge doesnt reach the part where I can write the code for Thinkscript to show the closest price to the downside (of the list of studies) related to the last price of the stock. Here the code I have up until now:

def x = Min(close, lowest (close, 20));
def y = Min(close, lowest (close, 63));
def a = Min(close,highest (close, 20));
def b = Min(close,highest (close, 63));
def c = Min(close, MovingAverage(averageType.simPLE, aggregationPeriod.week, 200));
def d = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.week, 21));
def e = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.week, 5));
def f = Min(close, MovingAverage(averageType.simPLE, aggregationPeriod.day, 200));
def g = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.day, 21));
def h = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.day, 5));

I understand that there is a MAX and MIN function in thinkscript that could help achieve the goal, but I dont really understand how to do it. In MS Excel there is a function that handles the issue (MAX and IF) but I havent been able to do it in thinkscript. Any help is appreciated.
 
Solution
Good day. I am trying to create a label showing the closest support price in relation to the last price of the stock. The support prices are based in several studies like Moving Averages 20, 50 periods and lows of 20, 50 periods etc. However, once I have defined the bunch of values, my knowledge doesnt reach the part where I can write the code for Thinkscript to show the closest price to the downside (of the list of studies) related to the last price of the stock. Here the code I have up until now:

def x = Min(close, lowest (close, 20));
def y = Min(close, lowest (close, 63));
def a = Min(close,highest (close, 20));
def b = Min(close,highest (close, 63));
def c = Min(close, MovingAverage(averageType.simPLE...
Good day. I am trying to create a label showing the closest support price in relation to the last price of the stock. The support prices are based in several studies like Moving Averages 20, 50 periods and lows of 20, 50 periods etc. However, once I have defined the bunch of values, my knowledge doesnt reach the part where I can write the code for Thinkscript to show the closest price to the downside (of the list of studies) related to the last price of the stock. Here the code I have up until now:

def x = Min(close, lowest (close, 20));
def y = Min(close, lowest (close, 63));
def a = Min(close,highest (close, 20));
def b = Min(close,highest (close, 63));
def c = Min(close, MovingAverage(averageType.simPLE, aggregationPeriod.week, 200));
def d = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.week, 21));
def e = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.week, 5));
def f = Min(close, MovingAverage(averageType.simPLE, aggregationPeriod.day, 200));
def g = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.day, 21));
def h = Min(close, MovingAverage(averageType.expoNENTIAL, aggregationPeriod.day, 5));

I understand that there is a MAX and MIN function in thinkscript that could help achieve the goal, but I dont really understand how to do it. In MS Excel there is a function that handles the issue (MAX and IF) but I havent been able to do it in thinkscript. Any help is appreciated.

see if this is close to what you want

it finds the signal closest to close and above it,
and finds the signal closest to close and below it.


Code:
# find_closest_value_to_close_00

#https://usethinkscript.com/threads/label-to-show-next-support-or-resistance-closest-to-last-price.13943/
#Label to show next support or resistance closest to last price

#def x = Min(close, Lowest (close, 20));
#def y = Min(close, Lowest (close, 63));
#def a = Min(close, Highest (close, 20));
#def b = Min(close, Highest (close, 63));
#def c = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.WEEK, 200));
#def d = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 21));
#def e = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 5));
#def f = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.DAY, 200));
#def g = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 21));
#def h = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 5));

#-------------------

def na = double.nan;
def cls = close;
def aggw = AggregationPeriod.WEEK;
def aggd = AggregationPeriod.DAY;

def a = Lowest(close, 20);
def b = Lowest(close, 63);
def c = Highest(close, 20);
def d = Highest(close, 63);
def e = MovingAverage(AverageType.SIMPLE, close(period = aggw), 200);
def f = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 21);
def g = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 5);
def h = MovingAverage(AverageType.SIMPLE, close(period = aggd), 200);
def i = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 21);
def j = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 5);


# signal diff to close
def a2 = (a - cls);
def b2 = (b - cls);
def c2 = (c - cls);
def d2 = (d - cls);
def e2 = (e - cls);
def f2 = (f - cls);
def g2 = (g - cls);
def h2 = (h - cls);
def i2 = (i - cls);
def j2 = (j - cls);


def y = 1000;

# find signal closest and above close
# above , up
def au = if a >= cls then a2 else y;
def bu = if b >= cls then b2 else y;
def cu = if c >= cls then c2 else y;
def du = if d >= cls then d2 else y;
def eu = if e >= cls then e2 else y;
def fu = if f >= cls then f2 else y;
def gu = if g >= cls then g2 else y;
def hu = if h >= cls then h2 else y;
def iu = if i >= cls then i2 else y;
def ju = if j >= cls then j2 else y;

def abovemin = Min(au, Min(bu, Min(cu, Min(du, Min(eu, Min(fu, Min(gu, Min(hu, Min(iu, ju)))))))));


def minup;
if au == abovemin then {
  minup = 1;
} else if bu == abovemin then {
  minup = 2;
} else if cu == abovemin then {
  minup = 3;
} else if du == abovemin then {
  minup = 4;
} else if eu == abovemin then {
  minup = 5;
} else if fu == abovemin then {
  minup = 6;
} else if gu == abovemin then {
  minup = 7;
} else if hu == abovemin then {
  minup = 8;
} else if iu == abovemin then {
  minup = 9;
} else if ju == abovemin then {
  minup = 10;
} else {
  minup = 99;
}


#---------------------------

# find signal closest and below
# below , down
def ad = if a <= cls then absvalue(a2) else y;
def bd = if b <= cls then absvalue(b2) else y;
def cd = if c <= cls then absvalue(c2) else y;
def dd = if d <= cls then absvalue(d2) else y;
def ed = if e <= cls then absvalue(e2) else y;
def fd = if f <= cls then absvalue(f2) else y;
def gd = if g <= cls then absvalue(g2) else y;
def hd = if h <= cls then absvalue(h2) else y;
def id = if i <= cls then absvalue(i2) else y;
def jd = if j <= cls then absvalue(j2) else y;

def belowmin = Min(ad, Min(bd, Min(cd, Min(dd, Min(ed, Min(fd, Min(gd, Min(hd, Min(id, jd))))))))); 

#--------------------------


def mindwn;
if ad == belowmin then {
  mindwn = 1;
} else if bd == belowmin then {
  mindwn = 2;
} else if cd == belowmin then {
  mindwn = 3;
} else if dd == belowmin then {
  mindwn = 4;
} else if ed == belowmin then {
  mindwn = 5;
} else if fd == belowmin then {
  mindwn = 6;
} else if gd == belowmin then {
  mindwn = 7;
} else if hd == belowmin then {
  mindwn = 8;
} else if id == belowmin then {
  mindwn = 9;
} else if jd == belowmin then {
  mindwn = 10;
} else {
  mindwn = 99;
}

#---------------------------

addlabel(1, " ", color.black);
addlabel(1, abovemin + " closest above, signal # " + minup, color.green);
addlabel(1, belowmin + " closest below, signal # " + mindwn, color.red);


#---------------------------

#---------------------------

addlabel(1, " ", color.black);
input test_values = yes;
addlabel(test_values, " price differences ", color.cyan);
addlabel(test_values, a2, color.cyan);
addlabel(test_values, b2, color.cyan);
addlabel(test_values, c2, color.cyan);
addlabel(test_values, d2, color.cyan);
addlabel(test_values, e2, color.cyan);
addlabel(test_values, f2, color.cyan);
addlabel(test_values, g2, color.cyan);
addlabel(test_values, h2, color.cyan);
addlabel(test_values, i2, color.cyan);
addlabel(test_values, j2, color.cyan);

addlabel(1, " ", color.black);
input test_adj_values = yes;
addlabel(test_adj_values, "values above close", color.lime);
addlabel(test_adj_values, au, color.lime);
addlabel(test_adj_values, bu, color.lime);
addlabel(test_adj_values, cu, color.lime);
addlabel(test_adj_values, du, color.lime);
addlabel(test_adj_values, eu, color.lime);
addlabel(test_adj_values, fu, color.lime);
addlabel(test_adj_values, gu, color.lime);
addlabel(test_adj_values, hu, color.lime);
addlabel(test_adj_values, iu, color.lime);
addlabel(test_adj_values, ju, color.lime);
addlabel(1, " ", color.black);


addlabel(1, " ", color.black);
addlabel(test_adj_values, "values below close", color.magenta);
addlabel(test_adj_values, ad, color.magenta);
addlabel(test_adj_values, bd, color.magenta);
addlabel(test_adj_values, cd, color.magenta);
addlabel(test_adj_values, dd, color.magenta);
addlabel(test_adj_values, ed, color.magenta);
addlabel(test_adj_values, fd, color.magenta);
addlabel(test_adj_values, gd, color.magenta);
addlabel(test_adj_values, hd, color.magenta);
addlabel(test_adj_values, id, color.magenta);
addlabel(test_adj_values, jd, color.magenta);
addlabel(1, " ", color.black);



# plot signals
input show_signals = no;
plot z1 = if show_signals then a else na;
plot z2 = if show_signals then b else na;
plot z3 = if show_signals then c else na;
plot z4 = if show_signals then d else na;
plot z5 = if show_signals then e else na;
plot z6 = if show_signals then f else na;
plot z7 = if show_signals then g else na;
plot z8 = if show_signals then h else na;
plot z9 = if show_signals then i else na;
plot z10 = if show_signals then j else na;

#

f4tBRTo.jpg
 
Solution
see if this is close to what you want

it finds the signal closest to close and above it,
and finds the signal closest to close and below it.


Code:
# find_closest_value_to_close_00

#https://usethinkscript.com/threads/label-to-show-next-support-or-resistance-closest-to-last-price.13943/
#Label to show next support or resistance closest to last price

#def x = Min(close, Lowest (close, 20));
#def y = Min(close, Lowest (close, 63));
#def a = Min(close, Highest (close, 20));
#def b = Min(close, Highest (close, 63));
#def c = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.WEEK, 200));
#def d = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 21));
#def e = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.WEEK, 5));
#def f = Min(close, MovingAverage(AverageType.SIMPLE, AggregationPeriod.DAY, 200));
#def g = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 21));
#def h = Min(close, MovingAverage(AverageType.EXPONENTIAL, AggregationPeriod.DAY, 5));

#-------------------

def na = double.nan;
def cls = close;
def aggw = AggregationPeriod.WEEK;
def aggd = AggregationPeriod.DAY;

def a = Lowest(close, 20);
def b = Lowest(close, 63);
def c = Highest(close, 20);
def d = Highest(close, 63);
def e = MovingAverage(AverageType.SIMPLE, close(period = aggw), 200);
def f = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 21);
def g = MovingAverage(AverageType.EXPONENTIAL, close(period = aggw), 5);
def h = MovingAverage(AverageType.SIMPLE, close(period = aggd), 200);
def i = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 21);
def j = MovingAverage(AverageType.EXPONENTIAL, close(period = aggd), 5);


# signal diff to close
def a2 = (a - cls);
def b2 = (b - cls);
def c2 = (c - cls);
def d2 = (d - cls);
def e2 = (e - cls);
def f2 = (f - cls);
def g2 = (g - cls);
def h2 = (h - cls);
def i2 = (i - cls);
def j2 = (j - cls);


def y = 1000;

# find signal closest and above close
# above , up
def au = if a >= cls then a2 else y;
def bu = if b >= cls then b2 else y;
def cu = if c >= cls then c2 else y;
def du = if d >= cls then d2 else y;
def eu = if e >= cls then e2 else y;
def fu = if f >= cls then f2 else y;
def gu = if g >= cls then g2 else y;
def hu = if h >= cls then h2 else y;
def iu = if i >= cls then i2 else y;
def ju = if j >= cls then j2 else y;

def abovemin = Min(au, Min(bu, Min(cu, Min(du, Min(eu, Min(fu, Min(gu, Min(hu, Min(iu, ju)))))))));


def minup;
if au == abovemin then {
  minup = 1;
} else if bu == abovemin then {
  minup = 2;
} else if cu == abovemin then {
  minup = 3;
} else if du == abovemin then {
  minup = 4;
} else if eu == abovemin then {
  minup = 5;
} else if fu == abovemin then {
  minup = 6;
} else if gu == abovemin then {
  minup = 7;
} else if hu == abovemin then {
  minup = 8;
} else if iu == abovemin then {
  minup = 9;
} else if ju == abovemin then {
  minup = 10;
} else {
  minup = 99;
}


#---------------------------

# find signal closest and below
# below , down
def ad = if a <= cls then absvalue(a2) else y;
def bd = if b <= cls then absvalue(b2) else y;
def cd = if c <= cls then absvalue(c2) else y;
def dd = if d <= cls then absvalue(d2) else y;
def ed = if e <= cls then absvalue(e2) else y;
def fd = if f <= cls then absvalue(f2) else y;
def gd = if g <= cls then absvalue(g2) else y;
def hd = if h <= cls then absvalue(h2) else y;
def id = if i <= cls then absvalue(i2) else y;
def jd = if j <= cls then absvalue(j2) else y;

def belowmin = Min(ad, Min(bd, Min(cd, Min(dd, Min(ed, Min(fd, Min(gd, Min(hd, Min(id, jd)))))))));

#--------------------------


def mindwn;
if ad == belowmin then {
  mindwn = 1;
} else if bd == belowmin then {
  mindwn = 2;
} else if cd == belowmin then {
  mindwn = 3;
} else if dd == belowmin then {
  mindwn = 4;
} else if ed == belowmin then {
  mindwn = 5;
} else if fd == belowmin then {
  mindwn = 6;
} else if gd == belowmin then {
  mindwn = 7;
} else if hd == belowmin then {
  mindwn = 8;
} else if id == belowmin then {
  mindwn = 9;
} else if jd == belowmin then {
  mindwn = 10;
} else {
  mindwn = 99;
}

#---------------------------

addlabel(1, " ", color.black);
addlabel(1, abovemin + " closest above, signal # " + minup, color.green);
addlabel(1, belowmin + " closest below, signal # " + mindwn, color.red);


#---------------------------

#---------------------------

addlabel(1, " ", color.black);
input test_values = yes;
addlabel(test_values, " price differences ", color.cyan);
addlabel(test_values, a2, color.cyan);
addlabel(test_values, b2, color.cyan);
addlabel(test_values, c2, color.cyan);
addlabel(test_values, d2, color.cyan);
addlabel(test_values, e2, color.cyan);
addlabel(test_values, f2, color.cyan);
addlabel(test_values, g2, color.cyan);
addlabel(test_values, h2, color.cyan);
addlabel(test_values, i2, color.cyan);
addlabel(test_values, j2, color.cyan);

addlabel(1, " ", color.black);
input test_adj_values = yes;
addlabel(test_adj_values, "values above close", color.lime);
addlabel(test_adj_values, au, color.lime);
addlabel(test_adj_values, bu, color.lime);
addlabel(test_adj_values, cu, color.lime);
addlabel(test_adj_values, du, color.lime);
addlabel(test_adj_values, eu, color.lime);
addlabel(test_adj_values, fu, color.lime);
addlabel(test_adj_values, gu, color.lime);
addlabel(test_adj_values, hu, color.lime);
addlabel(test_adj_values, iu, color.lime);
addlabel(test_adj_values, ju, color.lime);
addlabel(1, " ", color.black);


addlabel(1, " ", color.black);
addlabel(test_adj_values, "values below close", color.magenta);
addlabel(test_adj_values, ad, color.magenta);
addlabel(test_adj_values, bd, color.magenta);
addlabel(test_adj_values, cd, color.magenta);
addlabel(test_adj_values, dd, color.magenta);
addlabel(test_adj_values, ed, color.magenta);
addlabel(test_adj_values, fd, color.magenta);
addlabel(test_adj_values, gd, color.magenta);
addlabel(test_adj_values, hd, color.magenta);
addlabel(test_adj_values, id, color.magenta);
addlabel(test_adj_values, jd, color.magenta);
addlabel(1, " ", color.black);



# plot signals
input show_signals = no;
plot z1 = if show_signals then a else na;
plot z2 = if show_signals then b else na;
plot z3 = if show_signals then c else na;
plot z4 = if show_signals then d else na;
plot z5 = if show_signals then e else na;
plot z6 = if show_signals then f else na;
plot z7 = if show_signals then g else na;
plot z8 = if show_signals then h else na;
plot z9 = if show_signals then i else na;
plot z10 = if show_signals then j else na;

#

f4tBRTo.jpg
Thank you very much. It is very close to what I was looking for. The truth is that the sintaxsys of the MIN/MAX function is quite complicated with all those parenthesis. I also found here in the forum a similar thread which can be also be useful in the matter: https://usethinkscript.com/threads/can-you-use-min-max-for-3-variables-or-more.11710/#post-101305
 
Thank you very much. It is very close to what I was looking for. The truth is that the sintaxsys of the MIN/MAX function is quite complicated with all those parenthesis. I also found here in the forum a similar thread which can be also be useful in the matter: https://usethinkscript.com/threads/can-you-use-min-max-for-3-variables-or-more.11710/#post-101305

the code from that link uses the same functions, min() max(). they just used many formulas , where i used 1.
def abovemin = Min(au, Min(bu, Min(cu, Min(du, Min(eu, Min(fu, Min(gu, Min(hu, Min(iu, ju)))))))));

min and max can only have 2 parameters. so you could create about 9 formulas to find a minimum, or put them together in 1 formula.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
405 Online
Create Post

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