1. Home
  2. Functions
  3. thinkScript Highest and Lowest Functions (With Examples)

thinkScript Highest and Lowest Functions (With Examples)

Did you know ThinkorSwim has built-in functions for finding the highest or lowest value of some variable or function? It’s true!

In this guide, you will learn how to utilize the Highest() and Lowest() functions in your thinkScript code.

Some indicators involve finding breakout areas. The Highest and Lowest functions are often used to identify those extreme values.

thinkScript Highest() Function

The Highest() function in thinkScript returns the highest value of a condition or variable for the last specified bars.

Highest() syntax and parameters

The syntax for the Highest() function is Highest(source, length);

  • Source: The highest value from this data.
  • Length: The lookback period in which the highest value is found.

thinkScript Highest() Examples

Roll up your sleeves because we’re about to get our hands dirty.

Example 1: Plot highest high of the last 20 bars

input length = 20;
plot highestHigh = highest(high, length);

In the example above, we plot the highest high of the last 20 candles.

Example 2: Highest volume bar

declare lower;
input length = 50;
plot vol = volume;
plot highestVol = highest(vol, length);

Instead of using the price as our data source, this time we use volume.

The highest volume of the last 50 bars is plotted.

Example 3: Highest of a function

Highest() can also work with functions. In the example below, we plot the 9-period simple moving average. Furthermore, we use the Highest() function to find the highest value of that simple moving average.

input length = 9;
plot sma = SimpleMovingAvg(close, length);
plot highestSMA = Highest(sma, length);

thinkScript Lowest() Function

To fetch the lowest value in thinkScript, we use the Lowest() function.

Lowest() syntax and parameters

Similar to its counterpart, the Lowest() function also includes two parameters.

Lowest(data, length);

The first is the data source from which to obtain the lowest value. The second parameter is the length of the calculation (lookback period or the number of bars).

thinkScript Lowest() Examples

The examples posted above are even applicable to the Lowest() function. You can replace Highest with Lowest for the desired outcome.

However, for learning, I will post some more examples below.

Example 4: Plot lowest in the first 5 mins

The script below plots the lowest value in the first 5 minutes of market open.

input time = 0930;
input limit = 5;
def startimer = secondsFromTime(time) <= 60 * limit;
rec data = if startimer then lowest(low) else data[1];
plot first_low = data;

Example 5: Plot lowest close

This script will give us the lowest close of the last 5 bars.

input length = 5;
plot lowestClose = Lowest(close, length);

Example 6: Plot the lowest low

Let’s end this post with a classic example. The code below will plot the lowest low of the last 25 bars on your ThinkorSwim chart.

input length = 25;
plot lowestClose = Lowest(low, length);


thinkScript Highest and Lowest functions are frequently used to find the extreme values of a condition or variable.

Their syntaxes contain two input parameters. One defines the data and the other represents the number of bars back on which the highest or lowest value is found.

Not enough to feed your craving? Keep scrolling for more examples!

Plot Highest High and Lowest Low Into the Future

## OneNote Archive Name: Highest High & Lowest Low Plotted Into the Future _JQ
## Archive Section: Uncategorized
## Suggested Tos Name using JQ naming convention: HighestHighLowestLowIntoFuture_JQ
## Script Label follows:
    addlabel(yes, "HighestHighLowestLowIntoFuture_JQ",color.WHITE);
## Archive Date: 11.11.2018
## Archive Notes: Loosely based on a script by Mobius
## End OneNote Archive Header
## Lounge Notes:
# Mobius: Where high always has a value, low starts out with 0. Therefor, you need an index-1 value for low. Using the first barnumber() low is usually the easiest thing to do.
input PlotChartHHLL = yes;
input DisplayPriceBubbleOnHiLowBar = yes;
input DisplayPriceBubbleOnRightEdge = No; 
input LowBubbleText = "Lowest Low";
input HighBubbleText = "HIghest High";
input includePriceInBubbles = no;
def o = open;
def h = high;
def l = low;
def c = close;
def v = volume;
def bar = BarNumber();
    #Lowest Low Code
