本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Dynamic Neckline v1.5 — No EMA (MT4 port)", overlay = true, max_lines_count = 20, max_bars_back = 1200)
// TradingView translation of Dynamic_Neckline_Indicator_v1_5_No_EMA.mq4.
// Visual study only: no orders and no EMA filter.
// Necklines are computed from candle BODIES; pivots use wicks.
groupStructure = "Structure"
scanBars = input.int(700, "Scan bars", minval = 20, maxval = 1000, group = groupStructure)
minCorrectionCandles = input.int(1, "Min correction candles", minval = 1, maxval = 100, group = groupStructure)
segmentLengthBars = input.int(9, "Line length (bars)", minval = 1, maxval = 100, group = groupStructure)
groupLines = "Neckline lines"
activeLineWidth = input.int(2, "Active line width", minval = 1, maxval = 5, group = groupLines)
nextLineWidth = input.int(2, "Future line width", minval = 1, maxval = 5, group = groupLines)
showSellLines = input.bool(true, "Show SELL necklines", group = groupLines)
showBuyLines = input.bool(true, "Show BUY necklines", group = groupLines)
sellActiveColor = input.color(color.red, "SELL active color", group = groupLines)
sellNextColor = input.color(color.red, "SELL future color", group = groupLines)
buyActiveColor = input.color(color.blue, "BUY active color", group = groupLines)
buyNextColor = input.color(color.blue, "BUY future color", group = groupLines)
groupAlerts = "Alerts"
enableSellAlerts = input.bool(true, "Alerts for new/moved SELL active line", group = groupAlerts)
enableBuyAlerts = input.bool(true, "Alerts for new/moved BUY active line", group = groupAlerts)
// Draw one short line, or remove it when the pattern is not currently active.
f_line(line current, bool visible, int anchor, float level, color col, int width, bool dotted) =>
line out = current
if visible and anchor >= 0 and not na(level)
int x2 = anchor + segmentLengthBars
if na(out)
out := line.new(anchor, level, x2, level, xloc = xloc.bar_index, extend = extend.none, color = col, width = width, style = dotted ? line.style_dotted : line.style_solid)
else
line.set_xy1(out, anchor, level)
line.set_xy2(out, x2, level)
line.set_color(out, col)
line.set_width(out, width)
line.set_style(out, dotted ? line.style_dotted : line.style_solid)
else
if not na(out)
line.delete(out)
out := na
out
var line sellActiveLine = na
var line sellFutureLine = na
var line buyActiveLine = na
var line buyFutureLine = na
// Persist alert baseline across closed candles. Historical initial placement is silent.
var bool alertBaselineReady = false
var bool prevSellVisible = false
var bool prevBuyVisible = false
var int prevSellAnchor = na
var int prevBuyAnchor = na
var float prevSellPrice = na
var float prevBuyPrice = na
bool alertSell = false
bool alertBuy = false
// On the last bar, reconstruct from the same closed-candle history window as
// the MT4 version. An unfinished realtime bar is NOT used in pivot recognition.
if barstate.islast
int endOffset = barstate.isconfirmed ? 0 : 1
int oldest = math.min(scanBars, bar_index - 1)
if oldest >= endOffset + 2
// Price and bar-index of the most recent confirmed leg extrema.
float lastPeak = na
int lastPeakAt = na
float lastBottom = na
int lastBottomAt = na
float lastPeakBody = na
float lastBottomBody = na
float bullHigh = na
int bullHighAt = na
float bearLow = na
int bearLowAt = na
float bullBodyHigh = na
int bullBodyHighAt = na
float bearBodyLow = na
int bearBodyLowAt = na
float activeSell = na
int activeSellAt = na
float activeBuy = na
int activeBuyAt = na
float nextSell = na
int nextSellAt = na
float nextBuy = na
int nextBuyAt = na
bool sellPair = false
bool buyPair = false
bool sellBreak = false
bool buyBreak = false
int lastDirection = 0
int bullLegCandles = 0
int bearLegCandles = 0
for i = oldest to endOffset
float op = open[i]
float cl = close[i]
int dir = cl > op ? 1 : cl < op ? -1 : 0
if dir != 0
int ix = bar_index - i
if dir == 1
if lastDirection != 1
bullLegCandles := 0
bearLegCandles := 0
bullHigh := na
bullHighAt := na
bullBodyHigh := na
bullBodyHighAt := na
bullLegCandles += 1
if na(bullHigh) or high[i] >= bullHigh
bullHigh := high[i]
bullHighAt := ix
float bh = math.max(op, cl)
if na(bullBodyHigh) or bh >= bullBodyHigh
bullBodyHigh := bh
bullBodyHighAt := ix
lastDirection := 1
if bullLegCandles == minCorrectionCandles and not na(bearLow)
if not na(lastPeak) and bearLowAt > lastPeakAt
nextSell := bearBodyLow
nextSellAt := bearBodyLowAt
if not na(lastBottom) and not na(lastPeak) and lastPeakAt > lastBottomAt and lastPeakAt < bearLowAt
activeBuy := lastPeakBody
activeBuyAt := lastPeakAt
buyPair := true
buyBreak := false
lastBottom := bearLow
lastBottomAt := bearLowAt
lastBottomBody := bearBodyLow
if bullLegCandles >= minCorrectionCandles and not na(lastBottom) and not na(bullHigh) and bullHighAt > lastBottomAt
nextBuy := bullBodyHigh
nextBuyAt := bullBodyHighAt
else
if lastDirection != -1
bearLegCandles := 0
bullLegCandles := 0
bearLow := na
bearLowAt := na
bearBodyLow := na
bearBodyLowAt := na
bearLegCandles += 1
if na(bearLow) or low[i] <= bearLow
bearLow := low[i]
bearLowAt := ix
float bl = math.min(op, cl)
if na(bearBodyLow) or bl <= bearBodyLow
bearBodyLow := bl
bearBodyLowAt := ix
lastDirection := -1
if bearLegCandles == minCorrectionCandles and not na(bullHigh)
if not na(lastPeak) and not na(lastBottom) and lastBottomAt > lastPeakAt and lastBottomAt < bullHighAt
activeSell := lastBottomBody
activeSellAt := lastBottomAt
sellPair := true
sellBreak := false
nextSell := na
nextSellAt := na
lastPeak := bullHigh
lastPeakAt := bullHighAt
lastPeakBody := bullBodyHigh
if bearLegCandles >= minCorrectionCandles and not na(lastPeak) and not na(bearLow) and bearLowAt > lastPeakAt
nextSell := bearBodyLow
nextSellAt := bearBodyLowAt
// Mirror MT4 closed-candle neckline break state.
if sellPair and not na(activeSell) and not sellBreak and cl < activeSell and close[i + 1] >= activeSell
sellBreak := true
if buyPair and not na(activeBuy) and not buyBreak and cl > activeBuy and close[i + 1] <= activeBuy
buyBreak := true
float shownSell = not na(activeSell) and sellPair ? activeSell : nextSell
int shownSellAt = not na(activeSell) and sellPair ? activeSellAt : nextSellAt
float shownBuy = not na(activeBuy) and buyPair ? activeBuy : nextBuy
int shownBuyAt = not na(activeBuy) and buyPair ? activeBuyAt : nextBuyAt
bool sellVisible = showSellLines and not na(shownSell)
bool buyVisible = showBuyLines and not na(shownBuy)
bool sellFutureVisible = showSellLines and not na(activeSell) and not na(nextSell) and nextSellAt > activeSellAt and not sellBreak
bool buyFutureVisible = showBuyLines and not na(activeBuy) and not na(nextBuy) and nextBuyAt > activeBuyAt and not buyBreak
sellActiveLine := f_line(sellActiveLine, sellVisible, shownSellAt, shownSell, sellActiveColor, activeLineWidth, false)
buyActiveLine := f_line(buyActiveLine, buyVisible, shownBuyAt, shownBuy, buyActiveColor, activeLineWidth, false)
sellFutureLine := f_line(sellFutureLine, sellFutureVisible, nextSellAt, nextSell, sellNextColor, nextLineWidth, true)
buyFutureLine := f_line(buyFutureLine, buyFutureVisible, nextBuyAt, nextBuy, buyNextColor, nextLineWidth, true)
// Compare after the candle has closed; only active-line changes alert.
if barstate.isconfirmed
if alertBaselineReady
alertSell := enableSellAlerts and sellVisible and (not prevSellVisible or shownSellAt != prevSellAnchor or math.abs(shownSell - prevSellPrice) > syminfo.mintick * 0.1)
alertBuy := enableBuyAlerts and buyVisible and (not prevBuyVisible or shownBuyAt != prevBuyAnchor or math.abs(shownBuy - prevBuyPrice) > syminfo.mintick * 0.1)
prevSellVisible := sellVisible
prevBuyVisible := buyVisible
prevSellAnchor := shownSellAt
prevBuyAnchor := shownBuyAt
prevSellPrice := shownSell
prevBuyPrice := shownBuy
alertBaselineReady := true
alertcondition(alertSell, "New / moved SELL neckline", "Dynamic Neckline: New or moved SELL active neckline on {{ticker}} ({{interval}}).")
alertcondition(alertBuy, "New / moved BUY neckline", "Dynamic Neckline: New or moved BUY active neckline on {{ticker}} ({{interval}}).")
alertcondition(alertSell or alertBuy, "Any new / moved neckline", "Dynamic Neckline: Active neckline created/moved on {{ticker}} ({{interval}}).")