Gold Sniper Targets

Michael_Fx_Trader · study · 783 行 · 点赞 652 · TradingView 原页

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

Pine Script

//@version=6
 
// =============================================================================
//                         ORIGINAL SCRIPT DECLARATION
// =============================================================================
// Script Name   : Gold Sniper Targets
// Author        : Michael_Fx_Trader
// Publisher     : Michael_Fx_Trader
// Rights        : © Michael_Fx_Trader. All rights reserved.
//
// Originality Statement:
//   This is an original work, designed and coded from scratch by
//   Michael_Fx_Trader, purpose-built for Gold rather than a generic
//   moving-average crossover script. The 8-factor confluence scoring engine
//   (session VWAP position, RSI midline, MACD state, EMA alignment, ADX with
//   directional price filter, volume participation, a secondary confirmation
//   timeframe RSI, and a Gold/US Dollar Index inverse-correlation factor that
//   deliberately awards no point when the correlation is not reliably
//   negative), the Gold-only recognition guard, the London/New York session
//   restriction with soft-fail visual marking, the Average Daily Range
//   exhaustion filter, the minimum trade-spacing rule, the full ATR-based
//   entry/stop/three-target trade ladder with breakeven management and a
//   rolling trade-history tally, the fast-EMA retest highlighting, and the
//   multi-panel live dashboard were all independently conceived and
//   implemented for this publication. No proprietary source code, private
//   scripts, or copyrighted material belonging to any other author has been
//   copied, mashed-up, or reused in any part of this script.
//
// Author Verification / Declaration:
//   I, Michael_Fx_Trader, am the sole author and publisher of this script.
//   I hold full authorship rights over its source code, its underlying logic,
//   and its visual presentation. EMA crossovers, RSI, MACD, ADX, VWAP, ATR and
//   Fibonacci-style risk-reward ladders are all well-known, generic public-
//   domain technical-analysis building blocks not owned by any individual
//   author. Only the specific combination, scoring, filtering, trade-
//   management, and dashboard logic built around them here is original to
//   this script - nothing in this script was copied or adapted from another
//   author's published work.
// =============================================================================
 
indicator("Gold Sniper Targets", shorttitle = "Gold Sniper", overlay = true,
     max_lines_count = 500, max_labels_count = 500, max_boxes_count = 50)
 
// =============================================================================
// INPUTS
// =============================================================================
 
grpTrigger = "1. EMA Crossover Trigger"
fastLen = input.int(9, "Fast EMA Length", minval = 1, group = grpTrigger)
slowLen = input.int(21, "Slow EMA Length", minval = 1, group = grpTrigger)
confirmOnClose = input.bool(true, "Wait for Candle Close", group = grpTrigger,
     tooltip = "Only treat a crossover as valid once the candle has fully closed, so an intrabar wick that reverses cannot fake a signal.")
 
grpScore = "2. Confluence Score"
minScoreOf8 = input.int(5, "Minimum Factors Required (out of 8)", minval = 1, maxval = 8, group = grpScore,
     tooltip = "If the DXY factor is disabled below, total factors drop to 7 and this minimum is automatically rescaled.")
 
grpDxy = "8. DXY Inverse-Correlation Factor"
useDxyFactor = input.bool(true, "Enable DXY Factor", group = grpDxy)
dxySymbol    = input.symbol("TVC:DXY", "Dollar Index Symbol", group = grpDxy)
dxyEmaLen    = input.int(20, "DXY EMA Length", minval = 1, group = grpDxy)
corrLen      = input.int(20, "Correlation Lookback (bars)", minval = 5, group = grpDxy)
corrThreshold = input.float(-0.3, "Inverse-Correlation Threshold", maxval = 0.0, step = 0.05, group = grpDxy,
     tooltip = "The Gold/DXY correlation must be at or below this value for the inverse link to be considered active. Otherwise the DXY factor awards no point to either side.")
 
grpGold = "3. Gold-Only Guard"
goldOnlyMode = input.bool(true, "Only Signal on Recognized Gold Symbols", group = grpGold)
customGoldKeyword = input.string("", "Extra Custom Keyword (optional)", group = grpGold,
     tooltip = "If your broker's Gold symbol looks unusual, type a piece of its ticker text here to also count as recognized.")
 
grpSession = "4. Trading Session Filter"
useSessionFilter = input.bool(true, "Restrict Signals to London/New York", group = grpSession,
     tooltip = "Automatically ignored on Daily and higher timeframes.")
londonSession = input.session("0700-1030", "London Session", group = grpSession)
nySession     = input.session("1300-1600", "New York Session", group = grpSession)
sessionTZ     = input.string("GMT", "Session Timezone", group = grpSession)
 
grpAdr = "5. ADR Exhaustion Filter"
useAdrFilter = input.bool(true, "Enable ADR Exhaustion Filter", group = grpAdr,
     tooltip = "Automatically ignored on Weekly and Monthly timeframes.")
adrLookbackDays = input.int(14, "ADR Lookback (completed days)", minval = 2, group = grpAdr)
adrExhaustPct   = input.float(100, "Block New Entries After Using (% of ADR)", minval = 10, group = grpAdr)
 
grpSpacing = "Trade Spacing"
minBarsBetweenTrades = input.int(10, "Minimum Bars Between New Trades", minval = 0, group = grpSpacing)
 
