Investep TC2000 hourly :00 (RTH/ETH)

camicabrera · study · 252 行 · 点赞 1 · TradingView 原页

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

Pine Script

//@version=6
indicator("Investep · TC2000 hourly :00 (RTH/ETH)", overlay = true, behind_chart = false, explicit_plot_zorder = true,
     max_lines_count = 10, max_labels_count = 10)
 
// TradingView's standard directional palette; custom chart colors are not read here.
upCol = input.color(#26a69a, "Up body", inline = "c1")
dnCol = input.color(#ef5350, "Down body", inline = "c1")
upWickCol = input.color(#26a69a, "Up wick", inline = "c2")
dnWickCol = input.color(#ef5350, "Down wick", inline = "c2")
upBorderCol = input.color(#26a69a, "Completed up border", inline = "c3")
dnBorderCol = input.color(#ef5350, "Completed down border", inline = "c3")
ltf = input.timeframe("5", "Intrabar timeframe", options = ["1", "5", "15"])
hideNative = input.bool(true, "Make native 1H bodies transparent")
maskNativeBorders = input.bool(true, "Mask native 1H borders with chart background")
maskNativeWicks = input.bool(true, "Mask native 1H wicks with chart background")
showLive = input.bool(true, "Show developing clock-hour candle")
showLivePrice = input.bool(true, "Show dotted live close price line")
showLiveCountdown = input.bool(true, "Show clock-hour countdown in LIVE label")
livePriceFollowsBody = input.bool(true, "LIVE price guide follows candle color")
livePriceColor = input.color(#00bcd4, "Live close price line color")
occupiedPixels = input.int(520, "LIVE only: width occupied by visible bars (pixels)", minval = 100, maxval = 4000)
bodyPercent = input.int(70, "LIVE only: body width (% of estimated spacing)", minval = 10, maxval = 90)
is1h = timeframe.isminutes and timeframe.multiplier == 60
// Extended-hours 1H bars already start at :00 (04:00, 05:00, …), so each native
// bar IS the clock hour. Only the regular-session chart (09:30, 10:30, …) needs
// the intrabar reconstruction and the shifted placement below.
eth1h = is1h and syminfo.session == session.extended
shift1h = is1h and not eth1h
 
// Count actual bars, not elapsed hours across overnight/weekend gaps.
// Visible-range changes restart Pine; ordinary rollback avoids tick overcounting.
var int visibleBars = 0
if time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time
    visibleBars += 1
spacing = float(occupiedPixels) / math.max(visibleBars - 1, 1)
bodyWidth = int(math.max(1, math.min(50, math.floor(spacing * bodyPercent / 100.0))))
 
// Custom geometry is ONLY for the developing preview. Completed candles
// use plotcandle below and never depend on this calibration or object limits.
drawLiveCandle(int center, float o, float h, float l, float c, int width) =>
    candleColor = c >= o ? upCol : dnCol
    wick = line.new(center, l, center, h, xloc = xloc.bar_index, color = c >= o ? upWickCol : dnWickCol)
    body = line.new(center, o, center, c, xloc = xloc.bar_index, color = candleColor, width = width)
    // A zero-length line is invisible. Mark true dojis at their exact price
    // without changing OHLC; this small text marker does not scale with zoom.
    label doji = na
    if o == c
        doji := label.new(center, o, "─", xloc = xloc.bar_index,
             style = label.style_none, textcolor = candleColor, size = size.tiny)
    [body, wick, doji]
 
// Standard exchange data, not Heikin Ashi or other synthetic chart OHLC.
regularTicker = ticker.new(syminfo.prefix, syminfo.ticker, session.regular)
extendedTicker = ticker.new(syminfo.prefix, syminfo.ticker, session.extended)
// Same timeframe, no lookahead: on an ETH chart this is the native bar's own
// standard OHLC, including the forming bar on every feed update.
[xO, xH, xL, xC] = request.security(extendedTicker, timeframe.period, [open, high, low, close],
     gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
// Execute the accumulator INSIDE the lower-timeframe context. Its current
// bar's OHLC evolves on feed updates, including before that intrabar closes.
// Ordinary var rollback replays the developing intrabar without double counting.
clockHour() =>
    var int key = na
    var int started = na
    var int ends = na
    var float o = na
    var float h = na
    var float l = na
    var float c = na
    var float prevO = na
    var float prevH = na
    var float prevL = na
    var float prevC = na
    var int prevEnd = na
    if session.ismarket
        currentKey = time - (minute(time, syminfo.timezone) * 60 + second(time, syminfo.timezone)) * 1000
        if na(key) or currentKey != key
            // Only a contiguous prior hour can complete at this transition.
            contiguous = not na(key) and currentKey - key == 60 * 60 * 1000
            prevO := contiguous ? o : na
            prevH := contiguous ? h : na
            prevL := contiguous ? l : na
            prevC := contiguous ? c : na
            prevEnd := contiguous ? ends : na
            key := currentKey
            started := time
            ends := math.min(currentKey + 60 * 60 * 1000, time_close("1D"))
            o := open
            h := high
            l := low
        else
            h := math.max(h, high)
            l := math.min(l, low)
        c := close
    [started, ends, o, h, l, c, prevEnd, prevO, prevH, prevL, prevC]
 
// This snapshot is used ONLY for realtime rendering; historical candles keep
// the ordered lower_tf array path below. No lookahead or varip accumulation.
// 240 intrabars cover at least four hours at the smallest allowed LTF (1m),
// enough to warm up the current and previous clock-hour buckets. Bound ONLY
// this LIVE request: the completed-candle history below remains unrestricted.
[liveStart, liveEnd, liveO, liveH, liveL, liveC, priorEnd, priorO, priorH, priorL, priorC] = request.security(
     regularTicker, ltf, clockHour(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off,
     calc_bars_count = 240)
[aT, aO, aH, aL, aC, aRth, aLast] = request.security_lower_tf(regularTicker, ltf,
     [time, open, high, low, close, session.ismarket, session.islastbar_regular],
     ignore_invalid_timeframe = true)
 
var int bucket = na
var float bO = na
var float bH = na
var float bL = na
var float bC = na
float co = na
float ch = na
float cl = na
float cc = na
 
if shift1h and array.size(aT) > 0
    for i = 0 to array.size(aT) - 1
        if array.get(aRth, i)
            t = array.get(aT, i)
            key = t - (minute(t, syminfo.timezone) * 60 + second(t, syminfo.timezone)) * 1000
            if not na(bucket) and key != bucket
                // Do not carry an unfinished bucket across an overnight gap.
                if key - bucket == 60 * 60 * 1000
                    co := bO
                    ch := bH
                    cl := bL
                    cc := bC
                bO := na
            if na(bO)
                bO := array.get(aO, i)
                bH := array.get(aH, i)
                bL := array.get(aL, i)
            else
                bH := math.max(bH, array.get(aH, i))
                bL := math.min(bL, array.get(aL, i))
            bC := array.get(aC, i)
            bucket := key
            // Check the INTRABAR session end, not the forming chart bar.
            // Otherwise a 15:30 chart bar prematurely closes the 15:00 hour.
            if array.get(aLast, i)
                co := bO
                ch := bH
                cl := bL
                cc := bC
                bO := na
                bucket := na
 
if shift1h and barstate.isrealtime
    // At :00, capture the just-completed hour immediately, without waiting
    // for the first closed 5m intrabar of the NEW hour to enter the array.
    if not na(priorEnd) and priorEnd > time and priorEnd <= time_close
        co := priorO
        ch := priorH
        cl := priorL
        cc := priorC
    if session.islastbar_regular and barstate.isconfirmed
        co := liveO
        ch := liveH
        cl := liveL
        cc := liveC
 
done = shift1h and not na(co)
// A completed hour owns its slot; LIVE uses the next slot on collision.
liveVisible = showLive and shift1h and barstate.isrealtime and not barstate.isconfirmed and session.ismarket and
     not na(liveO) and not na(liveEnd) and timenow >= liveStart and timenow < liveEnd
overflowLive = liveVisible and done
// From :30 to :00 the current native bar is free, so LIVE uses the same
// plotcandle renderer as completed hours: native width, borders and zoom.
liveInSlot = liveVisible and not done
// On ETH the forming native bar is the developing clock hour itself.
ethLive = showLive and eth1h and barstate.isrealtime and not barstate.isconfirmed and not na(xO)
liveShown = liveVisible or ethLive
lvStart = eth1h ? time : liveStart
lvEnd = eth1h ? time_close : liveEnd
lvO = eth1h ? xO : liveO
lvH = eth1h ? xH : liveH
lvL = eth1h ? xL : liveL
lvC = eth1h ? xC : liveC
liveDirectionColor = lvC >= lvO ? upCol : dnCol
liveGuideColor = livePriceFollowsBody ? liveDirectionColor : livePriceColor
// RTH: use the reconstructed bucket end, NEVER the native chart's :30 time_close.
// timenow advances only on executions/feed updates, not on an independent timer.
secondsLeft = liveShown ? int(math.max(0, math.ceil((lvEnd - timenow) / 1000.0))) : na
countdownText = liveShown ? str.tostring(int(math.floor(secondsLeft / 60.0)), "00") + ":" + str.tostring(secondsLeft % 60, "00") : ""
 
// Pine cannot edit the native border setting. Paint a transparent-body copy
// of the ORIGINAL chart OHLC with background-colored borders/wicks, then draw the
// shifted candles last. This is a visual mask, not a native setting change.
// Border and wick masks are independently toggleable. Solid backgrounds work best;
// chart.bg_color is only the midpoint color of a gradient background.
// Keep a directional RGB base even while hiding the native body. A white
// base can also make the symbol's native last-price marker appear white.
// The native marker/countdown and its text contrast remain chart-controlled.
nativeHiddenColor = liveShown ? liveDirectionColor : close >= open ? upCol : dnCol
barcolor(is1h and hideNative ? color.new(nativeHiddenColor, 100) : na)
plotcandle(is1h and (maskNativeBorders or maskNativeWicks) ? open : na, high, low, close,
     "Native outline mask", color = color.new(chart.bg_color, 100),
     wickcolor = maskNativeWicks ? chart.bg_color : color.new(chart.bg_color, 100),
     bordercolor = maskNativeBorders ? chart.bg_color : color.new(chart.bg_color, 100),
     editable = false, display = display.pane)
// Restore the original completed-candle renderer: native zoom width, borders,
// and available intrabar history, with no custom-drawing retention limit.
pO = eth1h ? xO : done ? co : liveInSlot ? liveO : na
pH = eth1h ? xH : done ? ch : liveH
pL = eth1h ? xL : done ? cl : liveL
pC = eth1h ? xC : done ? cc : liveC
plotcandle(pO, pH, pL, pC, "Shifted hourly",
     color = pC >= pO ? upCol : dnCol, wickcolor = pC >= pO ? upWickCol : dnWickCol,
     bordercolor = pC >= pO ? upBorderCol : dnBorderCol)
 
var line liveBody = na
var line liveStem = na
var label liveDoji = na
var label liveLabel = na
var line livePriceLine = na
if barstate.islast
    line.delete(liveStem)
    line.delete(liveBody)
    label.delete(liveDoji)
    label.delete(liveLabel)
    line.delete(livePriceLine)
    // At :30 the next slot becomes the current slot, preserving the center.
    liveCenter = bar_index + (overflowLive ? 1 : 0)
    if liveShown and showLivePrice
        livePriceLine := line.new(liveCenter - 1, lvC, liveCenter, lvC,
             xloc = xloc.bar_index, extend = extend.both,
             color = liveGuideColor, style = line.style_dotted, width = 1)
    // Custom drawing remains ONLY for :00–:30, when LIVE sits in a future slot.
    if overflowLive
        [body, wick, doji] = drawLiveCandle(liveCenter, liveO, liveH, liveL, liveC, bodyWidth)
        liveBody := body
        liveStem := wick
        liveDoji := doji
    if liveShown
        liveLabel := label.new(liveCenter, lvH,
             str.format_time(lvStart, "HH:mm", syminfo.timezone) + "–" + str.format_time(lvEnd, "HH:mm", syminfo.timezone) +
             (showLiveCountdown ? "\n" + countdownText : ""),
             xloc = xloc.bar_index, style = label.style_label_down,
             color = color.new(chart.bg_color, 100), textcolor = liveDirectionColor, size = size.tiny)
 
// Expose the developing hour here; the :00–:30 drawing has no OHLC of its own.
plot(shift1h and barstate.islast ? visibleBars : na, "Visible bars (width calibration)", display = display.data_window)
plot(shift1h and barstate.islast ? bodyWidth : na, "LIVE body width (pixels)", display = display.data_window)
plot(liveShown ? lvO : na, "Live hourly open", display = display.data_window)
plot(liveShown ? lvH : na, "Live hourly high", display = display.data_window)
plot(liveShown ? lvL : na, "Live hourly low", display = display.data_window)
plot(liveShown ? lvC : na, "Live hourly close", display = display.data_window)
plot(liveShown and showLivePrice ? lvC : na, "Live close price",
     color = liveGuideColor, display = display.price_scale)

← 返回列表