HTF Candles - Side Panel

ThiagoCostaKF · study · 69 行 · 点赞 14 · TradingView 原页

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

Pine Script

//@version=6
// Version 1 · 27/10/2026 · public release (English UI, editable colors and spacing, higher-timeframe check)
indicator("HTF Candles - Side Panel", shorttitle="HTF Candles", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count=5)
 
tf     = input.timeframe("15", "Timeframe")
nBars  = input.int(3, "Number of candles", minval=1, maxval=50)
gap    = input.int(6, "Distance from last bar (bars)", minval=1, maxval=50)
width  = input.int(3, "Candle width (bars)", minval=1, maxval=10)
space  = input.int(2, "Space between candles (bars)", minval=0, maxval=10)
colUp  = input.color(color.aqua, "Bullish candle")
colDn  = input.color(color.purple, "Bearish candle")
 
bool tfOk = timeframe.in_seconds(tf) > timeframe.in_seconds()
 
tfText(string t) =>
    int s = timeframe.in_seconds(t)
    s < 3600 ? str.tostring(s / 60, "#") + "m" : s < 86400 ? str.tostring(s / 3600, "#") + "H" : t
 
// OHLC of the last n bars of the selected timeframe: [o0,h0,l0,c0, o1,h1,l1,c1, ...] (0 = forming bar)
htfBars(int n) =>
    array<float> a = array.new<float>()
    for i = 0 to n - 1
        a.push(open[i])
        a.push(high[i])
        a.push(low[i])
        a.push(close[i])
    a
 
array<float> ohlc = request.security(syminfo.tickerid, tf, htfBars(nBars), lookahead = barmerge.lookahead_off)
 
var box[]  bodies = array.new<box>()
var line[] wicks  = array.new<line>()
var label  tfLbl  = na
 
drawCandle(int x, float o, float h, float l, float c) =>
    color col = c >= o ? colUp : colDn
    float top = math.max(o, c)
    float bot = math.min(o, c)
    if top == bot
        top += syminfo.mintick / 2
        bot -= syminfo.mintick / 2
    int mid = x + int(width / 2)
    wicks.push(line.new(mid, h, mid, l, color=col, width=1))
    bodies.push(box.new(x, top, x + width - 1, bot, border_color=col, bgcolor=col))
 
if barstate.islast
    for b in bodies
        box.delete(b)
    for w in wicks
        line.delete(w)
    bodies.clear()
    wicks.clear()
    label.delete(tfLbl)
    if not tfOk
        tfLbl := label.new(bar_index + gap, high, "HTF Candles: choose a timeframe higher than the chart", style=label.style_label_left, color=color.new(color.black, 100), textcolor=color.gray, size=size.small)
    else if not na(ohlc)
        float hiAll = na
        for k = nBars - 1 to 0
            float o = ohlc.get(k * 4)
            float h = ohlc.get(k * 4 + 1)
            float l = ohlc.get(k * 4 + 2)
            float c = ohlc.get(k * 4 + 3)
            int x = bar_index + gap + (nBars - 1 - k) * (width + space)
            if not na(o)
                drawCandle(x, o, h, l, c)
                hiAll := na(hiAll) ? h : math.max(hiAll, h)
        int xMid = bar_index + gap + int((nBars - 1) * (width + space) / 2) + int(width / 2)
        if not na(hiAll)
            tfLbl := label.new(xMid, hiAll, tfText(tf), style=label.style_label_down, color=color.new(color.black, 100), textcolor=color.gray, size=size.small)

← 返回列表