2025-06-13[五]:KD30-70,震盪指標‧‧

 2025-06-13[五]:KD30-70,震盪指標‧‧

// Copyright © 2023, José Manuel Gassin Pérez-Traverso, All rights reserved.
// © JoseMetal
//@version=5
indicator("KDJ [JoseMetal]", "KDJ [JoseMetal]", false)



//== Constantes
c_granate = color.rgb(136, 14, 79, 20)
c_verde_radiactivo    = color.rgb(0, 255, 0, 0)
c_verde               = color.rgb(0, 128, 0, 0)
c_verde_oscuro        = color.rgb(0, 80, 0, 0)
c_rojo_radiactivo     = color.rgb(255, 0, 0, 0)
c_rojo                = color.rgb(128, 0, 0, 0)
c_rojo_oscuro         = color.rgb(80, 0, 0, 0)
c_azul_claro = color.rgb(0, 128, 255, 0)
c_negro = color.rgb(0, 0, 0, 0)
noneColor = color.new(color.white, 100)



//== Funciones
_MA(src, long, tipo) =>
    switch tipo
        "SMA"        => ta.sma(src, long)
        "EMA"        => ta.ema(src, long)
        "SMMA (RMA)" => ta.rma(src, long)
        "WMA"        => ta.wma(src, long)
        "HMA"        => ta.hma(src, long)
        "VWMA"       => ta.vwma(src, long)



//== Opciones de ploteo
GRUPO_PLOTEO = "Mostrar"
PLOTEO_mostrar_KDJ = input.bool(true, "KDJ", group=GRUPO_PLOTEO)
PLOTEO_mostrar_Histograma = input.bool(true, "Histograma", group=GRUPO_PLOTEO)
PLOTEO_mostrar_cruces = input.bool(true, "Cruces de K y D", group=GRUPO_PLOTEO)
PLOTEO_mostrar_relleno = input.bool(true, "Relleno (K y D)", group=GRUPO_PLOTEO)






