PDH PDL PWH PWL - Previous Day and Week Levels

itsredwick · study · 75 行 · 点赞 16 · TradingView 原页

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

Pine Script

//@version=6
indicator("PDH / PDL / PWH / PWL", shorttitle = "Previous D/W Levels", overlay = true, max_lines_count = 4, max_labels_count = 4)
 
// Previous completed periods from the symbol's daily/weekly data feed.
// The [1] offsets and lookahead_on keep requested prices confirmed.
string levelsGroup = "Levels"
bool showPDH = input.bool(true, "PDH", inline = "PDH", group = levelsGroup)
color pdhColor = input.color(color.lime, "", inline = "PDH", group = levelsGroup)
bool showPDL = input.bool(true, "PDL", inline = "PDL", group = levelsGroup)
color pdlColor = input.color(color.red, "", inline = "PDL", group = levelsGroup)
bool showPWH = input.bool(true, "PWH", inline = "PWH", group = levelsGroup)
color pwhColor = input.color(color.aqua, "", inline = "PWH", group = levelsGroup)
bool showPWL = input.bool(true, "PWL", inline = "PWL", group = levelsGroup)
color pwlColor = input.color(color.orange, "", inline = "PWL", group = levelsGroup)
 
string displayGroup = "Appearance"
int lineWidth = input.int(2, "Line width", minval = 1, maxval = 5, group = displayGroup)
string lineStyleInput = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = displayGroup)
bool showLabels = input.bool(true, "Show labels", group = displayGroup)
bool showPrices = input.bool(true, "Include price in labels", group = displayGroup)
int labelOffset = input.int(2, "Tag distance to the right (bars)", minval = 1, maxval = 100, group = displayGroup)
 
// Daily requests must not become lower-timeframe requests on weekly charts.
if timeframe.in_seconds() > timeframe.in_seconds("1D")
    runtime.error("Use this indicator on an intraday or 1D chart.")
 
[pdh, pdl] = request.security(syminfo.tickerid, "1D", [high[1], low[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[pwh, pwl] = request.security(syminfo.tickerid, "1W", [high[1], low[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
 
string selectedStyle = switch lineStyleInput
    "Dashed" => line.style_dashed
    "Dotted" => line.style_dotted
    => line.style_solid
 
// Each written call maintains its own line and label across updates.
drawLevel(bool enabled, float price, int periodStart, string caption, color levelColor) =>
    var line levelLine = na
    var label levelLabel = na
    if barstate.islast
        if enabled and not na(price) and not na(periodStart)
            // Stop at the latest candle; keep the tag beyond the line endpoint.
            int lineEnd = math.max(time, periodStart + 1)
            if na(levelLine)
                levelLine := line.new(periodStart, price, lineEnd, price, xloc = xloc.bar_time, extend = extend.none, color = levelColor, style = selectedStyle, width = lineWidth)
            else
                line.set_xy1(levelLine, periodStart, price)
                line.set_xy2(levelLine, lineEnd, price)
                line.set_extend(levelLine, extend.none)
                line.set_color(levelLine, levelColor)
                line.set_style(levelLine, selectedStyle)
                line.set_width(levelLine, lineWidth)
 
            if showLabels
                string labelText = caption + (showPrices ? "  " + str.tostring(price, format.mintick) : "")
                if na(levelLabel)
                    levelLabel := label.new(bar_index + labelOffset, price, labelText, xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_left, color = color.new(levelColor, 85), textcolor = levelColor, size = size.small)
                else
                    label.set_xy(levelLabel, bar_index + labelOffset, price)
                    label.set_text(levelLabel, labelText)
                    label.set_color(levelLabel, color.new(levelColor, 85))
                    label.set_textcolor(levelLabel, levelColor)
            else
                label.delete(levelLabel)
                levelLabel := na
        else
            line.delete(levelLine)
            label.delete(levelLabel)
            levelLine := na
            levelLabel := na
    price
 
drawLevel(showPDH, pdh, time("1D"), "PDH", pdhColor)
drawLevel(showPDL, pdl, time("1D"), "PDL", pdlColor)
drawLevel(showPWH, pwh, time("1W"), "PWH", pwhColor)
drawLevel(showPWL, pwl, time("1W"), "PWL", pwlColor)

← 返回列表