Quad Trend Cloud [@Mishu]

Mishundare · study · 97 行 · 点赞 9 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// © Mishundare | IntelligenceFlow
// Successor of "4-Lines [@Mishu]" / "4-Lines Moving Average [@Mishu]", rewritten and extended.
indicator("Quad Trend Cloud [@Mishu]", shorttitle = "QTC", overlay = true)
 
// ───── Inputs ─────
grpM = "Moving averages"
t1 = input.string("EMA", "MA 1", options = ["EMA", "SMA", "WMA", "HMA", "RMA"], inline = "m1", group = grpM)
l1 = input.int(20,  "", minval = 1, inline = "m1", group = grpM)
t2 = input.string("SMA", "MA 2", options = ["EMA", "SMA", "WMA", "HMA", "RMA"], inline = "m2", group = grpM)
l2 = input.int(50,  "", minval = 1, inline = "m2", group = grpM)
t3 = input.string("SMA", "MA 3", options = ["EMA", "SMA", "WMA", "HMA", "RMA"], inline = "m3", group = grpM)
l3 = input.int(100, "", minval = 1, inline = "m3", group = grpM)
t4 = input.string("SMA", "MA 4", options = ["EMA", "SMA", "WMA", "HMA", "RMA"], inline = "m4", group = grpM)
l4 = input.int(200, "", minval = 1, inline = "m4", group = grpM)
src = input.source(close, "Source", group = grpM)
 
grpV = "Display"
showLines = input.bool(true, "MA lines", group = grpV)
showCloud = input.bool(true, "Trend cloud (MA 1 ↔ MA 4)", group = grpV)
colorBars = input.bool(false, "Color bars by trend score", group = grpV)
showCross = input.bool(true, "Mark MA 2 / MA 4 crosses", group = grpV)
showTable = input.bool(true, "Trend table", group = grpV)
colBull   = input.color(#26a69a, "Bullish", group = grpV)
colBear   = input.color(#ef5350, "Bearish", group = grpV)
c1 = input.color(#ff9800, "MA 1", inline = "c", group = grpV)
c2 = input.color(#f23645, "MA 2", inline = "c", group = grpV)
c3 = input.color(#4caf50, "MA 3", inline = "c", group = grpV)
c4 = input.color(#00bcd4, "MA 4", inline = "c", group = grpV)
 
// ───── Calculation ─────
ma(string t, int len) =>
    e = ta.ema(src, len)
    s = ta.sma(src, len)
    w = ta.wma(src, len)
    h = ta.hma(src, len)
    r = ta.rma(src, len)
    switch t
        "EMA" => e
        "SMA" => s
        "WMA" => w
        "HMA" => h
        => r
 
m1 = ma(t1, l1)
m2 = ma(t2, l2)
m3 = ma(t3, l3)
m4 = ma(t4, l4)
 
// Trend score from -4 (fully bearish stack) to +4 (fully bullish stack)
score = (close > m1 ? 1 : -1) + (m1 > m2 ? 1 : -1) + (m2 > m3 ? 1 : -1) + (m3 > m4 ? 1 : -1)
scoreCol = color.from_gradient(score, -4, 4, colBear, colBull)
 
fullBull = score == 4 and score[1] != 4 and barstate.isconfirmed
fullBear = score == -4 and score[1] != -4 and barstate.isconfirmed
crossUp  = ta.crossover(m2, m4) and barstate.isconfirmed
crossDn  = ta.crossunder(m2, m4) and barstate.isconfirmed
 
// ───── Plots ─────
plot(showLines ? m1 : na, "MA 1", c1, 1)
plot(showLines ? m2 : na, "MA 2", c2, 1)
plot(showLines ? m3 : na, "MA 3", c3, 1)
plot(showLines ? m4 : na, "MA 4", c4, 2)
 
pc1 = plot(m1, "MA 1 (cloud)", color = na, editable = false, display = display.none)
pc4 = plot(m4, "MA 4 (cloud)", color = na, editable = false, display = display.none)
fill(pc1, pc4, color = showCloud ? color.new(scoreCol, 82) : na, title = "Trend cloud")
 
plotshape(showCross and crossUp, "MA 2 crosses above MA 4", shape.xcross, location.belowbar, colBull, size = size.tiny)
plotshape(showCross and crossDn, "MA 2 crosses below MA 4", shape.xcross, location.abovebar, colBear, size = size.tiny)
 
barcolor(colorBars ? scoreCol : na)
 
// ───── Trend table ─────
distPct(float m) => (close - m) / m * 100
 
var table tbl = table.new(position.top_right, 2, 6, border_width = 1)
if showTable and barstate.islast
    hdr = color.new(color.black, 30)
    table.cell(tbl, 0, 0, "Trend score", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 1, 0, (score > 0 ? "+" : "") + str.tostring(score) + " / 4", text_color = color.white, bgcolor = scoreCol)
    table.cell(tbl, 0, 1, "Price vs. MA", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 1, 1, "distance", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 0, 2, t1 + " " + str.tostring(l1), text_color = color.white, bgcolor = color.new(c1, 40))
    table.cell(tbl, 1, 2, str.tostring(distPct(m1), "#.##") + " %", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 0, 3, t2 + " " + str.tostring(l2), text_color = color.white, bgcolor = color.new(c2, 40))
    table.cell(tbl, 1, 3, str.tostring(distPct(m2), "#.##") + " %", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 0, 4, t3 + " " + str.tostring(l3), text_color = color.white, bgcolor = color.new(c3, 40))
    table.cell(tbl, 1, 4, str.tostring(distPct(m3), "#.##") + " %", text_color = color.white, bgcolor = hdr)
    table.cell(tbl, 0, 5, t4 + " " + str.tostring(l4), text_color = color.white, bgcolor = color.new(c4, 40))
    table.cell(tbl, 1, 5, str.tostring(distPct(m4), "#.##") + " %", text_color = color.white, bgcolor = hdr)
 
// ───── Alerts ─────
alertcondition(fullBull, "Full bullish stack", "QTC: all four MAs stacked bullish on {{ticker}} ({{interval}})")
alertcondition(fullBear, "Full bearish stack", "QTC: all four MAs stacked bearish on {{ticker}} ({{interval}})")
alertcondition(crossUp,  "MA 2 above MA 4", "QTC: MA 2 crossed above MA 4 on {{ticker}} ({{interval}})")
alertcondition(crossDn,  "MA 2 below MA 4", "QTC: MA 2 crossed below MA 4 on {{ticker}} ({{interval}})")

← 返回列表