本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © The Veiled Sage
//@version=6
// The Veiled Sage - EWO Band Reentry (vs_ewo_band_reentry_v1_c1)
// Components: Elliott Wave Oscillator (EMA 5 minus EMA 35 of close) and Bollinger-style deviation bands (John Bollinger's band construction)
// applied to the oscillator itself. Both are classic public concepts; no third-party Pine source is reused in this file.
// Lineage: Veiled Sage work (owner capture h7dwMhHP, corrected in internal v0.1/v0.2; external provenance not independently verified).
// All events and alerts are confirmed-bar only. The plotted oscillator and bands can still move during the developing bar.
// c1 (CANDIDATE, unvalidated, 9 Nov 2026): from v0.2 sha256 cf217185bf18d7df453a33bae5096f8b8480bcc9c4197cd3472904604add8313.
// Branding/title/MPL header/credits, default palette limited to gold, purple, orange and parchment, brand table, vs_alert/3 JSON alert(),
// neutral alertcondition titles. EWO/band/signal/Mayer/VWMA-guard calculations unchanged.
indicator("The Veiled Sage - EWO Band Reentry", shorttitle="VS EWO", overlay=false, max_labels_count=500)
string SCRIPT_VER = "vs_ewo_band_reentry_v1_c1"
string SCHEMA = "vs_alert/3"
keepSignalLabels = input.int(200, "Keep last N entry labels", minval=10, maxval=480)
var array<label> signalLabels = array.new_label()
f_keepLabel(label l) =>
signalLabels.push(l)
if signalLabels.size() > keepSignalLabels
label.delete(signalLabels.shift())
// === EWO Calculation ===
ewo = ta.ema(close, 5) - ta.ema(close, 35)
positiveColor = input.color(#D4A017, "Positive EWO color (gold)")
negativeColor = input.color(#7B2CBF, "Non-positive EWO color (purple)")
plotOpacity = input.int(0, "Plot transparency", minval=0, maxval=100)
ewoColor = color.new(ewo <= 0 ? negativeColor : positiveColor, plotOpacity)
// === Bollinger Bands on EWO ===
length = input.int(20, title="BB Length", minval=2)
mult = input.float(2.0, title="BB StdDev Multiplier", minval=0.01)
maType = input.string("SMA", "BB MA Type", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
ma(src, len, type) =>
switch type
"SMA" => ta.sma(src, len)
"EMA" => ta.ema(src, len)
"SMMA (RMA)" => ta.rma(src, len)
"WMA" => ta.wma(src, len)
"VWMA" => ta.vwma(src, len)
// VWMA fails closed if any selected-window volume is missing/negative,
// or total volume is zero. Other averages retain their original formula.
invalidVolumeCount = math.sum(na(volume) or volume < 0 ? 1.0 : 0.0, length)
windowVolume = math.sum(nz(volume), length)
volumeReady = not na(invalidVolumeCount) and invalidVolumeCount == 0 and windowVolume > 0
rawBasis = ma(ewo, length, maType)
basis = maType == "VWMA" and not volumeReady ? na : rawBasis
dev = mult * ta.stdev(ewo, length)
upper = basis + dev
lower = basis - dev
// === Signal Logic
rawBuy = ta.crossover(ewo, lower)
rawSell = ta.crossunder(ewo, upper)
buySignal = barstate.isconfirmed and not na(lower) and rawBuy
sellSignal = barstate.isconfirmed and not na(upper) and rawSell
// === Plot in Oscillator Pane
plot(ewo, title="EWO", color=ewoColor, style=plot.style_histogram, linewidth=2)
basisUp = basis > basis[1]
basisLineColor = input.color(#FF8C1A, title="BB Basis Fixed Color (orange)")
colorBasisBySlope = input.bool(true, "Color basis by slope (otherwise fixed color)")
basisColor = colorBasisBySlope ? (basisUp ? positiveColor : negativeColor) : basisLineColor
basisLineWidth = input.int(1, title="BB Basis Line Width", minval=1)
plot(basis, title="BB Basis", color=color.new(basisColor, plotOpacity), linewidth=basisLineWidth)
pUpper = plot(upper, title="BB Upper", color=color.new(negativeColor, plotOpacity))
pLower = plot(lower, title="BB Lower", color=color.new(positiveColor, plotOpacity))
fill(pUpper, pLower, color=input.color(color.new(#D4A017, 90), "Band fill color and transparency"), title="BB Fill")
// === Plot Signal Dots in Oscillator
plot(buySignal ? ewo : na, title="Re-entry up dot", color=color.new(positiveColor, plotOpacity), style=plot.style_circles, linewidth=2)
plot(sellSignal ? ewo : na, title="Re-entry down dot", color=color.new(negativeColor, plotOpacity), style=plot.style_circles, linewidth=2)
// === GUI Label Color Selection
buyLabelColor = input.color(#D4A017, title="Re-entry up label color")
sellLabelColor = input.color(#7B2CBF, title="Re-entry down label color")
buyLabelTextColor = input.color(#000000, "Re-entry up label text color")
sellLabelTextColor = input.color(#F5ECD0, "Re-entry down label text color")
// === Labels on Main Chart via force_overlay
if buySignal
f_keepLabel(label.new(bar_index, low, text=basisUp ? "Basis-rising Re-entry up" : "Basis-not-rising Re-entry up", style=label.style_label_up, color=buyLabelColor, textcolor=buyLabelTextColor, size=size.tiny, yloc=yloc.belowbar, tooltip="EWO crossed above lower BB", force_overlay=true))
if sellSignal
f_keepLabel(label.new(bar_index, high, text=basisUp ? "Basis-rising Re-entry down" : "Basis-not-rising Re-entry down", style=label.style_label_down, color=sellLabelColor, textcolor=sellLabelTextColor, size=size.tiny, yloc=yloc.abovebar, tooltip="EWO crossed below upper BB", force_overlay=true))
// === ALERTS (alertcondition: pick "Once Per Bar Close" when creating)
alertcondition(buySignal and basisUp, title="VS EWO Basis-rising Re-entry up", message="{{ticker}} Basis-rising Re-entry up: EWO crossed back above the lower band (basis rising). Closed bar.")
alertcondition(buySignal and not basisUp, title="VS EWO Basis-not-rising Re-entry up", message="{{ticker}} Basis-not-rising Re-entry up: EWO crossed back above the lower band (basis flat or falling). Closed bar.")
alertcondition(sellSignal and not basisUp, title="VS EWO Basis-not-rising Re-entry down", message="{{ticker}} Basis-not-rising Re-entry down: EWO crossed back below the upper band (basis flat or falling). Closed bar.")
alertcondition(sellSignal and basisUp, title="VS EWO Basis-rising Re-entry down", message="{{ticker}} Basis-rising Re-entry down: EWO crossed back below the upper band (basis rising). Closed bar.")
// === Mayer Multiple Background
mayerSmaLength = input.int(200, title="Price/SMA reference length (chart bars)", minval=2)
mayerThreshold = input.float(2.4, title="Ratio upper threshold")
mayerUndervalue = input.float(1.0, title="Ratio lower threshold")
mayerSMA = ta.sma(close, mayerSmaLength)
mayerMultiple = not na(mayerSMA) and mayerSMA != 0 ? close / mayerSMA : na
bgBuy = input.color(color.new(#D4A017, 90), title="Below reference-ratio background (not valuation)")
bgSell = input.color(color.new(#7B2CBF, 90), title="Above reference-ratio background (not valuation)")
bgcolor(mayerMultiple < mayerUndervalue ? bgBuy : na)
bgcolor(mayerMultiple > mayerThreshold ? bgSell : na)
// Visible explanation; never substitute a different average for VWMA.
showDataWarning = input.bool(true, "Show unavailable-volume warning")
var table dataWarning = table.new(position.bottom_left, 1, 1)
if barstate.islast
volumeBlocked = maType == "VWMA" and not volumeReady
warningText = showDataWarning and volumeBlocked ? "VWMA unavailable: volume missing, invalid or zero. VWMA bands, signals and alerts suppressed." : ""
table.cell(dataWarning, 0, 0, warningText, text_color=#D4A017, bgcolor=showDataWarning and volumeBlocked ? color.new(#000000, 10) : na, text_size=size.small)
// ── Branding ──
grpBr = "Branding"
showBrand = input.bool(true, "Show The Veiled Sage label", group=grpBr)
brandGold = input.color(#D4A017, "Brand colour", group=grpBr)
brandFade = input.int(45, "Brand transparency", minval=0, maxval=100, group=grpBr)
var table brandTbl = table.new(position.bottom_right, 1, 1)
if barstate.islast
table.cell(brandTbl, 0, 0, showBrand ? "The Veiled Sage · EWO Band Reentry" : "", text_size=size.small, text_color=color.new(brandGold, brandFade), bgcolor=color.new(#16121D, 100))
// ── JSON alert() (vs_alert/3, confirmed bars only). Create ONE alert: condition "Any alert() function call". ──
grpJ = "JSON alert"
jsonMode = input.string("Event closes only", "State JSON alert() mode", options=["Event closes only", "Every confirmed bar", "Off"], group=grpJ, tooltip="Event closes only: one JSON message on bars where a band re-entry event confirms. Every confirmed bar: a state snapshot each closed bar (events list may be empty). Off: no alert() calls. alertcondition() entries above are independent.")
bool basisKnown = not na(basis) and not na(basis[1])
jnum(float x, string fmt) => na(x) ? "null" : str.tostring(x, fmt)
jbool(bool x) => x ? "true" : "false"
buildPayload(string ev, string eventsJson) =>
string isoT = str.format_time(time, "yyyy-MM-dd'T'HH:mm:ss'Z'", "UTC")
string id = syminfo.ticker + "_" + timeframe.period + "_" + str.tostring(time) + "_" + ev
string s = "{\"schema\":\"" + SCHEMA + "\",\"id\":\"" + id + "\",\"event\":\"" + ev + "\""
s += ",\"version\":\"" + SCRIPT_VER + "\",\"indicator\":\"ewo_band_reentry\""
s += ",\"ticker\":\"" + syminfo.ticker + "\",\"exchange\":\"" + syminfo.prefix + "\",\"timeframe\":\"" + timeframe.period + "\""
s += ",\"bar_time\":" + str.tostring(time) + ",\"bar_time_iso\":\"" + isoT + "\",\"confirmed\":true"
s += ",\"open\":" + jnum(open, "#.########") + ",\"high\":" + jnum(high, "#.########") + ",\"low\":" + jnum(low, "#.########") + ",\"close\":" + jnum(close, "#.########") + ",\"volume\":" + jnum(volume, "#.####")
s += ",\"ewo\":" + jnum(ewo, "#.########") + ",\"ewo_basis\":" + jnum(basis, "#.########") + ",\"ewo_upper\":" + jnum(upper, "#.########") + ",\"ewo_lower\":" + jnum(lower, "#.########")
s += ",\"basis_rising\":" + (basisKnown ? jbool(basisUp) : "null")
s += ",\"band_ma_type\":\"" + maType + "\",\"band_length\":" + str.tostring(length) + ",\"band_mult\":" + jnum(mult, "#.##")
s += ",\"volume_ready\":" + jbool(volumeReady)
s += ",\"price_sma_ratio\":" + jnum(mayerMultiple, "#.####") + ",\"price_sma_length\":" + str.tostring(mayerSmaLength)
s += ",\"events\":" + eventsJson + ",\"state_only\":true}"
s
if barstate.isconfirmed and jsonMode != "Off"
string evList = ""
if buySignal
evList += "\"band_reentry_buy\""
if sellSignal
evList += (evList == "" ? "" : ",") + "\"band_reentry_sell\""
if jsonMode == "Every confirmed bar" or evList != ""
alert(buildPayload("ewo_band_reentry_snapshot", "[" + evList + "]"), alert.freq_once_per_bar_close)