Largest price range in set number of days.

lzstock

New member
I am looking for an indicator that will show the largest price range for a set period on a stock. It would show a label on the screen. So if the largest price range for the last 30 is $10.00 it would show that value. Any ideas?
 
subtract the low from the high, then use highest() to find the biggest value.

this study looks back at x bars, and find the 1 candle with the biggest difference between the high and low.

Code:
# bigrange_same_candle_01
# https://usethinkscript.com/threads/largest-price-range-in-set-number-of-days.6789/

input look_back_bars = 30;
input show_arrows = yes;
input show_big_range_values = yes;

def na = double.nan;
#def bn = barnumber();
def rng = (high  - low);

def bigrng = highest(rng, look_back_bars);

addlabel(1, "recent big range " + round(bigrng,2), color.yellow);

plot z = if show_arrows then (bigrng == rng) else na;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z.AssignValueColor(color.yellow);

plot w = if (show_big_range_values and bigrng == rng) then rng else na;
w.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
w.AssignValueColor(color.yellow);
w.hidebubble();


PvPztng.jpg
 
Hi to all!
I use on a daily chart a version of highest/lowest prices study taken from the initial "VWAP Anchored_v02" study and need your help with a few questions, please:

1
Input barnumber value is 30 in my case (less values do more noise), which works ok on the far left part of the chart, but not good within the latest 30 days. Fe on the screensave 2 grey boxes show values missed.
Do you have adjustment to fix it?
UecjwjL.jpg


2
Can you please change AddChartBubble for value only?

Stydy:

###

input n = 30;
input ticks = 2.0;

def bnOK = barNumber() > n;

def isHigher = fold i = 1 to n + 1 with p = 1
while p do high > GetValue(high, -i);
def HH = if bnOK and isHigher
and high == Highest(high, n)
then high else Double.NaN;

def isLower = fold j = 1 to n + 1 with q = 1
while q do low < GetValue(low, -j);
def LL = if bnOK and isLower
and low == Lowest(low, n)
then low else Double.NaN;

def PivH = if HH > 0 then HH else Double.NaN;
def PivL = if LL > 0 then LL else Double.NaN;

plot Up = !isNaN(PivL);
AddChartBubble(Up, low, low, Color.gray, no);

plot Dn = !isNaN(PivH);
AddChartBubble(Dn, high, high, Color.gray, yes);

###
 
subtract the low from the high, then use highest() to find the biggest value.

this study looks back at x bars, and find the 1 candle with the biggest difference between the high and low.

Code:
# bigrange_same_candle_01
# https://usethinkscript.com/threads/largest-price-range-in-set-number-of-days.6789/

input look_back_bars = 30;
input show_arrows = yes;
input show_big_range_values = yes;

def na = double.nan;
#def bn = barnumber();
def rng = (high  - low);

def bigrng = highest(rng, look_back_bars);

addlabel(1, "recent big range " + round(bigrng,2), color.yellow);

plot z = if show_arrows then (bigrng == rng) else na;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z.AssignValueColor(color.yellow);

plot w = if (show_big_range_values and bigrng == rng) then rng else na;
w.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
w.AssignValueColor(color.yellow);
w.hidebubble();


PvPztng.jpg
I have been looking for a script that will do this so thank you for posting. Do you know why I would be getting an error message that says "Addlable is not allowed in this context"?
 
I have been looking for a script that will do this so thank you for posting. Do you know why I would be getting an error message that says "Addlable is not allowed in this context"?

sorry, i don't know why. i just loaded the original on my screen and it works.
i copied the code above and pasted it into a new study, and it worked.

try coping the code again.
trying to think of what might cause it...
are you using a huge number for look back bars?
are you looking at a stock that just had an ipo?
are you looking at a chart with a big time fram, month, quarter...?
 
I am looking for an indicator that will show the largest price range for a set period on a stock. It would show a label on the screen. So if the largest price range for the last 30 is $10.00 it would show that value. Any ideas?

here is an updated version, that can also look at future bars
can pick to look at past bars, future bars, or both

Ruby:
# bigrange_same_candle_02

def na = double.nan;
def bn = barnumber();

input look_type = { default back, forward, both };

input look_back_bars = 20;
input look_forward_bars = 20;
input show_big_range_arrows = yes;
input show_big_range_values = yes;

def rng = (high  - low);
def bigrng_back = highest(rng, look_back_bars);

