DTC EMA Trend Cloud

Quantum-Edge-Capital · study · 205 行 · 点赞 86 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Quantum-Edge-Capital
//@version=6
indicator("DTC EMA Trend System 1.3.6", shorttitle="DTC 1.3.6", overlay=true, max_lines_count=300, max_labels_count=300)
 
// ───────────────────────── Groups ─────────────────────────
gEMA   = "EMA Trend System"
gSig   = "Buy / Sell Signals"
gTPSL  = "TP / SL Levels"
gATR   = "ATR Volatility Filter"
gDash  = "Multi-Timeframe Dashboard"
gCloud = "EMA Cloud"
gColor = "Colors"
 
// ───────────────────────── Inputs ─────────────────────────
len1 = input.int(30, "EMA 1", group=gEMA)
len2 = input.int(35, "EMA 2", group=gEMA)
len3 = input.int(40, "EMA 3", group=gEMA)
len4 = input.int(45, "EMA 4", group=gEMA)
len5 = input.int(50, "EMA 5", group=gEMA)
len6 = input.int(60, "EMA 6", group=gEMA)
 
showSignals = input.bool(true, "Show Buy/Sell Signals", group=gSig)
barsForward = input.int(30, "Projection Bars (TP/SL lines)", minval=5, maxval=200, group=gSig)
 
showTPSL = input.bool(true, "Show TP/SL Levels", group=gTPSL)
slMode   = input.string("Small", "SL Mode", options=["Tiny","Small","Mid","Large"], group=gTPSL)
 
useAtrFilter   = input.bool(true, "Enable ATR Volatility Filter", group=gATR)
atrPeriod      = input.int(14, "ATR Period", group=gATR)
atrSmoothPeriod= input.int(50, "ATR Average Period", group=gATR)
atrMult        = input.float(1.0, "ATR Threshold Multiplier", step=0.1, group=gATR)
 
showDash = input.bool(true, "Show Dashboard", group=gDash)
dashPos  = input.string("top_right", "Dashboard Position", options=["top_left","top_right","bottom_left","bottom_right"], group=gDash)
dashSize = input.string("small", "Dashboard Text Size", options=["tiny","small","normal","large"], group=gDash)
 
showCloud = input.bool(true, "Show EMA Cloud", group=gCloud)
 
bullColor    = input.color(color.new(color.teal, 0), "Bullish Color", group=gColor)
bearColor    = input.color(color.new(color.red, 0), "Bearish Color", group=gColor)
neutralColor = input.color(color.new(color.gray, 0), "Neutral Color", group=gColor)
 
// ───────────────────────── EMA Trend Engine ─────────────────────────
ema1 = ta.ema(close, len1)
ema2 = ta.ema(close, len2)
ema3 = ta.ema(close, len3)
ema4 = ta.ema(close, len4)
ema5 = ta.ema(close, len5)
ema6 = ta.ema(close, len6)
 
bullAlign = ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5 and ema5 > ema6
bearAlign = ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5 and ema5 < ema6
trendRaw  = bullAlign ? 1 : bearAlign ? -1 : 0
 
// ───────────────────────── ATR Volatility Filter ─────────────────────────
atrVal   = ta.atr(atrPeriod)
atrAvg   = ta.sma(atrVal, atrSmoothPeriod)
atrActive = atrVal > atrAvg * atrMult
 
// ───────────────────────── Signal Engine ─────────────────────────
buySignalRaw  = trendRaw == 1 and trendRaw[1] != 1
sellSignalRaw = trendRaw == -1 and trendRaw[1] != -1
 
buySignal  = showSignals and buySignalRaw and (not useAtrFilter or atrActive)
sellSignal = showSignals and sellSignalRaw and (not useAtrFilter or atrActive)
 
plotshape(buySignal, title="Buy Signal", style=shape.triangleup, location=location.belowbar, color=bullColor, size=size.tiny, text="BUY")
plotshape(sellSignal, title="Sell Signal", style=shape.triangledown, location=location.abovebar, color=bearColor, size=size.tiny, text="SELL")
 
