GENESIS Regime & ATR Structural Shield [Lite]

GenesisQuantResearch · study · 166 行 · 点赞 0 · TradingView 原页

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

Pine Script

//@version=6
// ============================================================================
// GENESIS Market Regime & ATR Structure Study [Community Lite]
// Estudio cuantitativo de regimen de mercado, volatilidad estructural ATR(14)
// y protocolo defensivo MODO ESCUDO sobre un activo individual en pantalla.
// Disenado bajo estandar causal cero-repainting (barmerge.lookahead_off).
// ============================================================================
indicator(
     title      = "GENESIS Regime & ATR Shield [Lite]",
     shorttitle = "GENESIS ATR Shield [Lite]",
     overlay    = true,
     max_boxes_count  = 50,
     max_lines_count  = 100,
     max_labels_count = 50
)
 
// ----------------------------------------------------------------------------
// 1. ENTRADAS DE CONFIGURACION DEL ESTUDIO (TIPADO EXPLICITO PINE V6)
// ----------------------------------------------------------------------------
string GRP_REG = "1. Pilares de Regimen Macro y Volatilidad"
string symBenchInput    = input.symbol("AMEX:SPY", "Indice de Referencia (Benchmark)", group = GRP_REG)
string symVixInput      = input.symbol("CBOE:VIX", "Indice de Volatilidad Implicita (VIX)", group = GRP_REG)
float  vixShieldThresh  = input.float(22.0, "Umbral Defensivo VIX (Modo Escudo)", minval = 12.0, maxval = 45.0, step = 0.5, group = GRP_REG)
float  spyBufferPct     = input.float(0.30, "Tolerancia Intradia Benchmark vs Apertura (%)", minval = 0.05, maxval = 1.50, step = 0.05, group = GRP_REG)
 
string GRP_ATR = "2. Estructura Causal ATR(14) Diario y Niveles de Estudio"
int    atrLenInput      = input.int(14, "Longitud ATR Diario Causal", minval = 5, maxval = 50, group = GRP_ATR)
int    smaFastLenInput  = input.int(20, "Media Estructural Rapida (1D)", minval = 5, maxval = 100, group = GRP_ATR)
int    smaSlowLenInput  = input.int(50, "Media Estructural Lenta (1D)", minval = 20, maxval = 200, group = GRP_ATR)
float  kPullbackAtr     = input.float(0.65, "Factor Descuento Zona de Observacion (x ATR 1D)", minval = 0.28, maxval = 1.20, step = 0.05, group = GRP_ATR)
float  kStopAtr         = input.float(0.35, "Factor Invalidacion Estructural (x ATR 1D)", minval = 0.25, maxval = 1.00, step = 0.05, group = GRP_ATR)
float  kTarget2Atr      = input.float(2.00, "Factor Expansion Elastica Estudio (x ATR 1D)", minval = 1.00, maxval = 3.50, step = 0.10, group = GRP_ATR)
 
string GRP_VIS = "3. Visualizacion y Panel Estadistico"
bool   showBandsInput   = input.bool(true, "Mostrar Bandas de Estudio Intradia (ATR)", group = GRP_VIS)
bool   showPocInput     = input.bool(true, "Mostrar Eje de Consenso de 20 Sesiones", group = GRP_VIS)
bool   showBgShield     = input.bool(true, "Sombreado Tenue en Postura Modo Escudo", group = GRP_VIS)
bool   showTableInput   = input.bool(true, "Mostrar Panel Cuantitativo en Esquina", group = GRP_VIS)
 
// ----------------------------------------------------------------------------
// 2. EXTRACCION CAUSAL MULTIMARCO (CERO REPAINTING: LOOKAHEAD_OFF + [1])
// ----------------------------------------------------------------------------
// Serie diaria confirmada del activo actual (usa barra diaria cerrada [1] en intradia)
float dClosePrev  = request.security(syminfo.tickerid, "1D", close[1], lookahead = barmerge.lookahead_off)
float dAtr14Prev  = request.security(syminfo.tickerid, "1D", ta.atr(atrLenInput)[1], lookahead = barmerge.lookahead_off)
float dSma20Prev  = request.security(syminfo.tickerid, "1D", ta.sma(close, smaFastLenInput)[1], lookahead = barmerge.lookahead_off)
float dSma50Prev  = request.security(syminfo.tickerid, "1D", ta.sma(close, smaSlowLenInput)[1], lookahead = barmerge.lookahead_off)
float dPoc20Prev  = request.security(syminfo.tickerid, "1D", ta.sma(hlc3, 20)[1], lookahead = barmerge.lookahead_off)
float dOpenToday  = request.security(syminfo.tickerid, "1D", open, lookahead = barmerge.lookahead_off)
float dHighToday  = request.security(syminfo.tickerid, "1D", high, lookahead = barmerge.lookahead_off)
 
