SPX Sector AD + Iceberg TICK + UT Bot

nellutla · study · 229 行 · 点赞 34 · TradingView 原页

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

Pine Script

//@version=6
indicator("SPX Sector AD + Iceberg TICK + UT Bot", shorttitle="Breadth+", overlay=false)
 
//========================
//   SETTINGS GROUPS
//========================
groupWeights = "Sector Weights"
groupStyle   = "Sector Plot Style"
groupTick    = "Iceberg TICK Settings"
groupUT      = "UT Bot Signals (On Weighted AD)"
groupLabel   = "Label Settings"
 
//========================
//   SYMBOL INPUTS
//========================
symXLK  = input.symbol("XLK",  "XLK - Technology")
symXLF  = input.symbol("XLF",  "XLF - Financial Services")
symXLC  = input.symbol("XLC",  "XLC - Communication Services")
symXLY  = input.symbol("XLY",  "XLY - Consumer Discretionary")
symXLV  = input.symbol("XLV",  "XLV - Healthcare")
symXLI  = input.symbol("XLI",  "XLI - Industrials")
symXLP  = input.symbol("XLP",  "XLP - Consumer Staples")
symXLE  = input.symbol("XLE",  "XLE - Energy")
symXLU  = input.symbol("XLU",  "XLU - Utilities")
symXLRE = input.symbol("XLRE", "XLRE - Real Estate")
symXLB  = input.symbol("XLB",  "XLB - Basic Materials")
 
//========================
//   TICK INPUTS
//========================
symTick      = input.symbol("TVC:TICK", "TICK Symbol", group=groupTick)
showTick     = input.bool(true, "Show Cumulative TICK", group=groupTick)
tickUpColor  = input.color(color.new(color.teal, 85), "Iceberg Color Up", group=groupTick)
tickDnColor  = input.color(color.new(color.maroon, 85), "Iceberg Color Down", group=groupTick)
tickDivisor  = input.float(2000.0, "Tick Visual Divisor", minval=1.0, group=groupTick, tooltip="Scales the TICK down to fit behind the sectors.")
 
//========================
//   UT BOT INPUTS
//========================
utKey    = input.float(1.0, "UT Key Value (Sensitivity)", step=0.1, group=groupUT)
utPeriod = input.int(10, "UT ATR Period", minval=1, group=groupUT)
showUT   = input.bool(true, "Show UT Signals", group=groupUT)
 
//========================
//   SECTOR WEIGHTS
//========================
wXLK  = input.float(33.39, "XLK weight (%)",  group=groupWeights)
wXLF  = input.float(12.88, "XLF weight (%)",  group=groupWeights)
wXLC  = input.float(11.02, "XLC weight (%)",  group=groupWeights)
wXLY  = input.float(10.44, "XLY weight (%)",  group=groupWeights)
wXLV  = input.float(9.45,  "XLV weight (%)",  group=groupWeights)
wXLI  = input.float(8.58,  "XLI weight (%)",  group=groupWeights)
wXLP  = input.float(5.00,  "XLP weight (%)",  group=groupWeights)
wXLE  = input.float(3.17,  "XLE weight (%)",  group=groupWeights)
wXLU  = input.float(2.25,  "XLU weight (%)",  group=groupWeights)
wXLRE = input.float(1.96,  "XLRE weight (%)", group=groupWeights)
wXLB  = input.float(1.85,  "XLB weight (%)",  group=groupWeights)
 
scaleFactor = input.float(1.0, "Output Scale Factor", minval=0.1, step=0.1, group=groupWeights)
 
totalW = wXLK + wXLF + wXLC + wXLY + wXLV + wXLI + wXLP + wXLE + wXLU + wXLRE + wXLB
norm(x) => totalW != 0.0 ? x / totalW : 0.0
 
nwXLK  = norm(wXLK)
nwXLF  = norm(wXLF)
nwXLC  = norm(wXLC)
nwXLY  = norm(wXLY)
nwXLV  = norm(wXLV)
nwXLI  = norm(wXLI)
nwXLP  = norm(wXLP)
nwXLE  = norm(wXLE)
nwXLU  = norm(wXLU)
nwXLRE = norm(wXLRE)
nwXLB  = norm(wXLB)
 
//========================
//   STYLE SETTINGS
//========================
showUnweighted = input.bool(true,  "Show Unweighted Line", group=groupStyle)
showWeighted   = input.bool(true,  "Show Weighted Bars",   group=groupStyle)
 
lineColor      = input.color(color.new(color.blue, 0), "Unweighted Line Color", group=groupStyle)
lineWidth      = input.int(2, "Unweighted Line Width", minval=1, maxval=5, group=groupStyle)
 