grpConfirmTf = "7. Secondary Confirmation RSI"
confirmTf = input.timeframe("5", "Confirmation Timeframe", group = grpConfirmTf)
confirmRsiLen = input.int(14, "Confirmation RSI Length", minval = 1, group = grpConfirmTf)
 
grpRisk = "6. Trade Ladder (ATR Stop + 3 Targets)"
atrLen = input.int(14, "ATR Length", minval = 1, group = grpRisk)
atrMult = input.float(1.5, "Stop Distance (x ATR)", minval = 0.1, step = 0.1, group = grpRisk)
useBreakeven = input.bool(true, "Move Stop to Breakeven After TP1", group = grpRisk)
tradeLabelOffset = input.int(12, "Open Trade Line/Label Forward Offset (bars)", minval = 1, group = grpRisk)
maxVisibleClosed = input.int(1, "Max Visible Completed Trade Ladders", minval = 0, maxval = 20, group = grpRisk,
     tooltip = "How many of the most recently CLOSED trades keep their entry/SL/TP lines drawn on the chart. Open trades are always drawn regardless of this setting. Lower this (e.g. 1) so the chart isn't cluttered with every historical trade - the History row in the dashboard still counts every completed trade up to Max Completed Trades Kept, whether or not it is still drawn.")
maxHistory = input.int(50, "Max Completed Trades Kept in History Tally", minval = 5, maxval = 200, group = grpRisk)
 
grpColors = "Colors"
bullColor = input.color(color.new(color.lime, 0), "Bullish / Buy Color", group = grpColors)
bearColor = input.color(color.new(color.red, 0),  "Bearish / Sell Color", group = grpColors)
hitColor  = input.color(color.new(color.yellow, 0), "Target Hit Color", group = grpColors)
retestColor = input.color(color.new(color.orange, 0), "EMA Retest Bar Color", group = grpColors)
 
// =============================================================================
// 3. GOLD-ONLY RECOGNITION GUARD
// =============================================================================
 
baseUp   = str.upper(syminfo.basecurrency)
tickerUp = str.upper(syminfo.ticker)
rootUp   = str.upper(syminfo.root)
keywordUp = str.upper(customGoldKeyword)
 
isGoldSymbol = str.contains(baseUp, "XAU") or str.contains(tickerUp, "XAU") or str.contains(tickerUp, "GOLD") or
     rootUp == "GC" or rootUp == "MGC" or (customGoldKeyword != "" and str.contains(tickerUp, keywordUp))
 
goldOk = not goldOnlyMode or isGoldSymbol
 
// =============================================================================
// CORE INDICATOR CALCULATIONS
// =============================================================================
 
fastEMA = ta.ema(close, fastLen)
slowEMA = ta.ema(close, slowLen)
rsiMain = ta.rsi(close, 14)
[macdLine, macdSignal, macdHist] = ta.macd(close, 12, 26, 9)
[diPlus, diMinus, adxValue] = ta.dmi(14, 14)
volAvg = ta.sma(volume, 20)
vwapValue = ta.vwap(hlc3)
atrValue = ta.atr(atrLen)
 
confirmRsi = request.security(syminfo.tickerid, confirmTf, ta.rsi(close, confirmRsiLen), lookahead = barmerge.lookahead_off)
 
dxyClose = request.security(dxySymbol, timeframe.period, close, lookahead = barmerge.lookahead_off)
dxyEma   = request.security(dxySymbol, timeframe.period, ta.ema(close, dxyEmaLen), lookahead = barmerge.lookahead_off)
correlationValue = ta.correlation(close, dxyClose, corrLen)
inverseActive = correlationValue <= corrThreshold
dxyWeak   = dxyClose < dxyEma
dxyStrong = dxyClose > dxyEma
 
// =============================================================================
// EMA CROSSOVER TRIGGER
// =============================================================================
 
triggerReady = confirmOnClose ? barstate.isconfirmed : true
crossUp = triggerReady and ta.crossover(fastEMA, slowEMA)
crossDn = triggerReady and ta.crossunder(fastEMA, slowEMA)
 
// =============================================================================
// 2 & 8. THE 8-FACTOR CONFLUENCE ENGINE
// =============================================================================
 
f1Bull = close > vwapValue
f1Bear = close < vwapValue
f2Bull = rsiMain > 50
f2Bear = rsiMain < 50
f3Bull = macdLine > macdSignal
f3Bear = macdLine < macdSignal
f4Bull = fastEMA > slowEMA
f4Bear = fastEMA < slowEMA
f5Bull = adxValue > 25 and close > fastEMA
f5Bear = adxValue > 25 and close < fastEMA
f6Bull = volume > volAvg and close > open
f6Bear = volume > volAvg and close < open
f7Bull = confirmRsi > 50
f7Bear = confirmRsi < 50
f8Bull = useDxyFactor and inverseActive and dxyWeak
f8Bear = useDxyFactor and inverseActive and dxyStrong
 
bullScore = (f1Bull ? 1 : 0) + (f2Bull ? 1 : 0) + (f3Bull ? 1 : 0) + (f4Bull ? 1 : 0) + (f5Bull ? 1 : 0) +
     (f6Bull ? 1 : 0) + (f7Bull ? 1 : 0) + (useDxyFactor ? (f8Bull ? 1 : 0) : 0)
