To highlight the last two swing high and low pivots on your "BoS and ChoCh Indicator," we can modify your existing code by adding labels or shapes at those pivot points. Below is the modified code with the necessary additions to highlight the last two high and low pivots:
//@version=5
indicator(title="BoS and ChoCh Indicator with Last Two Pivots Highlighted", shorttitle="BoS & ChoCh Indicator", overlay=true, max_lines_count=500, max_labels_count=500)
// Input parameters
swingLookback = input.int(title="Swing Lookback Period", defval=3, minval=1, maxval=20)
showBoS = input.bool(title="Show Break of Structure (BoS)", defval=true)
showChoCh = input.bool(title="Show Change of Character (ChoCh)", defval=true)
showLabels = input.bool(title="Show Labels", defval=true)
lineExtension = input.string(title="Line Extension", defval="Right", options=["None", "Right", "Both"])
higherTF = input.timeframe(title="Higher Timeframe", defval="", tooltip="Leave empty to use current timeframe")
highlightPivots = input.bool(title="Highlight Last Two High and Low Pivots", defval=true)
// Map line extension to extend parameter
extend_mode = switch lineExtension
"None" => extend.none
"Right" => extend.right
"Both" => extend.both
=> extend.right
// Function to detect swings and times
f_detectSwings(_swingLookback) =>
// Identify swing highs and lows
swh = ta.pivothigh(high, _swingLookback, _swingLookback)
int swhTime = na
if not na(swh)
swhTime := time[_swingLookback]
swl = ta.pivotlow(low, _swingLookback, _swingLookback)
int swlTime = na
if not na(swl)
swlTime := time[_swingLookback]
[swh, swhTime, swl, swlTime]
// Variables to store swing highs and lows
var float[] swingHighs = array.new_float()
var int[] swingHighTimes = array.new_int()
var float[] swingLows = array.new_float()
var int[] swingLowTimes = array.new_int()
// Get swings from higher timeframe if specified
[swh, swhTime, swl, swlTime] = request.security(syminfo.tickerid, higherTF == "" ? timeframe.period : higherTF, f_detectSwings(swingLookback), barmerge.gaps_off, barmerge.lookahead_off)
// Update swing highs and lows arrays
if not na(swh)
array.unshift(swingHighs, swh)
array.unshift(swingHighTimes, swhTime)
if array.size(swingHighs) > 10
array.pop(swingHighs)
array.pop(swingHighTimes)
if not na(swl)
array.unshift(swingLows, swl)
array.unshift(swingLowTimes, swlTime)
if array.size(swingLows) > 10
array.pop(swingLows)
array.pop(swingLowTimes)
// Determine the current trend
var string trend = na
if array.size(swingHighs) >= 2 and array.size(swingLows) >= 2
lastSwingHigh = array.get(swingHighs, 0)
prevSwingHigh = array.get(swingHighs, 1)
lastSwingLow = array.get(swingLows, 0)
prevSwingLow = array.get(swingLows, 1)
// If last swing high is higher than previous swing high and last swing low is higher than previous swing low
if lastSwingHigh > prevSwingHigh and lastSwingLow > prevSwingLow
trend := "Uptrend"
// If last swing high is lower than previous swing high and last swing low is lower than previous swing low
else if lastSwingHigh < prevSwingHigh and lastSwingLow < prevSwingLow
trend := "Downtrend"
else
trend := "Sideways"
// Variables to hold BoS and ChoCh lines and labels
var line bosLine = na
var label bosLabel = na
var line chochLine = na
var label chochLabel = na
// Variables to store event times
var int bosTime = na
var int chochTime = na
// Detect BoS and ChoCh based on trend
if trend == "Uptrend"
// Break of Structure (BoS) to the upside using High of candle
if showBoS and high > array.get(swingHighs, 0)
bosTime := time
// Delete previous BoS line and label
if not na(bosLine)
line.delete(bosLine)
if not na(bosLabel)
label.delete(bosLabel)
// Draw BoS line
bosLine := line.new(x1=array.get(swingHighTimes, 0), y1=array.get(swingHighs, 0), x2=bosTime, y2=array.get(swingHighs, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.green, width=2)
// Add label
if showLabels
bosLabel := label.new(x=bosTime, y=array.get(swingHighs, 0), text="BoS High", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny)
// Change of Character (ChoCh) to the downside
if showChoCh and low < array.get(swingLows, 0)
chochTime := time
// Delete previous ChoCh line and label
if not na(chochLine)
line.delete(chochLine)
if not na(chochLabel)
label.delete(chochLabel)
// Draw ChoCh line
chochLine := line.new(x1=array.get(swingLowTimes, 0), y1=array.get(swingLows, 0), x2=chochTime, y2=array.get(swingLows, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.orange, width=2)
// Add label
if showLabels
chochLabel := label.new(x=chochTime, y=array.get(swingLows, 0), text="ChoCh Low", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.orange, 0), textcolor=color.white, size=size.tiny)
// Reset trend
trend := "Downtrend"
else if trend == "Downtrend"
// Break of Structure (BoS) to the downside using Low of candle
if showBoS and low < array.get(swingLows, 0)
bosTime := time
// Delete previous BoS line and label
if not na(bosLine)
line.delete(bosLine)
if not na(bosLabel)
label.delete(bosLabel)
// Draw BoS line
bosLine := line.new(x1=array.get(swingLowTimes, 0), y1=array.get(swingLows, 0), x2=bosTime, y2=array.get(swingLows, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.red, width=2)
// Add label
if showLabels
bosLabel := label.new(x=bosTime, y=array.get(swingLows, 0), text="BoS Low", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny)
// Change of Character (ChoCh) to the upside
if showChoCh and high > array.get(swingHighs, 0)
chochTime := time
// Delete previous ChoCh line and label
if not na(chochLine)
line.delete(chochLine)
if not na(chochLabel)
label.delete(chochLabel)
// Draw ChoCh line
chochLine := line.new(x1=array.get(swingHighTimes, 0), y1=array.get(swingHighs, 0), x2=chochTime, y2=array.get(swingHighs, 0), xloc=xloc.bar_time, extend=extend_mode, color=color.purple, width=2)
// Add label
if showLabels
chochLabel := label.new(x=chochTime, y=array.get(swingHighs, 0), text="ChoCh High", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.purple, 0), textcolor=color.white, size=size.tiny)
// Reset trend
trend := "Uptrend"
// --- Display Current Trend Label on Top Right Corner of Chart ---
// Create a table to display the trend
var table trendTable = table.new(position.top_right, 1, 1, border_width = 1)
// Update the table with the current trend
if barstate.islast and not na(trend)
// Define background color based on trend
bgcolor = trend == "Uptrend" ? color.new(color.green, 80) : trend == "Downtrend" ? color.new(color.red, 80) : color.new(color.gray, 80)
// Update the table cell
table.cell(trendTable, 0, 0, text = "Current Trend:\n" + trend, text_color = color.white, bgcolor = bgcolor, text_halign = text.align_center, text_valign = text.align_center)
// --- Highlight Last Two High and Low Pivots ---
// Variables to hold swing high and low labels
var label shLabel1 = na
var label shLabel2 = na
var label slLabel1 = na
var label slLabel2 = na
// Delete previous labels
if not barstate.isrealtime and barstate.islast
if not na(shLabel1)
label.delete(shLabel1)
if not na(shLabel2)
label.delete(shLabel2)
if not na(slLabel1)
label.delete(slLabel1)
if not na(slLabel2)
label.delete(slLabel2)
if highlightPivots and array.size(swingHighs) >= 2
// Plot the last two swing highs
sh1 = array.get(swingHighs, 0)
sh1Time = array.get(swingHighTimes, 0)
shLabel1 := label.new(x=sh1Time, y=sh1, text="Swing High 1", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.small)
sh2 = array.get(swingHighs, 1)
sh2Time = array.get(swingHighTimes, 1)
shLabel2 := label.new(x=sh2Time, y=sh2, text="Swing High 2", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.small)
if highlightPivots and array.size(swingLows) >= 2
// Plot the last two swing lows
sl1 = array.get(swingLows, 0)
sl1Time = array.get(swingLowTimes, 0)
slLabel1 := label.new(x=sl1Time, y=sl1, text="Swing Low 1", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)
sl2 = array.get(swingLows, 1)
sl2Time = array.get(swingLowTimes, 1)
slLabel2 := label.new(x=sl2Time, y=sl2, text="Swing Low 2", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)
Explanation of Modifications:
-
Added Input Option:
- Introduced a new input parameter
highlightPivots, allowing you to enable or disable the highlighting of the last two high and low pivots.
highlightPivots = input.bool(title="Highlight Last Two High and Low Pivots", defval=true)
-
Created Variables for Labels:
- Declared variables to hold the labels for the last two swing highs and lows. This allows us to manage the labels effectively, ensuring we're only displaying the most recent ones.
var label shLabel1 = na
var label shLabel2 = na
var label slLabel1 = na
var label slLabel2 = na
-
Deleted Previous Labels:
- Before plotting new labels, we delete the previous ones. This prevents cluttering the chart with old labels and ensures only the last two pivots are highlighted.
if not barstate.isrealtime and barstate.islast
if not na(shLabel1)
label.delete(shLabel1)
if not na(shLabel2)
label.delete(shLabel2)
if not na(slLabel1)
label.delete(slLabel1)
if not na(slLabel2)
label.delete(slLabel2)
-
Plotted Labels for Last Two Swing Highs:
- Checked if there are at least two swing highs in the array. If so, we retrieve their values and times and create labels at those points.
if highlightPivots and array.size(swingHighs) >= 2
// Plot the last two swing highs
sh1 = array.get(swingHighs, 0)
sh1Time = array.get(swingHighTimes, 0)
shLabel1 := label.new(x=sh1Time, y=sh1, text="Swing High 1", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.small)
sh2 = array.get(swingHighs, 1)
sh2Time = array.get(swingHighTimes, 1)
shLabel2 := label.new(x=sh2Time, y=sh2, text="Swing High 2", xloc=xloc.bar_time, style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.small)
-
Plotted Labels for Last Two Swing Lows:
- Similar to the swing highs, we check for at least two swing lows and plot labels accordingly.
if highlightPivots and array.size(swingLows) >= 2
// Plot the last two swing lows
sl1 = array.get(swingLows, 0)
sl1Time = array.get(swingLowTimes, 0)
slLabel1 := label.new(x=sl1Time, y=sl1, text="Swing Low 1", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)
sl2 = array.get(swingLows, 1)
sl2Time = array.get(swingLowTimes, 1)
slLabel2 := label.new(x=sl2Time, y=sl2, text="Swing Low 2", xloc=xloc.bar_time, style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)
Note:
- The labels are designed to be dynamic, updating as new swing highs and lows are detected.
- The
size parameter for the labels is set to size.small for better visibility.
- The
label.delete() function is used to remove previous labels to keep the chart clean.
- The colors for the labels are set to red for swing highs and green for swing lows for easy differentiation.
Usage Instructions:
- Enabling/Disabling Pivot Highlighting:
- Use the input option
Highlight Last Two High and Low Pivots to toggle the visibility of the pivot highlights.
- Adjusting Swing Lookback Period:
- Modify the
Swing Lookback Period input to change the sensitivity of swing high and low detection.
- Viewing Break of Structure and Change of Character:
- Ensure the
Show Break of Structure (BoS) and Show Change of Character (ChoCh) options are enabled if you wish to see these features on your chart.
Visual Example:
- When applied, the chart will display labels for the last two swing highs and last two swing lows, marked as "Swing High 1," "Swing High 2," "Swing Low 1," and "Swing Low 2."
- The labels will update automatically as new swings are detected, always highlighting the most recent pivots.
By incorporating these changes, your indicator will now highlight the last two high and low pivots on the chart, enhancing your ability to analyze price action and market structure.