bullBarColor   = input.color(color.green, "Weighted Bar Up Color",   group=groupStyle)
bearBarColor   = input.color(color.red,   "Weighted Bar Down Color", group=groupStyle)
barTransp      = input.int(40, "Weighted Bar Transparency (0–100)", minval=0, maxval=100, group=groupStyle)
 
zeroLineColor  = input.color(color.gray, "Zero Line Color", group=groupStyle)
 
//========================
//   LABEL SETTINGS
//========================
showLabel      = input.bool(true, "Show Adv/Dec Label", group=groupLabel)
labelTextColor = input.color(color.white, "Label Text Color", group=groupLabel)
labelBgColor   = input.color(color.new(color.blue, 60), "Label Background Color", group=groupLabel)
 
//========================
//   CALCULATION: SECTORS
//========================
f_pct(sym) =>
    float c  = request.security(sym, timeframe.period, close)
    float oD = request.security(sym, "D", open)
    float result = oD != 0.0 ? (c - oD) / oD * 100.0 : 0.0
    result
 
pctXLK  = f_pct(symXLK)
pctXLF  = f_pct(symXLF)
pctXLC  = f_pct(symXLC)
pctXLY  = f_pct(symXLY)
pctXLV  = f_pct(symXLV)
pctXLI  = f_pct(symXLI)
pctXLP  = f_pct(symXLP)
pctXLE  = f_pct(symXLE)
pctXLU  = f_pct(symXLU)
pctXLRE = f_pct(symXLRE)
pctXLB  = f_pct(symXLB)
 
dirXLK  = pctXLK  > 0 ? 1 : pctXLK  < 0 ? -1 : 0
dirXLF  = pctXLF  > 0 ? 1 : pctXLF  < 0 ? -1 : 0
dirXLC  = pctXLC  > 0 ? 1 : pctXLC  < 0 ? -1 : 0
dirXLY  = pctXLY  > 0 ? 1 : pctXLY  < 0 ? -1 : 0
dirXLV  = pctXLV  > 0 ? 1 : pctXLV  < 0 ? -1 : 0
dirXLI  = pctXLI  > 0 ? 1 : pctXLI  < 0 ? -1 : 0
dirXLP  = pctXLP  > 0 ? 1 : pctXLP  < 0 ? -1 : 0
dirXLE  = pctXLE  > 0 ? 1 : pctXLE  < 0 ? -1 : 0
dirXLU  = pctXLU  > 0 ? 1 : pctXLU  < 0 ? -1 : 0
dirXLRE = pctXLRE > 0 ? 1 : pctXLRE < 0 ? -1 : 0
dirXLB  = pctXLB  > 0 ? 1 : pctXLB  < 0 ? -1 : 0
 
// Unweighted AD
sectorCount = 11.0
unweightedAD_raw = pctXLK + pctXLF + pctXLC + pctXLY + pctXLV + pctXLI + pctXLP + pctXLE + pctXLU + pctXLRE + pctXLB
unweightedAD = (unweightedAD_raw / sectorCount) * scaleFactor
 
// Weighted AD
weightedAD_raw = (pctXLK * nwXLK) + (pctXLF * nwXLF) + (pctXLC * nwXLC) + (pctXLY * nwXLY) + (pctXLV * nwXLV) + (pctXLI * nwXLI) + (pctXLP * nwXLP) + (pctXLE * nwXLE) + (pctXLU * nwXLU) + (pctXLRE * nwXLRE) + (pctXLB * nwXLB)
weightedAD = weightedAD_raw * scaleFactor
 
// Counts
advCount = (dirXLK==1?1:0) + (dirXLF==1?1:0) + (dirXLC==1?1:0) + (dirXLY==1?1:0) + (dirXLV==1?1:0) + (dirXLI==1?1:0) + (dirXLP==1?1:0) + (dirXLE==1?1:0) + (dirXLU==1?1:0) + (dirXLRE==1?1:0) + (dirXLB==1?1:0)
decCount = (dirXLK==-1?1:0) + (dirXLF==-1?1:0) + (dirXLC==-1?1:0) + (dirXLY==-1?1:0) + (dirXLV==-1?1:0) + (dirXLI==-1?1:0) + (dirXLP==-1?1:0) + (dirXLE==-1?1:0) + (dirXLU==-1?1:0) + (dirXLRE==-1?1:0) + (dirXLB==-1?1:0)
unchCount = 11 - advCount - decCount
 
//========================
//   CALCULATION: ICEBERG TICK
//========================
rawTick = request.security(symTick, timeframe.period, close)
var float cumTick = 0.0
isNewDay = session.isfirstbar_regular
if isNewDay
    cumTick := 0.0
cumTick := cumTick + (not na(rawTick) ? rawTick : 0)
scaledTick = cumTick / tickDivisor
 
