OPEN-SOURCE SCRIPT
SMC BOS/CHoCH + Auto Fib (5m/any TF) durane

//version=6
indicator('SMC BOS/CHoCH + Auto Fib (5m/any TF)', overlay = true, max_lines_count = 200, max_labels_count = 200)
// --------- Inputs ----------
left = input.int(3, 'Pivot Left', minval = 1)
right = input.int(3, 'Pivot Right', minval = 1)
minSwingSize = input.float(0.0, 'Min swing size (price units, 0 = disabled)', step = 0.1)
fib_levels = input.string('0.0,0.236,0.382,0.5,0.618,0.786,1.0', 'Fibonacci levels (comma separated)')
show_labels = input.bool(true, 'Show BOS/CHoCH labels')
lookbackHighLow = input.int(200, 'Lookback for structure (bars)')
// Parse fib levels
strs = str.split(fib_levels, ',')
var array<float> fibs = array.new_float()
if barstate.isfirst
for s in strs
array.push(fibs, str.tonumber(str.trim(s)))
// --------- Find pivot highs / lows ----------
pHigh = ta.pivothigh(high, left, right)
pLow = ta.pivotlow(low, left, right)
// store last confirmed swings
var float lastSwingHighPrice = na
var int lastSwingHighBar = na
var float lastSwingLowPrice = na
var int lastSwingLowBar = na
if not na(pHigh)
// check min size
if minSwingSize == 0 or pHigh - nz(lastSwingLowPrice, pHigh) >= minSwingSize
lastSwingHighPrice := pHigh
lastSwingHighBar := bar_index - right
lastSwingHighBar
if not na(pLow)
if minSwingSize == 0 or nz(lastSwingHighPrice, pLow) - pLow >= minSwingSize
lastSwingLowPrice := pLow
lastSwingLowBar := bar_index - right
lastSwingLowBar
// --------- Detect BOS & CHoCH (simple robust logic) ----------
var int lastBOSdir = 0 // 1 = bullish BOS (price broke above), -1 = bearish BOS
var int lastBOSbar = na
var float lastBOSprice = na
// Look for price closes beyond last structural swings within lookback
// Bullish BOS: close > recent swing high
condBullBOS = not na(lastSwingHighPrice) and close > lastSwingHighPrice and bar_index - lastSwingHighBar <= lookbackHighLow
// Bearish BOS: close < recent swing low
condBearBOS = not na(lastSwingLowPrice) and close < lastSwingLowPrice and bar_index - lastSwingLowBar <= lookbackHighLow
bosTriggered = false
chochTriggered = false
if condBullBOS
bosTriggered := true
if lastBOSdir != 1
// if previous BOS direction was -1, this is CHoCH (change of character)
chochTriggered := lastBOSdir == -1
chochTriggered
lastBOSdir := 1
lastBOSbar := bar_index
lastBOSprice := close
lastBOSprice
if condBearBOS
bosTriggered := true
if lastBOSdir != -1
chochTriggered := lastBOSdir == 1
chochTriggered
lastBOSdir := -1
lastBOSbar := bar_index
lastBOSprice := close
lastBOSprice
// --------- Plot labels for BOS / CHoCH ----------
if bosTriggered and show_labels
if chochTriggered
label.new(bar_index, high, text = lastBOSdir == 1 ? 'CHoCH ↑' : 'CHoCH ↓', style = label.style_label_up, color = color.new(color.orange, 0), textcolor = color.white, yloc = yloc.abovebar)
else
label.new(bar_index, high, text = lastBOSdir == 1 ? 'BOS ↑' : 'BOS ↓', style = label.style_label_left, color = lastBOSdir == 1 ? color.green : color.red, textcolor = color.white, yloc = yloc.abovebar)
// --------- Auto Fibonacci drawing ----------
var array<line> fib_lines = array.new_line()
var array<label> fib_labels = array.new_label()
var int lastFibId = na
// Function to clear previous fibs
f_clear() =>
if array.size(fib_lines) > 0
for i = 0 to array.size(fib_lines) - 1
line.delete(array.get(fib_lines, i))
if array.size(fib_labels) > 0
for i = 0 to array.size(fib_labels) - 1
label.delete(array.get(fib_labels, i))
array.clear(fib_lines)
array.clear(fib_labels)
// Decide anchors for fib: if lastBOSdir==1 (bullish) anchor from lastSwingLow -> lastSwingHigh
// if lastBOSdir==-1 (bearish) anchor from lastSwingHigh -> lastSwingLow
if lastBOSdir == 1 and not na(lastSwingLowPrice) and not na(lastSwingHighPrice)
// bullish fib: low -> high
startPrice = lastSwingLowPrice
endPrice = lastSwingHighPrice
// draw
f_clear()
for i = 0 to array.size(fibs) - 1 by 1
lvl = array.get(fibs, i)
priceLevel = startPrice + (endPrice - startPrice) * lvl
ln = line.new(x1 = lastSwingLowBar, y1 = priceLevel, x2 = bar_index, y2 = priceLevel, xloc = xloc.bar_index, extend = extend.right, color = color.new(color.green, 60), width = 1, style = line.style_solid)
array.push(fib_lines, ln)
lab = label.new(bar_index, priceLevel, text = str.tostring(lvl * 100, '#.0') + '%', style = label.style_label_right, color = color.new(color.green, 80), textcolor = color.white, yloc = yloc.price)
array.push(fib_labels, lab)
if lastBOSdir == -1 and not na(lastSwingHighPrice) and not na(lastSwingLowPrice)
// bearish fib: high -> low
startPrice = lastSwingHighPrice
endPrice = lastSwingLowPrice
f_clear()
for i = 0 to array.size(fibs) - 1 by 1
lvl = array.get(fibs, i)
priceLevel = startPrice + (endPrice - startPrice) * lvl
ln = line.new(x1 = lastSwingHighBar, y1 = priceLevel, x2 = bar_index, y2 = priceLevel, xloc = xloc.bar_index, extend = extend.right, color = color.new(color.red, 60), width = 1, style = line.style_solid)
array.push(fib_lines, ln)
lab = label.new(bar_index, priceLevel, text = str.tostring(lvl * 100, '#.0') + '%', style = label.style_label_right, color = color.new(color.red, 80), textcolor = color.white, yloc = yloc.price)
array.push(fib_labels, lab)
// --------- Optional: plot lastSwing points ----------
plotshape(not na(lastSwingHighPrice) ? lastSwingHighPrice : na, title = 'LastSwingHigh', location = location.absolute, style = shape.triangledown, size = size.tiny, color = color.red, offset = 0)
plotshape(not na(lastSwingLowPrice) ? lastSwingLowPrice : na, title = 'LastSwingLow', location = location.absolute, style = shape.triangleup, size = size.tiny, color = color.green, offset = 0)
// --------- Alerts ----------
alertcondition(bosTriggered and lastBOSdir == 1, title = 'Bullish BOS', message = 'Bullish BOS detected on {{ticker}} @ {{close}}')
alertcondition(bosTriggered and lastBOSdir == -1, title = 'Bearish BOS', message = 'Bearish BOS detected on {{ticker}} @ {{close}}')
alertcondition(chochTriggered, title = 'CHoCH Detected', message = 'CHoCH detected on {{ticker}} @ {{close}}')
// End
indicator('SMC BOS/CHoCH + Auto Fib (5m/any TF)', overlay = true, max_lines_count = 200, max_labels_count = 200)
// --------- Inputs ----------
left = input.int(3, 'Pivot Left', minval = 1)
right = input.int(3, 'Pivot Right', minval = 1)
minSwingSize = input.float(0.0, 'Min swing size (price units, 0 = disabled)', step = 0.1)
fib_levels = input.string('0.0,0.236,0.382,0.5,0.618,0.786,1.0', 'Fibonacci levels (comma separated)')
show_labels = input.bool(true, 'Show BOS/CHoCH labels')
lookbackHighLow = input.int(200, 'Lookback for structure (bars)')
// Parse fib levels
strs = str.split(fib_levels, ',')
var array<float> fibs = array.new_float()
if barstate.isfirst
for s in strs
array.push(fibs, str.tonumber(str.trim(s)))
// --------- Find pivot highs / lows ----------
pHigh = ta.pivothigh(high, left, right)
pLow = ta.pivotlow(low, left, right)
// store last confirmed swings
var float lastSwingHighPrice = na
var int lastSwingHighBar = na
var float lastSwingLowPrice = na
var int lastSwingLowBar = na
if not na(pHigh)
// check min size
if minSwingSize == 0 or pHigh - nz(lastSwingLowPrice, pHigh) >= minSwingSize
lastSwingHighPrice := pHigh
lastSwingHighBar := bar_index - right
lastSwingHighBar
if not na(pLow)
if minSwingSize == 0 or nz(lastSwingHighPrice, pLow) - pLow >= minSwingSize
lastSwingLowPrice := pLow
lastSwingLowBar := bar_index - right
lastSwingLowBar
// --------- Detect BOS & CHoCH (simple robust logic) ----------
var int lastBOSdir = 0 // 1 = bullish BOS (price broke above), -1 = bearish BOS
var int lastBOSbar = na
var float lastBOSprice = na
// Look for price closes beyond last structural swings within lookback
// Bullish BOS: close > recent swing high
condBullBOS = not na(lastSwingHighPrice) and close > lastSwingHighPrice and bar_index - lastSwingHighBar <= lookbackHighLow
// Bearish BOS: close < recent swing low
condBearBOS = not na(lastSwingLowPrice) and close < lastSwingLowPrice and bar_index - lastSwingLowBar <= lookbackHighLow
bosTriggered = false
chochTriggered = false
if condBullBOS
bosTriggered := true
if lastBOSdir != 1
// if previous BOS direction was -1, this is CHoCH (change of character)
chochTriggered := lastBOSdir == -1
chochTriggered
lastBOSdir := 1
lastBOSbar := bar_index
lastBOSprice := close
lastBOSprice
if condBearBOS
bosTriggered := true
if lastBOSdir != -1
chochTriggered := lastBOSdir == 1
chochTriggered
lastBOSdir := -1
lastBOSbar := bar_index
lastBOSprice := close
lastBOSprice
// --------- Plot labels for BOS / CHoCH ----------
if bosTriggered and show_labels
if chochTriggered
label.new(bar_index, high, text = lastBOSdir == 1 ? 'CHoCH ↑' : 'CHoCH ↓', style = label.style_label_up, color = color.new(color.orange, 0), textcolor = color.white, yloc = yloc.abovebar)
else
label.new(bar_index, high, text = lastBOSdir == 1 ? 'BOS ↑' : 'BOS ↓', style = label.style_label_left, color = lastBOSdir == 1 ? color.green : color.red, textcolor = color.white, yloc = yloc.abovebar)
// --------- Auto Fibonacci drawing ----------
var array<line> fib_lines = array.new_line()
var array<label> fib_labels = array.new_label()
var int lastFibId = na
// Function to clear previous fibs
f_clear() =>
if array.size(fib_lines) > 0
for i = 0 to array.size(fib_lines) - 1
line.delete(array.get(fib_lines, i))
if array.size(fib_labels) > 0
for i = 0 to array.size(fib_labels) - 1
label.delete(array.get(fib_labels, i))
array.clear(fib_lines)
array.clear(fib_labels)
// Decide anchors for fib: if lastBOSdir==1 (bullish) anchor from lastSwingLow -> lastSwingHigh
// if lastBOSdir==-1 (bearish) anchor from lastSwingHigh -> lastSwingLow
if lastBOSdir == 1 and not na(lastSwingLowPrice) and not na(lastSwingHighPrice)
// bullish fib: low -> high
startPrice = lastSwingLowPrice
endPrice = lastSwingHighPrice
// draw
f_clear()
for i = 0 to array.size(fibs) - 1 by 1
lvl = array.get(fibs, i)
priceLevel = startPrice + (endPrice - startPrice) * lvl
ln = line.new(x1 = lastSwingLowBar, y1 = priceLevel, x2 = bar_index, y2 = priceLevel, xloc = xloc.bar_index, extend = extend.right, color = color.new(color.green, 60), width = 1, style = line.style_solid)
array.push(fib_lines, ln)
lab = label.new(bar_index, priceLevel, text = str.tostring(lvl * 100, '#.0') + '%', style = label.style_label_right, color = color.new(color.green, 80), textcolor = color.white, yloc = yloc.price)
array.push(fib_labels, lab)
if lastBOSdir == -1 and not na(lastSwingHighPrice) and not na(lastSwingLowPrice)
// bearish fib: high -> low
startPrice = lastSwingHighPrice
endPrice = lastSwingLowPrice
f_clear()
for i = 0 to array.size(fibs) - 1 by 1
lvl = array.get(fibs, i)
priceLevel = startPrice + (endPrice - startPrice) * lvl
ln = line.new(x1 = lastSwingHighBar, y1 = priceLevel, x2 = bar_index, y2 = priceLevel, xloc = xloc.bar_index, extend = extend.right, color = color.new(color.red, 60), width = 1, style = line.style_solid)
array.push(fib_lines, ln)
lab = label.new(bar_index, priceLevel, text = str.tostring(lvl * 100, '#.0') + '%', style = label.style_label_right, color = color.new(color.red, 80), textcolor = color.white, yloc = yloc.price)
array.push(fib_labels, lab)
// --------- Optional: plot lastSwing points ----------
plotshape(not na(lastSwingHighPrice) ? lastSwingHighPrice : na, title = 'LastSwingHigh', location = location.absolute, style = shape.triangledown, size = size.tiny, color = color.red, offset = 0)
plotshape(not na(lastSwingLowPrice) ? lastSwingLowPrice : na, title = 'LastSwingLow', location = location.absolute, style = shape.triangleup, size = size.tiny, color = color.green, offset = 0)
// --------- Alerts ----------
alertcondition(bosTriggered and lastBOSdir == 1, title = 'Bullish BOS', message = 'Bullish BOS detected on {{ticker}} @ {{close}}')
alertcondition(bosTriggered and lastBOSdir == -1, title = 'Bearish BOS', message = 'Bearish BOS detected on {{ticker}} @ {{close}}')
alertcondition(chochTriggered, title = 'CHoCH Detected', message = 'CHoCH detected on {{ticker}} @ {{close}}')
// End
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội Quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội Quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.