本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Stream Master
//
// The market drawn as a river. The current is a smoothed centre of price. Around it,
// three bands of water follow the current with more and more lag, from a band that
// hugs it to a band that follows a much slower version of it, each drawn as a smooth
// gradient. In a move, the slow water trails behind the fast and the river leaves a
// wake: long and bright when the move is fast, gone when the market stalls. Each edge
// takes the up colour while it is flowing up and the down colour while it is flowing
// down, so a turn passes through the river from the inside out.
//
// The panel reads the river in words: which way the current flows and how hard, how
// many of the four layers have turned, how long the wake is and whether it is growing,
// stretched, fading or turning, and where price sits. Alerts carry the events: the
// current turning, price leaving or rejoining the stream, a pullback to the current
// that holds, the wake stretching further than recent wakes, and the wake fading.
indicator("Stream Master [SimpleAlgo]", "Stream Master [SimpleAlgo]", overlay = true)
// ───────────────────────── Settings ─────────────────────────
grpS = "Stream"
fastLen = input.int(10, "Current (bars)", minval = 2, maxval = 60, group = grpS, tooltip = "The current is price smoothed twice over this many bars. Lower follows price more closely, higher is calmer.")
slowLen = input.int(60, "Wake (bars)", minval = 10, maxval = 300, group = grpS, tooltip = "The slow water at the edge of the river is price smoothed twice over this many bars. The bigger the gap between this and the current, the longer the wake a move leaves.")
widthIn = input.float(3.5, "River width (ATRs)", minval = 0.5, maxval = 10.0, step = 0.5, group = grpS, tooltip = "How wide the river is when the market is slack, in ATRs each side of the current.")
histIn = input.int(1500, "History (bars)", minval = 200, maxval = 5000, step = 100, group = grpS, tooltip = "How far back the river is drawn. Scroll further back and it simply stops. Lower this if your chart feels slow.")
grpE = "Events"
leaveIn = input.float(1.5, "Price has left the stream at (ATRs)", minval = 0.5, maxval = 5.0, step = 0.25, group = grpE, tooltip = "How far price has to close from the current before it counts as having left the stream.")
joinIn = input.float(0.75, "And is back on it within (ATRs)", minval = 0.1, maxval = 3.0, step = 0.25, group = grpE)
pullIn = input.float(0.25, "A pullback reaches the current within (ATRs)", minval = 0.0, maxval = 2.0, step = 0.25, group = grpE, tooltip = "After price has left the stream on the side the river is flowing, a pullback counts when a bar comes back to within this distance of the current and then closes back on the flowing side of it.")
grpL = "Look"
themeIn = input.string("Neon", "Theme", options = ["Neon", "Classic", "Aurora", "Ice", "Custom"], group = grpL)
baseIn = input.color(#7B9CFF, "Slack", group = grpL, inline = "c")
upIn = input.color(#00FFA3, "Up", group = grpL, inline = "c")
dnIn = input.color(#FF3D71, "Down", group = grpL, inline = "c")
bright = input.int(3, "Brightness", minval = 1, maxval = 5, group = grpL)
slackDim = input.int(2, "Slack water", minval = 1, maxval = 5, group = grpL, tooltip = "How visible the river is when nothing is moving. 1 is almost invisible, 5 keeps it as bright as a move.")
showCore = input.bool(true, "Current line", group = grpL)
showPanel = input.bool(true, "Panel", group = grpL, inline = "p")
panelPos = input.string("Top right", "", options = ["Top left", "Top right", "Bottom left", "Bottom right"], group = grpL, inline = "p")
panelSize = input.string("Normal", "", options = ["Small", "Normal", "Large"], group = grpL, inline = "p")
[colBase, colUp, colDn, colPanel] = switch themeIn
"Neon" => [#7B9CFF, #00FFA3, #FF3D71, #262B38]
"Classic" => [#5B8DEF, #00FF31, #FF0013, #23262F]
"Aurora" => [#A78BFA, #34D399, #F472B6, #262B38]
"Ice" => [#67E8F9, #7DD3FC, #FDA4AF, #1E293B]
=> [baseIn, upIn, dnIn, #262B38]
// fixed tuning: how the layers are spread between the current and the slow water, how
// the width, the direction and the layer colours are smoothed, when a slope counts as
// flowing, and how many finished wakes are remembered to judge whether the present one
// is stretched
float shapeK = 1.5
int atrLen = 20
int dirLen = 8 // smoothing of the current's slope, which sets its direction
int colLen = 12 // smoothing of each layer's slope, which sets its colour
float fullAt = 0.12 // ATRs per bar for full colour
float deadAt = 0.15 // below this share of full colour a layer is slack
int keepWakes = 20
int stretchPct = 75 // a wake longer than this share of remembered wakes is stretched
// ───────────────────────── The current ─────────────────────────
float atr = nz(ta.atr(14), high - low)
float atrS = ta.ema(atr, atrLen)
float centre = ta.ema(ta.ema(close, fastLen), fastLen)
float cslow = ta.ema(ta.ema(close, slowLen), slowLen)
// the flow is the current's slope in ATRs per bar, smoothed, and t is the flow as a share
// of full colour, from -1 to 1. The wake is how far the current has run ahead of the slow
// water, in ATRs.
float slope = (centre - nz(centre[1], centre)) / atr
float flow = ta.ema(slope, dirLen)
float t = math.max(-1.0, math.min(1.0, flow / fullAt))
float wake = (centre - cslow) / atr
float wakeAbs = math.abs(wake)
// the current flows one way until its slope has crossed the dead zone to the other side
var int dir = 0
var int turnBar = na
int prevDir = dir
if t >= deadAt
dir := 1
else if t <= -deadAt
dir := -1
if dir != prevDir
turnBar := bar_index
// the wake: which way it points, its longest so far, whether it is growing or fading, and
// how it compares with the wakes before it
var peaks = array.new_float()
int wakeSign = wake > 0.5 ? 1 : wake < -0.5 ? -1 : 0
var int wakeSide = 0
var float wakePeak = 0.0
var bool fadeFired = false
var bool stretchFired = false
if wakeSign != wakeSide
if wakeSide != 0
array.push(peaks, wakePeak)
if array.size(peaks) > keepWakes
array.shift(peaks)
wakeSide := wakeSign
wakePeak := wakeAbs
fadeFired := false
stretchFired := false
else if wakeAbs > wakePeak
wakePeak := wakeAbs
bool wakeOn = wakeSide != 0
bool wakeTurning = wakeOn and dir != 0 and dir != wakeSide
bool wakeFading = wakeOn and not wakeTurning and wakeAbs < wakePeak * 0.75
bool wakeGrowing = wakeOn and not wakeTurning and wakeAbs >= wakePeak * 0.98
float stretchAt = array.size(peaks) >= 5 ? array.percentile_nearest_rank(peaks, stretchPct) : na
bool wakeStretched = wakeOn and not na(stretchAt) and wakeAbs >= stretchAt
// ───────────────────────── Price and the stream ─────────────────────────
// where price is relative to the current, in ATRs
float dist = (close - centre) / atr
// leaving and rejoining the stream
var bool outside = false
var int outSide = 0
bool leftNow = false
bool backNow = false
if not outside and math.abs(dist) >= leaveIn
outside := true
outSide := dist > 0 ? 1 : -1
leftNow := true
else if outside and math.abs(dist) <= joinIn
outside := false
backNow := true
// a pullback: price has left the stream on the side the river is flowing, comes back to
// the current, and closes back on the flowing side of it
int pullCtx = wakeOn and dir == wakeSide ? dir : 0
var int ahead = 0
var bool reached = false
if pullCtx == 0
ahead := 0
reached := false
else if outside and outSide == pullCtx
ahead := pullCtx
bool touched = pullCtx == 1 ? low <= centre + pullIn * atr : high >= centre - pullIn * atr
if ahead != 0 and touched
reached := true
bool pullDone = reached and (pullCtx == 1 ? close > centre : close < centre)
if pullDone
ahead := 0
reached := false
// one event a bar, the most important first
// 1 turned up, -1 turned down, 2 left above, -2 left below, 3 rejoined from above,
// -3 rejoined from below, 4 wake fading, 5 pullback held above, -5 pullback held below, 6 wake stretched
int evt = dir != prevDir ? dir : pullDone ? pullCtx * 5 : leftNow ? outSide * 2 : backNow ? outSide * 3 : (wakeFading and not fadeFired) ? 4 : (wakeStretched and not stretchFired) ? 6 : 0
if evt == 4
fadeFired := true
if evt == 6
stretchFired := true
bool fire = evt != 0 and barstate.isconfirmed
var int lastEvt = 0
var int lastEvtBar = na
if fire
lastEvt := evt
lastEvtBar := bar_index
// ───────────────────────── The river ─────────────────────────
// how far a slope is past the dead zone, 0 at its edge and 1 at full colour, so colour
// and brightness rise smoothly out of slack water instead of jumping
ramp(float x) =>
float a = math.abs(x)
a < deadAt ? 0.0 : (a - deadAt) / (1 - deadAt)
// colour by the direction a layer is flowing. A slope inside the dead zone is drawn
// slack, so the river does not flicker between colours while nothing is moving.
tint(float x) =>
float s = ramp(x)
x >= 0 ? color.from_gradient(s, 0, 1, colBase, colUp) : color.from_gradient(s, 0, 1, colBase, colDn)
// a layer's colour is held to steps of a tenth, and a step only moves once the slope has
// travelled a full step, so a fill keeps one colour for a run of bars instead of
// flickering between two shades while the slope sits on a boundary
steps(float raw) =>
var float held = 0.0
if raw >= held + 0.1
held := math.floor(raw * 10) / 10
else if raw <= held - 0.1
held := math.ceil(raw * 10) / 10
held
float brightMul = 0.4 + 0.2 * bright
float slackMul = 0.15 + 0.15 * slackDim
bool active = last_bar_index - bar_index <= histIn
// One edge of the river, f = 0 at the current and 1 at the slow water. The edge's
// reference blends from the current to the slow water, its width grows with f, and its
// own slope sets its colour, so a turn shows up at the inner edges first.
edge(float f) =>
float ref = cslow + (centre - cslow) * math.pow(1 - f, shapeK)
float sl = (ref - nz(ref[1], ref)) / atr
float fl = ta.ema(sl, colLen)
float tj = steps(math.max(-1.0, math.min(1.0, fl / fullAt)))
float w = f * widthIn * atrS
[active ? ref + w : na, active ? ref - w : na, tj]
// brightest at the current, fading to the slow water, and dimmer when the edge is slack
edgeColor(float f, float tj) =>
float alpha = (0.30 * (1 - f) + 0.04) * brightMul * (slackMul + (1 - slackMul) * ramp(tj))
int transp = math.min(99, math.max(0, math.round(100 - alpha * 100)))
color.new(tint(tj), transp)
// Each band is one gradient fill between two edges: the colour runs from the inner
// edge's colour at the inner edge to the outer edge's colour at the outer edge. In a
// wake the outer edge can sit on the far side of the inner one, so the gradient is
// always given its higher and lower value explicitly.
band(float yIn, float yOut, color cIn, color cOut) =>
bool inAbove = yIn >= yOut
[math.max(yIn, yOut), math.min(yIn, yOut), inAbove ? cIn : cOut, inAbove ? cOut : cIn]
float t0 = steps(t)
[u1, d1, t1] = edge(0.35)
[u2, d2, t2] = edge(0.70)
[u3, d3, t3] = edge(1.00)
color c0 = edgeColor(0.00, t0)
color c1 = edgeColor(0.35, t1)
color c2 = edgeColor(0.70, t2)
color c3 = edgeColor(1.00, t3)
pC = plot(active ? centre : na, "", color = na, display = display.none, editable = false)
pU1 = plot(u1, "", color = na, display = display.none, editable = false)
pU2 = plot(u2, "", color = na, display = display.none, editable = false)
pU3 = plot(u3, "", color = na, display = display.none, editable = false)
pD1 = plot(d1, "", color = na, display = display.none, editable = false)
pD2 = plot(d2, "", color = na, display = display.none, editable = false)
pD3 = plot(d3, "", color = na, display = display.none, editable = false)
[hiU1, loU1, hiCU1, loCU1] = band(centre, u1, c0, c1)
[hiU2, loU2, hiCU2, loCU2] = band(u1, u2, c1, c2)
[hiU3, loU3, hiCU3, loCU3] = band(u2, u3, c2, c3)
[hiD1, loD1, hiCD1, loCD1] = band(centre, d1, c0, c1)
[hiD2, loD2, hiCD2, loCD2] = band(d1, d2, c1, c2)
[hiD3, loD3, hiCD3, loCD3] = band(d2, d3, c2, c3)
fill(pC, pU1, hiU1, loU1, hiCU1, loCU1, title = "River", editable = false)
fill(pU1, pU2, hiU2, loU2, hiCU2, loCU2, title = "River", editable = false)
fill(pU2, pU3, hiU3, loU3, hiCU3, loCU3, title = "River", editable = false)
fill(pC, pD1, hiD1, loD1, hiCD1, loCD1, title = "River", editable = false)
fill(pD1, pD2, hiD2, loD2, hiCD2, loCD2, title = "River", editable = false)
fill(pD2, pD3, hiD3, loD3, hiCD3, loCD3, title = "River", editable = false)
// the current itself, with a soft glow
color coreCol = tint(t)
plot(showCore ? centre : na, "Current glow", color = color.new(coreCol, 86), linewidth = 6, display = display.pane, editable = false)
plot(showCore ? centre : na, "Current", color = coreCol, linewidth = 2, display = display.pane)
// how many of the four layers (the current and the three edges) are flowing each way
flowing(float x) =>
math.abs(x) < deadAt ? 0 : x > 0 ? 1 : -1
int f0 = flowing(t)
int f1 = flowing(t1)
int f2 = flowing(t2)
int f3 = flowing(t3)
int nUp = (f0 == 1 ? 1 : 0) + (f1 == 1 ? 1 : 0) + (f2 == 1 ? 1 : 0) + (f3 == 1 ? 1 : 0)
int nDn = (f0 == -1 ? 1 : 0) + (f1 == -1 ? 1 : 0) + (f2 == -1 ? 1 : 0) + (f3 == -1 ? 1 : 0)
// ───────────────────────── Panel ─────────────────────────
eventName(int e) =>
switch e
1 => "Current turned up"
-1 => "Current turned down"
2 => "Price left the stream above"
-2 => "Price left the stream below"
3 => "Price rejoined from above"
-3 => "Price rejoined from below"
4 => "Wake fading"
5 => "Pullback held above the current"
-5 => "Pullback held below the current"
6 => "Wake stretched"
=> "None yet"
eventColor(int e, color up, color dn, color plain) =>
e == 1 or e == 2 or e == -3 or e == 5 ? up : e == -1 or e == -2 or e == 3 or e == -5 ? dn : plain
var table panel = na
if showPanel and barstate.islast
if na(panel)
tablePos = switch panelPos
"Top left" => position.top_left
"Bottom left" => position.bottom_left
"Bottom right" => position.bottom_right
=> position.top_right
panel := table.new(tablePos, 2, 7, bgcolor = color.new(colPanel, 15), frame_color = color.new(colPanel, 0), frame_width = 1, border_width = 0)
txtSize = panelSize == "Small" ? size.small : panelSize == "Large" ? size.large : size.normal
color txt = #D1D4DC
color dim = color.new(#D1D4DC, 40)
float strength = math.abs(t)
bool slack = strength < deadAt
string strTxt = strength >= 0.66 ? "strong" : strength >= 0.33 ? "moderate" : "weak"
string curTxt = slack ? "Slack" : (dir == 1 ? "Flowing up, " : "Flowing down, ") + strTxt
color curCol = slack ? colBase : dir == 1 ? colUp : colDn
string layTxt = nUp == 0 and nDn == 0 ? "All slack" : nDn == 0 ? str.tostring(nUp) + " of 4 flowing up" : nUp == 0 ? str.tostring(nDn) + " of 4 flowing down" : str.tostring(nUp) + " up, " + str.tostring(nDn) + " down"
color layCol = nUp == 0 and nDn == 0 ? colBase : nUp > nDn ? colUp : nDn > nUp ? colDn : txt
string wakeState = wakeTurning ? ", turning" : wakeFading ? ", fading" : wakeStretched ? ", stretched" : wakeGrowing ? ", growing" : ""
string wakeTxt = not wakeOn ? "None" : (wakeSide == 1 ? "Up, " : "Down, ") + str.tostring(wakeAbs, "#.#") + " ATR" + wakeState
color wakeCol = not wakeOn ? txt : wakeSide == 1 ? colUp : colDn
string priceTxt = math.abs(dist) < joinIn ? "On the stream" : (dist > 0 ? "Above it by " : "Below it by ") + str.tostring(math.abs(dist), "#.#") + " ATR"
color priceCol = math.abs(dist) < joinIn ? txt : dist > 0 ? colUp : colDn
int sinceBars = na(turnBar) ? 0 : bar_index - turnBar
string sinceTxt = na(turnBar) ? "" : sinceBars == 0 ? "This bar" : sinceBars == 1 ? "1 bar" : str.tostring(sinceBars) + " bars"
int agoBars = na(lastEvtBar) ? 0 : bar_index - lastEvtBar
string ago = na(lastEvtBar) ? "" : agoBars == 0 ? ", this bar" : agoBars == 1 ? ", 1 bar ago" : ", " + str.tostring(agoBars) + " bars ago"
// an event on the live bar is shown as pending until the bar closes
bool pending = evt != 0 and not barstate.isconfirmed
string evtTxt = pending ? eventName(evt) + ", confirms on close" : eventName(lastEvt) + ago
color evtCol = pending ? dim : lastEvt == 4 or lastEvt == 6 ? wakeCol : eventColor(lastEvt, colUp, colDn, txt)
table.cell(panel, 0, 0, "STREAM MASTER", text_color = txt, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 0, "", text_color = dim, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 1, "Current", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 1, curTxt, text_color = curCol, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 2, "Layers", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 2, layTxt, text_color = layCol, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 3, "Wake", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 3, wakeTxt, text_color = wakeCol, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 4, "Price", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 4, priceTxt, text_color = priceCol, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 5, "Since the turn", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 5, sinceTxt, text_color = txt, text_size = txtSize, text_halign = text.align_right)
table.cell(panel, 0, 6, "Last event", text_color = dim, text_size = txtSize, text_halign = text.align_left)
table.cell(panel, 1, 6, evtTxt, text_color = evtCol, text_size = txtSize, text_halign = text.align_right)
// ───────────────────────── Alerts ─────────────────────────
alertcondition(fire and evt == 1, "Current turned up", "Stream Master: the current turned up")
alertcondition(fire and evt == -1, "Current turned down", "Stream Master: the current turned down")
alertcondition(fire and (evt == 2 or evt == -2), "Price left the stream", "Stream Master: price has left the stream")
alertcondition(fire and (evt == 3 or evt == -3), "Price rejoined the stream", "Stream Master: price is back on the stream")
alertcondition(fire and (evt == 5 or evt == -5), "Pullback to the current", "Stream Master: price pulled back to the current and held")
alertcondition(fire and evt == 6, "Wake stretched", "Stream Master: the wake is longer than most recent wakes, the move is stretched")
alertcondition(fire and evt == 4, "Wake fading", "Stream Master: the wake is fading, the move is losing speed")