def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(look_forward_bars - 1, lastBar - bn);
def bigrng_forward = GetValue(highest(rng, look_forward_bars), -offset);


def bigrng;
def rtype;
switch (look_type) {
case back:
  bigrng = bigrng_back;
  rtype = -1;
case forward:
  bigrng = bigrng_forward;
  rtype = 1;
case both:
  bigrng = if bigrng_back > bigrng_forward then bigrng_back else bigrng_forward;
  rtype = 0;
}


def bigrng2 = if bn == 1 then 0 else if (bigrng == rng) then bigrng else bigrng2[1];

plot z = if show_big_range_arrows then (bigrng == rng) else na;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z.AssignValueColor(color.yellow);

plot w = if (show_big_range_values and bigrng == rng) then rng else na;
w.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
w.AssignValueColor(color.yellow);
w.hidebubble();

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

addlabel( (rtype <> 1), "back bars " + look_back_bars , color.yellow);
addlabel( (rtype <> -1), "forward bars " + look_forward_bars , color.yellow);
addlabel(1, "recent big range " + round(bigrng2,2), color.yellow);

#--------------------------------------
# ref code
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#  post#10  robert
#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
#def offset = Min(length - 1, lastBar - bn);
#def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);
#
 
Hi to all!
I use on a daily chart a version of highest/lowest prices study taken from the initial "VWAP Anchored_v02" study and need your help with a few questions, please:

1
Input barnumber value is 30 in my case (less values do more noise), which works ok on the far left part of the chart, but not good within the latest 30 days. Fe on the screensave 2 grey boxes show values missed.
Do you have adjustment to fix it?


this version will find peaks near the last bar.
your version 'missed' them because when the fold loop ran over bars after the last bar , it caused an error in ishigher.

i added a check to see if the folded bar was valid , to the while code lines

i don't know what you are asking for in question 2,

Ruby:
# peaks4_01

input n = 30;
input ticks = 2.0;

def bnOK = barNumber() > n;

def isHigher = fold i = 1 to n + 1
 with p = 1
 while p and !isnan(GetValue(high, -i))
 do high > GetValue(high, -i);

def HH = if bnOK and isHigher
and high == Highest(high, n)
then high else Double.NaN;

addchartbubble(0, low*0.998,
ishigher + "\n" +
hh
, color.yellow, no);

def isLower = fold j = 1 to n + 1
 with q = 1
 while q and !isnan(GetValue(high, -j))
 do low < GetValue(low, -j);

def LL = if bnOK and isLower
and low == Lowest(low, n)
then low else Double.NaN;

def PivH = if HH > 0 then HH else Double.NaN;
def PivL = if LL > 0 then LL else Double.NaN;

plot Up = !isNaN(PivL);
AddChartBubble(Up, low, low, Color.light_gray, no);

plot Dn = !isNaN(PivH);
AddChartBubble(Dn, high, high, Color.light_gray, yes);
#

here is another way to look for the highest value from past and future bars, without using fold
https://usethinkscript.com/threads/largest-price-range-in-set-number-of-days.6789/#post-89459
it uses a negative offset to go to a future bar, then use highest() to look backwards.
 
  • Like
Reactions: Tow
this version will find peaks near the last bar.
your version 'missed' them because when the fold loop ran over bars after the last bar , it caused an error in ishigher.

i added a check to see if the folded bar was valid , to the while code lines

i don't know what you are asking for in question 2,

Hi halcyonguy,

Thanks a lot for the corrections, that is what i need.
Last question is less important, just thought if there was a chance to use figures only instead of figures in bubbles, but this is nothing. )
 
Hi halcyonguy,

Thanks a lot for the corrections, that is what i need.
Last question is less important, just thought if there was a chance to use figures only instead of figures in bubbles, but this is nothing. )

aah, i get it.
you can use these plot parameters to put a number above or below a candle (but not text)

add this to end of my study
then comment out the bubble lines, by putting a # in front of them

Code:
# ------------------------------
def na = double.nan;
plot upnum = if !isNaN(PivL) then low else na;
upnum.SetPaintingStrategy(PaintingStrategy.VALUES_below);
upnum.setdefaultcolor(color.white);

plot dnnum = if !isNaN(Pivh) then high else na;
dnnum.SetPaintingStrategy(PaintingStrategy.VALUES_above); 
dnnum.setdefaultcolor(color.white);
 
  • Like
