High Low Lookback for ThinkorSwim

korygill

Well-known member
VIP
I was working on a request from a friend for highlighting a bar a given number of bars back, and then tracking the subsequent highest high and lowest low from that bar.

This ties into some other work I can share later where you can see where the most recent bars are relative to a lookback period like you might use in a SMA or Stochastic, etc.

Here is what the study looks like on a Ford (F) 1Y 1D chart.

xbL10wh.png


Some things to notice:
  • labels
  • chart bubble at (high+low)/2 on the lookback bar
  • purple squares at high and low on the lookback bar
  • purple squares have Bubbles hidden (on the right vertical axis)
  • LOW chart bubble
  • LOW line
  • HIGH chart bubble
  • HIGH line
Keeping track of and calculating which bar numbers are meaningful is fairly straightforward, but did take me a few attempts to get right. Debugging with labels helps a lot to troubleshoot some issues, and plotting values can also be a good diagnostic tool.

Code for HighLowLookback

Code:
#
# HighLowLookback
#
# Study to show the highest high and lowest low after a given number of bars back.
#
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190710-2200-KG    - created
# ...
# ...
#

#
# Inputs
#
input length = 13;

#
# Common Variables that may also reduce calls to server
#
def vClose = close;
def vLow = low;
def vHigh = high;
def nan = double.NaN;

#
# Logic
#
def currentBarNumber = if !IsNaN(vClose) then BarNumber() else nan;
def lastBarNumber = HighestAll(currentBarNumber);
def lookbackBar = lastBarNumber - length + 1;
def doPlot = if currentBarNumber >= lookbackBar then 1 else 0;

def mostRecentHigh = CompoundValue(1, if doPlot && vHigh > mostRecentHigh[1] then vHigh else mostRecentHigh[1],double.NEGATIVE_INFINITY);
def highBarNumber  = CompoundValue(1, if doPlot && vHIgh > mostRecentHigh[1] then currentBarNumber else highBarNumber[1],0);
plot mrh = if currentBarNumber >= HighestAll(highBarNumber) then mostRecentHigh else nan;
mrh.SetDefaultColor(Color.GREEN);

def mostRecentLow = CompoundValue(1, if doPlot && vLow < mostRecentLow[1] then vLow else mostRecentLow[1],double.POSITIVE_INFINITY);
def lowBarNumber  = CompoundValue(1, if doPlot && vLow < mostRecentLow[1] then currentBarNumber else lowBarNumber[1],0);
plot mrl = if currentBarNumber >= HighestAll(lowBarNumber) then mostRecentLow else nan;
mrl.SetDefaultColor(Color.RED);

#
# Visualizations
#
AddChartBubble(
    currentBarNumber == HighestAll(highBarNumber),
    vHigh,
    "HIGH",
    Color.WHITE,
    yes);

AddChartBubble(
    currentBarNumber == HighestAll(lowBarNumber),
    vLow,
    "LOW",
    Color.WHITE,
    no);

AddChartBubble(
    currentBarNumber == lookbackBar,
    (vHigh+vLow)/2,
    length+"\nBar",
    Color.GRAY,
    yes);

plot bbH = if currentBarNumber == lookbackBar then vHigh + TickSize()*1 else nan;
bbH.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbH.SetDefaultColor(Color.MAGENTA);
bbH.SetLineWeight(5);
bbH.HideBubble();

plot bbL = if currentBarNumber == lookbackBar then vLow - TickSize()*1 else nan;
bbL.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbL.SetDefaultColor(Color.MAGENTA);
bbL.SetLineWeight(5);
bbL.HideBubble();

#
# Labels
#
AddLabel(yes,
"mostRecentHigh: " + mostRecentHigh,
Color.Gray);

AddLabel(yes,
"mostRecentLow: " + mostRecentLow,
Color.Gray);

Shareable Link to flexgrid and code
https://tos.mx/lNzBi8
 
Last edited by a moderator:

korygill

Well-known member
VIP
Hello can make a scanner from this great indicator
For a scanner, would need to know what it would scan for... Right now, this is just an example of how you can keep track of older bars, and carry those forward for new calculations. Maybe @BenTen has some plans for how he uses this into a study/scanner.
 

Jeff MulHolland

New member
Hi good day. I follow the $ VOLSPD movement What code do I have to use for my label to show if the $ VOLSPD candle is increasing in volume or decreasing?
Do you have any tutorials for this?
 

Robert_J

New member
@korygill, hi I try to use options to hide this study after I put this study in. It won't work or won't hide this study even if you check off "show study". Is there a fix for this issue? Thanks.
 

korygill

Well-known member
VIP
"show study" does not appear to hide chart bubbles and labels. you'd have to add a new input and add that to the conditions for those things (and the plots).
 

katsrx

New member
Lifetime
I was working on a request from a friend for highlighting a bar a given number of bars back, and then tracking the subsequent highest high and lowest low from that bar.

This ties into some other work I can share later where you can see where the most recent bars are relative to a lookback period like you might use in a SMA or Stochastic, etc.

Here is what the study looks like on a Ford (F) 1Y 1D chart.

xbL10wh.png