def lowOnChart = 
if bar == 1
then low
else if low < lowOnChart[1]
then low
else lowOnChart[1];
def barWithChartLow = if l == lowOnChart
then bar
else Double.NaN;
def lowToExtend = if BarNumber() == HighestAll(barWithChartLow)  # changed this from highestAll
then lowOnChart
else lowToExtend[1];
plot lowestLowLine = if lowToExtend > 1 
then lowToExtend
else Double.NaN;   # was >1
AddChartBubble(bar == barWithChartLow and PlotChartHHLL and DisplayPriceBubbleOnHiLowBar, lowestLowLine, (if includePriceInBubbles > 0 then  LowBubbleText + " " + LowestLowLine else LowBubbleText), LowestLowLIne.takeValuecolor(),no);
AddChartBubble(barNumber() == highestAll(barnumber()) and PlotChartHHLL and DisplayPriceBubbleOnRightEdge, lowestLowLine,  (if includePriceInBubbles > 0 then  LowBubbleText + " " + LowestLowLine else LowBubbleText), Color.LIGHT_GRAY,no); 
    #Highest Hig Code
def HighOnChart = 
if bar == 1
then high
else if high > highOnChart[1]
then high
else highOnChart[1];
def barWithChartHigh = if high == HIghOnChart
then bar
else Double.NaN;
def HighToExtend = if BarNumber() == HighestAll(barWithChartHigh)  # changed this from highestAll
then HIghOnChart
else HighToExtend[1];
plot HighestHighLine = if HighToExtend > 1 
then HighToExtend
else Double.NaN;   # was >1

AddChartBubble(bar == barWithChartHigh and PlotChartHHLL and DisplayPriceBubbleOnHiLowBar, highestHighLine, (if includePriceInBubbles > 0 then  HighBubbleText + " " + HighestHighLine else HIghBubbleText), HighestHighLine.takevaluecolor(),YES);

AddChartBubble(barNumber() == highestAll(barnumber()) and PlotChartHHLL and DisplayPriceBubbleOnRightEdge, HighestHighLine,  (if includePriceInBubbles > 0 then  HighBubbleText + " " + HIghestHighLine else HighBubbleText), Color.LIGHT_GRAY,YES); 
# End Code 
    # JQ debug
input JQ_debug = no;
addlabel(JQ_debug, "ONlow: " + lowOnChart, color.PINK);
addlabel(JQ_debug, "barWithChartLow: " + barWithChartLow, color.PINK);
addlabel(JQ_debug, "lowToExtend: " + lowToExtend, color.PINK);
addlabel(JQ_debug, "lowestLowLine: " +lowestLowLine, color.PINK);  

Find Highest High and Lowest Low in Last 21 bars

#Park - Chatroom Request by Lar
#Find Highest High and Lowest Low in last 21 bars; and then, the highest high and lowest low in the next 21 bars starting from the barnumbers for the high and low in the first 21 bars 
#Park seemed to indicate the higher the number of bars between the 2 highs and 2 lows, respectively, provides a better basis to determine future direction using the prior 21 bar's direction from their highest high/lowest low
#The expansion area on the right side of the chart needs to be set to zero for this script
def bn = HighestAll(BarNumber());
def lowdata = if BarNumber() < bn - 20 then Double.NaN else if BarNumber() == bn - 20 then low else if BarNumber() >= bn - 20 and low <= lowdata[1] then low else lowdata[1];
def lowdatabn = if low == LowestAll(lowdata) and BarNumber() >= bn - 20 then BarNumber() else Double.NaN;
def lowdata1 = if BarNumber() == lowdatabn then Lowest(low[1], 21) else Double.NaN;
def lowdata1bn = if low == LowestAll(lowdata1) and BarNumber() >= bn - 42 then BarNumber() else Double.NaN;
AddChartBubble(lowdatabn, low, BarNumber() + "\n" + "$" + "" + low, Color.GREEN, up = no);
AddChartBubble(lowdata1bn, low, BarNumber() + "\n" + "$" + "" + low, Color.RED, up = no);

