本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BOSWaves
//@version=6
indicator("Laguerre Structure Levels [BOSWaves]", overlay=true, max_lines_count=500, max_boxes_count=500, max_labels_count=500)
// ┌────────────────────────────── BOSWaves ─ Constants ──────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string g1 = "Trend Engine"
const string g2 = "Structure Levels"
const string g3 = "Signals & Display"
color BULL = #00FF66
color BEAR = #FF0066
// ┌────────────────────────────── BOSWaves ─ Tooltips ───────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
const string tt_src = "Price source used for the Laguerre trend calculation."
const string tt_gamma = "Laguerre smoothing factor. Higher values produce a smoother, slower trend."
const string tt_ribbon = "EMA length applied to the Laguerre filter to form the trend ribbon. The filter must be on the correct side of the ribbon for a trend flip."
const string tt_atr = "Number of bars used to calculate the Average True Range for the flip buffer, ribbon proximity, zone thickness, level merging, and break buffer."
const string tt_buffer = "ATR distance price must clear beyond the trend filter before direction can change."
const string tt_slope = "Number of bars back used to measure the Laguerre filter's slope. The filter must be rising for a bullish flip and falling for a bearish flip."
const string tt_swing = "Pivot length used to locate structural swing highs and lows."
const string tt_maxLevels = "Maximum number of structure levels kept on the chart. The oldest level is removed when a new one forms."
const string tt_pullback = "Maximum ATR distance a swing can form from the active trend ribbon and still become a continuation level."
const string tt_zone = "Thickness of swing structure zones as a fraction of ATR."
const string tt_merge = "Minimum ATR distance between levels of the same direction. A new swing closer than this to an existing level does not create a new level."
const string tt_break = "ATR distance price must close beyond a level's zone before the level is removed."
const string tt_originAge = "Maximum number of bars old the last opposing swing can be to create a trend origin level when a new trend begins."
const string tt_origin = "Creates a stronger structural level from the last opposing swing when a new trend begins."
const string tt_cont = "Creates levels from swings that form near the trend ribbon during an active trend, marking pullback structure in the trend direction."
const string tt_retests = "Marks the first return into an active structure level after price has moved away from it."
const string tt_priceFill = "Toggles the gradient fill between price and the Laguerre trend line."
const string tt_ribbonVis = "Toggles the trend ribbon line and the fill between it and the Laguerre trend line."
const string tt_glow = "Toggles the glow effect around the Laguerre trend line."
const string tt_signals = "Toggles triangle markers on bullish and bearish trend flips."
const string tt_paint = "Colors candles based on the current trend direction."
const string tt_width = "Line width of the Laguerre trend line."
const string tt_colors = "Bullish colors uptrend elements, support levels, and bullish markers. Bearish colors downtrend elements, resistance levels, and bearish markers."
// ┌────────────────────────────── BOSWaves ─ Inputs ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
// ─────────────────── Trend Engine ───────────────────
float src = input.source(hlc3, "Source", group=g1, tooltip=tt_src)
float gamma = input.float(0.72, "Laguerre Gamma", minval=0.10, maxval=0.95, step=0.01, group=g1, tooltip=tt_gamma)
int ribbonLen = input.int(8, "Ribbon Length", minval=2, group=g1, tooltip=tt_ribbon)
int atrLen = input.int(14, "ATR Length", minval=2, group=g1, tooltip=tt_atr)
float flipBuffer = input.float(0.20, "Flip Buffer ATR", minval=0.0, maxval=2.0, step=0.05, group=g1, tooltip=tt_buffer)
int slopeLookback = input.int(2, "Slope Lookback", minval=1, maxval=10, group=g1, tooltip=tt_slope)
// ─────────────────── Structure Levels ───────────────────
int swingLen = input.int(5, "Swing Length", minval=2, maxval=30, group=g2, tooltip=tt_swing)
int maxLevels = input.int(4, "Maximum Levels", minval=1, maxval=10, group=g2, tooltip=tt_maxLevels)
float pullbackATR = input.float(0.80, "Ribbon Proximity ATR", minval=0.10, maxval=3.0, step=0.05, group=g2, tooltip=tt_pullback)
float zoneATR = input.float(0.12, "Zone Thickness ATR", minval=0.02, maxval=0.50, step=0.01, group=g2, tooltip=tt_zone)
float mergeATR = input.float(0.30, "Level Merge ATR", minval=0.05, maxval=1.5, step=0.05, group=g2, tooltip=tt_merge)
float breakATR = input.float(0.08, "Break Buffer ATR", minval=0.0, maxval=1.0, step=0.02, group=g2, tooltip=tt_break)
int originMaxAge = input.int(100, "Origin Swing Max Age", minval=10, maxval=500, group=g2, tooltip=tt_originAge)
bool showOrigin = input.bool(true, "Trend Origin Levels", group=g2, tooltip=tt_origin)
bool showCont = input.bool(true, "Continuation Levels", group=g2, tooltip=tt_cont)
bool showRetests = input.bool(true, "Level Retests", group=g2, tooltip=tt_retests)
// ─────────────────── Signals & Display ───────────────────
bool showPriceFill = input.bool(true, "Price Gradient", group=g3, tooltip=tt_priceFill)
bool showRibbon = input.bool(true, "Trend Ribbon", group=g3, tooltip=tt_ribbonVis)
bool showGlow = input.bool(true, "Filter Glow", group=g3, tooltip=tt_glow)
bool showSignals = input.bool(true, "Trend Flip Markers", group=g3, tooltip=tt_signals)
bool paintBars = input.bool(false, "Paint Candles", group=g3, tooltip=tt_paint)
int lineWidth = input.int(3, "Trend Line Width", minval=1, maxval=6, group=g3, tooltip=tt_width)
color bullCol = input.color(BULL, "Bullish", inline="col", group=g3, tooltip=tt_colors)
color bearCol = input.color(BEAR, "Bearish", inline="col", group=g3, tooltip=tt_colors)
// ┌────────────────────────────── BOSWaves ─ Laguerre Engine ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
var float l0 = na
var float l1 = na
var float l2 = na
var float l3 = na
l0 := (1.0 - gamma) * src + gamma * nz(l0[1], src)
l1 := -gamma * l0 + nz(l0[1], src) + gamma * nz(l1[1], src)
l2 := -gamma * l1 + nz(l1[1], src) + gamma * nz(l2[1], src)
l3 := -gamma * l2 + nz(l2[1], src) + gamma * nz(l3[1], src)
float filt = (l0 + 2.0 * l1 + 2.0 * l2 + l3) / 6.0
float ribbon = ta.ema(filt, ribbonLen)
float atr = ta.atr(atrLen)
bool bullReady = close > filt + atr * flipBuffer and filt > filt[slopeLookback] and filt > ribbon
bool bearReady = close < filt - atr * flipBuffer and filt < filt[slopeLookback] and filt < ribbon
var int trend = 0
if bullReady
trend := 1
else if bearReady
trend := -1
else if trend == 0
trend := filt >= ribbon ? 1 : -1
bool bullFlip = trend == 1 and trend[1] == -1
bool bearFlip = trend == -1 and trend[1] == 1
color col = trend == 1 ? bullCol : bearCol
// ┌────────────────────────────── BOSWaves ─ Trend Plotting ─────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
pFilter = plot(filt, "Laguerre Trend", col, lineWidth)
pPrice = plot(close, "Price Reference", display=display.none, editable=false)
pRibbon = plot(showRibbon ? ribbon : na, "Ribbon", color.new(col, 58), 2)
// Price gradient
float priceTop = math.max(close, filt)
float priceBottom = math.min(close, filt)
color priceTopCol = close > filt ? color.new(col, 92) : color.new(col, 20)
color priceBottomCol = close > filt ? color.new(col, 20) : color.new(col, 92)
fill(pPrice, pFilter, priceTop, priceBottom,
showPriceFill ? priceTopCol : na,
showPriceFill ? priceBottomCol : na,
title="Price Gradient")
// Ribbon gradient
float ribbonTop = math.max(filt, ribbon)
float ribbonBottom = math.min(filt, ribbon)
color ribbonTopCol = filt > ribbon ? color.new(col, 18) : color.new(col, 88)
color ribbonBottomCol = filt > ribbon ? color.new(col, 88) : color.new(col, 18)
fill(pFilter, pRibbon, ribbonTop, ribbonBottom,
showRibbon ? ribbonTopCol : na,
showRibbon ? ribbonBottomCol : na,
title="Trend Ribbon")
// Filter glow
float glow = atr * 0.045
float outerGlow = glow * 2.2
pGlowU = plot(showGlow ? filt + glow : na, display=display.none, editable=false)
pGlowL = plot(showGlow ? filt - glow : na, display=display.none, editable=false)
pOuterU = plot(showGlow ? filt + outerGlow : na, display=display.none, editable=false)
pOuterL = plot(showGlow ? filt - outerGlow : na, display=display.none, editable=false)
fill(pOuterU, pOuterL, showGlow ? color.new(col, 93) : na, title="Outer Glow")
fill(pGlowU, pGlowL, showGlow ? color.new(col, 72) : na, title="Inner Glow")
barcolor(paintBars ? col : na)
// ┌────────────────────────────── BOSWaves ─ Swing Structure ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
float ph = ta.pivothigh(high, swingLen, swingLen)
float pl = ta.pivotlow(low, swingLen, swingLen)
var float lastPH = na
var float lastPL = na
var int lastPHBar = na
var int lastPLBar = na
if not na(ph)
lastPH := ph
lastPHBar := bar_index - swingLen
if not na(pl)
lastPL := pl
lastPLBar := bar_index - swingLen
// ┌────────────────────────────── BOSWaves ─ Level Type ─────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
type StructureLevel
float price
float top
float bottom
int direction
bool origin
bool armed
line ln
box outer
box inner
var StructureLevel[] levels = array.new<StructureLevel>()
// ┌────────────────────────────── BOSWaves ─ Level Helpers ──────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
f_delete_level(StructureLevel lv) =>
line.delete(lv.ln)
box.delete(lv.outer)
box.delete(lv.inner)
f_clear_opposite(int direction) =>
bool cleared = false
if levels.size() > 0
for i = levels.size() - 1 to 0
StructureLevel lv = levels.get(i)
if lv.direction != direction
f_delete_level(lv)
levels.remove(i)
cleared := true
cleared
f_add_level(float price, int pivotBar, int direction, bool origin, float levelAtr) =>
bool add = true
float mergeDistance = levelAtr * mergeATR
if levels.size() > 0
for i = 0 to levels.size() - 1
StructureLevel old = levels.get(i)
if old.direction == direction and math.abs(old.price - price) <= mergeDistance
add := false
break
if add
color css = direction == 1 ? bullCol : bearCol
float height = levelAtr * zoneATR
float top = price + height
float bottom = price - height
float innerH = height * 0.42
int outerAlpha = origin ? 88 : 92
int innerAlpha = origin ? 68 : 78
int lineAlpha = origin ? 5 : 22
int width = origin ? 2 : 1
box outer = box.new(pivotBar, top, pivotBar + 1, bottom,
xloc=xloc.bar_index, extend=extend.right,
border_color=color.new(css, 100), bgcolor=color.new(css, outerAlpha))
box inner = box.new(pivotBar, price + innerH, pivotBar + 1, price - innerH,
xloc=xloc.bar_index, extend=extend.right,
border_color=color.new(css, 100), bgcolor=color.new(css, innerAlpha))
line ln = line.new(pivotBar, price, pivotBar + 1, price,
xloc=xloc.bar_index, extend=extend.right,
color=color.new(css, lineAlpha), width=width,
style=origin ? line.style_solid : line.style_dotted)
levels.unshift(StructureLevel.new(price, top, bottom, direction, origin, false, ln, outer, inner))
while levels.size() > maxLevels
StructureLevel old = levels.pop()
f_delete_level(old)
add
// ┌────────────────────────────── BOSWaves ─ Trend Origin Levels ────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
if bullFlip
f_clear_opposite(1)
if showOrigin and not na(lastPL) and not na(lastPLBar) and bar_index - lastPLBar <= originMaxAge
f_add_level(lastPL, lastPLBar, 1, true, nz(atr[bar_index - lastPLBar], atr))
if bearFlip
f_clear_opposite(-1)
if showOrigin and not na(lastPH) and not na(lastPHBar) and bar_index - lastPHBar <= originMaxAge
f_add_level(lastPH, lastPHBar, -1, true, nz(atr[bar_index - lastPHBar], atr))
// ┌────────────────────────────── BOSWaves ─ Continuation Levels ────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
if showCont and not na(pl) and trend[swingLen] == 1
float pivotAtr = nz(atr[swingLen], atr)
float ribbonTop = math.max(filt[swingLen], ribbon[swingLen])
float ribbonBottom = math.min(filt[swingLen], ribbon[swingLen])
float distance =
pl < ribbonBottom ? ribbonBottom - pl :
pl > ribbonTop ? pl - ribbonTop :
0.0
if distance <= pivotAtr * pullbackATR
f_add_level(pl, bar_index - swingLen, 1, false, pivotAtr)
if showCont and not na(ph) and trend[swingLen] == -1
float pivotAtr = nz(atr[swingLen], atr)
float ribbonTop = math.max(filt[swingLen], ribbon[swingLen])
float ribbonBottom = math.min(filt[swingLen], ribbon[swingLen])
float distance =
ph > ribbonTop ? ph - ribbonTop :
ph < ribbonBottom ? ribbonBottom - ph :
0.0
if distance <= pivotAtr * pullbackATR
f_add_level(ph, bar_index - swingLen, -1, false, pivotAtr)
// ┌────────────────────────────── BOSWaves ─ Level Lifecycle ────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
bool bullRetest = false
bool bearRetest = false
if levels.size() > 0
for i = levels.size() - 1 to 0
StructureLevel lv = levels.get(i)
bool invalid =
lv.direction == 1
? close < lv.bottom - atr * breakATR
: close > lv.top + atr * breakATR
if invalid
f_delete_level(lv)
levels.remove(i)
else
bool outside =
lv.direction == 1
? low > lv.top
: high < lv.bottom
bool touch = high >= lv.bottom and low <= lv.top
if outside
lv.armed := true
else if lv.armed and touch and trend == lv.direction
lv.armed := false
if lv.direction == 1
bullRetest := true
else
bearRetest := true
levels.set(i, lv)
// ┌────────────────────────────── BOSWaves ─ Signals ────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
plotshape(showSignals and bullFlip, "Bull Trend", shape.triangleup, location.belowbar, bullCol, size=size.tiny)
plotshape(showSignals and bearFlip, "Bear Trend", shape.triangledown, location.abovebar, bearCol, size=size.tiny)
plotshape(showRetests and bullRetest, "Bull Structure Retest", shape.diamond, location.belowbar, bullCol, size=size.tiny)
plotshape(showRetests and bearRetest, "Bear Structure Retest", shape.diamond, location.abovebar, bearCol, size=size.tiny)
// ┌────────────────────────────── BOSWaves ─ Data Window ────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
plot(filt - ribbon, "Ribbon Spread", display=display.data_window)
plot(trend, "Trend State", display=display.data_window)
// ┌────────────────────────────── BOSWaves ─ Alerts ─────────────────────────────────┐
// └──────────────────────────────────────────────────────────────────────────────────┘
alertcondition(bullFlip, "Bullish Trend", "Laguerre Structure Levels bullish trend | {{exchange}}:{{ticker}}")
alertcondition(bearFlip, "Bearish Trend", "Laguerre Structure Levels bearish trend | {{exchange}}:{{ticker}}")
alertcondition(bullRetest, "Bull Structure Retest", "Laguerre Structure Levels bullish level retest | {{exchange}}:{{ticker}}")
alertcondition(bearRetest, "Bear Structure Retest", "Laguerre Structure Levels bearish level retest | {{exchange}}:{{ticker}}")