alertcondition(buySignal, title="DTC Buy Signal", message="DTC EMA Trend: Bullish alignment confirmed")
alertcondition(sellSignal, title="DTC Sell Signal", message="DTC EMA Trend: Bearish alignment confirmed")
 
// ───────────────────────── EMA Cloud ─────────────────────────
cloudColor = trendRaw == 1 ? color.new(bullColor, 80) : trendRaw == -1 ? color.new(bearColor, 80) : color.new(neutralColor, 92)
p1 = plot(showCloud ? ema1 : na, "EMA 1 (30)", color=trendRaw == 1 ? bullColor : trendRaw == -1 ? bearColor : neutralColor, linewidth=2)
p6 = plot(showCloud ? ema6 : na, "EMA 6 (60)", color=trendRaw == 1 ? bullColor : trendRaw == -1 ? bearColor : neutralColor, linewidth=2)
fill(p1, p6, color=showCloud ? cloudColor : na, title="EMA Cloud")
 
plot(showCloud ? ema2 : na, "EMA 2 (35)", color=color.new(neutralColor, 60), linewidth=1)
plot(showCloud ? ema3 : na, "EMA 3 (40)", color=color.new(neutralColor, 60), linewidth=1)
plot(showCloud ? ema4 : na, "EMA 4 (45)", color=color.new(neutralColor, 60), linewidth=1)
plot(showCloud ? ema5 : na, "EMA 5 (50)", color=color.new(neutralColor, 60), linewidth=1)
 
// ───────────────────────── TP / SL Engine ─────────────────────────
slMultVal = slMode == "Tiny" ? 0.5 : slMode == "Small" ? 1.0 : slMode == "Mid" ? 1.5 : 2.0
 
var line entryLine = na
var line slLine = na
var line[] tpLines = array.new_line(7, na)
var label entryLabel = na
var label slLabel = na
var label[] tpLabels = array.new_label(7, na)
 
f_clearDrawings() =>
    if not na(entryLine)
        line.delete(entryLine)
    if not na(slLine)
        line.delete(slLine)
    if not na(entryLabel)
        label.delete(entryLabel)
    if not na(slLabel)
        label.delete(slLabel)
    if array.size(tpLines) > 0
        for i = 0 to array.size(tpLines) - 1
            ln = array.get(tpLines, i)
            if not na(ln)
                line.delete(ln)
            lb = array.get(tpLabels, i)
            if not na(lb)
                label.delete(lb)
 
if (buySignal or sellSignal) and showTPSL
    f_clearDrawings()
    entryPrice = close
    risk = atrVal * slMultVal
    slPrice = buySignal ? entryPrice - risk : entryPrice + risk
    x1 = bar_index
    x2 = bar_index + barsForward
 
    entryLine := line.new(x1, entryPrice, x2, entryPrice, color=color.new(color.blue, 0), width=2, style=line.style_solid)
    entryLabel := label.new(x2, entryPrice, "Entry " + str.tostring(entryPrice, format.mintick), style=label.style_label_left, color=color.new(color.blue, 70), textcolor=color.white, size=size.small)
 
    slLine := line.new(x1, slPrice, x2, slPrice, color=color.new(bearColor, 0), width=2, style=line.style_dashed)
    slLabel := label.new(x2, slPrice, "SL " + str.tostring(slPrice, format.mintick), style=label.style_label_left, color=color.new(bearColor, 70), textcolor=color.white, size=size.small)
 
    for i = 0 to 6
        n = i + 1
        tpPrice = buySignal ? entryPrice + risk * n : entryPrice - risk * n
        tpCol = color.from_gradient(n, 1, 7, color.new(bullColor, 70), color.new(bullColor, 10))
        tpLn = line.new(x1, tpPrice, x2, tpPrice, color=tpCol, width=1, style=line.style_dotted)
        tpLb = label.new(x2, tpPrice, "TP" + str.tostring(n) + " " + str.tostring(tpPrice, format.mintick), style=label.style_label_left, color=tpCol, textcolor=color.white, size=size.tiny)
        array.set(tpLines, i, tpLn)
        array.set(tpLabels, i, tpLb)
 
