Sector ETF Intraday Tracker WW

wyndmillwillie · study · 146 行 · 点赞 7 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © windmillwillie
 
//@version=6
// Market Sector ETFs Intraday Tracker
// Tracks the % change from daily open of sector ETFs, to visually identify expansion, contraction, or rotation thru color coded plus or minus open percentages.
 
indicator(title='Sector ETF Intraday Tracker WW', shorttitle="Sector ETF Intraday Tracker WW", overlay=true)
 
// Configuration Groups
grp_pos = "Layout & Positioning"
grp_style = "Colors & Contrast"
grp_dim = "Thresholds & Dimensions"
 
// Positioning Inputs
friendlyTextSize = input.string(group=grp_pos, defval="Normal", title="Text Size", options=["Tiny", "Small", "Normal", "Large", "Huge"], inline="p1")
friendlyPosition = input.string(group=grp_pos, defval="Bottom Center", title="Position", options=["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], inline="p1")
verticalOffset = input.int(group=grp_pos, defval=1, title="Vertical Offset Rows", minval=0, maxval=5, step=1, tooltip="Row-sized spacing cushions between the table and chart edge.")
 
// Widths & Sizing
headerWidth = input.float(group=grp_dim, defval=5.0, title="Header Width (%)", minval=1.0, maxval=25.0, step=0.5, inline="w1")
cellWidth = input.float(group=grp_dim, defval=4.0, title="Data Cell Width (%)", minval=1.0, maxval=25.0, step=0.5, inline="w1")
atrFraction = input.float(group=grp_dim, defval=0.8, title="ATR Sensitivity Factor", minval=0.1, maxval=2.0, step=0.1, tooltip="Portion of 14-day ATR required before a sector reaches full color saturation.")
 
// Note: Pine uses Transparency (0=Solid), UI shows Opacity (100=Solid). 
// 30 Transparency = 70% UI Opacity | 70 Transparency = 30% UI Opacity
fadedTransp = input.int(group=grp_dim, defval=70, title="Sub-ATR Transparency", minval=0, maxval=90, step=5, tooltip="Faded transparency for mild moves. 70 = 30% opacity on screen.")
solidTransp = input.int(group=grp_dim, defval=30, title="Max Intensity Transparency", minval=0, maxval=50, step=5, tooltip="Caps maximum opacity so text doesn't blow out. 30 = 70% opacity on screen.")
 
// Style & Colors (Math inverted so UI sliders show 80%, 90%, 70%, 70%)
colorHeader = input.color(group=grp_style, defval=color.new(#8b1c24, 20), title="Title Column BG / Text", inline="c0")
colorHeaderText = input.color(group=grp_style, defval=color.white, title="", inline="c0")
 
colorModerate = input.color(group=grp_style, defval=color.new(#0d3b94, 10), title="Ticker Labels BG / Text", inline="c1")
colorModerateText = input.color(group=grp_style, defval=color.white, title="", inline="c1")
 
// The transparency set here only applies if the base color is used without dynamic reassignment. 
// It is set to 30 to display 70% on the slider, matching the solidTransp cap.
colorBullish = input.color(group=grp_style, defval=color.new(#11d27e, 30), title="Bullish BG / Text", inline="c2")
colorBullishText = input.color(group=grp_style, defval=color.white, title="", inline="c2")
 
colorBearish = input.color(group=grp_style, defval=color.new(#9b1b22, 30), title="Bearish BG / Text", inline="c3")
colorBearishText = input.color(group=grp_style, defval=color.white, title="", inline="c3")
 
// Types
type Symbol
    string name
    string description
    float percentOfMarket = 0.0
    float o = 0.0
    float h = 0.0
    float l = 0.0
    float c = 0.0
    float atr = 0.0
 
// Helper Functions
getPosition(string pos) =>
    switch pos
        "Top Left" => position.top_left
        "Top Center" => position.top_center
        "Top Right" => position.top_right
        "Middle Left" => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right" => position.middle_right
        "Bottom Left" => position.bottom_left
        "Bottom Center" => position.bottom_center
        "Bottom Right" => position.bottom_right
        => position.bottom_center
 
getTextSize(string sz) =>
    switch sz
        "Tiny" => size.tiny
        "Small" => size.small
        "Normal" => size.normal
        "Large" => size.large
        "Huge" => size.huge
        => size.tiny
 
getSymbol(string sym, string desc, float percentOfMarket) =>
    [o, h, l, c, atrVal] = request.security(sym, 'D', [open, high, low, close, ta.atr(14)])
    Symbol.new(sym, desc, percentOfMarket, o, h, l, c, atrVal)
 
// Indicator Logic
collection = array.new<Symbol>()
array.push(collection, getSymbol('XLK', 'Technology Select Sector SPDR Fund', 33.88))
array.push(collection, getSymbol('XLF', 'Financial Select Sector SPDR Fund', 11.82))
array.push(collection, getSymbol('XLV', 'Health Care Select Sector SPDR Fund', 11.73))
array.push(collection, getSymbol('XLY', 'Consumer Discretionary Select Sector SPDR Fund', 9.98))
array.push(collection, getSymbol('XLC', 'Communication Services Select Sector SPDR Fund', 9.22))
array.push(collection, getSymbol('XLI', 'Industrial Select Sector SPDR Fund', 7.62))
array.push(collection, getSymbol('XLP', 'Consumer Staples Select Sector SPDR Fund', 5.79))
array.push(collection, getSymbol('XLE', 'Energy Select Sector SPDR Fund', 3.52))
array.push(collection, getSymbol('XLU', 'Utilities Select Sector SPDR Fund', 2.37))
array.push(collection, getSymbol('XLB', 'Materials Select Sector SPDR Fund', 2.2))
array.push(collection, getSymbol('XLRE', 'Real Estate Select Sector SPDR Fund', 2.1))
 
var table dash = na
 
if barstate.islast and timeframe.in_seconds() <= timeframe.in_seconds('D')
    if not na(dash)
        table.delete(dash)
    
    tablePos = getPosition(friendlyPosition)
    curSize = getTextSize(friendlyTextSize)
    isBottom = str.contains(friendlyPosition, "Bottom")
    
    totalCols = array.size(collection) + 1
    totalRows = 2 + verticalOffset
    
    dash := table.new(tablePos, totalCols, totalRows)
    
    r0 = isBottom ? 0 : verticalOffset
    r1 = isBottom ? 1 : verticalOffset + 1
    
    // Dedicated title styling (directly respects color picker opacity)
    table.cell(dash, 0, r0, 'Sector', bgcolor=colorHeader, text_color=colorHeaderText, text_size=curSize)
    table.cell(dash, 0, r1, '% Move', bgcolor=colorHeader, text_color=colorHeaderText, text_size=curSize)
    table.cell_set_width(dash, 0, r0, headerWidth)
    table.cell_set_width(dash, 0, r1, headerWidth)
    
    for i = 0 to array.size(collection) - 1
        Symbol symbol = array.get(collection, i)
        colIdx = i + 1
        
        diff = symbol.c - symbol.o
        changePercent = (symbol.o < symbol.c ? diff / symbol.c : diff / symbol.o) * 100.0
        
        // Background dynamically overrides the color picker opacity with solidTransp or fadedTransp
        transparency = math.abs(changePercent) > (symbol.atr * atrFraction) ? solidTransp : fadedTransp
        bgColor = color.new(changePercent > 0 ? colorBullish : colorBearish, transparency)
        textColor = changePercent > 0 ? colorBullishText : colorBearishText
        
        tip = symbol.description + " (" + str.tostring(symbol.percentOfMarket) + "% of S&P)"
        
        // Ticker headers respect color picker directly
        table.cell(dash, colIdx, r0, str.upper(symbol.name), bgcolor=colorModerate, text_color=colorModerateText, text_size=curSize, tooltip=tip)
        // Data cells rely on the calculated bgColor override
        table.cell(dash, colIdx, r1, str.tostring(changePercent, "#.##") + "%", bgcolor=bgColor, text_color=textColor, text_size=curSize, tooltip=tip)
        
        table.cell_set_width(dash, colIdx, r0, cellWidth)
        table.cell_set_width(dash, colIdx, r1, cellWidth)
 
    if verticalOffset > 0
        for r = 0 to verticalOffset - 1
            spacerRow = isBottom ? 2 + r : r
            table.cell(dash, 0, spacerRow, text="\u00A0", text_color=color.new(color.white, 100), text_size=curSize)

← 返回列表