Hidden Gem

CopyCats · study · 138 行 · 点赞 43 · TradingView 原页

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

Pine Script

//@version=6
indicator("CRT 4H Range 2-6 AM (New York)", "Hidden Gem", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
 
// ─────────────────────────────────────────────────────────────
// CRT 4H candle: the 2:00–6:00 AM New York range
// Draws the High, Low and 50% midpoint with prices,
// then extends them through the trading day.
// Works on any timeframe 4H or lower. Turn ETH (extended hours) ON.
// ─────────────────────────────────────────────────────────────
 
// ── Inputs ───────────────────────────────────────────────────
grpS     = "Session"
tz       = input.string("America/New_York", "Timezone", group = grpS)
crtSess  = input.session("0200-0600", "CRT candle window", group = grpS)
extSess  = input.session("0600-1700", "Extend levels through", group = grpS)
keepDays = input.int(5, "Days to keep on chart", minval = 1, maxval = 100, group = grpS)
 
grpV      = "Style"
colorMode = input.string("Auto", "Color", options = ["Auto", "Black", "White"], tooltip = "Auto = black on a light chart, white on a dark chart", group = grpV)
lw        = input.int(2, "Line width", minval = 1, maxval = 4, group = grpV)
midStyleS = input.string("Dashed", "Midpoint style", options = ["Solid", "Dashed", "Dotted"], group = grpV)
showBox   = input.bool(true, "Shade the 2-6 AM range", group = grpV)
showLbl   = input.bool(true, "Show price labels", group = grpV)
lblSize   = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = grpV)
 
grpX      = "Signals (optional)"
showSweep = input.bool(false, "Mark CRT sweeps (wick beyond range, close back inside)", group = grpX)
 
// ── Guards & helpers ─────────────────────────────────────────
if timeframe.in_seconds() > 4 * 60 * 60
    runtime.error("CRT 2-6: use a 4H or lower timeframe.")
 
midStyle = midStyleS == "Solid" ? line.style_solid : midStyleS == "Dotted" ? line.style_dotted : line.style_dashed
lSize    = lblSize == "Tiny" ? size.tiny : lblSize == "Normal" ? size.normal : size.small
 
// black & white palette
inkCol = colorMode == "Black" ? color.black : colorMode == "White" ? color.white : chart.fg_color
hiCol  = inkCol
loCol  = inkCol
midCol = inkCol
boxCol = color.new(inkCol, 92)
 
fmt(float p) => str.tostring(p, format.mintick)
 
inCRT    = not na(time(timeframe.period, crtSess, tz))
inExt    = not na(time(timeframe.period, extSess, tz))
crtStart = inCRT and not inCRT[1]
 
// ── State ────────────────────────────────────────────────────
var float crtHi  = na
var float crtLo  = na
var bool  active = false
 
var line  lnHi  = na
var line  lnLo  = na
var line  lnMid = na
var label lbHi  = na
var label lbLo  = na
var label lbMid = na
var box   bx    = na
 
var array<line>  allLines  = array.new<line>()
var array<label> allLabels = array.new<label>()
var array<box>   allBoxes  = array.new<box>()
 
// ── New CRT window starts (2:00 AM) ──────────────────────────
if crtStart
    crtHi  := high
    crtLo  := low
    active := true
    m = (crtHi + crtLo) / 2
 
    lnHi  := line.new(time, crtHi, time, crtHi, xloc = xloc.bar_time, color = hiCol, width = lw)
    lnLo  := line.new(time, crtLo, time, crtLo, xloc = xloc.bar_time, color = loCol, width = lw)
    lnMid := line.new(time, m, time, m, xloc = xloc.bar_time, color = midCol, width = lw, style = midStyle)
    array.push(allLines, lnHi)
    array.push(allLines, lnLo)
    array.push(allLines, lnMid)
 
    lbHi  := showLbl ? label.new(time, crtHi, "", xloc = xloc.bar_time, color = color.new(color.black, 100), style = label.style_label_left, textcolor = hiCol, size = lSize) : na
    lbLo  := showLbl ? label.new(time, crtLo, "", xloc = xloc.bar_time, color = color.new(color.black, 100), style = label.style_label_left, textcolor = loCol, size = lSize) : na
    lbMid := showLbl ? label.new(time, m, "", xloc = xloc.bar_time, color = color.new(color.black, 100), style = label.style_label_left, textcolor = midCol, size = lSize) : na
    if showLbl
        array.push(allLabels, lbHi)
        array.push(allLabels, lbLo)
        array.push(allLabels, lbMid)
 
    bx := showBox ? box.new(time, crtHi, time, crtLo, xloc = xloc.bar_time, border_color = color.new(boxCol, 100), bgcolor = boxCol) : na
    if showBox
        array.push(allBoxes, bx)
 
    // keep only the last N days
    while array.size(allLines) > keepDays * 3
        line.delete(array.shift(allLines))
    while array.size(allLabels) > keepDays * 3
        label.delete(array.shift(allLabels))
    while array.size(allBoxes) > keepDays
        box.delete(array.shift(allBoxes))
 
// ── Build the range during 2:00–6:00 AM ──────────────────────
if active and inCRT
    crtHi := math.max(crtHi, high)
    crtLo := math.min(crtLo, low)
    if showBox
        box.set_top(bx, crtHi)
        box.set_bottom(bx, crtLo)
        box.set_right(bx, time)
 
// ── Draw / extend the levels ─────────────────────────────────
if active and (inCRT or inExt)
    m = (crtHi + crtLo) / 2
    line.set_y1(lnHi, crtHi),  line.set_y2(lnHi, crtHi),  line.set_x2(lnHi, time)
    line.set_y1(lnLo, crtLo),  line.set_y2(lnLo, crtLo),  line.set_x2(lnLo, time)
    line.set_y1(lnMid, m),     line.set_y2(lnMid, m),     line.set_x2(lnMid, time)
    if showLbl
        label.set_xy(lbHi, time, crtHi)
        label.set_text(lbHi, "CRT High " + fmt(crtHi))
        label.set_xy(lbLo, time, crtLo)
        label.set_text(lbLo, "CRT Low " + fmt(crtLo))
        label.set_xy(lbMid, time, m)
        label.set_text(lbMid, "CRT 50% " + fmt(m))
 
// stop extending once the extend window ends
if active and not inCRT and not inExt
    active := false
 
// ── Optional CRT sweep marks & alerts (after 6:00 AM) ────────
crtDone   = active and not inCRT and not na(crtHi)
crtMid    = (crtHi + crtLo) / 2
sweepHigh = crtDone and high > crtHi and close < crtHi   // bearish CRT: took the high, closed back inside
sweepLow  = crtDone and low < crtLo and close > crtLo    // bullish CRT: took the low, closed back inside
 
plotshape(showSweep and sweepHigh, "Sweep of CRT High", shape.triangledown, location.abovebar, loCol, size = size.tiny)
plotshape(showSweep and sweepLow, "Sweep of CRT Low", shape.triangleup, location.belowbar, hiCol, size = size.tiny)
 
alertcondition(sweepHigh, "CRT high swept", "MGC swept the 2-6 AM CRT high and closed back inside")
alertcondition(sweepLow, "CRT low swept", "MGC swept the 2-6 AM CRT low and closed back inside")
alertcondition(crtDone and ta.cross(close, crtMid), "CRT 50% touched", "Price crossed the 2-6 AM CRT 50% midpoint")

← 返回列表