bearScore = (f1Bear ? 1 : 0) + (f2Bear ? 1 : 0) + (f3Bear ? 1 : 0) + (f4Bear ? 1 : 0) + (f5Bear ? 1 : 0) +
     (f6Bear ? 1 : 0) + (f7Bear ? 1 : 0) + (useDxyFactor ? (f8Bear ? 1 : 0) : 0)
 
totalFactors = useDxyFactor ? 8 : 7
effectiveMin = math.round(minScoreOf8 * totalFactors / 8.0)
 
bullQualifies = bullScore >= effectiveMin
bearQualifies = bearScore >= effectiveMin
 
gapScore = bullScore - bearScore
biasLabel = gapScore >= 4 ? "STRONG BULL" : gapScore >= 2 ? "BULL" : gapScore >= 1 ? "MILD BULL" :
     gapScore <= -4 ? "STRONG BEAR" : gapScore <= -2 ? "BEAR" : gapScore <= -1 ? "MILD BEAR" : "NEUTRAL"
biasColor = gapScore > 0 ? bullColor : gapScore < 0 ? bearColor : color.gray
 
// =============================================================================
// 4. SESSION FILTER
// =============================================================================
 
londonWin = not na(time(timeframe.period, londonSession, sessionTZ))
nyWin     = not na(time(timeframe.period, nySession, sessionTZ))
sessionActive = londonWin or nyWin
sessionFilterApplies = useSessionFilter and timeframe.isintraday
sessionOk = not sessionFilterApplies or sessionActive
sessionName = londonWin and nyWin ? "OVERLAP" : londonWin ? "LONDON" : nyWin ? "NEW YORK" : "OFF-SESSION"
 
bgcolor(timeframe.isintraday and londonWin and not nyWin ? color.new(color.blue, 92) : na, title = "London Session")
bgcolor(timeframe.isintraday and nyWin and not londonWin ? color.new(color.purple, 92) : na, title = "New York Session")
bgcolor(timeframe.isintraday and londonWin and nyWin ? color.new(color.yellow, 85) : na, title = "Session Overlap")
 
// =============================================================================
// 5. ADR EXHAUSTION FILTER
// =============================================================================
 
adrValue = request.security(syminfo.tickerid, "D", ta.sma(high - low, adrLookbackDays), lookahead = barmerge.lookahead_off)[1]
todayHigh = request.security(syminfo.tickerid, "D", high, lookahead = barmerge.lookahead_off)
todayLow  = request.security(syminfo.tickerid, "D", low, lookahead = barmerge.lookahead_off)
todayRangeSoFar = todayHigh - todayLow
adrUsedPct = adrValue > 0 ? todayRangeSoFar / adrValue * 100.0 : 0.0
isDailyPlus = timeframe.isdaily or timeframe.isweekly or timeframe.ismonthly
adrFilterApplies = useAdrFilter and not isDailyPlus
adrExhausted = adrUsedPct >= adrExhaustPct
adrOk = not adrFilterApplies or not adrExhausted
 
// =============================================================================
// TRADE SPACING RULE
// =============================================================================
 
var int lastTradeOpenBar = na
spacingOk = na(lastTradeOpenBar) or (bar_index - lastTradeOpenBar) >= minBarsBetweenTrades
 
// =============================================================================
// FINAL SIGNAL DECISION
// =============================================================================
 
canSignalNow = goldOk and sessionOk and adrOk and spacingOk
rawBuySignal  = crossUp and bullQualifies
rawSellSignal = crossDn and bearQualifies
buySignal  = rawBuySignal and canSignalNow
sellSignal = rawSellSignal and canSignalNow
anyCross = crossUp or crossDn
blockedCross = anyCross and not (buySignal or sellSignal)
 
var string lastBlockReason = "NONE"
var string lastBlockDir = "NONE"
if blockedCross
    reasonTxt = not goldOk ? "NOT GOLD SYMBOL" : not sessionOk ? "OFF-SESSION" : not adrOk ? "ADR EXHAUSTED" :
         not spacingOk ? "TOO SOON (SPACING)" : crossUp ? "SCORE TOO LOW (BULL)" : "SCORE TOO LOW (BEAR)"
    lastBlockReason := reasonTxt
    lastBlockDir := crossUp ? "LONG" : "SHORT"
 
// =============================================================================
// SIGNAL / TRIGGER VISUALS
// =============================================================================
 
barcolor(buySignal ? bullColor : sellSignal ? bearColor : na, title = "Signal Candle")
plotshape(blockedCross, title = "Blocked Crossover", style = shape.xcross,
     location = location.abovebar, color = color.gray, size = size.tiny)
 
p1 = plot(fastEMA, "Fast EMA", color = color.new(color.aqua, 30))
p2 = plot(slowEMA, "Slow EMA", color = color.new(color.orange, 30))
fill(p1, p2, color = fastEMA > slowEMA ? color.new(color.green, 75) : color.new(color.red, 75), title = "EMA Ribbon")
 
plotshape(buySignal, title = "BUY", style = shape.labelup, location = location.belowbar, color = bullColor,
     text = "BUY", textcolor = color.white, size = size.small)
plotshape(sellSignal, title = "SELL", style = shape.labeldown, location = location.abovebar, color = bearColor,
     text = "SELL", textcolor = color.white, size = size.small)
 