// Contexto del indice de referencia (SPY) y volatilidad implicita (VIX) sin lookahead
float spyOpenDay  = request.security(symBenchInput, "1D", open, lookahead = barmerge.lookahead_off)
float spyClosePrev = request.security(symBenchInput, "1D", close[1], lookahead = barmerge.lookahead_off)
float spySma20    = request.security(symBenchInput, "1D", ta.sma(close, 20)[1], lookahead = barmerge.lookahead_off)
float spySma50    = request.security(symBenchInput, "1D", ta.sma(close, 50)[1], lookahead = barmerge.lookahead_off)
float spyLivePx   = request.security(symBenchInput, timeframe.period, close, lookahead = barmerge.lookahead_off)
float vixLivePx   = request.security(symVixInput, timeframe.period, close, lookahead = barmerge.lookahead_off)
 
// Confirmacion de momento local en el marco actual
float ema9Local   = ta.ema(close, 9)
 
// ----------------------------------------------------------------------------
// 3. EVALUACION DE LOS 4 PILARES DE CONTEXTO Y PROTOCOLO MODO ESCUDO
// ----------------------------------------------------------------------------
float safeClosePrev = math.max(nz(dClosePrev, close), 0.0001)
float safeAtr14     = math.max(nz(dAtr14Prev, ta.atr(atrLenInput)), syminfo.mintick)
float atrPct1D      = (safeAtr14 / safeClosePrev) * 100.0
 
// Pilar 1: Estructura 1D del Benchmark (SATS simplificado sobre cierres confirmados)
bool pilar1SpyTrend = nz(spyClosePrev, 0.0) > nz(spySma20, 0.0) and nz(spySma20, 0.0) >= nz(spySma50, 0.0) * 0.995
 
// Pilar 2: Direccion Intradia del Benchmark vs Apertura RTH
float spyUmbralIntradia = nz(spyOpenDay, spyLivePx) * (1.0 - spyBufferPct / 100.0)
bool  pilar2SpyIntradia = nz(spyLivePx, 0.0) >= spyUmbralIntradia
float spyCambioOpenPct  = nz(spyOpenDay, 0.0) > 0.0 ? ((nz(spyLivePx, 0.0) / spyOpenDay) - 1.0) * 100.0 : 0.0
 
// Pilar 3: Regimen de Volatilidad Implicita (VIX bajo umbral defensivo)
bool pilar3VixCalmo = nz(vixLivePx, 18.0) < vixShieldThresh
 
// Pilar 4: Alineacion Estructural del Activo en Pantalla (Tendencia 1D + Soporte EMA9)
bool pilar4ActivoSano = close > nz(dSma50Prev, close) and nz(dSma20Prev, close) >= nz(dSma50Prev, close) * 0.99 and close >= ema9Local * 0.9985
 
int pilaresActivos = (pilar1SpyTrend ? 1 : 0) + (pilar2SpyIntradia ? 1 : 0) + (pilar3VixCalmo ? 1 : 0) + (pilar4ActivoSano ? 1 : 0)
 
// Determinacion determinista de postura de estudio:
// - MODO ESCUDO: VIX >= umbral defensivo o Benchmark perforando apertura sin soporte estructural
bool modoEscudoActivo = (not pilar3VixCalmo) or (not pilar2SpyIntradia and not pilar1SpyTrend)
bool regimenFavorable = (not modoEscudoActivo) and pilaresActivos >= 3
 