Reactions: Tow
sorry, i don't know why. i just loaded the original on my screen and it works.
i copied the code above and pasted it into a new study, and it worked.

try coping the code again.
trying to think of what might cause it...
are you using a huge number for look back bars?
are you looking at a stock that just had an ipo?
are you looking at a chart with a big time fram, month, quarter...?
Thank you so much for your reply. I went back in and copied it and it worked. I didn't create the new study correctly. It works great!!
 
aah, i get it.
you can use these plot parameters to put a number above or below a candle (but not text)

add this to end of my study
then comment out the bubble lines, by putting a # in front of them

Code:
# ------------------------------
def na = double.nan;
plot upnum = if !isNaN(PivL) then low else na;
upnum.SetPaintingStrategy(PaintingStrategy.VALUES_below);
upnum.setdefaultcolor(color.white);

plot dnnum = if !isNaN(Pivh) then high else na;
dnnum.SetPaintingStrategy(PaintingStrategy.VALUES_above);
dnnum.setdefaultcolor(color.white);
Hi Halcyonguy,
Thank for posting on the largest range bar (LRB) (y)
I'm also looking into something related to that in that is today high higher than the LRB high?
This is my code but working sometimes only...

Code:
input ArrDist = 0.2;
input LookBack = 20;

def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def RangeMax = high;
def RangeMin = low;
def Range = high - low;

def LRB = Highest(Range, LookBack);
def LRBHigh = GetValue(high, LRB, 0);
def LRBLow = GetValue(low, LRB, 0);

def Cover =
    if Cover[1] == 0 then
        if RangeMax >= LRBHigh then 1
        else 0
    else
        if RangeMax <= GetValue(LRBHigh, Cover[1] + 1, 0)
            then Cover[1] + 1
        else 0;


def plotHigh = High + Range * ArrDist;
plot CoverSignal =  if RangeMax >= LRBHigh then plotHigh else double.NaN;


CoverSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
CoverSignal.SetDefaultColor(GetColor(5));
CoverSignal.SetLineWeight(2);

Appreciate any feedback.

Thanks 🙏
 
Hi Halcyonguy,
Thank for posting on the largest range bar (LRB) (y)
I'm also looking into something related to that in that is today high higher than the LRB high?
This is my code but working sometimes only...

Code:
input ArrDist = 0.2;
input LookBack = 20;

def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def RangeMax = high;
def RangeMin = low;
def Range = high - low;

def LRB = Highest(Range, LookBack);
def LRBHigh = GetValue(high, LRB, 0);
def LRBLow = GetValue(low, LRB, 0);

def Cover =
    if Cover[1] == 0 then
        if RangeMax >= LRBHigh then 1
        else 0
    else
        if RangeMax <= GetValue(LRBHigh, Cover[1] + 1, 0)
            then Cover[1] + 1
        else 0;


def plotHigh = High + Range * ArrDist;
plot CoverSignal =  if RangeMax >= LRBHigh then plotHigh else double.NaN;


CoverSignal.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
CoverSignal.SetDefaultColor(GetColor(5));
CoverSignal.SetLineWeight(2);

Appreciate any feedback.

Thanks 🙏

glad my LRB helped you out.
honestly , i have no idea what LRB is. i look at so many codes, i can't remember them a day later.
next time, add a link to a post, by clicking on the post number in the upper right, copying the link, and pasting it. and don't use abbreviations, spell out the words.


i'm not sure what you are asking. your code seems way overly complicated for comparing 2 numbers?
if you want to compare a high to the highest number of the past 20 bars then ,
def ishigher = if high > lrb then 1 else 0;


the main problem is , you are putting a price value (LRB) in as an offset in getvalue.
def LRB = Highest(Range, LookBack);
def LRBHigh = GetValue(high, LRB, 0);
def LRBLow = GetValue(low, LRB, 0);


do you want to find the offset to some number in the past 20 bars?
maybe this function will help with that?
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/GetMaxValueOffset
 
Hi Halcyonguy,
Thanks for the speedy respond...
Was typing to u when I was up all night so yeah... forgot the link and the my question seem kind of incomplete...


The link is from this
https://usethinkscript.com/threads/largest-price-range-in-set-number-of-days.6789/#post-89459...

What I trying to do is
1. Find the largest range bar (LRB) 10 period back
2. Get the high of the largest range bar
3. Check if current high is higher than the largest range bar high
4. If yes, signal it either by an arrow or point...
5. I would like to use this to filter out stock that fit this condition...

