RTH Range Gauge [JackVi]

mbjx2daddy · study · 153 行 · 点赞 11 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JackViTrading
 
//@version=6
indicator("RTH Range Gauge [JackVi]", shorttitle = "RTH Range Gauge", overlay = true, max_lines_count = 500, max_labels_count = 100)
 
// ------------------------------------------------------------
// Inputs
// ------------------------------------------------------------
G1 = "Session"
sessStr   = input.session("0930-1600", "Regular session", group = G1)
sessTz    = input.string("America/New_York", "Session time zone", group = G1)
lookback  = input.int(20, "Average over last N sessions", minval = 5, maxval = 250, group = G1)
 
G2 = "Levels"
showHL     = input.bool(true,  "Show today's session high / low", group = G2)
showProj   = input.bool(true,  "Show average-range projection levels", group = G2,
     tooltip = "Low + average range and High - average range. These are the prices where today's range would equal a normal-sized day.")
showOpen   = input.bool(true,  "Show session open", group = G2)
extendBars = input.int(15, "Extend lines to the right (bars)", minval = 0, maxval = 200, group = G2)
 
G3 = "Expansion"
lvl1 = input.float(100.0, "Expansion level 1 (% of average)", minval = 10, step = 5, group = G3)
lvl2 = input.float(150.0, "Expansion level 2 (% of average)", minval = 10, step = 5, group = G3)
tint = input.bool(true, "Tint background when expanded", group = G3)
 
G4 = "Display"
showTable = input.bool(true, "Show gauge table", group = G4)
tblPos    = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = G4)
cHL   = input.color(color.new(#9aa5b8, 0),  "High / low",  group = G4)
cProj = input.color(color.new(#e07a32, 0),  "Projection",  group = G4)
cOpen = input.color(color.new(#5b6b85, 30), "Open",        group = G4)
 
// ------------------------------------------------------------
// Session tracking (no future data: only completed sessions feed the average)
// ------------------------------------------------------------
inSess   = not na(time(timeframe.period, sessStr, sessTz))
newSess  = inSess and not inSess[1]
endSess  = not inSess and inSess[1]
 
var float sHi   = na
var float sLo   = na
var float sOpen = na
var int   sStartTime = na
var int   sBars = 0
var array<float> ranges = array.new<float>()
 
if endSess and not na(sHi)
    ranges.push(sHi - sLo)
    if ranges.size() > lookback
        ranges.shift()
 
if newSess
    sHi := high
    sLo := low
    sOpen := open
    sStartTime := time
    sBars := 1
else if inSess
    sHi := math.max(sHi, high)
    sLo := math.min(sLo, low)
    sBars += 1
 
avgRange = ranges.size() > 0 ? ranges.avg() : na
curRange = not na(sHi) ? sHi - sLo : na
pctAvg   = not na(avgRange) and avgRange > 0 and not na(curRange) ? curRange / avgRange * 100 : na
 
// Percentile rank of today's (so far) range vs. completed sessions
float pctRank = na
if not na(curRange) and ranges.size() > 0
    int below = 0
    for r in ranges
        if r <= curRange
            below += 1
    pctRank := float(below) / ranges.size() * 100
 
// Share of the session elapsed (by clock time)
sessLenMs = input.float(6.5, "Session length (hours, for elapsed %)", minval = 0.5, maxval = 24, group = G1) * 60 * 60 * 1000
elapsedPct = inSess and not na(sStartTime) ? math.min(100, (time_close - sStartTime) / sessLenMs * 100) : na
 
projUp = not na(avgRange) and inSess ? sLo + avgRange : na
projDn = not na(avgRange) and inSess ? sHi - avgRange : na
 
// ------------------------------------------------------------
// Lines (one set per session, updated in place)
// ------------------------------------------------------------
var line lHi = na
var line lLo = na
var line lUp = na
var line lDn = na
var line lOp = na
 
f_line(line ln, float y, color c, int w, string st) =>
    line out = ln
    if na(out)
        out := line.new(bar_index, y, bar_index + extendBars, y, color = c, width = w, style = st)
    else
        out.set_xy2(bar_index + extendBars, y)
        out.set_y1(y)
    out
 
if newSess
    lHi := na
    lLo := na
    lUp := na
    lDn := na
    lOp := na
 
if inSess
    if showHL
        lHi := f_line(lHi, sHi, cHL, 1, line.style_solid)
        lLo := f_line(lLo, sLo, cHL, 1, line.style_solid)
    if showOpen
        lOp := f_line(lOp, sOpen, cOpen, 1, line.style_dotted)
    if showProj and not na(avgRange)
        lUp := f_line(lUp, projUp, cProj, 1, line.style_dashed)
        lDn := f_line(lDn, projDn, cProj, 1, line.style_dashed)
 
// ------------------------------------------------------------
// Background tint + alerts
// ------------------------------------------------------------
exp1 = not na(pctAvg) and pctAvg >= lvl1
exp2 = not na(pctAvg) and pctAvg >= lvl2
bgcolor(tint and inSess ? (exp2 ? color.new(cProj, 88) : exp1 ? color.new(cProj, 94) : na) : na, title = "Expansion tint")
 
alertcondition(exp1 and not exp1[1] and inSess, "Range reached level 1", "{{ticker}}: RTH range reached expansion level 1 vs its recent average")
alertcondition(exp2 and not exp2[1] and inSess, "Range reached level 2", "{{ticker}}: RTH range reached expansion level 2 vs its recent average")
 
// ------------------------------------------------------------
// Gauge table
// ------------------------------------------------------------
pos = switch tblPos
    "Top left"     => position.top_left
    "Bottom right" => position.bottom_right
    "Bottom left"  => position.bottom_left
    => position.top_right
 
var table tb = table.new(pos, 2, 6, bgcolor = color.new(#101a2b, 8), border_color = color.new(#2a3a55, 0), border_width = 1, frame_color = color.new(#2a3a55, 0), frame_width = 1)
 
f_row(int r, string k, string v, color vc) =>
    tb.cell(0, r, k, text_color = color.new(#b8c2d3, 0), text_size = size.small, text_halign = text.align_left)
    tb.cell(1, r, v, text_color = vc, text_size = size.small, text_halign = text.align_right)
 
if showTable and barstate.islast
    string state = na(pctAvg) ? "Warming up" : not inSess ? "Last session" : exp2 ? "Expanded+" : exp1 ? "Expanded" : "Inside average"
    color sc = exp2 or exp1 ? cProj : color.white
    tb.cell(0, 0, "RTH Range Gauge", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    tb.cell(1, 0, state, text_color = sc, text_size = size.small, text_halign = text.align_right)
    f_row(1, "Today's range",  na(curRange) ? "-" : str.tostring(curRange, format.mintick), color.white)
    f_row(2, str.format("Avg range ({0})", ranges.size()), na(avgRange) ? "warming up" : str.tostring(avgRange, format.mintick), color.white)
    f_row(3, "% of average",   na(pctAvg) ? "-" : str.tostring(pctAvg, "#") + "%", sc)
    f_row(4, "Percentile",     na(pctRank) ? "-" : str.tostring(pctRank, "#") + "th", color.white)
    f_row(5, "Session elapsed", na(elapsedPct) ? "-" : str.tostring(elapsedPct, "#") + "%", color.white)

← 返回列表