Certainly! The lag you're experiencing is due to the way the ta.pivothigh and ta.pivotlow functions work. They require a certain number of bars on both sides of the pivot to confirm it, which introduces a delay. To reduce this lag and include minor pivots, we can modify the swing detection logic to react more quickly to price movements.
Below is the updated code where I've adjusted the swing detection function to identify swings with minimal lag. This is done by checking if the current high or low is the highest or lowest over the specified lookback period without waiting for future bars to confirm the pivot.
//@version=5
indicator(title="BoS and ChoCh Indicator", shorttitle="BoS & ChoCh Indicator", overlay=true, max_lines_count=500, max_labels_count=500)
// Input parameters
swingLookback = input.int(title="Swing Lookback Period", defval=1, 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")
// 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 with minimal lag
f_detectSwings(_swingLookback) =>
// Identify swing highs and lows without lag
swh = high == ta.highest(high, _swingLookback) ? high : na
int swhTime = na
if not na(swh)
swhTime := time
swl = low == ta.lowest(low, _swingLookback) ? low : na
int swlTime = na
if not na(swl)
swlTime := time
[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
if showBoS and close > 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 to Downtrend
trend := "Downtrend"
else if trend == "Downtrend"
// Break of Structure (BoS) to the downside
if showBoS and close < 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 to Uptrend
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)
Explanation of Changes:
-
Reduced Swing Lookback Period: Changed the default swingLookback to 1 to detect more recent swings, including minor pivots. You can adjust this value to fine-tune the sensitivity.
-
Modified Swing Detection Function: Updated the f_detectSwings function to identify swings without waiting for future bars. It now checks if the current high is the highest over the lookback period or if the current low is the lowest, which minimizes lag.
swh = high == ta.highest(high, _swingLookback) ? high : na
swl = low == ta.lowest(low, _swingLookback) ? low : na
-
Immediate Trend Updates: With the new swing detection, the trend and BoS/ChoCh signals are updated more quickly, allowing the indicator to respond promptly to recent price movements.
Note: By increasing the sensitivity, you might get more signals, including minor swings. It's essential to test the indicator on different timeframes and adjust the swingLookback parameter to suit your trading style.
Recommendation:
-
Adjust Swing Lookback Period: Try experimenting with different swingLookback values (e.g., 1 to 5) to find a balance between responsiveness and the number of signals generated.
-
Combine with Other Indicators: For better accuracy, consider using this indicator in conjunction with other technical analysis tools to filter out false signals.
Hope this modification helps you achieve quicker detection of pivots with minimal lag!