// =============================================================================
// RETEST HIGHLIGHT
// =============================================================================
 
var int openTradeCount = 0
touchFast = low <= fastEMA and high >= fastEMA
notCrossedSlow = fastEMA > slowEMA ? low > slowEMA : high < slowEMA
retestBar = openTradeCount > 0 and touchFast and notCrossedSlow and not buySignal and not sellSignal
barcolor(retestBar ? retestColor : na, title = "EMA Retest")
 
// =============================================================================
// 6. TRADE LADDER - OPEN NEW TRADES
// =============================================================================
 
var int[]    tDir       = array.new_int()
var float[]  tEntry     = array.new_float()
var int[]    tEntryBar  = array.new_int()
var float[]  tSl        = array.new_float()
var float[]  tTp1       = array.new_float()
var float[]  tTp2       = array.new_float()
var float[]  tTp3       = array.new_float()
var bool[]   tTp1Hit    = array.new_bool()
var bool[]   tTp2Hit    = array.new_bool()
var bool[]   tTp3Hit    = array.new_bool()
var bool[]   tBE        = array.new_bool()
var bool[]   tClosed    = array.new_bool()
var bool[]   tHidden    = array.new_bool()
var int[]    tCloseBar  = array.new_int()
var string[] tOutcome   = array.new_string()
var line[]   tEntryLine = array.new_line()
var line[]   tSlLine    = array.new_line()
var line[]   tTp1Line   = array.new_line()
var line[]   tTp2Line   = array.new_line()
var line[]   tTp3Line   = array.new_line()
var label[]  tEntryLabel = array.new_label()
var label[]  tSlLabel    = array.new_label()
var label[]  tTp1Label   = array.new_label()
var label[]  tTp2Label   = array.new_label()
var label[]  tTp3Label   = array.new_label()
 
fmtDist(priceDist) =>
    pts = priceDist / syminfo.mintick / 10
    "$" + str.tostring(priceDist, format.mintick) + " / " + str.tostring(pts, "#.#") + " pts"
 
if buySignal or sellSignal
    dir = buySignal ? 1 : -1
    entryP = close
    riskDist = atrValue * atrMult
    slP  = dir == 1 ? entryP - riskDist : entryP + riskDist
    tp1P = dir == 1 ? entryP + riskDist : entryP - riskDist
    tp2P = dir == 1 ? entryP + riskDist * 2 : entryP - riskDist * 2
    tp3P = dir == 1 ? entryP + riskDist * 3 : entryP - riskDist * 3
    sideColor = dir == 1 ? bullColor : bearColor
    xEnd = bar_index + tradeLabelOffset
 
    eLine = line.new(bar_index, entryP, xEnd, entryP, color = sideColor, style = line.style_dashed, width = 1)
    sLine = line.new(bar_index, slP, xEnd, slP, color = color.new(color.red, 0), style = line.style_solid, width = 1)
    t1Line = line.new(bar_index, tp1P, xEnd, tp1P, color = color.new(color.teal, 0), style = line.style_dashed, width = 1)
    t2Line = line.new(bar_index, tp2P, xEnd, tp2P, color = color.new(color.teal, 0), style = line.style_dashed, width = 1)
    t3Line = line.new(bar_index, tp3P, xEnd, tp3P, color = color.new(color.teal, 0), style = line.style_dashed, width = 1)
 
    eLabel = label.new(xEnd, entryP, "ENTRY: " + str.tostring(entryP, format.mintick), style = label.style_label_left,
         color = color.new(color.black, 100), textcolor = sideColor, size = size.small)
    sLabel = label.new(xEnd, slP, "SL: " + str.tostring(slP, format.mintick) + " (" + fmtDist(riskDist) + ")",
         style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.red, size = size.small)
    t1Label = label.new(xEnd, tp1P, "TP1: " + str.tostring(tp1P, format.mintick) + " (" + fmtDist(riskDist) + ")",
         style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.teal, size = size.small)
    t2Label = label.new(xEnd, tp2P, "TP2: " + str.tostring(tp2P, format.mintick) + " (" + fmtDist(riskDist * 2) + ")",
         style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.teal, size = size.small)
    t3Label = label.new(xEnd, tp3P, "TP3: " + str.tostring(tp3P, format.mintick) + " (" + fmtDist(riskDist * 3) + ")",
         style = label.style_label_left, color = color.new(color.black, 100), textcolor = color.teal, size = size.small)
 
    array.push(tDir, dir)
    array.push(tEntry, entryP)
    array.push(tEntryBar, bar_index)
    array.push(tSl, slP)
    array.push(tTp1, tp1P)
    array.push(tTp2, tp2P)
    array.push(tTp3, tp3P)
    array.push(tTp1Hit, false)
    array.push(tTp2Hit, false)
    array.push(tTp3Hit, false)
    array.push(tBE, false)
    array.push(tClosed, false)
    array.push(tHidden, false)
    array.push(tCloseBar, -1)
    array.push(tOutcome, "")
    array.push(tEntryLine, eLine)
    array.push(tSlLine, sLine)
    array.push(tTp1Line, t1Line)
    array.push(tTp2Line, t2Line)
    array.push(tTp3Line, t3Line)
    array.push(tEntryLabel, eLabel)
    array.push(tSlLabel, sLabel)
    array.push(tTp1Label, t1Label)
    array.push(tTp2Label, t2Label)
    array.push(tTp3Label, t3Label)
 
    lastTradeOpenBar := bar_index
 
