Peak and Valley indicator

Brazilianpillar

New member
Can someone please help me write a code for a column that will change background color when the ShortReversalRange or Long ReversalRange is true?

Code:
# +--------------------------------------------------+
# |   Example showing how to hide a plot when the    |
# | most recent close is not within a certain range. |
# |                   robert payne                   |
# |              [rrpayne.blogspot.com]              |
# +--------------------------------------------------+

# | define a peak and plot it
input magnitude = 5;

def peak = high > Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakValue = if peak then high else peakValue[1];
plot peakLine = peakValue;
     peakLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     peakLine.SetDefaultColor(Color.green);

# | get the bar numbers for the most recent close
# | and the most recent peak
def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
def peakBar = if peak then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastPeakValue = GetValue(high, BarNumber() - HighestAll(peakBar));
def prevPeakValue = GetValue(peakValue[1], BarNumber() - HighestAll(peakBar));

# | find the value of the most recent close
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);

# | define what is considered to be "in range" of the previous peak
input percent = 0.5;

def inRange = mostRecentClose > (prevPeakValue * (1 - percent / 100)) and mostRecentClose < (prevPeakValue * (1 + percent / 100));

# | extend the most recent peak
plot lastPeakExtension = if BarNumber() >= HighestAll(peakBar) then lastPeakValue else Double.NaN;
     lastPeakExtension.SetDefaultColor(Color.light_green);
# | extend the previous peak only if the most recent close value is "in range"
plot prevPeakExtension = if BarNumber() >= HighestAll(peakBar) - 1 then prevPeakValue else Double.NaN;
     prevPeakExtension.SetDefaultColor(Color.Dark_Orange);
     prevPeakExtension.SetHiding(!inRange);

# | define a valley and plot it


def valley = low < lowest(low[1], magnitude) and low <= lowest(low[-magnitude], magnitude);
def valleyValue = if valley then low else valleyValue[1];
plot valleyLine = valleyValue;
     valleyLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     valleyLine.SetDefaultColor(Color.red);

# | get the bar numbers for the most recent close
# | and the most recent valley

def valleyBar = if valley then BarNumber() else Double.NaN;

# | find the values of the most recent peak and the one before it
def lastvalleyValue = GetValue(low, BarNumber() - lowestAll(valleyBar));
def prevvalleyValue = GetValue(valleyValue[1], BarNumber() - lowestAll(valleyBar));




# | define what is considered to be "in range" of the previous peak


def inRange2 = mostRecentClose < (prevValleyValue * (1 - percent / 100)) and mostRecentClose > (prevValleyValue * (1 + percent / 100));

# | extend the most recent peak
plot lastValleyExtension =  if BarNumber() <= lowestAll(valleyBar) then lastvalleyValue else Double.NaN;
     lastValleyExtension.SetDefaultColor(Color.plum);
# | extend the previous peak only if the most recent close value is "in range"
plot prevValleyExtension = if BarNumber() >= lowestAll(ValleyBar) - 1 then prevValleyValue else Double.NaN;
     prevValleyExtension.SetDefaultColor(Color.Dark_Orange);
     prevValleyExtension.SetHiding(!inRange2);

plot breakout = close crosses above peakline ;
#breakout.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     breakout.SetDefaultColor(Color.Green);
     breakout.SetLineWeight(2);
     breakout.HideBubble();
plot breakdown =  close crosses below valleyline ;
    # breakdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
     breakdown.SetDefaultColor(Color.red);
     breakdown.SetLineWeight(2);
     breakdown.HideBubble();


AddCloud(if peakline  > valleyline + .30 then valleyline else double.nan, valleyline + 0.11, CreateColor(255, 204, 204));
AddCloud(if peakline  > valleyline + .30 then peakline else double.nan, peakline - 0.11, CreateColor(204, 255, 204));

# paint entry candle if user so desires
input PaintEntryCandle = yes;

plot ShortReversalRange = close  crosses above peakline -.10 and  (peakline - .10) > (valleyline + 0.10);
ShortReversalRange.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
     ShortReversalRange.SetDefaultColor(Color.red);
     ShortReversalRange.SetLineWeight(2);
     ShortReversalRange.HideBubble();
plot LongReversalRange = close crosses below valleyline +.10 and  (peakline - .10) > (valleyline + 0.10);
    LongReversalRange.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     LongReversalRange.SetDefaultColor(Color.Green);
     LongReversalRange.SetLineWeight(2);
     LongReversalRange.HideBubble();

#AssignPriceColor(if PaintEntryCandle and  ShortReversalRange then Color.red else Color.CURRENT);
#AssignPriceColor(if PaintEntryCandle and LongReversalRange  then color.yellow else Color.CURRENT);
 
Last edited by a moderator:
Solution
That code in post 5, looks at 10 bars Before & After the current bar, to define a peak.
magnitude = 10;
you will need to use
within 11 bars
to possibly find one.

or change magnitude to a smaller number.

or edit the code formula for
peak=
so that it doesn't look at so many future bars.
def future = 3;
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-future], future);

scans and column studies want only 1 output function. there are 2 plots in post #5. more than 1 and the output may default to the 1st plot or just be wrong.
Thanks so much That sounds about right because i just scanned and yes it worked after 12 bars i guess i need to find another way to scan for bottoms reversing....

RobertPayne

Member
The code you are basing your peaks / valleys on is from when I was first getting started. The way it is constructed works in the past, but results in a Double.NaN on the right edge of the charts. That is to say, it doesn't have a value on the most recent candle. That's why your scan isn't working.

I have since developed a better way of identifying swing highs / lows (peaks / valleys). Please see this example. It will work for you.
 
How to interpret/understand "Peak to Valley DD: -433.10%" ?

I saw this on one of the posts here but I was confused on what it exactly means and how it should be interpreted. Can someone please explain? Thanks in advance!
 

dmillz

Member
Code:
input magnitude = 10;

# define and plot the most recent peak
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakvalue = if BarNumber() < magnitude then Double.NaN else if peak then high else Double.NaN;
plot peakline = peakvalue;
        peakline.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
        peakline.SetDefaultColor(Color.DOWNTICK);
        peakline.SetLineWeight(1);


# define and plot the most recent valley
def valley = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def valleyvalue = if BarNumber() < magnitude then Double.NaN else if valley then low else Double.NaN;
plot valleyline = valleyvalue;
        valleyline.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
        valleyline.SetDefaultColor(Color.UPTICK);
        valleyline.SetLineWeight(1);
 

halcyonguy

Well-known member
VIP
Lifetime
I am also having trouble scanning for reversal arrows.... I set it to TRUE within 4 bars and no results any help?
That code in post 5, looks at 10 bars Before & After the current bar, to define a peak.
magnitude = 10;
you will need to use
within 11 bars
to possibly find one.

or change magnitude to a smaller number.

or edit the code formula for
peak=
so that it doesn't look at so many future bars.
def future = 3;
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-future], future);

scans and column studies want only 1 output function. there are 2 plots in post #5. more than 1 and the output may default to the 1st plot or just be wrong.
 

dmillz

Member
That code in post 5, looks at 10 bars Before & After the current bar, to define a peak.
magnitude = 10;
you will need to use
within 11 bars
to possibly find one.

or change magnitude to a smaller number.

or edit the code formula for
peak=
so that it doesn't look at so many future bars.
def future = 3;
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-future], future);

scans and column studies want only 1 output function. there are 2 plots in post #5. more than 1 and the output may default to the 1st plot or just be wrong.
Thanks so much That sounds about right because i just scanned and yes it worked after 12 bars i guess i need to find another way to scan for bottoms reversing....
 
Solution

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