Certainly! Below is a basic Pine Script example that identifies Break of Structure (BOS) and Change of Character (CHOCH) events on a price chart. This script uses swing highs and lows to detect when price breaks previous structure points.
Explanation:
BOS (Break of Structure): When price breaks the previous swing high (in an uptrend) or swing low (in a downtrend).
CHOCH (Change of Character): When price breaks the opposite structure level indicating a potential trend reversal.
Pine Script v5 Example:
pinescript
//@version=5
indicator("BOS/CHOCH Detector", overlay=true)
// Input parameters
length = input.int(5, "Swing Length", minval=1)
// Function to find swing highs and lows
isSwingHigh(len) =>
high[len] < high[len-1] and
high[len-1] > high[len-2] and
high[len-1] > high[len] and
high[len-1] > high[len-3]
isSwingLow(len) =>
low[len] > low[len-1] and
low[len-1] < low[len-2] and
low[len-1] < low[len] and
low[len-1] < low[len-3]
// Arrays to store swing points
var float[] swingHighs = array.new_float()
var int[] swingHighBars = array.new_int()
var float[] swingLows = array.new_float()
var int[] swingLowBars = array.new_int()
// Detect swing highs and lows
if isSwingHigh(length)
array.push(swingHighs, high[length-1])
array.push(swingHighBars, bar_index - (length-1))
if isSwingLow(length)
array.push(swingLows, low[length-1])
array.push(swingLowBars, bar_index - (length-1))
// Get last two swing highs and lows
lastSwingHigh = array.size(swingHighs) > 0 ? array.get(swingHighs, array.size(swingHighs) - 1) : na
prevSwingHigh = array.size(swingHighs) > 1 ? array.get(swingHighs, array.size(swingHighs) - 2) : na
lastSwingLow = array.size(swingLows) > 0 ? array.get(swingLows, array.size(swingLows) - 1) : na
prevSwingLow = array.size(swingLows) > 1 ? array.get(swingLows, array.size(swingLows) - 2) : na
// Initialize variables to track structure
var string trend = "none"
var bool bosUp = false
var bool bosDown = false
var bool chochUp = false
var bool chochDown = false
// Detect BOS and CHOCH
if not na(lastSwingHigh) and not na(prevSwingHigh)
if lastSwingHigh > prevSwingHigh
bosUp := true
chochDown := false
trend := "up"
else
bosUp := false
if not na(lastSwingLow) and not na(prevSwingLow)
if lastSwingLow < prevSwingLow
bosDown := true
chochUp := false
trend := "down"
else
bosDown := false
// Detect CHOCH (Change of Character)
// CHOCH is when price breaks opposite structure level after BOS
if trend == "up" and not na(lastSwingLow) and not na(prevSwingLow)
if lastSwingLow < prevSwingLow
chochDown := true
trend := "down"
bosUp := false
if trend == "down" and not na(lastSwingHigh) and not na(prevSwingHigh)
if lastSwingHigh > prevSwingHigh
chochUp := true
trend := "up"
bosDown := false
// Plot signals on chart
plotshape(bosUp, title="BOS Up", location=location.abovebar, color=color.green, style=shape.triangleup, size=size.small, text="BOS↑")
plotshape(bosDown, title="BOS Down", location=location.belowbar, color=color.red, style=shape.triangledown, size=size.small, text="BOS↓")
plotshape(chochUp, title="CHOCH Up", location=location.abovebar, color=color.lime, style=shape.labelup, size=size.small, text="CHOCH↑")
plotshape(chochDown, title="CHOCH Down", location=location.belowbar, color=color.orange, style=shape.labeldown, size=size.small, text="CHOCH↓")
// Optional: Plot swing highs and lows for visualization
plotshape(not na(lastSwingHigh), location=location.abovebar, color=color.blue, style=shape.cross, size=size.tiny)
plotshape(not na(lastSwingLow), location=location.belowbar, color=color.purple, style=shape.cross, size=size.tiny)
How it works:
The script detects swing highs and lows using a simple 4-bar comparison.
It stores these swing points in arrays.
It compares the last two swing highs and lows to detect BOS and CHOCH.
BOS Up: New swing high breaks previous swing high.
BOS Down: New swing low breaks previous swing low.
CHOCH Up/Down: Opposite break indicating a potential trend reversal.
Signals are plotted on the chart as shapes with labels.
Notes:
This is a simplified approach and may need tuning for your specific strategy or timeframe.
You can adjust the length input to change sensitivity.
More advanced versions might include trend filters, volume confirmation, or multi-timeframe analysis.
If you want, I can help you customize or improve this script further!