Higher Low Lower High

lance

New member
Hi - can someone create an indicator and a scan that shows when a higher low or lower high is created? The strength of the swing/pivot can be user-defined.
 

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

Hi - so based on swing/pivot strength i.e. a strength of 1 means the a bar is a high if it's higher than those 1 bar before and after it. strength of 2 means it's higher than those 2 bars before and after it, etc.

Does that help?
i think so. so you want to find a,
higher pivot valley
or lower pivot peak.

not just a high or a low.
 
Hi - can someone create an indicator and a scan that shows when a higher low or lower high is created? The strength of the swing/pivot can be user-defined.


this is an upper study for testing

look for peaks and valleys. look for ones that are above or below the previous one.
peaks, valleys, defined by x bars before and after current bar. default is 7
input length = 7;

can choose to ignore the last bar when looking for pivots
input ignore_last_bar = yes;

can choose to show dots on peaks valleys
input show_dots_on_peaks_valleys = yes;

can pick how many bars back from last bar, to look for a pivot. default is 4
input peak_valley_within_last_xbars = 4;

can pick any of 4 conditions to look for,
input look_for_peak_higher = yes;
input look_for_peak_lower = yes;
input look_for_valley_higher = yes;
input look_for_valley_lower = yes;


Code:
# find_next_peak_valley_00_upper

# https://usethinkscript.com/threads/higher-low-lower-high.13855/
# Higher Low , Lower High

#------------------------
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# define swing low points , robert payne
# modifified by halcyonguy to ignore last bar
#------------------------

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

input length = 7;
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbar) then 0 else 1;

def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

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

input show_dots_on_peaks_valleys = yes;
def vert = 0.001;

plot z1 = if show_dots_on_peaks_valleys and peak then high*(1+vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.POINTS);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(3);
z1.hidebubble();

plot z2 = if show_dots_on_peaks_valleys and valley then low*(1-vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.POINTS);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(3);
z2.hidebubble();

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

input peak_valley_within_last_xbars = 4;

# save high of peak to compare to next peak
def peak_lvl = if bn == 1 then 0
  else if peak then high
  else peak_lvl[1];

def peak_higher = (peak and peak_lvl[1] > 0 and peak_lvl > peak_lvl[1]);
def peak_lower = (peak and peak_lvl[1] > 0 and peak_lvl < peak_lvl[1]);
def pkhi_lastx = (sum(peak_higher, peak_valley_within_last_xbars) > 0);
def pklo_lastx = (sum(peak_lower, peak_valley_within_last_xbars) > 0); 

# save low of valley to compare to next valley
def valley_lvl = if bn == 1 then 0
  else if valley then low
  else valley_lvl[1];

def valley_higher = (valley and valley_lvl[1] > 0 and valley_lvl > valley_lvl[1]);
def valley_lower = (valley and valley_lvl[1] > 0 and valley_lvl < valley_lvl[1]);
def valhi_lastx = (sum(valley_higher, peak_valley_within_last_xbars) > 0);
def vallo_lastx = (sum(valley_lower, peak_valley_within_last_xbars) > 0);

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

input look_for_peak_higher = yes;
input look_for_peak_lower = yes;
input look_for_valley_higher = yes;
input look_for_valley_lower = yes;

def hipk = (if look_for_peak_higher and pkhi_lastx then 1 else 0);
def lopk = (if look_for_peak_lower and pklo_lastx then 1 else 0);
def hival = (if look_for_valley_higher and valhi_lastx then 1 else 0);
def loval = (if look_for_valley_lower and vallo_lastx then 1 else 0);

def scan = hipk or lopk or hival or loval;

addlabel(1, " ", color.black);
addlabel(1, (if scan then "" else "no ") + "pivot, within " + peak_valley_within_last_xbars + " bars", (if scan then color.white else color.gray));

addlabel(hipk, (if hipk then " > peak " else ""), color.green);
addlabel(hival, (if hival then " > valley " else ""), color.green);
addlabel(lopk, (if lopk then " < peak " else ""), color.red);
addlabel(loval, (if loval then " < valley " else ""), color.red);
addlabel(1, " ", color.black);
#

