本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Sage Wave (RichPole)
// Adds up volume inside each price wave and shows the total as columns.
// When a wave ends, a short note on the price chart says what kind of wave it was.
// Wave turns use closed candles only. Finished waves are never redrawn.
// Educational tool. It does not give buy or sell signals.
indicator("Sage Wave (RichPole)", shorttitle = "Sage Wave", overlay = false, format = format.volume, max_labels_count = 500)
// ---------- Inputs ----------
grpW = "Wave detection"
atrPeriod = input.int(14, "ATR period", minval = 1, group = grpW, display = display.none)
reversalATR = input.float(1.0, "Reversal size (x ATR)", minval = 0.0, step = 0.1, group = grpW, display = display.none, tooltip = "Bigger = fewer, longer waves. Smaller = more, shorter waves.")
minMoveMode = input.string("Off", "Minimum reversal type", options = ["Off", "Percent of price", "Ticks", "Price units"], group = grpW, display = display.none)
minMoveVal = input.float(0.025, "Minimum reversal value", minval = 0.0, step = 0.005, group = grpW, display = display.none)
smoothLen = input.int(2, "Price smoothing (EMA of closes)", minval = 1, group = grpW, display = display.none)
confirmBars = input.int(2, "Closed bars to confirm a turn", minval = 1, group = grpW, display = display.none)
minWaveBars = input.int(2, "Minimum bars in a wave", minval = 1, group = grpW, display = display.none)
grpN = "Wave notes"
showNotes = input.bool(true, "Show wave notes", group = grpN, display = display.none)
notePlace = input.string("Price chart", "Notes on", options = ["Price chart", "Volume pane"], group = grpN, display = display.none)
noteFilter = input.string("Key waves only", "Which waves", options = ["All waves", "Key waves only"], group = grpN, display = display.none, tooltip = "Key waves = Strong, Tiring and Heavy volume.")
noteText = input.string("Words only", "Note text", options = ["Words + volume", "Words only", "Volume only"], group = grpN, display = display.none)
avgWaves = input.int(10, "Waves to compare with", minval = 3, group = grpN, display = display.none, tooltip = "Each wave is compared with the average of this many earlier waves.")
strongMult = input.float(2.0, "Strong = volume above (x average)", minval = 1.0, step = 0.1, group = grpN, display = display.none)
lightMult = input.float(0.5, "Light = volume below (x average)", minval = 0.0, maxval = 1.0, step = 0.1, group = grpN, display = display.none)
blockMult = input.float(0.5, "Heavy volume = move below (x average move)", minval = 0.0, maxval = 1.0, step = 0.1, group = grpN, display = display.none, tooltip = "Heavy volume, buy = sell: at least average volume, but price moved less than this share of the average wave move.")
grpC = "Colours"
upColor = input.color(#26a69a, "Buying (up wave)", group = grpC, display = display.none)
dnColor = input.color(#ef5350, "Selling (down wave)", group = grpC, display = display.none)
tireColor = input.color(#f7c948, "Tiring note", group = grpC, display = display.none)
heavyColor = input.color(#ff9800, "Heavy volume note", group = grpC, display = display.none)
colorBars = input.bool(false, "Colour candles by wave", group = grpC, display = display.none)
// ---------- Helpers ----------
minMove(float p) =>
switch minMoveMode
"Percent of price" => p * minMoveVal / 100.0
"Ticks" => minMoveVal * syminfo.mintick
"Price units" => minMoveVal
=> 0.0
fmtVol(float x) =>
x >= 1000000.0 ? str.tostring(x / 1000000.0, "#.##") + "M" : x >= 10000.0 ? str.tostring(x / 1000.0, "#.#") + "K" : x >= 1000.0 ? str.tostring(x / 1000.0, "#.##") + "K" : str.tostring(x, "#")
pushLimited(array<float> a, float v, int n) =>
array.push(a, v)
if array.size(a) > n
array.shift(a)
// ---------- Volume check ----------
var int volBars = 0
if not na(volume) and volume > 0
volBars += 1
bool hasVolume = volBars > 0
// ---------- Wave state ----------
var int direction = 0
var float extreme = na
var float smoothed = na
var float seed = na
var float rawExtreme = na
var float waveStart = na
var float atrv = 0.0
var int samples = 0
var int pending = 0
var int waveBars = 0
var float acc = 0.0
var float waveHigh = na
var int waveHighBar = na
var float waveLow = na
var int waveLowBar = na
var bool haveUp = false
var bool haveDn = false
var float lastUpTotal = 0.0
var float lastDnTotal = 0.0
var float lastUpExt = 0.0
var float lastDnExt = 0.0
var array<float> volHist = array.new<float>()
var array<float> moveHist = array.new<float>()
float vol = nz(volume)
float alpha = 2.0 / (smoothLen + 1.0)
bool newUp = false
bool newDown = false
bool tiringEnd = false
bool heavyEnd = false
bool strongEnd = false
if barstate.isconfirmed
if bar_index == 0
extreme := close
smoothed := close
seed := close
rawExtreme := close
else
float tr = math.max(high - low, math.max(math.abs(high - close[1]), math.abs(low - close[1])))
samples += 1
atrv := samples <= atrPeriod ? atrv + (tr - atrv) / samples : atrv + (tr - atrv) / atrPeriod
smoothed := smoothed + alpha * (close - smoothed)
float price = smoothed
float threshold = math.max(minMove(close), reversalATR * atrv)
if samples < atrPeriod
seed := price
else if direction == 0
int candidate = 0
if price - seed >= threshold
candidate := 1
else if seed - price >= threshold
candidate := -1
if candidate == 0
pending := 0
else if candidate > 0
pending := pending > 0 ? pending + 1 : 1
else
pending := pending < 0 ? pending - 1 : -1
if math.abs(pending) >= confirmBars
direction := candidate
if direction != 0
extreme := price
rawExtreme := close
waveStart := close
acc := vol
pending := 0
waveBars := 1
waveHigh := high
waveHighBar := bar_index
waveLow := low
waveLowBar := bar_index
else
bool beyond = false
if direction == 1
if price > extreme
extreme := price
beyond := extreme - price >= threshold
if close > rawExtreme
rawExtreme := close
else
if price < extreme
extreme := price
beyond := price - extreme >= threshold
if close < rawExtreme
rawExtreme := close
pending := beyond ? pending + 1 : 0
if pending >= confirmBars and waveBars >= minWaveBars
// ----- The wave that just finished -----
bool up = direction == 1
float move = math.abs(rawExtreme - waveStart)
bool tiring = false
if up
tiring := haveUp and rawExtreme > lastUpExt and acc < lastUpTotal
haveUp := true
lastUpTotal := acc
lastUpExt := rawExtreme
else
tiring := haveDn and rawExtreme < lastDnExt and acc < lastDnTotal
haveDn := true
lastDnTotal := acc
lastDnExt := rawExtreme
bool enough = array.size(volHist) >= 3
float vAvg = enough ? array.avg(volHist) : na
float mAvg = enough ? array.avg(moveHist) : na
bool heavy = enough and not tiring and acc >= vAvg and mAvg > 0 and move <= blockMult * mAvg
bool strong = enough and not tiring and not heavy and acc >= strongMult * vAvg
bool light = enough and not tiring and not heavy and not strong and acc <= lightMult * vAvg
// Note style: bubble colour, text colour, size
color waveCol = up ? upColor : dnColor
string words = up ? "Buying" : "Selling"
string tip = "Normal volume for recent waves."
color bg = waveCol
color fg = color.white
string sz = size.tiny
if tiring
words := up ? "Buyers tiring" : "Sellers tiring"
tip := up ? "New high, but less volume than the last up wave. Buying may be running out." : "New low, but less volume than the last down wave. Selling may be running out."
bg := tireColor
fg := color.black
else if heavy
words := "Heavy vol, buy = sell"
tip := "Heavy volume, but price barely moved. Buyers and sellers are matched."
bg := heavyColor
fg := color.black
else if strong
words := up ? "Strong buying" : "Strong selling"
tip := "Much more volume than recent waves."
else if light
words := up ? "Light buying" : "Light selling"
tip := "Much less volume than recent waves."
bg := color.new(waveCol, 25)
bool isKey = tiring or heavy or strong
if showNotes and hasVolume and (noteFilter == "All waves" or isKey)
string vtxt = fmtVol(acc)
string txt = noteText == "Words only" ? words : noteText == "Volume only" ? vtxt : words + " " + vtxt
string tt = words + " (" + vtxt + ")\n" + tip
if notePlace == "Price chart"
if up
label.new(x = waveHighBar, y = waveHigh, text = txt, xloc = xloc.bar_index, style = label.style_label_down, color = bg, textcolor = fg, size = sz, tooltip = tt, text_formatting = text.format_bold, force_overlay = true)
else
label.new(x = waveLowBar, y = waveLow, text = txt, xloc = xloc.bar_index, style = label.style_label_up, color = bg, textcolor = fg, size = sz, tooltip = tt, text_formatting = text.format_bold, force_overlay = true)
else
label.new(x = bar_index - 1, y = acc, text = txt, xloc = xloc.bar_index, style = label.style_label_down, color = bg, textcolor = fg, size = sz, tooltip = tt, text_formatting = text.format_bold)
tiringEnd := tiring
heavyEnd := heavy
strongEnd := strong
pushLimited(volHist, acc, avgWaves)
pushLimited(moveHist, move, avgWaves)
// ----- The new wave starts on this confirmation candle -----
waveStart := rawExtreme
direction := -direction
newUp := direction == 1
newDown := direction == -1
pending := 0
extreme := price
rawExtreme := close
acc := vol
waveBars := 1
waveHigh := high
waveHighBar := bar_index
waveLow := low
waveLowBar := bar_index
else
acc += vol
waveBars += 1
if high > waveHigh
waveHigh := high
waveHighBar := bar_index
if low < waveLow
waveLow := low
waveLowBar := bar_index
// ---------- Plots ----------
// While a candle is still open, its volume is added live. Wave turns wait for the close.
float shown = barstate.isconfirmed ? acc : acc + vol
plot(hasVolume and direction == 1 ? shown : na, "Up wave volume", color = upColor, style = plot.style_columns)
plot(hasVolume and direction == -1 ? shown : na, "Down wave volume", color = dnColor, style = plot.style_columns)
// Invisible line that leaves room above the tallest column when notes sit in the volume pane
plot(hasVolume and direction != 0 and notePlace == "Volume pane" ? shown * 1.2 : na, "Headroom", color = color.new(color.black, 100), display = display.pane, editable = false)
barcolor(colorBars and hasVolume and direction != 0 ? (direction == 1 ? upColor : dnColor) : na, title = "Candle colour by wave")
// ---------- No-volume warning ----------
var table warn = table.new(position.middle_center, 1, 1)
if barstate.islast and not hasVolume
table.cell(warn, 0, 0, "Sage Wave: this symbol has no volume data.\nPick a symbol from an exchange or broker,\nfor example BINANCE:BTCUSDT or OANDA:XAUUSD.", text_color = color.white, bgcolor = color.new(color.orange, 20), text_size = size.normal)
// ---------- Alerts (fire once the candle closes) ----------
alertcondition(newUp, "New up wave", "Sage Wave: a new up wave started")
alertcondition(newDown, "New down wave", "Sage Wave: a new down wave started")
alertcondition(tiringEnd, "Tiring wave", "Sage Wave: the last wave made a new extreme on less volume")
alertcondition(heavyEnd, "Heavy volume, buy = sell", "Sage Wave: heavy volume but price barely moved")
alertcondition(strongEnd, "Strong wave", "Sage Wave: the last wave had much more volume than usual")