52W High & Low | Dashboard

obaid78 · study · 134 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
indicator("52W High & Low | Dashboard v2", overlay=true, max_lines_count=10, max_labels_count=10)
 
// ─────────────────────────────────────
// 1. CALCULATION SETTINGS
// ─────────────────────────────────────
groupCalc = "Calculation"
lookback = input.int(252, "52W Lookback (Trading Days)", minval=20, group=groupCalc)
 
// ─────────────────────────────────────
// 2. DASHBOARD SETTINGS
// ─────────────────────────────────────
groupDash = "Dashboard"
showDashboard = input.bool(true, "Show Dashboard", group=groupDash)
dashLocation = input.string("Top Right", "Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=groupDash)
dashSize = input.string("Normal", "Text Size", options=["Small", "Normal", "Large"], group=groupDash)
 
// ─────────────────────────────────────
// 3. HIGH LINE SETTINGS
// ─────────────────────────────────────
groupHigh = "52W High Level"
showHighLine = input.bool(true, "Show High Line", group=groupHigh)
highColor = input.color(color.teal, "High Color", group=groupHigh)
highStyle = input.string("Dashed", "Line Style", options=["Solid", "Dashed", "Dotted"], group=groupHigh)
highWidth = input.int(2, "Line Width", minval=1, maxval=5, group=groupHigh)
 
// ─────────────────────────────────────
// 4. LOW LINE SETTINGS
// ─────────────────────────────────────
groupLow = "52W Low Level"
showLowLine = input.bool(true, "Show Low Line", group=groupLow)
lowColor = input.color(color.red, "Low Color", group=groupLow)
lowStyle = input.string("Dashed", "Line Style", options=["Solid", "Dashed", "Dotted"], group=groupLow)
lowWidth = input.int(2, "Line Width", minval=1, maxval=5, group=groupLow)
 
// ─────────────────────────────────────
// 5. LINE & LABEL SETTINGS
// ─────────────────────────────────────
groupLines = "Line & Label Settings"
lineMode = input.string("From High/Low Date", "Line Position", options=["From High/Low Date", "Full Chart Width", "From Current Candle"], group=groupLines)
showLabels = input.bool(true, "Show Price & Date Labels", group=groupLines)
labelOffset = input.int(2, "Label Offset (Bars)", minval=0, maxval=100, group=groupLines)
 
// ─────────────────────────────────────
// 6. HELPER FUNCTIONS
// ─────────────────────────────────────
getStyle(s) =>
    s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
 
getPosition(p) =>
    switch p
        "Top Left" => position.top_left
        "Bottom Right" => position.bottom_right
        "Bottom Left" => position.bottom_left
        => position.top_right
 
getSize(s) =>
    s == "Small" ? size.small : s == "Large" ? size.large : size.normal
 
formatDate(t) =>
    str.format_time(t, "dd MMM yyyy", syminfo.timezone)
 
// ─────────────────────────────────────
// 7. DAILY 52W CALCULATION
// ─────────────────────────────────────
get52W() =>
    hiOffset = math.abs(ta.highestbars(high, lookback))
    loOffset = math.abs(ta.lowestbars(low, lookback))
    hiPrice = high[hiOffset]
    loPrice = low[loOffset]
    hiTime = time[hiOffset]
    loTime = time[loOffset]
    [hiPrice, loPrice, hiTime, loTime]
 
[weekHigh, weekLow, highDate, lowDate] = request.security(syminfo.tickerid, "D", get52W(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
// ─────────────────────────────────────
// 8. DRAW HORIZONTAL LEVELS
// ─────────────────────────────────────
var line highLine = na
var line lowLine = na
var label highLabel = na
var label lowLabel = na
 
if barstate.islast
    if not na(highLine)
        line.delete(highLine)
    if not na(lowLine)
        line.delete(lowLine)
    if not na(highLabel)
        label.delete(highLabel)
    if not na(lowLabel)
        label.delete(lowLabel)
 
    int currentTime = time
    int barDuration = timeframe.in_seconds() * 1000
    int futureTime = currentTime + math.max(barDuration, 60000)
 
    int highStart = lineMode == "From Current Candle" ? currentTime : highDate
    int lowStart = lineMode == "From Current Candle" ? currentTime : lowDate
 
    lineExtend = lineMode == "Full Chart Width" ? extend.both : extend.right
 
    if showHighLine and not na(weekHigh) and not na(highStart)
        highLine := line.new(highStart, weekHigh, futureTime, weekHigh, xloc=xloc.bar_time, extend=lineExtend, color=highColor, style=getStyle(highStyle), width=highWidth)
 
        if showLabels
            highLabel := label.new(bar_index + labelOffset, weekHigh, "52W HIGH  " + str.tostring(weekHigh, format.mintick) + "\n" + formatDate(highDate), style=label.style_label_left, color=highColor, textcolor=color.white, size=size.small)
 
    if showLowLine and not na(weekLow) and not na(lowStart)
        lowLine := line.new(lowStart, weekLow, futureTime, weekLow, xloc=xloc.bar_time, extend=lineExtend, color=lowColor, style=getStyle(lowStyle), width=lowWidth)
 
        if showLabels
            lowLabel := label.new(bar_index + labelOffset, weekLow, "52W LOW  " + str.tostring(weekLow, format.mintick) + "\n" + formatDate(lowDate), style=label.style_label_left, color=lowColor, textcolor=color.white, size=size.small)
 
// ─────────────────────────────────────
// 9. DASHBOARD
// ─────────────────────────────────────
var table dashboard = table.new(getPosition(dashLocation), 2, 3, border_width=1)
 
if barstate.islast
    if showDashboard
        txtSize = getSize(dashSize)
 
        table.cell(dashboard, 0, 0, "52 WEEK RANGE", bgcolor=color.rgb(35, 40, 50), text_color=color.white, text_size=txtSize)
        table.cell(dashboard, 1, 0, syminfo.ticker, bgcolor=color.rgb(35, 40, 50), text_color=color.white, text_size=txtSize)
 
        table.cell(dashboard, 0, 1, "HIGH\n" + formatDate(highDate), bgcolor=color.new(highColor, 85), text_color=highColor, text_size=txtSize)
        table.cell(dashboard, 1, 1, str.tostring(weekHigh, format.mintick), bgcolor=color.new(highColor, 85), text_color=highColor, text_size=txtSize)
 
        table.cell(dashboard, 0, 2, "LOW\n" + formatDate(lowDate), bgcolor=color.new(lowColor, 85), text_color=lowColor, text_size=txtSize)
        table.cell(dashboard, 1, 2, str.tostring(weekLow, format.mintick), bgcolor=color.new(lowColor, 85), text_color=lowColor, text_size=txtSize)
    else
        table.clear(dashboard, 0, 0, 1, 2)

← 返回列表