//========================
//   CALCULATION: UT BOT (ON WEIGHTED AD)
//========================
// We use the Weighted AD as the "Source" for the Bot
// We calculate a synthetic ATR based on the volatility of the AD line itself
utSrc = weightedAD
utATR = ta.atr(utPeriod) // This uses price ATR, which is wrong for the indicator pane.
// Correct approach: Calculate ATR of the Weighted AD Line manually
// TR of AD = max(high-low, abs(high-prevClose), abs(low-prevClose))
// Since AD is a single line per bar, High=Low=Close.
// So TR is effectively abs(currentAD - prevAD).
utTR = math.abs(utSrc - utSrc[1])
utSmoothedTR = ta.rma(utTR, utPeriod) // Wilders Smoothing (standard for ATR)
utLoss = utKey * utSmoothedTR
 
var float xATRTrailingStop = 0.0
xATRTrailingStop := 
     (utSrc > nz(xATRTrailingStop[1], 0) and utSrc[1] > nz(xATRTrailingStop[1], 0)) ? math.max(nz(xATRTrailingStop[1]), utSrc - utLoss) :
     (utSrc < nz(xATRTrailingStop[1], 0) and utSrc[1] < nz(xATRTrailingStop[1], 0)) ? math.min(nz(xATRTrailingStop[1]), utSrc + utLoss) :
     (utSrc > nz(xATRTrailingStop[1], 0)) ? utSrc - utLoss : utSrc + utLoss
 
var int pos = 0
// We also need to fix 'pos' below, which likely used iff as well in your previous attempts
pos := 
     (utSrc[1] < nz(xATRTrailingStop[1], 0) and utSrc > nz(xATRTrailingStop[1], 0)) ? 1 :
     (utSrc[1] > nz(xATRTrailingStop[1], 0) and utSrc < nz(xATRTrailingStop[1], 0)) ? -1 : nz(pos[1], 0)
 
utBuy  = showUT and utSrc > xATRTrailingStop and pos == 1 and pos[1] != 1
utSell = showUT and utSrc < xATRTrailingStop and pos == -1 and pos[1] != -1
 
//========================
//   PLOTTING
//========================
// 1. ICEBERG TICK
tickColor = scaledTick >= 0 ? tickUpColor : tickDnColor
plot(showTick ? scaledTick : na, "Cumulative TICK", color=tickColor, style=plot.style_area)
 
// 2. SECTOR AD PLOTS
hline(0, "Zero Line", color=zeroLineColor)
 
// Weighted Histogram
// Color logic: If UT Bot is Long (pos==1), bars are Green/DarkGreen. If Short, Red/DarkRed.
wColor = pos == 1 ? (weightedAD >= 0 ? color.new(bullBarColor, barTransp) : color.new(bullBarColor, barTransp+30)) : (weightedAD < 0 ? color.new(bearBarColor, barTransp) : color.new(bearBarColor, barTransp+30))
 
plot(showWeighted ? weightedAD : na, title="Weighted (Bar)", color=wColor, style=plot.style_histogram, linewidth=4)
 
// Unweighted Line
plot(showUnweighted ? unweightedAD : na, title="Unweighted (Line)", color=lineColor, linewidth=lineWidth)
 
// 3. UT BOT PLOTS
plot(showUT ? xATRTrailingStop : na, "Trailing Stop", color=pos == 1 ? color.green : color.red, linewidth=1, style=plot.style_linebr)
 
plotshape(utBuy,  title="Buy Signal",  style=shape.labelup,   location=location.bottom, color=color.green, text="B", textcolor=color.white, size=size.tiny)
plotshape(utSell, title="Sell Signal", style=shape.labeldown, location=location.top,    color=color.red,   text="S", textcolor=color.white, size=size.tiny)
 
//========================
//   LABELS & ALERTS
//========================
// Data Window
plotchar(advCount, "Advancing Sectors", "", location.top, color=color.green)
plotchar(decCount, "Declining Sectors", "", location.top, color=color.red)
 
// Dashboard Label
var label infoLabel = label.new(na, na, "", style=label.style_label_left, textcolor=labelTextColor, color=labelBgColor)
if showLabel and barstate.islast
    label.set_xy(infoLabel, bar_index + 1, unweightedAD)
    labelText = "Sectors\n" + "Adv: " + str.tostring(advCount) + "\n" + "Dec: " + str.tostring(decCount) + "Unch: " + str.tostring(unchCount) + "Cum TICK: " + str.tostring(math.round(cumTick)) + "Signal: " + (pos == 1 ? "LONG" : "SHORT")
    label.set_text(infoLabel, labelText)
    label.set_color(infoLabel, pos == 1 ? color.new(color.green, 60) : color.new(color.red, 60))
 
 
// Alerts
alertcondition(utBuy,  "Breadth Buy",  "Weighted Sector AD flipped Bullish")
alertcondition(utSell, "Breadth Sell", "Weighted Sector AD flipped Bearish")

← 返回列表