// =============================================================================
// TRADE LADDER - MANAGE OPEN TRADES EVERY BAR
// =============================================================================
 
openCountNow = 0
openDirNow = 0
 
if array.size(tDir) > 0
    for i = 0 to array.size(tDir) - 1
        if not array.get(tClosed, i)
            dir  = array.get(tDir, i)
            entryP = array.get(tEntry, i)
            slP  = array.get(tSl, i)
            tp1P = array.get(tTp1, i)
            tp2P = array.get(tTp2, i)
            tp3P = array.get(tTp3, i)
            be   = array.get(tBE, i)
            tp1h = array.get(tTp1Hit, i)
            tp2h = array.get(tTp2Hit, i)
            tp3h = array.get(tTp3Hit, i)
            effectiveStop = be ? entryP : slP
            closedNow = false
            outcomeNow = ""
 
            if dir == 1
                if low <= effectiveStop
                    closedNow := true
                    outcomeNow := be ? "BE" : "SL"
                else
                    if not tp1h and high >= tp1P
                        tp1h := true
                        array.set(tTp1Hit, i, true)
                        line.set_color(array.get(tTp1Line, i), hitColor)
                        label.set_textcolor(array.get(tTp1Label, i), hitColor)
                        if useBreakeven
                            be := true
                            array.set(tBE, i, true)
                            line.set_style(array.get(tEntryLine, i), line.style_dotted)
                    if tp1h and not tp2h and high >= tp2P
                        tp2h := true
                        array.set(tTp2Hit, i, true)
                        line.set_color(array.get(tTp2Line, i), hitColor)
                        label.set_textcolor(array.get(tTp2Label, i), hitColor)
                    if tp2h and not tp3h and high >= tp3P
                        tp3h := true
                        array.set(tTp3Hit, i, true)
                        line.set_color(array.get(tTp3Line, i), hitColor)
                        label.set_textcolor(array.get(tTp3Label, i), hitColor)
                        closedNow := true
                        outcomeNow := "TP3_FULL"
            else
                if high >= effectiveStop
                    closedNow := true
                    outcomeNow := be ? "BE" : "SL"
                else
                    if not tp1h and low <= tp1P
                        tp1h := true
                        array.set(tTp1Hit, i, true)
                        line.set_color(array.get(tTp1Line, i), hitColor)
                        label.set_textcolor(array.get(tTp1Label, i), hitColor)
                        if useBreakeven
                            be := true
                            array.set(tBE, i, true)
                            line.set_style(array.get(tEntryLine, i), line.style_dotted)
                    if tp1h and not tp2h and low <= tp2P
                        tp2h := true
                        array.set(tTp2Hit, i, true)
                        line.set_color(array.get(tTp2Line, i), hitColor)
                        label.set_textcolor(array.get(tTp2Label, i), hitColor)
                    if tp2h and not tp3h and low <= tp3P
                        tp3h := true
                        array.set(tTp3Hit, i, true)
                        line.set_color(array.get(tTp3Line, i), hitColor)
                        label.set_textcolor(array.get(tTp3Label, i), hitColor)
                        closedNow := true
                        outcomeNow := "TP3_FULL"
 
            if closedNow
                array.set(tClosed, i, true)
                array.set(tCloseBar, i, bar_index)
                array.set(tOutcome, i, outcomeNow)
                line.set_x2(array.get(tEntryLine, i), bar_index)
                line.set_x2(array.get(tSlLine, i), bar_index)
                line.set_x2(array.get(tTp1Line, i), bar_index)
                line.set_x2(array.get(tTp2Line, i), bar_index)
                line.set_x2(array.get(tTp3Line, i), bar_index)
                slHitColor = outcomeNow == "SL" ? color.new(color.red, 0) : color.new(color.gray, 0)
                if outcomeNow == "SL" or outcomeNow == "BE"
                    label.set_text(array.get(tSlLabel, i), label.get_text(array.get(tSlLabel, i)) + "  ✕")
            else
                xEnd = bar_index + tradeLabelOffset
                line.set_x2(array.get(tEntryLine, i), xEnd)
                line.set_x2(array.get(tSlLine, i), xEnd)
                line.set_x2(array.get(tTp1Line, i), xEnd)
                line.set_x2(array.get(tTp2Line, i), xEnd)
                line.set_x2(array.get(tTp3Line, i), xEnd)
                label.set_x(array.get(tEntryLabel, i), xEnd)
                label.set_x(array.get(tSlLabel, i), xEnd)
                label.set_x(array.get(tTp1Label, i), xEnd)
                label.set_x(array.get(tTp2Label, i), xEnd)
                label.set_x(array.get(tTp3Label, i), xEnd)
                openCountNow += 1
                openDirNow := dir
 
openTradeCount := openCountNow
 
// =============================================================================
// DECLUTTER - only the most recently CLOSED trades keep their ladder drawn;
// older completed trades still count toward the history tally below, they
// just stop cluttering the chart visually
// =============================================================================
 