string etiquetaRegimen = modoEscudoActivo ? "MODO ESCUDO (DEFENSIVO)" : (regimenFavorable ? "C1 FAVORABLE EXPANSIVO" : "C2/C3 SELECTIVO NEUTRAL")
color  colorRegimen    = modoEscudoActivo ? color.new(#D97706, 0) : (regimenFavorable ? color.new(#059669, 0) : color.new(#2563EB, 0))
 
// ----------------------------------------------------------------------------
// 4. NIVELES DE ESTUDIO BASADOS EN ATR(14) CAUSAL Y ESCALONES DE PARIDAD
// ----------------------------------------------------------------------------
float refHighIntradia = math.max(nz(dHighToday, high), high)
float descuentoMaxPct = 0.0075 * refHighIntradia
float descuentoAtr    = math.min(kPullbackAtr * safeAtr14, descuentoMaxPct)
 
// Zona de Observacion en retroceso controlado (Pullback ATR acotado)
float nivelObservacionLmt = refHighIntradia - descuentoAtr
 
// Distancia estructural de invalidacion acotada entre 0.95% y 1.35% de la cotizacion o factor ATR
float distStopPctRaw      = math.min(0.0135, math.max(0.0095, (kStopAtr * safeAtr14) / math.max(nivelObservacionLmt, 0.0001)))
float nivelInvalidacionSl = nivelObservacionLmt * (1.0 - distStopPctRaw)
 
// Escalones de estudio: Paridad (Escalon 1) y Expansion Elastica (2.00 ATR)
float tp1PctEff           = math.max(0.0065, math.min(0.0085, 0.28 * (atrPct1D / 100.0)))
float nivelParidadTp1     = nivelObservacionLmt * (1.0 + tp1PctEff)
float nivelCojinFreeRoll  = nivelObservacionLmt * (1.0 + 0.50 * tp1PctEff)
 
float tp2PctEff           = math.min(0.0850, math.max(0.0180, kTarget2Atr * (atrPct1D / 100.0)))
float nivelExpansionTp2   = nivelObservacionLmt * (1.0 + tp2PctEff)
 
// ----------------------------------------------------------------------------
// 5. TRAZADO EN GRAFICO (OVERLAY LIMPIO E INSTITUCIONAL)
// ----------------------------------------------------------------------------
plot(showPocInput ? dPoc20Prev : na, "Consenso 20D (POC)", color = color.new(#64748B, 35), linewidth = 2, style = plot.style_circles)
plot(showBandsInput ? nivelExpansionTp2 : na, "Nivel Estudio Expansion (2.0 ATR)", color = color.new(#10B981, 25), linewidth = 1)
plot(showBandsInput ? nivelParidadTp1 : na, "Nivel Estudio Paridad (Escalon 1)", color = color.new(#0EA5E9, 25), linewidth = 1)
plot(showBandsInput ? nivelObservacionLmt : na, "Zona de Observacion Pullback ATR", color = color.new(#F59E0B, 10), linewidth = 2)
plot(showBandsInput ? nivelInvalidacionSl : na, "Invalidacion Estructural (ATR)", color = color.new(#EF4444, 25), linewidth = 1)
 
bgcolor(showBgShield and modoEscudoActivo ? color.new(#F59E0B, 92) : na, title = "Fondo Postura Modo Escudo")
 
// ----------------------------------------------------------------------------
// 6. PANEL ESTADISTICO COMPACTO EN ESQUINA SUPERIOR DERECHA
// ----------------------------------------------------------------------------
var table panelEstudio = table.new(position.top_right, 2, 8, border_width = 1, frame_color = color.new(#334155, 40), border_color = color.new(#334155, 60))
 
if barstate.islast and showTableInput
    color bgHeader = color.new(#0F172A, 8)
    color bgRow    = color.new(#1E293B, 12)
    color txtLight = color.new(#F8FAFC, 0)
    color txtMuted = color.new(#CBD5E1, 0)
 
    table.cell(panelEstudio, 0, 0, "GENESIS REGIME & ATR STUDY [LITE]", text_color = txtLight, bgcolor = bgHeader, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 0, syminfo.ticker + " (" + timeframe.period + ")", text_color = txtMuted, bgcolor = bgHeader, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 1, "Postura Cuantitativa", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 1, etiquetaRegimen, text_color = txtLight, bgcolor = colorRegimen, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 2, "Pilares Alineados (4P)", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 2, str.tostring(pilaresActivos) + " / 4", text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 3, "Benchmark vs Open | VIX", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 3, str.tostring(spyCambioOpenPct, "#.##") + "% | " + str.tostring(nz(vixLivePx, 0.0), "#.##"), text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 4, "ATR(14) Diario Causal", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 4, str.tostring(safeAtr14, "#.##") + " (" + str.tostring(atrPct1D, "#.##") + "%)", text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 5, "Zona Observacion Pullback", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 5, modoEscudoActivo ? "PAUSA DEFENSIVA" : str.tostring(nivelObservacionLmt, format.mintick), text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 6, "Escalon 1 Paridad | Cojin", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 6, str.tostring(nivelParidadTp1, format.mintick) + " | " + str.tostring(nivelCojinFreeRoll, format.mintick), text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
    table.cell(panelEstudio, 0, 7, "Invalidacion | Expansion 2ATR", text_color = txtMuted, bgcolor = bgRow, text_size = size.small, text_halign = text.align_left)
    table.cell(panelEstudio, 1, 7, str.tostring(nivelInvalidacionSl, format.mintick) + " | " + str.tostring(nivelExpansionTp2, format.mintick), text_color = txtLight, bgcolor = bgRow, text_size = size.small, text_halign = text.align_right)
 
// ----------------------------------------------------------------------------
// 7. CONDICIONES DE ALERTA EDUCATIVA (SIN LENGUAJE IMPERATIVO)
// ----------------------------------------------------------------------------
alertcondition(modoEscudoActivo and not modoEscudoActivo[1], title = "Activacion Postura Modo Escudo", message = "GENESIS [Lite]: Contexto macro en postura defensiva (Modo Escudo). Priorizar preservacion estadistica.")
alertcondition(regimenFavorable and not regimenFavorable[1], title = "Transicion a Regimen Favorable C1", message = "GENESIS [Lite]: Alineacion de pilares hacia Regimen Favorable C1.")

← 返回列表