Humble LinReg Candles + ZLMA Trend Levels + 3 MAs + Dashboard

ap733688 · study · 486 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
indicator("Humble LinReg Candles + ZLMA Trend Levels + 3 MAs + Dashboard", shorttitle="LinRegZLMA", format=format.price, precision=4, overlay=true, max_labels_count=500)
 
// ==================== ORIGINAL LINREG INPUTS ====================
signal_length = input.int(11, "Signal Smoothing", minval=1, maxval=200)
sma_signal    = input.bool(true, "Simple MA (Signal Line)")
 
lin_reg       = input.bool(true, "Lin Reg")
linreg_length = input.int(11, "Linear Regression Length", minval=1, maxval=200)
 
// ==================== SIGNAL INPUTS ====================
showSignals   = input.bool(true, "Show Signals")
overlapPct    = input.float(50.0, "Reset Threshold: Body Overlap %", minval=1.0, maxval=100.0, step=5.0, tooltip="If regular candle overlaps LinReg candle by this much, the state resets.")
requireGreen  = input.bool(false, "Require Current Candle to be Bullish", tooltip="Buy signal only if close > open")
requireRed    = input.bool(false, "Require Current Candle to be Bearish", tooltip="Sell signal only if close < open")
 
// --- NEW: Two Theories - Overlap Logic vs Close Confluence ---
// Default: Close Confluence = CHECKED, Overlap Logic = UNCHECKED
useOverlapLogic = input.bool(false, "Use Overlap Logic", tooltip="OLD Logic. Buy/Sell only when normal candle body is fully separated with a GAP from LinReg candle body and both are same colour. Overlap of 50% resets and allows next signal.")
useCloseConfluence = input.bool(true, "Use Close Confluence", tooltip="NEW Logic. During LinReg Bearish phase, wait for a Bearish regular candle with Close below LinReg Close. During LinReg Bullish phase, wait for a Bullish regular candle with Close above LinReg Close. Ignores opposite-colour retracement candles. Re-arms only when Close crosses back to the other side of LinReg Close.")
 
// ==================== OFFSET INPUTS ====================
atrMethod     = input.string("Basic ATR (14)", "ATR Method for Offset", options=["Basic ATR (14)", "Custom ATR"])
atrLength     = input.int(14, "Custom ATR Length", minval=1, tooltip="Only used if 'Custom ATR' is selected above")
offsetMulti   = input.float(0.5, "Vertical Offset Multiplier (x ATR)", minval=0.0, step=0.1, tooltip="Multiplier to push LinReg signals further away from the candle. 0 = exactly on the wick.")
 
// --- LinReg Triangle Size 8-40 + Shape + COLORS ---
linRegSizeNum = input.int(14, "LinReg Signal Size (8-40)", minval=8, maxval=40, step=1, tooltip="8 = smallest, 20 = biggest single triangle. Above 20 it stacks.")
linRegSize = linRegSizeNum <= 9 ? size.tiny : linRegSizeNum <= 11 ? size.small : linRegSizeNum <= 14 ? size.normal : linRegSizeNum <= 17 ? size.large : size.huge
linRegRepeat = linRegSizeNum <= 20 ? 1 : linRegSizeNum <= 30 ? 2 : 3
linRegUpTxt = linRegRepeat == 1 ? "▲" : linRegRepeat == 2 ? "▲▲" : "▲▲▲"
linRegDnTxt = linRegRepeat == 1 ? "▼" : linRegRepeat == 2 ? "▼▼" : "▼▼▼"
linRegCircleTxt = linRegRepeat == 1 ? "●" : linRegRepeat == 2 ? "●●" : "●●●"
linRegShape = input.string("Triangle", "LinReg Signal Shape", options=["Triangle", "Circle", "Label"], tooltip="Triangle = ▲ ▼, Circle = ●, Label = TradingView label")
linRegBullColor = input.color(color.lime, "LinReg Bull Color", tooltip="Color for LinReg Buy triangle")
linRegBearColor = input.color(color.red, "LinReg Bear Color", tooltip="Color for LinReg Sell triangle")
 