def highdata = if BarNumber() < bn - 20 then Double.NaN else if BarNumber() == bn - 20 then high else if BarNumber() >= bn - 20 and high >= highdata[1] then high else highdata[1];
def highdatabn = if high == HighestAll(highdata) and BarNumber() >= bn - 20 then BarNumber() else Double.NaN;
def highdata1 = if BarNumber() == highdatabn then Highest(high[1], 21) else Double.NaN;
def highdata1bn = if high == HighestAll(highdata1) and BarNumber() >= bn - 42 then BarNumber() else Double.NaN;
AddChartBubble(highdatabn, high, BarNumber() + "\n" + "$" + "" + high, Color.GREEN, up = yes);
AddChartBubble(highdata1bn, high, BarNumber() + "\n" + "$" + "" + high, Color.RED, up = yes);

LowestClose using FOLD()

# Variable length test for lowest close in a variable lenght using FOLD expression
# Mobius
# Chat Room discussion 06.23.2020

def c = close;
def o = open;
def upCount = if c > c[1] 
              then upCount[1] + 1
              else upCount[1];
AddLabel(1, "UpCount = " + UpCount, Color.GREEN);
def currentBar = if isNaN(c[-1]) and !isNaN(c) then barNumber() else currentBar[1];
def llOpen = fold i = 0 to highestAll(upCount)
             with s = o
             do if GetValue(o, i) < s
                then GetValue(o, i)
                else s;
AddLabel(1, "lowest Open = " + llOpen, Color.GREEN);

# Show limits of test for low close
AddVerticalLine(barNumber() == highestAll(currentBar - upCount), "limits of upCount");
def barsBack = highestAll(upCount);
def barCount = if barnumber()== 1 
               then highestall(currentBar) 
               else if barCount[1] > 1 
                    then barCount[1] - 1 
                    else 0;
plot count =  if barCount <= barsback then barCount else double.nan;

Minor High and Low Indicator

# Copyright 2019 Scott J. Johnson (https://scottjjohnson.com)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#      http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
# MinorHighLowIndicator
# Mark days with the highest high or lowest low in past/future N periods.
declare upper;
input interval = 9;
def isMinorHigh = fold i = -interval to interval+1 with isHigh = 1 do (if getValue(high, i) > high then 0 else isHigh);
def isMinorLow = fold j = -interval to interval+1 with isLow = 1 do (if getValue(low, j) < low then 0 else isLow);
AddChartBubble(isMinorHigh == 1, high, RoundUp(high, 0), Color.LIGHT_GRAY, yes);
AddChartBubble(isMinorLow == 1, low, RoundDown(low, 0), Color.LIGHT_GRAY, no);

Unlock the Power of ThinkorSwim

Get the latest news and updates on TD Ameritrade, ThinkorSwim indicators, thinkScript tutorials, and trading strategies delivered straight to your inbox every week.

We don’t spam! Unsubscribe at anytime.

4 thoughts on “thinkScript Highest and Lowest Functions (With Examples)

  1. Great work. Will greatly appreciate if you can add FIB levels to last 20 high /low script. And length as input parameters. Thank you.

  2. Hi Melvin,

    How to use the highest function if I want to check if RSI from 1 bars ago is higher than highest of last n bars?
    Code below does not produce the above result…

    input rsiLength = 14;
    input rsiAverageType = AverageType.WILDERS;

    def rsi = reference RSI(length = rsiLength, averageType = rsiAverageType);

    plot highRSI = RSI from 1 bars ago > Highest (RSI, 20);


  3. Hello Melvin, I’m new to using and trying to code in thinkScript, how would I begin to code to look a period any period, say the last 50 bars and pick out the lows during that period, say it has 4 low periods within that 50day period, can lowest low be used to reference those 4 periods? Any help would be appreciated. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *