本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Dynamic Chandelier Exit Trader [KedArc Quant])", shorttitle="DCET[KedArc Quant]", overlay=true)
// ──────────────────────────────────────────────────────────────────────────────
// Inputs
const string calcGroup = "Calculation"
length = input.int(22, title="ATR Period", group=calcGroup)
mult = input.float(3.0, step=0.1, title="ATR Multiplier", group=calcGroup)
useClose = input.bool(true, title="Use Close Price for Extremums", group=calcGroup)
const string visGroup = "Visuals"
showLabels = input.bool(true, title="Show Buy/Sell Labels", group=visGroup)
highlightBg = input.bool(true, title="Highlight State", group=visGroup)
const string alertGroup = "Alerts"
awaitBarConfirmation = input.bool(true, title="Await Bar Confirmation", group=alertGroup)
const string rrGroup = "Risk/Reward Overlay"
enableRR = input.bool(true, title="Show R:R Box/Lines", group=rrGroup)
rrMultiple = input.float(2.0, step=0.1, minval=0.1, title="Target: R multiple", group=rrGroup)
entryPriceSrc = input.string("Signal bar close", options=["Signal bar close","Next bar open"], title="Entry Price", group=rrGroup)
slMethod = input.string("CE Line", options=["CE Line","Swing HL","ATR x"], title="Stop Basis", group=rrGroup)
swingLen = input.int(10, minval=1, title="Swing Length (for Swing HL)", group=rrGroup)
atrSLmult = input.float(1.0, step=0.1, minval=0.1, title="ATR Multiplier (for ATR x)", group=rrGroup)
persistBoxes = input.bool(true, title="Keep old R:R boxes on chart", group=rrGroup)
liveUpdateSLTP = input.bool(true, title="Update SL/TP with current CE (lightweight)", group=rrGroup)
// ──────────────────────────────────────────────────────────────────────────────
// Core CE logic (kept identical so signals match your CE)
atrRaw = ta.atr(length)
atr = mult * atrRaw
// Highest/lowest every bar (consistency)
hhClose = ta.highest(close, length)
hhHigh = ta.highest(length) // implicit source = high
llClose = ta.lowest(close, length)
llLow = ta.lowest(length) // implicit source = low
longBase = useClose ? hhClose : hhHigh
shortBase = useClose ? llClose : llLow
longStop = longBase - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = shortBase + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir
buySignal = dir == 1 and dir[1] == -1
sellSignal = dir == -1 and dir[1] == 1
await = awaitBarConfirmation ? barstate.isconfirmed : true
// ──────────────────────────────────────────────────────────────────────────────
// Plots (same look as CE)
const color textColor = color.white
const color longColor = color.new(color.green, 0)
const color shortColor = color.new(color.red, 0)
const color longFillColor = color.new(color.green, 85)
const color shortFillColor = color.new(color.red, 85)
longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
shortStopPlot = plot(dir == -1 ? shortStop : na, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
midPricePlot = plot(ohlc4, title="", display=display.none, editable=false)
fill(midPricePlot, longStopPlot, title="Long State Filling", color=(highlightBg and dir == 1 ? longFillColor : na))
fill(midPricePlot, shortStopPlot, title="Short State Filling", color=(highlightBg and dir == -1 ? shortFillColor : na))
plotshape(buySignal and await and showLabels ? longStop : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=textColor)
plotshape(sellSignal and await and showLabels ? shortStop : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=textColor)
// Alerts
alertcondition(dir != dir[1] and await, title="CE Direction Change", message="Chandelier Exit changed direction, {{exchange}}:{{ticker}}")
alertcondition(buySignal and await, title="CE Buy", message="CE Buy, {{exchange}}:{{ticker}}")
alertcondition(sellSignal and await, title="CE Sell", message="CE Sell, {{exchange}}:{{ticker}}")
// ──────────────────────────────────────────────────────────────────────────────
// R:R overlay (indicator-only; does not affect CE)
var box rrBox = na
var line rrEntry = na
var line rrSL = na
var line rrTP = na
// Keep track of the last signal's entry & side (for optional live update)
// sideActive: 1 = long, -1 = short, 0 = none yet
var float epActive = na
var int sideActive = 0
// Precompute helpers for consistency
entryPrice = entryPriceSrc == "Next bar open" ? open : close
swingLow = ta.lowest(low, swingLen)
swingHigh = ta.highest(high, swingLen)
// Per-bar stop choices (computed every bar)
slLong_now =
slMethod == "CE Line" ? longStop :
slMethod == "Swing HL" ? swingLow :
entryPrice - atrRaw * atrSLmult
slShort_now =
slMethod == "CE Line" ? shortStop :
slMethod == "Swing HL" ? swingHigh :
entryPrice + atrRaw * atrSLmult
// Helper: target from entry+stop and R multiple
f_tp_for(ep, sp, rMultiple) =>
risk = math.abs(ep - sp)
ep + (ep > sp ? rMultiple * risk // long
: -rMultiple * risk) // short
// Extend last guides forward
f_extendGuides() =>
if not na(rrBox)
box.set_right(rrBox, bar_index + 1)
if not na(rrEntry)
line.set_x2(rrEntry, bar_index + 1)
if not na(rrSL)
line.set_x2(rrSL, bar_index + 1)
if not na(rrTP)
line.set_x2(rrTP, bar_index + 1)
// Create guides only on confirmed flips
if enableRR
if (buySignal or sellSignal) and await
isLong = buySignal
ep = entryPrice
sp = isLong ? slLong_now : slShort_now
tp = f_tp_for(ep, sp, rrMultiple)
// delete old if not persisting
if not persistBoxes
if not na(rrBox)
box.delete(rrBox)
if not na(rrEntry)
line.delete(rrEntry)
if not na(rrSL)
line.delete(rrSL)
if not na(rrTP)
line.delete(rrTP)
// create new guides
boxColor = isLong ? color.new(color.green, 80) : color.new(color.red, 80)
rrBox := box.new(left=bar_index, top=math.max(ep, tp), right=bar_index+1,
bottom=math.min(sp, ep), bgcolor=boxColor, border_color=color.new(color.white, 0))
rrEntry := line.new(x1=bar_index, y1=ep, x2=bar_index+1, y2=ep, width=1, color=color.new(color.gray, 0))
rrSL := line.new(x1=bar_index, y1=sp, x2=bar_index+1, y2=sp, width=1, color=color.new(color.aqua, 0), style=line.style_dotted)
rrTP := line.new(x1=bar_index, y1=tp, x2=bar_index+1, y2=tp, width=1, color=color.new(color.white, 0), style=line.style_dashed)
// store last signal info for optional live updates
epActive := ep
sideActive := isLong ? 1 : -1
// Optional lightweight live update: adjust SL/TP with current CE while keeping entry fixed
if liveUpdateSLTP and sideActive != 0 and not na(epActive) and not na(rrSL) and not na(rrTP) and not na(rrBox)
sp_live = sideActive == 1 ? (
slMethod == "CE Line" ? longStop :
slMethod == "Swing HL" ? swingLow :
epActive - atrRaw * atrSLmult
) : (
slMethod == "CE Line" ? shortStop :
slMethod == "Swing HL" ? swingHigh :
epActive + atrRaw * atrSLmult
)
tp_live = f_tp_for(epActive, sp_live, rrMultiple)
// update lines
line.set_y1(rrSL, sp_live), line.set_y2(rrSL, sp_live)
line.set_y1(rrTP, tp_live), line.set_y2(rrTP, tp_live)
// update box bounds (entry stays fixed)
newTop = math.max(epActive, tp_live)
newBottom = math.min(sp_live, epActive)
box.set_top(rrBox, newTop)
box.set_bottom(rrBox, newBottom)
f_extendGuides()