Some things to notice:
  • labels
  • chart bubble at (high+low)/2 on the lookback bar
  • purple squares at high and low on the lookback bar
  • purple squares have Bubbles hidden (on the right vertical axis)
  • LOW chart bubble
  • LOW line
  • HIGH chart bubble
  • HIGH line
Keeping track of and calculating which bar numbers are meaningful is fairly straightforward, but did take me a few attempts to get right. Debugging with labels helps a lot to troubleshoot some issues, and plotting values can also be a good diagnostic tool.

Code for HighLowLookback

Code:
#
# HighLowLookback
#
# Study to show the highest high and lowest low after a given number of bars back.
#
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190710-2200-KG    - created
# ...
# ...
#

#
# Inputs
#
input length = 13;

#
# Common Variables that may also reduce calls to server
#
def vClose = close;
def vLow = low;
def vHigh = high;
def nan = double.NaN;

#
# Logic
#
def currentBarNumber = if !IsNaN(vClose) then BarNumber() else nan;
def lastBarNumber = HighestAll(currentBarNumber);
def lookbackBar = lastBarNumber - length + 1;
def doPlot = if currentBarNumber >= lookbackBar then 1 else 0;

def mostRecentHigh = CompoundValue(1, if doPlot && vHigh > mostRecentHigh[1] then vHigh else mostRecentHigh[1],double.NEGATIVE_INFINITY);
def highBarNumber  = CompoundValue(1, if doPlot && vHIgh > mostRecentHigh[1] then currentBarNumber else highBarNumber[1],0);
plot mrh = if currentBarNumber >= HighestAll(highBarNumber) then mostRecentHigh else nan;
mrh.SetDefaultColor(Color.GREEN);

def mostRecentLow = CompoundValue(1, if doPlot && vLow < mostRecentLow[1] then vLow else mostRecentLow[1],double.POSITIVE_INFINITY);
def lowBarNumber  = CompoundValue(1, if doPlot && vLow < mostRecentLow[1] then currentBarNumber else lowBarNumber[1],0);
plot mrl = if currentBarNumber >= HighestAll(lowBarNumber) then mostRecentLow else nan;
mrl.SetDefaultColor(Color.RED);

#
# Visualizations
#
AddChartBubble(
    currentBarNumber == HighestAll(highBarNumber),
    vHigh,
    "HIGH",
    Color.WHITE,
    yes);

AddChartBubble(
    currentBarNumber == HighestAll(lowBarNumber),
    vLow,
    "LOW",
    Color.WHITE,
    no);

AddChartBubble(
    currentBarNumber == lookbackBar,
    (vHigh+vLow)/2,
    length+"\nBar",
    Color.GRAY,
    yes);

plot bbH = if currentBarNumber == lookbackBar then vHigh + TickSize()*1 else nan;
bbH.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbH.SetDefaultColor(Color.MAGENTA);
bbH.SetLineWeight(5);
bbH.HideBubble();

plot bbL = if currentBarNumber == lookbackBar then vLow - TickSize()*1 else nan;
bbL.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbL.SetDefaultColor(Color.MAGENTA);
bbL.SetLineWeight(5);
bbL.HideBubble();

#
# Labels
#
AddLabel(yes,
"mostRecentHigh: " + mostRecentHigh,
Color.Gray);

AddLabel(yes,
"mostRecentLow: " + mostRecentLow,
Color.Gray);

Shareable Link to flexgrid and code
https://tos.mx/lNzBi8
I was actually looking for something like this. but is there a way to add a code that changes automatically the input length to the desire time frame. Example when I look at the min, 5 min or 15 minute I want to see the last day period the high and low lines
 

tlee404

New member
I am trying to scan to select stocks with the High of the week Greater Than the High of week before.
To test it I wrote the following code, but it is not correctly working on the weekly chart.
It shows history bars even for week with lower high after and initial week with a new high.
When I use it a scan I get no result with all stocks.
I need help.

#test
declare lower;
Plot newhigh = high > high[-1];

This question was moved here from the new question area. Apparently I don't have enough skill to understand the solution listed previously.
Note that I assume there are only 2 Highs to compare, the current weeks High and the previous weeks high since I am using the weekly chart.
I am not looking to calculate each weeks high separately and then compare them as the days in a week varies.

I am assuming now that this is posted here it is not consider new and will not be seen until some one decides to review the the first topic listed.
 
Last edited:

tomsk

Well-known member
VIP
@tlee404 Sounds like you may be new to indexing stocks. Note that in your code snippet above you are comparing this week's high with next week's high. That's what the index "-1" means. In order to scan for stocks that have a weekly high GREATER THAN the high of the previous week, place the following code directly in the scanner

Code:
high > high[1]

Using this code on a Weekly aggregation, scanning against the S&P 500, the scan returned 364 results
 

tlee404

New member
@tomsk You are correct. This morning i just realized that I was using the negative sign in error. When I changed it to positive it worked.
Thank you for your correction
 

NewbieOver60

New member
Hi

I need help in writing script in ToS in counting the number of new highs set within certain period e.g. last 30 days. I could not find any counter function

Any help through tips, samples etc. will be appreciated.

Thanks ... Ratilal
 

Nikola

Member
Trying to scan for breakouts either from a recent swing high (look back 3 weeks)....or making a new 10 day high
 

Rome

New member
Can someone write me a CODE for showing either high or low price for one particular bar ?

Thank you
 

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