//== Colores
GRUPO_COLORES  = "Colores"
color_K = input.color(color.green, "• Color K/D/J", group=GRUPO_COLORES, inline="kdj_colors")
color_D = input.color(color.red, "", group=GRUPO_COLORES, inline="kdj_colors")
color_J = input.color(color.white, "", group=GRUPO_COLORES, inline="kdj_colors")
histograma_alcista_decrece = input.color(color.new(#388e3c, 50), "• Color histograma ↗ | ↗↗ | ↘ | ↘↘", group=GRUPO_COLORES, inline="hist_colors")
histograma_alcista_crece = input.color(color.new(#a5d6a7, 50), "", group=GRUPO_COLORES, inline="hist_colors")
histograma_bajista_decrece = input.color(color.new(#faa1a4, 50), "", group=GRUPO_COLORES, inline="hist_colors")
histograma_bajista_crece = input.color(color.new(#f7525f, 50), "", group=GRUPO_COLORES, inline="hist_colors")




//== Inputs y cálculos
// KDJ
GRUPO_STOCH    = "KDJ"
STOCH_length   = input.int(title="• Longitud", group=GRUPO_STOCH, defval=9, minval=1) // 42 21 21 para medio plazo o 14,21,21
K_length = input.int(title="• K y D", group=GRUPO_STOCH, defval=3, minval=1, inline="stoch_calc")
D_length = input.int(title="", group=GRUPO_STOCH, defval=3, minval=1, inline="stoch_calc")
tipo_MA = input.string(title="• Tipo MA", defval="SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "HMA", "VWMA"], group=GRUPO_STOCH)
hist_smooth = input.int(title="• Suavizado Histograma", group=GRUPO_STOCH, defval=7, minval=1)
STOCH_nivel_os     = input.int(title="• Sobreventa / Sobrecompra", group=GRUPO_STOCH, defval=20, minval=1, inline="niveles")
STOCH_nivel_ob     = input.int(title="", group=GRUPO_STOCH, defval=80, minval=1, inline="niveles")

STOCH = ta.stoch(close, high, low, STOCH_length)
K = _MA(STOCH, K_length, tipo_MA)
D = _MA(K, D_length, tipo_MA)
J = 3*K - 2*D

// Histograma (J vs D)
hist = ta.ema(J - D + 50, hist_smooth)

//hist_2 = K - D + 50
//hist_2_alcista = hist_2 > 50
//hist_2_color = hist_2_alcista ? c_verde_radiactivo : c_rojo_radiactivo
//plot(hist_2, title="Histograma", style=plot.style_histogram, color=hist_2_color, histbase=50)



//== Condiciones
stoch_alcista = K >= D
cruce_alcista = J > K and J > D and (J[1] < D[1] or J[1] < K[1])
cruce_bajista = J < K and J < D and (J[1] > D[1] or J[1] > K[1])
hist_alcista = hist > 50
hist_creciente = hist[1] < hist
//mov_fuerte = hist_alcista ? hist > J : hist < J // Histograma es superior a J (en alcista) o inferior (en bajista)



//== Ploteo en pantalla
// Niveles
hline(STOCH_nivel_ob, "Sobrecompra", color=color.new(c_rojo_radiactivo, 40), linestyle=hline.style_solid)
hline(50, "Centro", color=color.new(color.gray, 40), linestyle=hline.style_dashed)
hline(STOCH_nivel_os, "Sobreventa", color=color.new(c_verde_radiactivo, 40), linestyle=hline.style_solid)

// Histograma
hist_color = hist_alcista ? (hist_creciente ? histograma_alcista_crece : histograma_alcista_decrece) : (hist_creciente ? histograma_bajista_crece : histograma_bajista_decrece)
plot(PLOTEO_mostrar_Histograma ? hist : na, title="Histograma", style=plot.style_columns, color=hist_color, histbase=50)

// Estocástico + J
plot_K = plot(PLOTEO_mostrar_KDJ ? K : na, "Estocástico K", color=color_K, linewidth=2)
plot_D = plot(PLOTEO_mostrar_KDJ ? D : na, "Estocástico D", color=color_D, linewidth=2)
plot(PLOTEO_mostrar_KDJ ? J : na, "J", color=color_J, linewidth=2)
fill(plot_K, plot_D, PLOTEO_mostrar_KDJ and PLOTEO_mostrar_relleno ? (stoch_alcista ? c_verde_oscuro : c_rojo_oscuro) : noneColor, "Relleno K-D")

// Señales de cruce de K con D
punto_cruce_stoch = (K + D) / 2
color_cruce_stoch = cruce_alcista ? c_verde_radiactivo : cruce_bajista ? c_rojo_radiactivo : noneColor
plot(PLOTEO_mostrar_KDJ and PLOTEO_mostrar_cruces ? punto_cruce_stoch : na, color=cruce_alcista or cruce_bajista ? c_negro : noneColor, linewidth=5, style=plot.style_circles)
plot(PLOTEO_mostrar_KDJ and PLOTEO_mostrar_cruces ? punto_cruce_stoch : na, color=color_cruce_stoch, linewidth=4, style=plot.style_circles)



//== Color de fondo
color_fondo = cruce_alcista ? color.new(color.lime, 75) : cruce_bajista ? color.new(color.red, 70) : stoch_alcista ? color.new(color.green, 90) : color.new(color.red, 90)
bgcolor(color_fondo)



//== Alertas
alertcondition(cruce_alcista, "🟢Abrir LONG🟢", "🟢Abrir LONG🟢 ({{ticker}}:{{interval}})")
alertcondition(cruce_bajista, "🔴Abrir SHORT🔴", "🔴Abrir SHORT🔴 ({{ticker}}:{{interval}})")

留言

這個網誌中的熱門文章

2025-06-13[五]:4EMA+MACD指標‧‧

2025-06-13[五]:RSI超買超賣指標‧‧