本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Weis Wave, close-extreme engine.
// Rules from David Weis, "Trades About to Happen", chapters 9 and 10:
// 1. Waves are built from bar closes.
// 2. A wave continues on any close at or beyond its extreme close.
// 3. A wave reverses when a close is the reversal amount or more off the extreme close.
// 4. Volume after the extreme stays untotaled. If the wave reverses, that volume belongs to the new wave.
// State changes only on confirmed bars, so completed waves do not repaint.
indicator("Weis Wave, close-extreme engine", shorttitle = "WeisWave", overlay = false, format = format.volume, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ───────────────────────── Inputs
string GRP_R = "Reversal size"
string M_AUTO = "Auto (Weis table)"
string M_TICK = "Ticks"
string M_PRICE = "Price"
string M_PCT = "Percent"
string M_ATR = "ATR (frozen)"
string revMode = input.string(M_AUTO, "Mode", options = [M_AUTO, M_TICK, M_PRICE, M_PCT, M_ATR], group = GRP_R, tooltip = "Auto uses Weis's published intraday reversal for the symbol and falls back to frozen ATR when he published none (crypto, energy, indices). The size is latched when a wave starts and never changes inside a wave.")
float presetMult = input.float(1.0, "Preset multiplier", minval = 0.1, step = 0.5, group = GRP_R, tooltip = "Scales the Auto preset. Weis's figures date from about 2011. ES traded near 1300 then, so 0.75 points today is a much smaller fraction of price. Raise this until the wave count per session is readable.")
int revTicks = input.int(3, "Ticks", minval = 1, group = GRP_R)
float revPrice = input.float(0.75, "Price", minval = 0.0, step = 0.01, group = GRP_R)
float revPct = input.float(0.05, "Percent of price", minval = 0.001, step = 0.01, group = GRP_R)
int atrLen = input.int(14, "ATR length", minval = 1, group = GRP_R)
float atrMult = input.float(0.05, "ATR multiplier, intraday", minval = 0.001, step = 0.01, group = GRP_R, tooltip = "Applied to the prior day's ATR on intraday charts, or the prior bar's ATR on daily and higher. ATR sizing is not in the book.")
float atrMultHtf = input.float(1.0, "ATR multiplier, daily and higher", minval = 0.001, step = 0.1, group = GRP_R, tooltip = "Applied to the prior bar's ATR when the chart is daily or higher. The intraday multiplier is sized against a daily ATR, so reusing it on a daily chart turns the wave on almost every bar.")
string GRP_V = "Volume"
string useTR = input.string("Auto", "Use true range instead of volume", options = ["Auto", "Always", "Never"], group = GRP_V, tooltip = "Auto substitutes true range when the symbol has no volume. Weis uses range as the activity proxy in forex.")
bool autoFmt = input.bool(true, "Auto format volume (K, M)", group = GRP_V)
float volDiv = input.float(1000.0, "Volume divisor when auto format is off", minval = 1.0, group = GRP_V)
string GRP_D = "Display"
string H_BOX = "Wave boxes"
string H_COL = "Running columns"
string histMode = input.string(H_BOX, "Histogram", options = [H_BOX, H_COL], group = GRP_D, tooltip = "Wave boxes: one block per wave with exact volume attribution, limited to the last 500 waves. Running columns: unlimited history, cumulative per bar, untotaled bars shown in gray.")
bool showPaneTxt = input.bool(false, "Volume text inside boxes", group = GRP_D)
bool showZig = input.bool(true, "Wave line on price chart", group = GRP_D)
int zigWidth = input.int(1, "Wave line width", minval = 1, maxval = 4, group = GRP_D)
bool showLbl = input.bool(true, "Labels at turning points", group = GRP_D)
bool lblLen = input.bool(false, "Label: wave length in ticks", group = GRP_D)
bool lblDur = input.bool(false, "Label: wave duration in bars", group = GRP_D)
string lblSizeIn = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = GRP_D)
color upCol = input.color(color.new(#26a69a, 0), "Up wave", group = GRP_D)
color dnCol = input.color(color.new(#ef5350, 0), "Down wave", group = GRP_D)
color limboCol = input.color(color.new(color.gray, 40), "Untotaled volume", group = GRP_D)
bool showTbl = input.bool(true, "Status table", group = GRP_D)
int maxWaves = input.int(400, "Waves kept", minval = 20, maxval = 500, group = GRP_D)
float scaleCap = input.float(3.0, "Pane scale cap, multiple of median wave volume", minval = 0.0, step = 0.5, group = GRP_D, tooltip = "Box mode only. Stops one outlier wave, such as a closing-auction bar, from flattening every other wave. Taller boxes run off the top of the pane and their true volume stays in the chart label. 0 disables the cap.")
string GRP_A = "Analytics and alerts"
int lookN = input.int(3, "Compare against prior N same-direction waves", minval = 1, maxval = 20, group = GRP_A, tooltip = "A wave with more volume than all N is drawn solid. A completed wave with less volume than all N is drawn faint.")
bool showSot = input.bool(true, "Flag shortening of thrust", group = GRP_A, tooltip = "Three successive same-direction waves making new highs (or lows) where the last gain is smaller than the one before. Measured on bar highs and lows, as the book specifies.")
bool alFlip = input.bool(true, "Alert: wave reversal", group = GRP_A)
bool alBig = input.bool(false, "Alert: largest volume in N waves", group = GRP_A)
bool alSot = input.bool(false, "Alert: shortening of thrust", group = GRP_A)
// ───────────────────────── Reversal size
// Weis's published intraday reversals ("Weis Wave Trade Setups"). na when he published none.
f_weisPreset() =>
string r = syminfo.root
string t = syminfo.type
float p = na
if r == "ES" or r == "MES"
p := 0.75
if r == "NQ" or r == "MNQ"
p := 2.0
if r == "YM" or r == "MYM"
p := 5.0
if r == "RTY" or r == "M2K"
p := 0.40
if r == "ZB"
p := 0.09375
if r == "ZN" or r == "ZF"
p := 0.015625
if r == "GC" or r == "MGC"
p := 5.0
if r == "SI" or r == "SIL"
p := 0.05
if r == "HG"
p := 0.0025
if r == "ZS" or r == "ZC" or r == "ZW"
p := 1.0
if r == "SB" or r == "CT"
p := 0.10
if r == "CC"
p := 2.0
if r == "KC"
p := 0.25
if r == "6E" or r == "6B" or r == "6A" or r == "6C" or r == "6S" or r == "6N" or r == "M6E"
p := 0.0003
if na(p) and t == "forex"
// 3 pips. A pip is 10 minticks when the quote carries a fractional pip digit.
int decimals = int(math.round(-math.log10(syminfo.mintick)))
float pip = decimals % 2 == 1 ? syminfo.mintick * 10.0 : syminfo.mintick
p := 3.0 * pip
if na(p) and (t == "stock" or t == "fund" or t == "dr")
p := 0.10
p
float preset = f_weisPreset()
bool presetFound = not na(preset)
// Prior completed ATR, so the value is fixed for the whole session on intraday charts.
string atrTf = timeframe.isintraday ? "D" : timeframe.period
float frozenAtr = request.security(syminfo.tickerid, atrTf, ta.atr(atrLen)[1], lookahead = barmerge.lookahead_on)
float atrM = timeframe.isintraday ? atrMult : atrMultHtf
float rawRev = na
if revMode == M_TICK
rawRev := revTicks * syminfo.mintick
else if revMode == M_PRICE
rawRev := revPrice
else if revMode == M_PCT
rawRev := close * revPct / 100.0
else if revMode == M_ATR
rawRev := frozenAtr * atrM
else
rawRev := presetFound ? preset * presetMult : frozenAtr * atrM
// Quantize to whole ticks, minimum one tick.
float revNow = na(rawRev) ? na : math.max(math.round(rawRev / syminfo.mintick) * syminfo.mintick, syminfo.mintick)
float EPS = syminfo.mintick * 0.01
// ───────────────────────── Volume source
float trv = ta.tr(true)
bool trActive = useTR == "Always" or (useTR == "Auto" and na(volume))
float v = nz(trActive ? trv : volume)
// ───────────────────────── Wave storage
type Wave
int dir
int startBar
float startPrice
int startTime
int endBar
float endPrice
int endTime
float vol
float hi
float lo
int rank = 0
bool sot = false
line zz
label lb
box bx
string lblSize = lblSizeIn == "Tiny" ? size.tiny : lblSizeIn == "Small" ? size.small : size.normal
bool useBoxes = histMode == H_BOX
f_volStr(float x) =>
trActive ? str.tostring(math.round(x / syminfo.mintick)) + "t" : autoFmt ? str.tostring(x, format.volume) : str.tostring(x / volDiv, "#,##0.#")
f_ticks(Wave w) =>
int(math.round(math.abs(w.endPrice - w.startPrice) / syminfo.mintick))
// 1 when the wave's volume exceeds all of the prior n same-direction waves.
// -1 when a completed wave's volume is below all of them. 0 otherwise or when history is short.
f_rank(array<Wave> ws, int idx, int n, bool isFinal) =>
Wave w = ws.get(idx)
float mx = na
float mn = na
int cnt = 0
int i = idx - 2
while i >= 0 and cnt < n
Wave p = ws.get(i)
mx := na(mx) ? p.vol : math.max(mx, p.vol)
mn := na(mn) ? p.vol : math.min(mn, p.vol)
cnt += 1
i -= 2
int res = 0
if cnt == n
if w.vol > mx
res := 1
else if isFinal and w.vol < mn
res := -1
res
f_sot(array<Wave> ws, int idx) =>
bool res = false
if idx >= 4
Wave a = ws.get(idx - 4)
Wave b = ws.get(idx - 2)
Wave c = ws.get(idx)
if c.dir == 1
res := c.hi > b.hi and b.hi > a.hi and (c.hi - b.hi) < (b.hi - a.hi)
else
res := c.lo < b.lo and b.lo < a.lo and (b.lo - c.lo) < (a.lo - b.lo)
res
method render(Wave w) =>
color c = w.dir == 1 ? upCol : dnCol
int transp = w.rank == 1 ? 0 : w.rank == -1 ? 80 : 45
string vs = f_volStr(w.vol)
int ticks = f_ticks(w)
int bars = w.endBar - w.startBar
if showZig
if na(w.zz)
w.zz := line.new(w.startBar, w.startPrice, w.endBar, w.endPrice, xloc = xloc.bar_index, color = c, width = zigWidth, force_overlay = true)
w.zz.set_xy2(w.endBar, w.endPrice)
if showLbl
string txt = vs + (lblLen ? "\n" + str.tostring(ticks) + "t" : "") + (lblDur ? "\n" + str.tostring(bars) + "b" : "") + (w.sot and showSot ? "\nSOT" : "")
float perTick = ticks > 0 ? w.vol / ticks : na
float mins = (w.endTime - w.startTime) / 60000.0
string tip = (w.dir == 1 ? "Up wave" : "Down wave") + "\nVolume: " + vs + "\nLength: " + str.tostring(ticks) + " ticks" + "\nDuration: " + str.tostring(bars) + " bars, " + str.tostring(mins, "#.#") + " min" + "\nVolume per tick: " + (na(perTick) ? "n/a" : f_volStr(perTick)) + (w.rank == 1 ? "\nLargest volume in " + str.tostring(lookN) + " same-direction waves" : w.rank == -1 ? "\nSmallest volume in " + str.tostring(lookN) + " same-direction waves" : "") + (w.sot ? "\nShortening of thrust" : "")
if na(w.lb)
w.lb := label.new(w.endBar, w.endPrice, txt, xloc = xloc.bar_index, yloc = w.dir == 1 ? yloc.abovebar : yloc.belowbar, color = color(na), style = label.style_none, textcolor = c, size = lblSize, tooltip = tip, force_overlay = true)
w.lb.set_x(w.endBar)
w.lb.set_text(txt)
w.lb.set_tooltip(tip)
if useBoxes
if na(w.bx)
w.bx := box.new(w.startBar, w.vol, w.endBar, 0.0, border_color = c, border_width = 1, xloc = xloc.bar_index, bgcolor = color.new(c, transp), text_size = size.small, text_color = chart.fg_color, text_valign = text.align_top)
w.bx.set_right(w.endBar)
w.bx.set_top(w.vol)
w.bx.set_bgcolor(color.new(c, transp))
w.bx.set_text(showPaneTxt ? vs : "")
true
method erase(Wave w) =>
w.zz.delete()
w.lb.delete()
w.bx.delete()
true
// ───────────────────────── State
var array<Wave> waves = array.new<Wave>()
var int dir = 0
var float extPrice = na
var int extBar = na
var int extTime = na
var float rev = na
var float pendVol = 0.0
var float pendHi = na
var float pendLo = na
var box pendBox = na
var int wavesToday = 0
var float medVol = na
if timeframe.change("D")
wavesToday := 0
bool evFlipUp = false
bool evFlipDn = false
bool evBig = false
bool evSot = false
float plotVol = na
color plotCol = na
if barstate.isconfirmed and not na(revNow)
if na(extPrice)
// Anchor. Direction is undetermined until price moves a full reversal from here.
extPrice := close
extBar := bar_index
extTime := time
rev := revNow
else
bool newExt = dir == 1 ? close >= extPrice : dir == -1 ? close <= extPrice : false
bool flipUp = dir != 1 and close - extPrice >= rev - EPS
bool flipDn = dir != -1 and extPrice - close >= rev - EPS
if newExt
Wave w = waves.last()
w.vol := w.vol + pendVol + v
w.endBar := bar_index
w.endPrice := close
w.endTime := time
w.hi := math.max(w.hi, nz(pendHi, high), high)
w.lo := math.min(w.lo, nz(pendLo, low), low)
int prevRank = w.rank
w.rank := f_rank(waves, waves.size() - 1, lookN, false)
evBig := w.rank == 1 and prevRank != 1
extPrice := close
extBar := bar_index
extTime := time
pendVol := 0.0
pendHi := na
pendLo := na
w.render()
plotVol := w.vol
plotCol := color.new(dir == 1 ? upCol : dnCol, w.rank == 1 ? 0 : 45)
else if flipUp or flipDn
int nd = flipUp ? 1 : -1
if waves.size() > 0
Wave o = waves.last()
// The thrust extreme can print on a bar after the extreme close, including this one.
o.hi := math.max(o.hi, nz(pendHi, high), high)
o.lo := math.min(o.lo, nz(pendLo, low), low)
o.rank := f_rank(waves, waves.size() - 1, lookN, true)
o.sot := f_sot(waves, waves.size() - 1)
evSot := o.sot
o.render()
// Median volume of the last 30 completed waves, used only to cap the pane scale.
array<float> recent = array.new<float>()
int k = waves.size() - 1
while k >= 0 and recent.size() < 30
Wave q = waves.get(k)
recent.push(q.vol)
k -= 1
medVol := recent.median()
Wave n = Wave.new(nd, extBar, extPrice, extTime, bar_index, close, time, pendVol + v, math.max(nz(pendHi, high), high), math.min(nz(pendLo, low), low))
waves.push(n)
n.rank := f_rank(waves, waves.size() - 1, lookN, false)
evBig := n.rank == 1
n.render()
dir := nd
evFlipUp := flipUp
evFlipDn := flipDn
wavesToday += 1
extPrice := close
extBar := bar_index
extTime := time
rev := revNow
pendVol := 0.0
pendHi := na
pendLo := na
plotVol := n.vol
plotCol := color.new(nd == 1 ? upCol : dnCol, n.rank == 1 ? 0 : 45)
else
pendVol += v
pendHi := math.max(nz(pendHi, high), high)
pendLo := math.min(nz(pendLo, low), low)
plotVol := pendVol
plotCol := limboCol
if waves.size() > maxWaves
Wave old = waves.shift()
old.erase()
// Untotaled volume block. It is absorbed by the current wave or becomes the next wave.
if useBoxes and pendVol > 0
if na(pendBox)
pendBox := box.new(extBar, pendVol, bar_index, 0.0, border_color = limboCol, border_width = 1, xloc = xloc.bar_index, bgcolor = color.new(limboCol, 70))
pendBox.set_left(extBar)
pendBox.set_right(bar_index)
pendBox.set_top(pendVol)
if pendVol == 0 and not na(pendBox)
pendBox.delete()
pendBox := na
if alFlip and (evFlipUp or evFlipDn) and waves.size() > 1
Wave done = waves.get(waves.size() - 2)
alert(syminfo.ticker + " Weis wave turned " + (evFlipUp ? "up" : "down") + ". Completed " + (done.dir == 1 ? "up" : "down") + " wave: volume " + f_volStr(done.vol) + ", " + str.tostring(f_ticks(done)) + " ticks, " + str.tostring(done.endBar - done.startBar) + " bars.", alert.freq_once_per_bar_close)
if alBig and evBig
alert(syminfo.ticker + " " + (dir == 1 ? "up" : "down") + " wave volume is the largest of the last " + str.tostring(lookN) + " same-direction waves.", alert.freq_once_per_bar_close)
if alSot and evSot
alert(syminfo.ticker + " shortening of thrust on the completed " + (dir == 1 ? "down" : "up") + " wave.", alert.freq_once_per_bar_close)
// Volume of the live wave through its extreme bar.
float liveVol = na
if waves.size() > 0
Wave lw = waves.last()
liveVol := lw.vol
// Provisional column for the open realtime bar. Nothing is committed until the bar closes.
if not barstate.isconfirmed and dir != 0 and waves.size() > 0
bool pExt = dir == 1 ? close >= extPrice : close <= extPrice
bool pFlip = dir == 1 ? extPrice - close >= rev - EPS : close - extPrice >= rev - EPS
plotVol := pExt ? liveVol + pendVol + v : pendVol + v
plotCol := pExt ? color.new(dir == 1 ? upCol : dnCol, 45) : pFlip ? color.new(dir == 1 ? dnCol : upCol, 45) : limboCol
// ───────────────────────── Output
plot(useBoxes ? na : plotVol, "Wave volume", color = plotCol, style = plot.style_columns)
// Drawing objects do not drive autoscale, so an invisible plot carries the scale in box mode.
float rawScale = math.max(liveVol, pendVol)
float capVal = scaleCap > 0 and not na(medVol) ? medVol * scaleCap : rawScale
float scaleVal = useBoxes and not na(liveVol) ? math.min(rawScale, capVal) : na
plot(scaleVal, "Scale anchor", color = color.new(color.gray, 100), editable = false, display = display.pane)
hline(0, "Zero", color = color.new(color.gray, 60), linestyle = hline.style_solid)
alertcondition(evFlipUp, "Wave turned up", "Weis wave turned up")
alertcondition(evFlipDn, "Wave turned down", "Weis wave turned down")
alertcondition(evBig, "Largest volume in N waves", "Weis wave volume is the largest of the last N same-direction waves")
alertcondition(evSot, "Shortening of thrust", "Shortening of thrust on the completed Weis wave")
var table info = table.new(position.top_right, 1, 1)
if showTbl and barstate.islast
string src = revMode == M_AUTO ? (presetFound ? "Weis preset x" + str.tostring(presetMult) : "no Weis preset, ATR fallback") : revMode
string txt = "Reversal " + str.tostring(rev, format.mintick) + " = " + str.tostring(math.round(rev / syminfo.mintick)) + " ticks, " + src + (timeframe.isintraday ? "\nWaves today: " + str.tostring(wavesToday) : "") + (trActive ? "\nTrue range used as volume" : "")
if waves.size() > 0
Wave firstKept = waves.first()
txt := txt + "\nDrawn since " + str.format_time(firstKept.startTime, "yyyy-MM-dd", syminfo.timezone) + ", " + str.tostring(waves.size()) + " waves"
table.cell(info, 0, 0, txt, text_color = chart.fg_color, text_size = size.small, text_halign = text.align_right, bgcolor = color.new(chart.bg_color, 20))