Hope that clarify my question...

U know how it is... sometimes u go in a loop and keep digging in the wrong direction and making it complicated while actually there is a much simpler way...
I think maybe I'm on this route... o_O

Appreciate all help to point me in the right direction...
Again, thanks a million for helping...
 
Hi Halcyonguy,
Thanks for the speedy respond...
Was typing to u when I was up all night so yeah... forgot the link and the my question seem kind of incomplete...


The link is from this
https://usethinkscript.com/threads/largest-price-range-in-set-number-of-days.6789/#post-89459...

What I trying to do is
1. Find the largest range bar (LRB) 10 period back
2. Get the high of the largest range bar
3. Check if current high is higher than the largest range bar high
4. If yes, signal it either by an arrow or point...
5. I would like to use this to filter out stock that fit this condition...

Hope that clarify my question...

U know how it is... sometimes u go in a loop and keep digging in the wrong direction and making it complicated while actually there is a much simpler way...
I think maybe I'm on this route... o_O

Appreciate all help to point me in the right direction...
Again, thanks a million for helping...

untested, i think this will work.

Code:
#1. Find the largest range bar (LRB) 10 period back
input len = 10;
def hi_rng_off = GetMaxValueOffset(( high - low), len);

#2. Get the high of the largest range bar
def hi_rng_hi = getvalue( high, hi_rng_off);

#3. Check if current high is higher than the largest range bar high
def ishi_higher = if ( high > hi_rng_hi) then 1 else 0;

#4. If yes, signal it either by an arrow or point...
plot z = if ishi_higher then low else double.nan;
z.SetPaintingStrategy(PaintingStrategy.ARROW_UP); 
z.SetDefaultColor(Color.green); 
z.SetLineWeight(2);


#5. I would like to use this to filter out stock that fit this condition...

if this is to be used as a scan, delete the step #4 plot and use this.
plot z2 = ishi_higher;
 
Hi Halcyonguy,
Exactly what I wanted...
Totally not what I would be able to do without your help...

I'm still trying to understand about the GetMaxValueOffset after you mention it but after repeated reading TOS explanation, still could not figure out how it work... which is why I didn't couldn't/don't know to use it ... even after I see how u use it, still trying to figure this one out...

I think I get what the GetMaxValueOffset finally...
It does 2 thing,
1. Get the max. value of what is in the bracket...
eg. GetMaxValueOffset(close, 20) mean it get the max. close price 20 period ago
2. Then it index/locate this bar ref. to the current bar... which is if the highest close in the pass 20 bar is 5 bar from the current, the GetMaxValueOffset will return 5

I hope that is correct...

Anyway, thanks a million in helping out 🙏 (y)
 
Last edited:
Hi Halcyonguy,

I was trying to improve this code to auto the Lookback period in that when there is less than 20 bar available, Lookback period==5, else Lookback period==10 ...

I try both codes but doesn't seem to work ...
Def LookBack = if BarNumber() <20 then 5 else 10 ;
Def LookBack = if BarNumber() <=20 then LookBack==5 else LookBack==10 ;

Appreciates if anyone could shed some light on this ... :unsure:😅

Thanks
 
Hi Halcyonguy,

I was trying to improve this code to auto the Lookback period in that when there is less than 20 bar available, Lookback period==5, else Lookback period==10 ...

I try both codes but doesn't seem to work ...
Def LookBack = if BarNumber() <20 then 5 else 10 ;
Def LookBack = if BarNumber() <=20 then LookBack==5 else LookBack==10 ;

Appreciates if anyone could shed some light on this ... :unsure:😅

Thanks

sorry about not replying to other post.
i'm bad about following up sometimes.

--------------
EDIT , i think i misunderstood your question
you want to change a length for looking at past bars...


this is for looking at future bars
take a look at this .
look at offset = formula and how he uses it
https://usethinkscript.com/threads/...y-demand-zones-for-thinkorswim.172/#post-7048
 
Last edited:
Hi Halcyonguy,

No problem at all ... You have help in more way then u know, to me and to all the other here ...
And thank for taking the time to post some help for this ... really appreciates it... 👍

can you post your current code you want modified?

guessing a change will be something like this,


Code:
input length = 10;
def LRB;
if barnumber() < 20 then {
 LRB = Highest(Range, (length / 2));
} else {
 LRB = Highest(Range, length );
}
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
393 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