if array.size(tClosed) > 0
    for i = 0 to array.size(tClosed) - 1
        if array.get(tClosed, i) and not array.get(tHidden, i)
            myCloseBar = array.get(tCloseBar, i)
            rank = 0
            for j = 0 to array.size(tClosed) - 1
                if array.get(tClosed, j) and array.get(tCloseBar, j) > myCloseBar
                    rank += 1
            if rank >= maxVisibleClosed
                elnH = array.get(tEntryLine, i)
                slnH = array.get(tSlLine, i)
                t1H  = array.get(tTp1Line, i)
                t2H  = array.get(tTp2Line, i)
                t3H  = array.get(tTp3Line, i)
                if not na(elnH)
                    line.delete(elnH)
                    array.set(tEntryLine, i, na)
                if not na(slnH)
                    line.delete(slnH)
                    array.set(tSlLine, i, na)
                if not na(t1H)
                    line.delete(t1H)
                    array.set(tTp1Line, i, na)
                if not na(t2H)
                    line.delete(t2H)
                    array.set(tTp2Line, i, na)
                if not na(t3H)
                    line.delete(t3H)
                    array.set(tTp3Line, i, na)
                elbH = array.get(tEntryLabel, i)
                slbH = array.get(tSlLabel, i)
                t1bH = array.get(tTp1Label, i)
                t2bH = array.get(tTp2Label, i)
                t3bH = array.get(tTp3Label, i)
                if not na(elbH)
                    label.delete(elbH)
                    array.set(tEntryLabel, i, na)
                if not na(slbH)
                    label.delete(slbH)
                    array.set(tSlLabel, i, na)
                if not na(t1bH)
                    label.delete(t1bH)
                    array.set(tTp1Label, i, na)
                if not na(t2bH)
                    label.delete(t2bH)
                    array.set(tTp2Label, i, na)
                if not na(t3bH)
                    label.delete(t3bH)
                    array.set(tTp3Label, i, na)
                array.set(tHidden, i, true)
 
// ---- prune oldest completed trade once beyond the history limit ----
closedTotal = 0
if array.size(tClosed) > 0
    for i = 0 to array.size(tClosed) - 1
        if array.get(tClosed, i)
            closedTotal += 1
 
if closedTotal > maxHistory
    oldestIdx = -1
    oldestBar = 999999999
    for i = 0 to array.size(tClosed) - 1
        if array.get(tClosed, i) and array.get(tCloseBar, i) < oldestBar
            oldestBar := array.get(tCloseBar, i)
            oldestIdx := i
    if oldestIdx >= 0
        elnP = array.get(tEntryLine, oldestIdx)
        slnP = array.get(tSlLine, oldestIdx)
        t1P  = array.get(tTp1Line, oldestIdx)
        t2P  = array.get(tTp2Line, oldestIdx)
        t3P  = array.get(tTp3Line, oldestIdx)
        elbP = array.get(tEntryLabel, oldestIdx)
        slbP = array.get(tSlLabel, oldestIdx)
        t1bP = array.get(tTp1Label, oldestIdx)
        t2bP = array.get(tTp2Label, oldestIdx)
        t3bP = array.get(tTp3Label, oldestIdx)
        if not na(elnP)
            line.delete(elnP)
        if not na(slnP)
            line.delete(slnP)
        if not na(t1P)
            line.delete(t1P)
        if not na(t2P)
            line.delete(t2P)
        if not na(t3P)
            line.delete(t3P)
        if not na(elbP)
            label.delete(elbP)
        if not na(slbP)
            label.delete(slbP)
        if not na(t1bP)
            label.delete(t1bP)
        if not na(t2bP)
            label.delete(t2bP)
        if not na(t3bP)
            label.delete(t3bP)
        array.remove(tDir, oldestIdx)
        array.remove(tEntry, oldestIdx)
        array.remove(tEntryBar, oldestIdx)
        array.remove(tSl, oldestIdx)
        array.remove(tTp1, oldestIdx)
        array.remove(tTp2, oldestIdx)
        array.remove(tTp3, oldestIdx)
        array.remove(tTp1Hit, oldestIdx)
        array.remove(tTp2Hit, oldestIdx)
        array.remove(tTp3Hit, oldestIdx)
        array.remove(tBE, oldestIdx)
        array.remove(tClosed, oldestIdx)
        array.remove(tHidden, oldestIdx)
        array.remove(tCloseBar, oldestIdx)
        array.remove(tOutcome, oldestIdx)
        array.remove(tEntryLine, oldestIdx)
        array.remove(tSlLine, oldestIdx)
        array.remove(tTp1Line, oldestIdx)
        array.remove(tTp2Line, oldestIdx)
        array.remove(tTp3Line, oldestIdx)
        array.remove(tEntryLabel, oldestIdx)
        array.remove(tSlLabel, oldestIdx)
        array.remove(tTp1Label, oldestIdx)
        array.remove(tTp2Label, oldestIdx)
        array.remove(tTp3Label, oldestIdx)
 
// =============================================================================
// LIVE DASHBOARDS (rendered on the last bar only)
// =============================================================================
 
tick(cond) => cond ? "✓" : "✕"
tickColor(cond) => cond ? bullColor : bearColor
 
var table mainDash = table.new(position.top_right, 2, 24, bgcolor = color.new(color.black, 10),
     border_color = color.new(color.gray, 40), border_width = 1)
var table howItWorks = table.new(position.top_left, 1, 8, bgcolor = color.new(color.black, 10),
     border_color = color.new(color.gray, 40), border_width = 1)
