DC Range Guard

DC_SniperFlow · study · 130 行 · 点赞 18 · 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/ MPL-2.0
// DC TRADING - DC RANGE GUARD
// Aviso sutil: ¿el precio está estirado en un extremo del rango de 24 h? (regla del diario de danny)
//   Estirado = rango 24 h > X veces el rango diario medio  Y  precio en el tercio superior / inferior de ese rango
//   Rango    = rango 24 h < Y veces el rango diario medio (mercado comprimido)
//   Normal   = lo demás
//   + Kill Zones: cuál está activa y cuánto queda, o cuál es la próxima (hora de Nueva York)
//@version=6
indicator("DC Range Guard", shorttitle = "DC Range Guard", overlay = true)
 
var G_R = "Regla"
rgMult  = input.float(1.2, "Estirado si rango 24 h >", minval = 0.5, step = 0.05, group = G_R, tooltip = "Veces el rango diario medio. 1,2 = la regla que salió de tu diario.")
rgZone  = input.float(33.3, "Extremo = tercio (%)", minval = 10, maxval = 50, step = 1, group = G_R, tooltip = "33,3 = tercio superior o inferior del rango de 24 h.")
rgAdrN  = input.int(20, "Rango diario medio (días)", minval = 5, maxval = 100, group = G_R)
rgRngOn = input.bool(true, "Avisar también rango comprimido", inline = "rc", group = G_R)
rgRngX  = input.float(0.7, "si rango 24 h <", minval = 0.1, step = 0.05, inline = "rc", group = G_R)
 
