Create column based on 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:

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.

Bobbydigital83

Member
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!