// ───────────────────────── Unrealized PnL Since Last Signal ─────────────────────────
var float lastEntryPrice = na
var int lastSide = 0
 
if buySignal
    lastEntryPrice := close
    lastSide := 1
if sellSignal
    lastEntryPrice := close
    lastSide := -1
 
pnlPts = na(lastEntryPrice) ? 0.0 : lastSide == 1 ? (close - lastEntryPrice) : lastSide == -1 ? (lastEntryPrice - close) : 0.0
pnlPct = na(lastEntryPrice) or lastEntryPrice == 0 ? 0.0 : (pnlPts / lastEntryPrice) * 100.0
 
// ───────────────────────── Multi-Timeframe Trend ─────────────────────────
f_trend(tf) =>
    [e1, e2, e3, e4, e5, e6] = request.security(syminfo.tickerid, tf, [ta.ema(close, len1), ta.ema(close, len2), ta.ema(close, len3), ta.ema(close, len4), ta.ema(close, len5), ta.ema(close, len6)], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
    b = e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5 and e5 > e6
    s = e1 < e2 and e2 < e3 and e3 < e4 and e4 < e5 and e5 < e6
    b ? 1 : s ? -1 : 0
 
trend1H = f_trend("60")
trend4H = f_trend("240")
trendD  = f_trend("1D")
trendW  = f_trend("1W")
trendM  = f_trend("1M")
 
f_label(t) =>
    t == 1 ? "Bullish" : t == -1 ? "Bearish" : "Neutral"
 
f_color(t) =>
    t == 1 ? bullColor : t == -1 ? bearColor : neutralColor
 
f_size(s) =>
    s == "tiny" ? size.tiny : s == "small" ? size.small : s == "normal" ? size.normal : size.large
 
f_pos(p) =>
    p == "top_left" ? position.top_left : p == "top_right" ? position.top_right : p == "bottom_left" ? position.bottom_left : position.bottom_right
 
// ───────────────────────── Dashboard ─────────────────────────
var table dashTable = na
 
if barstate.islast and showDash
    if not na(dashTable)
        table.delete(dashTable)
    dashTable := table.new(f_pos(dashPos), 2, 8, bgcolor=color.new(color.gray, 85), border_width=1, border_color=color.new(color.gray, 50))
 
    table.cell(dashTable, 0, 0, "DTC Dashboard", text_color=color.white, bgcolor=color.new(color.gray, 70), text_size=f_size(dashSize))
    table.cell(dashTable, 1, 0, "", bgcolor=color.new(color.gray, 70))
 
    table.cell(dashTable, 0, 1, "1H Trend", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 1, f_label(trend1H), text_color=f_color(trend1H), text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 2, "4H Trend", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 2, f_label(trend4H), text_color=f_color(trend4H), text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 3, "Daily Trend", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 3, f_label(trendD), text_color=f_color(trendD), text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 4, "Weekly Trend", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 4, f_label(trendW), text_color=f_color(trendW), text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 5, "Monthly Trend", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 5, f_label(trendM), text_color=f_color(trendM), text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 6, "ATR Filter", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 6, atrActive ? "Active" : "Inactive", text_color=atrActive ? bullColor : bearColor, text_size=f_size(dashSize))
 
    table.cell(dashTable, 0, 7, "Unrealized PnL", text_color=color.white, text_size=f_size(dashSize))
    table.cell(dashTable, 1, 7, str.tostring(pnlPts, format.mintick) + " (" + str.tostring(pnlPct, "#.##") + "%)", text_color=pnlPts >= 0 ? bullColor : bearColor, text_size=f_size(dashSize))

← 返回列表