// ==================== ZLMA / EMA TREND LEVELS INPUTS ====================
groupZ = "Zero-Lag MA Trend Levels"
show_levl  = input.bool(true, "Show Trend Levels (ZLMA boxes)", group=groupZ)
zlmaLength = input.int(15, "ZLMA / EMA Length", minval=1, group=groupZ)
upColor    = input.color(#30d453, "ZL Bull Color (+)", group=groupZ, inline="zc", tooltip="Color for ZL Bull crossover triangle + ZL line")
dnColor    = input.color(#4043f1, "ZL Bear Color (-)", group=groupZ, inline="zc", tooltip="Color for ZL Bear crossover triangle + ZL line")
zlmaAtr    = ta.atr(200)
 
zlCrossOffsetMulti = input.float(0.5, "ZL Crossover Vertical Offset (x ATR)", minval=0.0, step=0.1, group=groupZ, tooltip="Pushes ZL crossover triangle away from ZL line. 0 = exactly on line.")
zlCrossShape = input.string("Triangle", "ZL Crossover Shape", options=["Triangle", "Circle", "Label"], group=groupZ, tooltip="Triangle = ▲ ▼, Circle = ●, Label = TradingView label")
crossLabelSizeNum = input.int(12, "Crossover Label Size (8-40)", minval=8, maxval=40, step=1, group=groupZ, tooltip="8 = smallest, 20 = biggest single triangle. Above 20 it stacks.")
crossLabelSize = crossLabelSizeNum <= 9 ? size.tiny : crossLabelSizeNum <= 11 ? size.small : crossLabelSizeNum <= 14 ? size.normal : crossLabelSizeNum <= 17 ? size.large : size.huge
crossRepeat = crossLabelSizeNum <= 20 ? 1 : crossLabelSizeNum <= 30 ? 2 : 3
crossUpTxt = crossRepeat == 1 ? "▲" : crossRepeat == 2 ? "▲▲" : "▲▲▲"
crossDnTxt = crossRepeat == 1 ? "▼" : crossRepeat == 2 ? "▼▼" : "▼▼▼"
crossCircleTxt = crossRepeat == 1 ? "●" : crossRepeat == 2 ? "●●" : "●●●"
showResume = input.bool(true, "Show Trend Resume Triangle", group=groupZ, tooltip="After a crossover, if ZL briefly flips colour and then flips back to trend colour WITHOUT a crossover, plot one more triangle.")
 
// ==================== 3 ADDITIONAL MOVING AVERAGES ====================
groupMA = "Additional Moving Averages"
 
ma1Show   = input.bool(true,  "Show MA 1",  group=groupMA)
ma1Type   = input.string("SMA", "MA 1 Type", options=["SMA","EMA","WMA","RMA"], group=groupMA)
ma1Src    = input.string("high", "MA 1 Source", options=["close", "open", "high", "low", "hl2", "hlc3", "ohlc4"], group=groupMA)
ma1Length = input.int(50, "MA 1 Length", minval=1, group=groupMA)
ma1Color  = input.color(color.yellow, "MA 1 Color", group=groupMA)
 
ma2Show   = input.bool(true,  "Show MA 2",  group=groupMA)
ma2Type   = input.string("SMA", "MA 2 Type", options=["SMA","EMA","WMA","RMA"], group=groupMA)
ma2Src    = input.string("low", "MA 2 Source", options=["close", "open", "high", "low", "hl2", "hlc3", "ohlc4"], group=groupMA) 
ma2Length = input.int(50, "MA 2 Length", minval=1, group=groupMA)
ma2Color  = input.color(color.orange, "MA 2 Color", group=groupMA)
 
ma3Show   = input.bool(true,  "Show MA 3",  group=groupMA)
ma3Type   = input.string("WMA", "MA 3 Type", options=["SMA","EMA","WMA","RMA"], group=groupMA)
ma3Src    = input.string("close", "MA 3 Source", options=["close", "open", "high", "low", "hl2", "hlc3", "ohlc4"], group=groupMA)
ma3Length = input.int(200, "MA 3 Length", minval=1, group=groupMA)
ma3Color  = input.color(color.aqua, "MA 3 Color", group=groupMA)
 
getSrc(srcStr) =>
    srcStr == "open"  ? open  :
     srcStr == "high"  ? high  :
     srcStr == "low"   ? low   :
     srcStr == "hl2"   ? hl2   :
     srcStr == "hlc3"  ? hlc3  :
     srcStr == "ohlc4" ? ohlc4 :
     close
 
maCalc(src, len, typ) =>
    typ == "SMA" ? ta.sma(src, len) :
     typ == "EMA" ? ta.ema(src, len) :
     typ == "WMA" ? ta.wma(src, len) :
     ta.rma(src, len)
 
// ==================== LINEAR REGRESSION CANDLES ====================
bopen  = lin_reg ? ta.linreg(open,  linreg_length, 0) : open
bhigh  = lin_reg ? ta.linreg(high,  linreg_length, 0) : high
blow   = lin_reg ? ta.linreg(low,   linreg_length, 0) : low
bclose = lin_reg ? ta.linreg(close, linreg_length, 0) : close
 
r = bopen < bclose
 
signal = sma_signal ? ta.sma(bclose, signal_length) : ta.ema(bclose, signal_length)
 
plotcandle(r ? bopen : na, r ? bhigh : na, r ? blow : na, r ? bclose : na, title="LinReg Bull Candle", color=color.green, wickcolor=color.green, bordercolor=color.green)
plotcandle(r ? na : bopen, r ? na : bhigh, r ? na : blow, r ? na : bclose, title="LinReg Bear Candle", color=color.red, wickcolor=color.red, bordercolor=color.red)
 
plot(signal, color=color.white, title="Signal Line")
 
// ==================== BODY OVERLAP CALCULATION - For Overlap Logic ====================
curTop = math.max(open, close)
curBot = math.min(open, close)
lrTop  = math.max(bopen, bclose)
lrBot  = math.min(bopen, bclose)
 
overlapTop = math.min(curTop, lrTop)
overlapBot = math.max(curBot, lrBot)
overlapSize = math.max(0.0, overlapTop - overlapBot)
 
curBodySize = math.max(0.00001, curTop - curBot)
lrBodySize  = math.max(0.00001, lrTop - lrBot)
 
isOverlapping = (overlapSize / curBodySize >= (overlapPct / 100)) or (overlapSize / lrBodySize >= (overlapPct / 100))
 
// ==================== THEORY 1: OVERLAP LOGIC ====================
// Trigger = Gap / Separation. Re-Arm = 50% Overlap.
bullishOverlap = (close > open) and (bclose > bopen) and (curBot > lrTop)
bearishOverlap = (close < open) and (bclose < bopen) and (curTop < lrBot)
 
// ==================== THEORY 2: CLOSE CONFLUENCE ====================
// Trigger = Same colour + Close beyond LinReg Close. Re-Arm = Close crosses back to other side.
// Bearish: LinReg Bearish + Regular Bearish + Close < LinReg Close
// Bullish: LinReg Bullish + Regular Bullish + Close > LinReg Close
bullishCloseConf = (bclose > bopen) and (close > open) and (close > bclose)
bearishCloseConf = (bclose < bopen) and (close < open) and (close < bclose)
 
// ==================== STATE MACHINES - Separate for each Theory ====================
var int stateOverlap = 0  // 0 = Ready, 1 = Buy locked, -1 = Sell locked
var int stateCloseConf = 0 // 0 = Ready, 1 = Buy locked, -1 = Sell locked
 
var bool triggerBuy = false
var bool triggerSell = false
 
triggerBuy  := false
triggerSell := false
 
// --- Overlap Logic State Machine ---
if useOverlapLogic
    if isOverlapping
        stateOverlap := 0
 
    if stateOverlap == 0
        if bullishOverlap and (requireGreen ? close > open : true)
            stateOverlap := 1 
            triggerBuy := true
        else if bearishOverlap and (requireRed ? close < open : true)
            stateOverlap := -1 
            triggerSell := true
 
// --- Close Confluence State Machine ---
if useCloseConfluence
    // Re-Arm: If locked in Buy (1) and Close goes BELOW LinReg Close -> Ready
    if stateCloseConf == 1 and close < bclose
        stateCloseConf := 0
    // Re-Arm: If locked in Sell (-1) and Close goes ABOVE LinReg Close -> Ready
    if stateCloseConf == -1 and close > bclose
        stateCloseConf := 0
 
    if stateCloseConf == 0
        if bullishCloseConf and (requireGreen ? close > open : true)
            stateCloseConf := 1
            triggerBuy := true
        else if bearishCloseConf and (requireRed ? close < open : true)
            stateCloseConf := -1
            triggerSell := true
 
// ==================== EXIT ARMING ====================
// Exit is armed ONLY after a Buy / Sell. It fires once, then disarms itself.
var bool longArmed = false
var bool shortArmed = false
 
if triggerBuy
    longArmed := true
    shortArmed := false
if triggerSell
    shortArmed := true
    longArmed := false
 
// ==================== ATR CALCULATION FOR OFFSET ====================
calcAtr = atrMethod == "Basic ATR (14)" ? ta.atr(14) : ta.atr(atrLength)
offsetDistance = calcAtr * offsetMulti
zlCrossOffset = calcAtr * zlCrossOffsetMulti
 
// ==================== PLOT LINREG SIGNALS ====================
if showSignals and triggerBuy
    labelY = low - offsetDistance
    if linRegShape == "Triangle"
        label.new(bar_index, labelY, linRegUpTxt, style=label.style_none, color=linRegBullColor, textcolor=linRegBullColor, size=linRegSize)
    else if linRegShape == "Circle"
        label.new(bar_index, labelY, linRegCircleTxt, style=label.style_none, color=linRegBullColor, textcolor=linRegBullColor, size=linRegSize)
    else
        label.new(bar_index, labelY, linRegUpTxt, style=label.style_label_up, color=linRegBullColor, textcolor=color.white, size=linRegSize)
 
if showSignals and triggerSell
    labelY = high + offsetDistance
    if linRegShape == "Triangle"
        label.new(bar_index, labelY, linRegDnTxt, style=label.style_none, color=linRegBearColor, textcolor=linRegBearColor, size=linRegSize)
    else if linRegShape == "Circle"
        label.new(bar_index, labelY, linRegCircleTxt, style=label.style_none, color=linRegBearColor, textcolor=linRegBearColor, size=linRegSize)
    else
        label.new(bar_index, labelY, linRegDnTxt, style=label.style_label_down, color=linRegBearColor, textcolor=color.white, size=linRegSize)
 
// ==================== ZLMA / EMA TREND LEVELS ====================
var box box1 = box(na)
 
series float emaValue   = ta.ema(close, zlmaLength)
series float correction = close + (close - emaValue)
series float zlma       = ta.ema(correction, zlmaLength)
 
bool signalUp = ta.crossover(zlma, emaValue)
bool signalDn = ta.crossunder(zlma, emaValue)
 
color zlma_color = zlma > zlma[3] ? upColor : zlma < zlma[3] ? dnColor : na
color ema_col    = emaValue < zlma ? upColor : dnColor
 
bool zlIsUp = zlma > zlma[3]
bool zlIsDn = zlma < zlma[3]
 
var int lastCrossDir = 0
var bool wentOpposite = false
bool resumeUp = false
bool resumeDn = false
 
if signalUp
    lastCrossDir := 1
    wentOpposite := false
else if signalDn
    lastCrossDir := -1
    wentOpposite := false
 
if lastCrossDir == -1 and zlIsUp
    wentOpposite := true
if lastCrossDir == 1 and zlIsDn
    wentOpposite := true
 
if lastCrossDir == -1 and wentOpposite and zlIsDn and not signalUp and not signalDn
    resumeDn := true
    wentOpposite := false
 
if lastCrossDir == 1 and wentOpposite and zlIsUp and not signalUp and not signalDn
    resumeUp := true
    wentOpposite := false
 
p1 = plot(zlma,     color=zlma_color, linewidth=1, title="ZLMA")
p2 = plot(emaValue, color=ema_col,    linewidth=1, title="ZLMA EMA")
fill(p1, p2, zlma, emaValue, color.new(zlma_color, 80), color.new(ema_col, 80))
 
method draw_box(color col, top, bot, price) =>
    box.new(
         bar_index, top, bar_index, bot, col, 1,
         bgcolor     = color.new(col, 90),
         text        = str.tostring(math.round(price, 2)),
         text_size   = size.tiny,
         text_color  = chart.fg_color,
         text_halign = text.align_right
         )
 
bool crossUnderBot = ta.crossunder(high, box1.get_bottom())
bool crossOverTop  = ta.crossover(low, box1.get_top())
 
if show_levl
    bool check_signals = signalUp or signalDn
 
    switch
        signalUp => box1 := upColor.draw_box(zlma, zlma - zlmaAtr, close)
        signalDn => box1 := dnColor.draw_box(zlma + zlmaAtr, zlma, close)
    switch
        not signalUp or not signalDn => box1.set_right(bar_index + 4)
        => box1 := box(na)
    
    if crossUnderBot and not check_signals[1] and not check_signals and emaValue > zlma
        label.new(bar_index - 1, high[1], "▼", color=color(na), textcolor=dnColor, style=label.style_label_down)
        
    if crossOverTop and not check_signals and not check_signals[1] and emaValue < zlma
        label.new(bar_index - 1, low[1], "▲", color=color(na), textcolor=upColor, style=label.style_label_up)
 
if signalUp
    zlY = zlma - zlCrossOffset
    if zlCrossShape == "Triangle"
        label.new(bar_index, zlY, crossUpTxt, style=label.style_none, color=color(na), textcolor=upColor, size=crossLabelSize)
    else if zlCrossShape == "Circle"
        label.new(bar_index, zlY, crossCircleTxt, style=label.style_none, color=color(na), textcolor=upColor, size=crossLabelSize)
    else
        label.new(bar_index, zlY, crossUpTxt, style=label.style_label_up, color=upColor, textcolor=color.white, size=crossLabelSize)
 
if signalDn
    zlY = zlma + zlCrossOffset
    if zlCrossShape == "Triangle"
        label.new(bar_index, zlY, crossDnTxt, style=label.style_none, color=color(na), textcolor=dnColor, size=crossLabelSize)
    else if zlCrossShape == "Circle"
        label.new(bar_index, zlY, crossCircleTxt, style=label.style_none, color=color(na), textcolor=dnColor, size=crossLabelSize)
    else
        label.new(bar_index, zlY, crossDnTxt, style=label.style_label_down, color=dnColor, textcolor=color.white, size=crossLabelSize)
 
if showResume and resumeUp
    zlY = zlma - zlCrossOffset
    if zlCrossShape == "Triangle"
        label.new(bar_index, zlY, crossUpTxt, style=label.style_none, color=color(na), textcolor=upColor, size=crossLabelSize)
    else if zlCrossShape == "Circle"
        label.new(bar_index, zlY, crossCircleTxt, style=label.style_none, color=color(na), textcolor=upColor, size=crossLabelSize)
    else
        label.new(bar_index, zlY, crossUpTxt, style=label.style_label_up, color=upColor, textcolor=color.white, size=crossLabelSize)
 
if showResume and resumeDn
    zlY = zlma + zlCrossOffset
    if zlCrossShape == "Triangle"
        label.new(bar_index, zlY, crossDnTxt, style=label.style_none, color=color(na), textcolor=dnColor, size=crossLabelSize)
    else if zlCrossShape == "Circle"
        label.new(bar_index, zlY, crossCircleTxt, style=label.style_none, color=color(na), textcolor=dnColor, size=crossLabelSize)
    else
        label.new(bar_index, zlY, crossDnTxt, style=label.style_label_down, color=dnColor, textcolor=color.white, size=crossLabelSize)
 
// ==================== EXIT SIGNAL - White Small X ====================
// Long Exit: After Buy, if ANY of 3 happens -> Exit
// 1) Close < Open of Green Bullish LinReg Candle
// 2) Close < Fast ZLMA (zlma)
// 3) Fast ZLMA turned Bearish (zlma < zlma[3])
// Short Exit: Mirror
bool exitLong = false
bool exitShort = false
 
bool longExitCond = longArmed and not triggerBuy and ((bclose > bopen and close < bopen) or (close < zlma) or (zlma < zlma[3]))
bool shortExitCond = shortArmed and not triggerSell and ((bclose < bopen and close > bopen) or (close > zlma) or (zlma > zlma[3]))
 
if longExitCond
    exitLong := true
    longArmed := false
    // Full Reset - Next Buy / Sell can fire immediately, no need to wait for old re-arm
    stateOverlap := 0
    stateCloseConf := 0
 
if shortExitCond
    exitShort := true
    shortArmed := false
    // Full Reset - Next Buy / Sell can fire immediately
    stateOverlap := 0
    stateCloseConf := 0
 
bool exitSignal = exitLong or exitShort
 
if showSignals and exitLong
    label.new(bar_index, high + offsetDistance, "✕", style=label.style_none, color=color(na), textcolor=color.white, size=size.small)
 
if showSignals and exitShort
    label.new(bar_index, low - offsetDistance, "✕", style=label.style_none, color=color(na), textcolor=color.white, size=size.small)
 
// ==================== PLOT 3 ADDITIONAL MOVING AVERAGES ====================
ma1Val = maCalc(getSrc(ma1Src), ma1Length, ma1Type)
ma2Val = maCalc(getSrc(ma2Src), ma2Length, ma2Type)
ma3Val = maCalc(getSrc(ma3Src), ma3Length, ma3Type)
 
plot(ma1Show ? ma1Val : na, color=ma1Color, linewidth=2, title="MA 1")
plot(ma2Show ? ma2Val : na, color=ma2Color, linewidth=2, title="MA 2")
plot(ma3Show ? ma3Val : na, color=ma3Color, linewidth=2, title="MA 3")
 
// ==================== DASHBOARD ====================
groupDash = "Dashboard Settings"
showDash = input.bool(true, "Show Dashboard", group=groupDash)
dashPos  = input.string("Top Right", "Dashboard Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group=groupDash)
rsiLength = input.int(10, "RSI Length", minval=1, group=groupDash)
atrDashLength = input.int(10, "ATR Length", minval=1, group=groupDash)
 
dashSizeNum = input.int(10, "Dashboard Text Size (8-20)", minval=8, maxval=20, step=1, group=groupDash, tooltip="8 = smallest for mobile, 20 = biggest")
dashTextSize = dashSizeNum <= 9 ? size.tiny : dashSizeNum <= 11 ? size.small : dashSizeNum <= 14 ? size.normal : dashSizeNum <= 17 ? size.large : size.huge
 
dashGreen = #30d453
dashRed   = #ff0000
dashGray  = color.gray
 
if showDash
    rsiVal = ta.rsi(close, rsiLength)
    [macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)
    macdDiff = macdLine - signalLine
    
    string rsiStr = str.tostring(rsiVal, "#.##")
    color rsiBg = rsiVal >= 52 ? dashGreen : rsiVal <= 48 ? dashRed : dashGray
    color rsiText = (rsiVal >= 52 or rsiVal <= 48) ? color.white : color.black
    
    string macdStr = str.tostring(macdDiff, "#.###")
    color macdBg = macdDiff > 0 ? dashGreen : dashRed
    color macdText = color.white
    
    string zlText = ""
    color zlBg = dashGray
    color zlTextColor = color.black
    
    bool bothGreen = (zlma > zlma[3]) and (emaValue > emaValue[3])
    bool bothRed   = (zlma < zlma[3]) and (emaValue < emaValue[3])
    
    if bothGreen
        zlText := "BULLISH"
        zlBg := dashGreen
        zlTextColor := color.white
    else if bothRed
        zlText := "BEARISH"
        zlBg := dashRed
        zlTextColor := color.white
    else
        zlText := "NEUTRAL"
        zlBg := dashGray
        zlTextColor := color.black
        
    string maText = ""
    color maBg = dashGray
    color maTextColor = color.black
    
    float maUpper = math.max(ma1Val, ma2Val)
    float maLower = math.min(ma1Val, ma2Val)
    
    if close > maUpper
        maText := "BULLISH"
        maBg := dashGreen
        maTextColor := color.white
    else if close < maLower
        maText := "BEARISH"
        maBg := dashRed
        maTextColor := color.white
    else
        maText := "NEUTRAL"
        maBg := dashGray
        maTextColor := color.black
        
    atrDashVal = ta.atr(atrDashLength)
    string atrStr = str.tostring(atrDashVal, "#.####")
    string rsiLabel = "RSI " + str.tostring(rsiLength)
    string atrLabel = "ATR " + str.tostring(atrDashLength)
    
    dashPosition = dashPos == "Top Right" ? position.top_right : dashPos == "Top Left" ? position.top_left : dashPos == "Bottom Right" ? position.bottom_right : dashPos == "Bottom Left" ? position.bottom_left : dashPos == "Middle Right" ? position.middle_right : position.middle_left
    var table dash = table.new(dashPosition, 2, 6, bgcolor = color.new(color.black, 80), border_width = 1, border_color = color.new(color.white, 80))
         
    table.cell(dash, 0, 0, "Indicator", text_color=color.white, text_size=dashTextSize)
    table.cell(dash, 1, 0, "Status", text_color=color.white, text_size=dashTextSize)
    table.cell(dash, 0, 1, rsiLabel, text_color=color.white, text_size=dashTextSize, bgcolor=color.new(color.gray, 80))
    table.cell(dash, 1, 1, rsiStr, text_color=rsiText, bgcolor=rsiBg, text_size=dashTextSize)
    table.cell(dash, 0, 2, "MACD", text_color=color.white, text_size=dashTextSize, bgcolor=color.new(color.gray, 80))
    table.cell(dash, 1, 2, macdStr, text_color=macdText, bgcolor=macdBg, text_size=dashTextSize)
    table.cell(dash, 0, 3, "ZL BAND", text_color=color.white, text_size=dashTextSize, bgcolor=color.new(color.gray, 80))
    table.cell(dash, 1, 3, zlText, text_color=zlTextColor, bgcolor=zlBg, text_size=dashTextSize)
    table.cell(dash, 0, 4, "MA BAND", text_color=color.white, text_size=dashTextSize, bgcolor=color.new(color.gray, 80))
    table.cell(dash, 1, 4, maText, text_color=maTextColor, bgcolor=maBg, text_size=dashTextSize)
    table.cell(dash, 0, 5, atrLabel, text_color=color.white, text_size=dashTextSize, bgcolor=color.new(color.gray, 80))
    table.cell(dash, 1, 5, atrStr, text_color=color.white, bgcolor=color.new(color.gray, 50), text_size=dashTextSize)
 
// ==================== ALERTS - 7 Individual + Any Function Call ====================
alertcondition(triggerBuy,  title="Bullish LinReg Signal", message="Bullish LinReg signal on {{ticker}} at {{close}}")
alertcondition(triggerSell, title="Bearish LinReg Signal", message="Bearish LinReg signal on {{ticker}} at {{close}}")
alertcondition(signalUp,    title="ZLMA Bullish Cross",    message="ZLMA crossed above EMA on {{ticker}} at {{close}}")
alertcondition(signalDn,    title="ZLMA Bearish Cross",    message="ZLMA crossed below EMA on {{ticker}} at {{close}}")
alertcondition(resumeUp,    title="ZLMA Bullish Resume",   message="ZLMA trend resumed bullish on {{ticker}} at {{close}}")
alertcondition(resumeDn,    title="ZLMA Bearish Resume",   message="ZLMA trend resumed bearish on {{ticker}} at {{close}}")
alertcondition(exitSignal,  title="Exit Signal",           message="Exit signal on {{ticker}} at {{close}} - White X plotted, Buy/Sell re-armed")
 
// 8th - Any Signal - includes Exit now
alertcondition(triggerBuy or triggerSell or signalUp or signalDn or resumeUp or resumeDn or exitSignal, title="Any Signal - LinReg / ZLMA", message="Any Signal fired on {{ticker}} at {{close}} - Check chart for Buy/Sell / ZLMA Cross/Resume / Exit")
 
// ==================== ANY ALERT() FUNCTION CALL - Crisp Message - Now 7 Types - AFTER CANDLE CLOSE ====================
// In TradingView Create Alert -> Condition: LinRegZLMA -> Select "Any alert() function call"
// This will now fire AFTER candle close / at beginning of new candle - NO repaint
string anyMsg = na
 
if exitLong
    anyMsg := "⚪ EXIT | Long Exit | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if exitShort
    anyMsg := "⚪ EXIT | Short Exit | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if triggerBuy
    anyMsg := "🟢 BUY | LinReg Bullish | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if triggerSell
    anyMsg := "🔴 SELL | LinReg Bearish | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if signalUp
    anyMsg := "🟢 ZLMA Bullish Cross | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if signalDn
    anyMsg := "🔴 ZLMA Bearish Cross | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if resumeUp
    anyMsg := "🟢 ZLMA Bullish Resume | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
else if resumeDn
    anyMsg := "🔴 ZLMA Bearish Resume | " + syminfo.ticker + " @ " + str.tostring(close, format.mintick)
 
if not na(anyMsg)
    alert(anyMsg, alert.freq_once_per_bar_close)

← 返回列表