Weekly OHLC [Legendary - Investments]

PipSweep · study · 133 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
indicator("Weekly OHLC [Legendary - Investments]", overlay=true, max_lines_count=4, max_labels_count=4)
 
//---------------------------
// USER SETTINGS
//---------------------------
 
groupLevels = "Weekly Levels"
showHigh = input.bool(true, "Show Previous Week High", group=groupLevels)
showLow = input.bool(true, "Show Previous Week Low", group=groupLevels)
showOpen = input.bool(true, "Show Previous Week Open", group=groupLevels)
showClose = input.bool(true, "Show Previous Week Close", group=groupLevels)
 
groupLines = "Line Settings"
lineWidth = input.int(1, "Line Width", minval=1, maxval=5, group=groupLines)
lineStyle = input.string("Solid", "Line Style", options=["Solid", "Dashed", "Dotted"], group=groupLines)
 
pwhColor = input.color(color.green, "PW High Color", group=groupLines)
pwlColor = input.color(color.red, "PW Low Color", group=groupLines)
pwoColor = input.color(color.orange, "PW Open Color", group=groupLines)
pwcColor = input.color(color.purple, "PW Close Color", group=groupLines)
 
groupLabels = "Label Settings"
showLabels = input.bool(true, "Show Labels", group=groupLabels)
fontSize = input.string("Tiny", "Font Size", options=["Tiny", "Small", "Medium", "Large"], group=groupLabels)
 
//---------------------------
// LINE AND FONT STYLES
//---------------------------
 
selectedStyle = switch lineStyle
    "Dashed" => line.style_dashed
    "Dotted" => line.style_dotted
    => line.style_solid
 
selectedSize = switch fontSize
    "Small" => size.small
    "Medium" => size.normal
    "Large" => size.large
    => size.tiny
 
//---------------------------
// PREVIOUS WEEKLY OHLC
//---------------------------
 
[pwo, pwh, pwl, pwc] = request.security(
     syminfo.tickerid,
     "1W",
     [open[1], high[1], low[1], close[1]],
     lookahead=barmerge.lookahead_on)
 
//---------------------------
// CURRENT WEEK START
//---------------------------
 
var int weekStartBar = na
 
if timeframe.change("1W") or na(weekStartBar)
    weekStartBar := bar_index
 
//---------------------------
// LEVEL ARRAYS
//---------------------------
 
var array<line> levelLines = array.new_line(4)
var array<label> levelLabels = array.new_label(4)
 
levels = array.from(pwo, pwh, pwl, pwc)
colors = array.from(pwoColor, pwhColor, pwlColor, pwcColor)
names = array.from("PW Open", "PW High", "PW Low", "PW Close")
enabled = array.from(showOpen, showHigh, showLow, showClose)
 
//---------------------------
// DRAW LATEST WEEK ONLY
//---------------------------
 
if barstate.islast
    for i = 0 to 3
        price = array.get(levels, i)
        levelColor = array.get(colors, i)
        levelName = array.get(names, i)
        isEnabled = array.get(enabled, i)
 
        ln = array.get(levelLines, i)
        lb = array.get(levelLabels, i)
 
        if isEnabled and not na(price)
            if na(ln)
                ln := line.new(
                     weekStartBar, price,
                     bar_index + 1, price,
                     xloc=xloc.bar_index,
                     color=levelColor,
                     style=selectedStyle,
                     width=lineWidth)
                array.set(levelLines, i, ln)
            else
                line.set_xy1(ln, weekStartBar, price)
                line.set_xy2(ln, bar_index + 1, price)
                line.set_color(ln, levelColor)
                line.set_style(ln, selectedStyle)
                line.set_width(ln, lineWidth)
 
            if showLabels
                labelText = levelName + " (" + str.tostring(price, format.mintick) + ")"
 
                if na(lb)
                    lb := label.new(
                         bar_index + 1, price,
                         labelText,
                         xloc=xloc.bar_index,
                         style=label.style_label_left,
                         color=color.new(color.black, 100),
                         textcolor=levelColor,
                         size=selectedSize)
                    array.set(levelLabels, i, lb)
                else
                    label.set_xy(lb, bar_index + 1, price)
                    label.set_text(lb, labelText)
                    label.set_textcolor(lb, levelColor)
                    label.set_size(lb, selectedSize)
            else
                if not na(lb)
                    label.delete(lb)
                    array.set(levelLabels, i, na)
        else
            if not na(ln)
                line.delete(ln)
                array.set(levelLines, i, na)
 
            if not na(lb)
                label.delete(lb)
                array.set(levelLabels, i, na)

← 返回列表