var G_K = "Kill Zones (hora NY)"
kzOn   = input.bool(true, "Mostrar kill zones", group = G_K)
kz1On  = input.bool(true, "", inline = "k1", group = G_K)
kz1Nm  = input.string("Asia", "", inline = "k1", group = G_K)
kz1Ss  = input.session("2000-2300", "", inline = "k1", group = G_K)
kz2On  = input.bool(true, "", inline = "k2", group = G_K)
kz2Nm  = input.string("NY AM", "", inline = "k2", group = G_K)
kz2Ss  = input.session("1000-1130", "", inline = "k2", group = G_K)
kz3On  = input.bool(false, "", inline = "k3", group = G_K)
kz3Nm  = input.string("NY PM", "", inline = "k3", group = G_K)
kz3Ss  = input.session("1400-1530", "", inline = "k3", group = G_K)
kzColA = input.color(color.new(#3fe0a0, 0), "Activa", inline = "kc", group = G_K)
kzColF = input.color(color.new(#787b86, 0), "Fuera", inline = "kc", group = G_K)
 
var G_V = "Dashboard"
rgPos   = input.string("Arriba derecha", "Posición", options = ["Arriba derecha", "Arriba centro", "Arriba izquierda", "Abajo derecha", "Abajo centro", "Abajo izquierda", "Centro derecha"], group = G_V)
rgSize  = input.string("Tiny", "Tamaño", options = ["Tiny", "Small", "Normal"], group = G_V)
rgDet   = input.bool(true, "Mostrar detalle (x ADR · zona)", group = G_V)
rgOnlyW = input.bool(false, "Solo mostrar cuando NO es normal", group = G_V)
rgColS  = input.color(color.new(#ff9800, 0), "Estirado", inline = "c1", group = G_V)
rgColR  = input.color(color.new(#5b9cf6, 0), "Rango", inline = "c1", group = G_V)
rgColN  = input.color(color.new(#787b86, 0), "Normal", inline = "c1", group = G_V)
rgBg    = input.color(color.new(#000000, 100), "Fondo", inline = "c2", group = G_V)
 
// Datos: rango de las últimas 24 velas de 1 h (incluye la hora en curso) y rango diario medio de días cerrados
int  chartSec = timeframe.in_seconds()
bool okTf     = timeframe.isintraday and chartSec <= 3600
string tfH    = okTf ? "60" : timeframe.period
[hi24, lo24] = request.security(syminfo.tickerid, tfH, [ta.highest(high, 24), ta.lowest(low, 24)], lookahead = barmerge.lookahead_off)
float adr    = request.security(syminfo.tickerid, "D", ta.sma(high - low, rgAdrN)[1], lookahead = barmerge.lookahead_on)
 
posOf(string p) =>
    switch p
        "Arriba derecha"   => position.top_right
        "Arriba centro"    => position.top_center
        "Arriba izquierda" => position.top_left
        "Abajo derecha"    => position.bottom_right
        "Abajo centro"     => position.bottom_center
        "Abajo izquierda"  => position.bottom_left
        => position.middle_right
 
szOf(string s) =>
    switch s
        "Small"  => size.small
        "Normal" => size.normal
        => size.tiny
 
var table tb = table.new(posOf(rgPos), 1, 2, bgcolor = rgBg, frame_width = 0, border_width = 0)
 
// Kill zones: "HHMM-HHMM" → minutos del día (NY)
sMin(string s, bool st) =>
    string part = st ? str.substring(s, 0, 4) : str.substring(s, 5, 9)
    int v = int(str.tonumber(part))
    int(math.floor(v / 100)) * 60 + v % 100
 
hhmm(int m) =>
    str.format("{0,number,00}:{1,number,00}", int(math.floor(m / 60)), m % 60)
 
durTxt(int m) =>
    m >= 60 ? str.tostring(int(math.floor(m / 60))) + "h " + str.tostring(m % 60) + "m" : str.tostring(m) + "m"
 
// Devuelve [activa, minutos que quedan, minutos hasta que empiece]
kzState(string s, int now) =>
    int a = sMin(s, true)
    int b = sMin(s, false)
    bool act = a < b ? (now >= a and now < b) : (now >= a or now < b)
    int left = act ? (b - now + 1440) % 1440 : 0
    int until = (a - now + 1440) % 1440
    [act, left, until]
 
if barstate.islast
    tb.clear(0, 0, 0, 1)
    // Kill zones (en replay usa la hora de la vela, no la real)
    if kzOn and timeframe.isintraday
        int nowT = math.min(timenow, time_close)
        int nowM = hour(nowT, "America/New_York") * 60 + minute(nowT, "America/New_York")
        string kTxt = ""
        bool   kAct = false
        int    best = 99999
        string nxt  = ""
        array<bool>   ons = array.from(kz1On, kz2On, kz3On)
        array<string> nms = array.from(kz1Nm, kz2Nm, kz3Nm)
        array<string> sss = array.from(kz1Ss, kz2Ss, kz3Ss)
        for i = 0 to 2
            if ons.get(i)
                [act, left, until] = kzState(sss.get(i), nowM)
                if act and not kAct
                    kAct := true
                    kTxt := "● KZ " + nms.get(i) + " activa · quedan " + durTxt(left)
                if not act and until < best
                    best := until
                    nxt  := nms.get(i) + " " + hhmm(sMin(sss.get(i), true))
        if not kAct and nxt != ""
            kTxt := "Fuera de KZ · próxima " + nxt + " (en " + durTxt(best) + ")"
        if kTxt != ""
            tb.cell(0, 1, kTxt, text_color = kAct ? kzColA : kzColF, text_size = szOf(rgSize), text_halign = text.align_right)
    if okTf and not na(adr) and adr > 0 and not na(hi24) and hi24 > lo24
        float h    = math.max(hi24, high)
        float l    = math.min(lo24, low)
        float r24  = h - l
        float xAdr = r24 / adr
        float pos  = (close - l) / r24 * 100
        bool  top  = pos >= 100 - rgZone
        bool  bot  = pos <= rgZone
        bool  isStr = xAdr > rgMult and (top or bot)
        bool  rng  = rgRngOn and xAdr < rgRngX
        string zona = top ? "tercio alto" : bot ? "tercio bajo" : "medio"
        string txt  = isStr ? (top ? "⚠ Estirado arriba · no entrar" : "⚠ Estirado abajo · no entrar") : rng ? "Rango comprimido" : "Normal"
        color  col  = isStr ? rgColS : rng ? rgColR : rgColN
        if rgDet
            txt := txt + "  ·  24h " + str.tostring(xAdr, "#.##") + "x ADR · " + zona
        if not rgOnlyW or isStr or rng
            tb.cell(0, 0, txt, text_color = col, text_size = szOf(rgSize), text_halign = text.align_right)

← 返回列表