# +--------------------------------------------------+
# | 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 = if close crosses above peakline then 1 else 0;
breakout.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
breakout.SetDefaultColor(Color.Green);
breakout.SetLineWeight(2);
breakout.HideBubble();
plot breakdown = if close crosses below valleyline then 1 else 0;
breakdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
breakdown.SetDefaultColor(Color.red);
breakdown.SetLineWeight(2);
breakdown.HideBubble();
AddCloud(valleyline, valleyline + 0.11, CreateColor(255, 204, 204));
AddCloud(peakline, peakline - 0.11, CreateColor(204, 255, 204));