Key Levels Lite [HansaTrades]

HansaTrades · study · 139 行 · 点赞 56 · 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/
// © HansaTrades
//@version=6
indicator("HansaTrades · Key Levels Lite", shorttitle = "HT Key Levels Lite", overlay = true, max_lines_count = 40, max_labels_count = 40)
 
// Higher-timeframe key levels on any chart timeframe:
// previous day high/low, previous week high/low, 5-day high/low,
// 200-day SMA, 50-week SMA and 200-week SMA.
// Highs/lows use only completed days/weeks, so they do not repaint.
// The moving averages show their current (live) value.
// Optionally the nearest level above and below price is highlighted.
 
// ───────────── Inputs ─────────────
grpL    = "Levels"
showPD  = input.bool(true, "Previous day high / low (PDH / PDL)", group = grpL)
showPW  = input.bool(true, "Previous week high / low (PWH / PWL)", group = grpL)
show5D  = input.bool(true, "5-day high / low", group = grpL)
show200 = input.bool(true, "200-day SMA", group = grpL)
show50W = input.bool(true, "50-week SMA", group = grpL)
show2W  = input.bool(true, "200-week SMA", group = grpL)
 
grpD     = "Display"
hlNear   = input.bool(true, "Highlight nearest level above / below", group = grpD)
showPx   = input.bool(true, "Show price in labels", group = grpD)
mergePct = input.float(0.15, "Merge labels closer than (%)", minval = 0, maxval = 2, step = 0.05, group = grpD, tooltip = "Levels closer together than this share one label, e.g. 'PDH · 5D H'.")
lenBack  = input.int(40, "Line length to the left (bars)", minval = 5, maxval = 300, group = grpD)
scheme   = input.string("Black & White", "Color scheme", options = ["Black & White", "Classic (dark)", "Light", "Colorblind-safe", "Amber (dark)", "Custom colors"], group = grpD, tooltip = "Light = for white chart backgrounds. Custom colors uses the color fields below.")
cHLIn    = input.color(#FFFFFF, "Custom: highs / lows", group = grpD)
cMAIn    = input.color(#9E9E9E, "Custom: moving averages", group = grpD)
cHotIn   = input.color(#FFB000, "Custom: nearest level", group = grpD)
 
color colHL = switch scheme
    "Classic (dark)"  => #B0BEC5
    "Light"           => #263238
    "Colorblind-safe" => #56B4E9
    "Amber (dark)"    => #E0E0E0
    "Custom colors"   => cHLIn
    => #FFFFFF
color colMA = switch scheme
    "Classic (dark)"  => #7E57C2
    "Light"           => #78909C
    "Colorblind-safe" => #009E73
    "Amber (dark)"    => #4C9AFF
    "Custom colors"   => cMAIn
    => #9E9E9E
color colHot = switch scheme
    "Classic (dark)"  => #FFEB3B
    "Light"           => #E65100
    "Colorblind-safe" => #E69F00
    "Custom colors"   => cHotIn
    => #FFB000
 
// ───────────── Data ─────────────
// Completed periods only ([1] + lookahead_on = value of the last closed day/week, no repaint)
pdh  = request.security(syminfo.tickerid, "D", high[1], lookahead = barmerge.lookahead_on)
pdl  = request.security(syminfo.tickerid, "D", low[1],  lookahead = barmerge.lookahead_on)
pwh  = request.security(syminfo.tickerid, "W", high[1], lookahead = barmerge.lookahead_on)
pwl  = request.security(syminfo.tickerid, "W", low[1],  lookahead = barmerge.lookahead_on)
d5h  = request.security(syminfo.tickerid, "D", ta.highest(high, 5)[1], lookahead = barmerge.lookahead_on)
d5l  = request.security(syminfo.tickerid, "D", ta.lowest(low, 5)[1],   lookahead = barmerge.lookahead_on)
// Moving averages: live value of the running day/week
sma200d = request.security(syminfo.tickerid, "D", ta.sma(close, 200))
sma50w  = request.security(syminfo.tickerid, "W", ta.sma(close, 50))
sma200w = request.security(syminfo.tickerid, "W", ta.sma(close, 200))
 
// ───────────── Drawing ─────────────
type Lvl
    float  px
    string name
    bool   isMA
 
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
 
addLvl(array<Lvl> a, bool on, float px, string name, bool isMA) =>
    if on and not na(px)
        a.push(Lvl.new(px, name, isMA))
 
if barstate.islast
    for l in lns
        l.delete()
    for b in lbs
        b.delete()
    lns.clear()
    lbs.clear()
 
    a = array.new<Lvl>()
    addLvl(a, showPD,  pdh,     "PDH",      false)
    addLvl(a, showPD,  pdl,     "PDL",      false)
    addLvl(a, showPW,  pwh,     "PWH",      false)
    addLvl(a, showPW,  pwl,     "PWL",      false)
    addLvl(a, show5D,  d5h,     "5D H",     false)
    addLvl(a, show5D,  d5l,     "5D L",     false)
    addLvl(a, show200, sma200d, "200D SMA", true)
    addLvl(a, show50W, sma50w,  "50W SMA",  true)
    addLvl(a, show2W,  sma200w, "200W SMA", true)
 
    // nearest level above / below the current close
    float upPx = na
    float dnPx = na
    for l in a
        if l.px > close and (na(upPx) or l.px < upPx)
            upPx := l.px
        if l.px < close and (na(dnPx) or l.px > dnPx)
            dnPx := l.px
 
    // sort by price (simple insertion sort, max 9 items)
    n = a.size()
    if n > 1
        for i = 1 to n - 1
            j = i
            while j > 0 and a.get(j - 1).px > a.get(j).px
                t = a.get(j - 1)
                a.set(j - 1, a.get(j))
                a.set(j, t)
                j -= 1
 
    x1 = bar_index - lenBack
    x2 = bar_index + 8
    i = 0
    while i < n
        // group levels that are very close together into one label
        g0   = a.get(i)
        txt  = g0.name
        hot  = hlNear and (g0.px == upPx or g0.px == dnPx)
        allMA = g0.isMA
        k = i + 1
        while k < n and math.abs(a.get(k).px - g0.px) / g0.px * 100 <= mergePct
            txt   += " · " + a.get(k).name
            hot   := hot or (hlNear and (a.get(k).px == upPx or a.get(k).px == dnPx))
            allMA := allMA and a.get(k).isMA
            k += 1
        for m = i to k - 1
            lv = a.get(m)
            c  = hot ? colHot : lv.isMA ? colMA : colHL
            lns.push(line.new(x1, lv.px, x2, lv.px, color = color.new(c, hot ? 0 : 35), width = hot ? 2 : 1, style = lv.isMA ? line.style_dashed : line.style_solid))
        c0 = hot ? colHot : allMA ? colMA : colHL
        lbs.push(label.new(x2, g0.px, showPx ? txt + "  " + str.tostring(g0.px, format.mintick) : txt, style = label.style_label_left, color = color.new(color.black, 100), textcolor = c0, size = size.small, text_font_family = font.family_monospace))
        i := k

← 返回列表