var table confluenceTbl = table.new(position.bottom_left, 2, 13, bgcolor = color.new(color.black, 10),
     border_color = color.new(color.gray, 40), border_width = 1)
var table footerTbl = table.new(position.bottom_center, 3, 2, bgcolor = color.new(color.black, 10),
     border_color = color.new(color.gray, 40), border_width = 1)
 
if barstate.islast
    // ---- tally closed-trade history ----
    tp1Count = 0
    fullCount = 0
    slCount = 0
    beCount = 0
    if array.size(tClosed) > 0
        for i = 0 to array.size(tClosed) - 1
            if array.get(tClosed, i)
                if array.get(tTp1Hit, i)
                    tp1Count += 1
                oc = array.get(tOutcome, i)
                if oc == "TP3_FULL"
                    fullCount += 1
                if oc == "SL"
                    slCount += 1
                if oc == "BE"
                    beCount += 1
    closedShown = math.min(closedTotal, maxHistory)
 
    latestDirTxt = "NONE"
    latestStatusTxt = "NONE"
    if array.size(tDir) > 0
        lastIdx = array.size(tDir) - 1
        latestDirTxt := array.get(tDir, lastIdx) == 1 ? "LONG" : "SHORT"
        lc = array.get(tClosed, lastIdx)
        latestStatusTxt := not lc ? "OPEN" : "CLOSED " + array.get(tOutcome, lastIdx)
 
    goldStatusTxt = isGoldSymbol ? "RECOGNIZED" : "NOT RECOGNIZED"
    goldStatusColor = isGoldSymbol ? bullColor : bearColor
    dashBg = (goldOnlyMode and not isGoldSymbol) ? color.new(color.red, 60) : color.new(color.black, 10)
    table.set_bgcolor(mainDash, dashBg)
 
    signalLineTxt = buySignal ? "NEW BUY SIGNAL FIRED" : sellSignal ? "NEW SELL SIGNAL FIRED" :
         "LAST BLOCK: " + lastBlockDir + " - " + lastBlockReason
 
    entryGatesTxt = canSignalNow ? "ALL OPEN" : (not goldOk ? "GOLD BLOCK" : not sessionOk ? "SESSION BLOCK" :
         not adrOk ? "ADR BLOCK" : "SPACING BLOCK")
 
    r = 0
    table.cell(mainDash, 0, r, "GOLD SNIPER", text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(mainDash, 1, r, syminfo.ticker, text_color = color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Instrument", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, goldStatusTxt, text_color = goldStatusColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Bull Score", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(bullScore) + "/" + str.tostring(totalFactors) + "  (" +
         str.tostring(math.round(bullScore / totalFactors * 100)) + "%)", text_color = bullColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Bear Score", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(bearScore) + "/" + str.tostring(totalFactors) + "  (" +
         str.tostring(math.round(bearScore / totalFactors * 100)) + "%)", text_color = bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Market Bias", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, biasLabel, text_color = biasColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Price / VWAP", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, close > vwapValue ? "ABOVE" : "BELOW", text_color = close > vwapValue ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "RSI (14)", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(rsiMain, "#.#"), text_color = rsiMain > 50 ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "MACD", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, (macdLine > macdSignal ? "BULL " : "BEAR ") + str.tostring(macdHist, "#.##"),
         text_color = macdLine > macdSignal ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "ADX (14)", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(adxValue, "#.#") + (diPlus > diMinus ? " ▲" : " ▼"),
         text_color = adxValue > 25 ? (diPlus > diMinus ? bullColor : bearColor) : color.gray, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "EMA " + str.tostring(fastLen) + "/" + str.tostring(slowLen), text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, fastEMA > slowEMA ? "BULL" : "BEAR", text_color = fastEMA > slowEMA ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Volume", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, volume > volAvg ? "HIGH" : "LOW", text_color = volume > volAvg ? bullColor : color.gray, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, confirmTf + " RSI", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(confirmRsi, "#.#"), text_color = confirmRsi > 50 ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "DXY (inverse)", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, (dxyWeak ? "▼ " : "▲ ") + str.tostring(dxyClose, "#.##") + "  r " + str.tostring(correlationValue, "#.##") +
         (inverseActive ? " ✓" : " ✕"), text_color = inverseActive ? (dxyWeak ? bullColor : bearColor) : color.gray, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Session", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, sessionName, text_color = sessionActive ? color.yellow : color.gray, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "ADR (" + str.tostring(adrLookbackDays) + "d)", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, "$" + str.tostring(adrValue, format.mintick) + " - used " + str.tostring(math.round(adrUsedPct)) + "%",
         text_color = adrExhausted ? bearColor : color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "ATR (" + str.tostring(atrLen) + ")", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, fmtDist(atrValue), text_color = color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Next Stop Distance", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, fmtDist(atrValue * atrMult), text_color = color.orange, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Confluence", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(math.max(bullScore, bearScore)) + "/" + str.tostring(totalFactors) +
         " (min " + str.tostring(effectiveMin) + ")", text_color = color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Entry Gates", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, entryGatesTxt, text_color = canSignalNow ? bullColor : bearColor, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Open Trades", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(openTradeCount) + (openTradeCount > 0 ? (openDirNow == 1 ? " LONG" : " SHORT") : ""),
         text_color = openTradeCount > 0 ? (openDirNow == 1 ? bullColor : bearColor) : color.gray, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Latest Trade", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, latestDirTxt + " - " + latestStatusTxt, text_color = color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "History (last " + str.tostring(maxHistory) + ")", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, str.tostring(closedShown) + " closed - TP1 " + str.tostring(tp1Count) + " - full " +
         str.tostring(fullCount) + " - SL " + str.tostring(slCount) + " - BE " + str.tostring(beCount), text_color = color.white, text_size = size.small)
    r += 1
    table.cell(mainDash, 0, r, "Signal", text_color = color.gray, text_size = size.small)
    table.cell(mainDash, 1, r, signalLineTxt, text_color = (buySignal ? bullColor : sellSignal ? bearColor : color.gray), text_size = size.small)
 
    // ---- how it works legend ----
    table.cell(howItWorks, 0, 0, "HOW IT WORKS", text_color = color.yellow, text_size = size.small)
    table.cell(howItWorks, 0, 1, "1. Trigger: fast/slow EMA cross on bar close", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 2, "2. Score: " + str.tostring(effectiveMin) + "/" + str.tostring(totalFactors) + " factors must reach the minimum", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 3, "3. DXY inverse-correlation confirms the gold side", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 4, "4. Trade window: London / New York only", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 5, "5. ADR exhausted = no late entries", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 6, "6. ATR stop, 3 targets, BE at TP1, each trade runs to SL / TP", text_color = color.white, text_size = size.small)
    table.cell(howItWorks, 0, 7, "7. Entries stay at least " + str.tostring(minBarsBetweenTrades) + " bars apart. Gold-only: XAUUSD, GOLD, GC", text_color = color.gray, text_size = size.small)
 
    // ---- confluence checklist (whichever side currently scores stronger) ----
    showBull = bullScore >= bearScore
 
    table.cell(confluenceTbl, 0, 0, (showBull ? "BUY" : "SELL") + " CONFLUENCE  " + str.tostring(showBull ? bullScore : bearScore) + "/" + str.tostring(totalFactors),
         text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(confluenceTbl, 1, 0, "", bgcolor = color.new(color.black, 0))
    table.cell(confluenceTbl, 0, 1, "1 Price vs VWAP", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 1, tick(showBull ? f1Bull : f1Bear), text_color = tickColor(showBull ? f1Bull : f1Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 2, "2 RSI vs 50", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 2, tick(showBull ? f2Bull : f2Bear), text_color = tickColor(showBull ? f2Bull : f2Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 3, "3 MACD vs signal", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 3, tick(showBull ? f3Bull : f3Bear), text_color = tickColor(showBull ? f3Bull : f3Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 4, "4 Fast EMA vs slow", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 4, tick(showBull ? f4Bull : f4Bear), text_color = tickColor(showBull ? f4Bull : f4Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 5, "5 ADX > 25 on side", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 5, tick(showBull ? f5Bull : f5Bear), text_color = tickColor(showBull ? f5Bull : f5Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 6, "6 Volume push, matching candle", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 6, tick(showBull ? f6Bull : f6Bear), text_color = tickColor(showBull ? f6Bull : f6Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 7, "7 " + confirmTf + " RSI vs 50", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 7, tick(showBull ? f7Bull : f7Bear), text_color = tickColor(showBull ? f7Bull : f7Bear), text_size = size.small)
    table.cell(confluenceTbl, 0, 8, "8 DXY inverse link", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 8, useDxyFactor ? tick(showBull ? f8Bull : f8Bear) : "n/a", text_color = useDxyFactor ? tickColor(showBull ? f8Bull : f8Bear) : color.gray, text_size = size.small)
 
    table.cell(confluenceTbl, 0, 9, "GOLD GATES", text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(confluenceTbl, 1, 9, "", bgcolor = color.new(color.black, 0))
    table.cell(confluenceTbl, 0, 10, "Gold chart", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 10, tick(goldOk), text_color = tickColor(goldOk), text_size = size.small)
    table.cell(confluenceTbl, 0, 11, "London / New York session", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 11, tick(sessionOk), text_color = tickColor(sessionOk), text_size = size.small)
    table.cell(confluenceTbl, 0, 12, "ADR not exhausted", text_color = color.white, text_size = size.small)
    table.cell(confluenceTbl, 1, 12, tick(adrOk), text_color = tickColor(adrOk), text_size = size.small)
 
    // ---- static strategy footer ----
    table.cell(footerTbl, 0, 0, "STRATEGY", text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(footerTbl, 1, 0, "BEST FOR", text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(footerTbl, 2, 0, "WORKS ON", text_color = color.yellow, bgcolor = color.new(color.black, 0), text_size = size.small)
    table.cell(footerTbl, 0, 1, "8-Factor Gold Confluence", text_color = color.white, text_size = size.small)
    table.cell(footerTbl, 1, 1, "XAUUSD 5m-1H intraday", text_color = color.white, text_size = size.small)
    table.cell(footerTbl, 2, 1, "Spot XAU - GC / MGC futures", text_color = color.white, text_size = size.small)
 
// =============================================================================
// ALERTS
// =============================================================================
 
alertcondition(buySignal, title = "Gold Sniper BUY", message = "Gold Sniper Targets: BUY signal fired.")
alertcondition(sellSignal, title = "Gold Sniper SELL", message = "Gold Sniper Targets: SELL signal fired.")

← 返回列表