3 labels , list 2 recent pivots and the direction they moved
6w4anP6.jpg



------------------------


i think this will work for a scan. it has 1 output true or false

scan / lower study

Code:
# find_next_peak_valley_00_lower

# lower / scan

# https://usethinkscript.com/threads/higher-low-lower-high.13855/
# Higher Low Lower High

#------------------------
#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# define swing low points , robert payne
# modifified by halcyonguy to ignore last bar
#------------------------

declare lower;

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

input length = 7;
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

input ignore_last_bar = yes;
def ignorelast = if (ignore_last_bar and bn == lastbar) then 0 else 1;

def peak = ignorelast and high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = ignorelast and low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

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

#input show_dots_on_peaks_valleys = yes;
#def vert = 0.001;

#plot z1 = if show_dots_on_peaks_valleys and peak then high*(1+vert) else na;
#z1.SetPaintingStrategy(PaintingStrategy.POINTS);
#z1.SetDefaultColor(Color.cyan);
#z1.setlineweight(3);
#z1.hidebubble();

#plot z2 = if show_dots_on_peaks_valleys and valley then low*(1-vert) else na;
#z2.SetPaintingStrategy(PaintingStrategy.POINTS);
#z2.SetDefaultColor(Color.cyan);
#z2.setlineweight(3);
#z2.hidebubble();

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

input peak_valley_within_last_xbars = 4;

# save high of peak to compare to next peak
def peak_lvl = if bn == 1 then 0
  else if peak then high
  else peak_lvl[1];

def peak_higher = (peak and peak_lvl[1] > 0 and peak_lvl > peak_lvl[1]);
def peak_lower = (peak and peak_lvl[1] > 0 and peak_lvl < peak_lvl[1]);
def pkhi_lastx = (sum(peak_higher, peak_valley_within_last_xbars) > 0);
def pklo_lastx = (sum(peak_lower, peak_valley_within_last_xbars) > 0); 

# save low of valley to compare to next valley
def valley_lvl = if bn == 1 then 0
  else if valley then low
  else valley_lvl[1];

def valley_higher = (valley and valley_lvl[1] > 0 and valley_lvl > valley_lvl[1]);
def valley_lower = (valley and valley_lvl[1] > 0 and valley_lvl < valley_lvl[1]);
def valhi_lastx = (sum(valley_higher, peak_valley_within_last_xbars) > 0);
def vallo_lastx = (sum(valley_lower, peak_valley_within_last_xbars) > 0);

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

input look_for_peak_higher = yes;
input look_for_peak_lower = yes;
input look_for_valley_higher = yes;
input look_for_valley_lower = yes;

def hipk = (if look_for_peak_higher and pkhi_lastx then 1 else 0);
def lopk = (if look_for_peak_lower and pklo_lastx then 1 else 0);
def hival = (if look_for_valley_higher and valhi_lastx then 1 else 0);
def loval = (if look_for_valley_lower and vallo_lastx then 1 else 0);

def scan = hipk or lopk or hival or loval;
# add a plot for scanner output
plot z = scan;

#addlabel(1, " ", color.black);
#addlabel(1, (if scan then "" else "no ") + "pivot, within " + peak_valley_within_last_xbars + " bars", (if scan then color.white else color.gray));

#addlabel(hipk, (if hipk then " > peak " else ""), color.green);
#addlabel(hival, (if hival then " > valley " else ""), color.green);
#addlabel(lopk, (if lopk then " < peak " else ""), color.red);
#addlabel(loval, (if loval then " < valley " else ""), color.red);
#addlabel(1, " ", color.black);
#

PmhUIbM.jpg
 
thanks - can you add an option to not plot/scan peaks & valleys that have inside bars to the left and right of the bar being analyzed?

i.e. a peak is valid if the bars to the left and right of it have lower lows
ALSO a valley is valid if the bars to the left and right of it have higher highs
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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