// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Halving32 // _ _ _ _ ____ ___ // | | | | | | (_) |___ \__ \ // | |__| | __ _| |_ ___ _ __ __ _ __) | ) | // | __ |/ _` | \ \ / / | '_ \ / _` ||__ < / / // | | | | (_| | |\ V /| | | | | (_| |___) / /_ // |_| |_|\__,_|_| \_/ |_|_| |_|\__, |____/____| // __/ | // |___/ //@version=6 indicator("[H32] Detector de Tendencia Indicador V4.3", shorttitle = '[H32] IndTrend', overlay=false) // --- CONFIGURACIÓN --- grp_adx = "Configuración ADX" adx_len = input.int(14, "Longitud ADX", group=grp_adx) adx_smooth = input.int(10, "Suavizado ADX", group=grp_adx) grp_aroon = "Configuración Aroon" aroon_len = input.int(25, "Longitud Aroon", group=grp_aroon) grp_bb = "Configuración Bandas de Bollinger" bb_len = input.int(20, "Longitud BB", group=grp_bb) bb_mult = input.float(2.0, "Multiplicador BB", group=grp_bb) grp_strategy = "Configuración Simulación Trading" take_profit_pct = input.float(1.0, "Take Profit (%)", minval=0.1, maxval=20, step=0.1, group=grp_strategy) / 100 stop_loss_pct = input.float(3.0, "Stop Loss (%)", minval=0.1, maxval=100, step=0.1, group=grp_strategy) / 100 min_bars_s2 = input.int(3, "Mínimo velas S2_BEAR", minval=1, group=grp_strategy) min_bars_s3 = input.int(2, "Mínimo velas S3_BEAR", minval=1, group=grp_strategy) min_bars_s4 = input.int(1, "Mínimo velas S4_BEAR", minval=1, group=grp_strategy) // --- CÁLCULOS TÉCNICOS --- [di_plus, di_minus, adx] = ta.dmi(adx_len, adx_smooth) aroon_up = 100 * (ta.highestbars(high, aroon_len) + aroon_len) / aroon_len aroon_down = 100 * (ta.lowestbars(low, aroon_len) + aroon_len) / aroon_len [bb_middle, bb_upper, bb_lower] = ta.bb(close, bb_len, bb_mult) bb_width = bb_upper - bb_lower bb_sma_width = ta.sma(bb_width, 50) // --- DETECCIÓN INTELIGENTE (Lógica de la Estrategia) --- is_squeeze = bb_width < bb_sma_width breakout_up = close > bb_upper breakout_down = close < bb_lower is_breakout = breakout_up or breakout_down aroon_trend = math.abs(aroon_up - aroon_down) > 50 aroon_flat = aroon_up < 50 and aroon_down < 50 making_new_lows = low < ta.lowest(low[1], 5) making_new_highs = high > ta.highest(high[1], 5) price_action_active = making_new_lows or making_new_highs // --- LÓGICA DE DECISIÓN (Lógica de la Estrategia) --- is_range_setup = (is_squeeze and not is_breakout) or (aroon_flat and not price_action_active) or (adx < 20 and not price_action_active and not is_breakout) trend_strength = 0 if not is_range_setup if adx > 20 trend_strength += 1 if is_breakout trend_strength += 2 if price_action_active trend_strength += 1 if aroon_trend trend_strength += 1 is_final_range = is_range_setup or trend_strength == 0 sig_1 = is_final_range sig_2 = not sig_1 and trend_strength >= 1 and adx <= 40 sig_3 = not sig_1 and trend_strength >= 1 and adx > 40 and adx <= 60 sig_4 = not sig_1 and adx > 60 and adx <= 80 sig_5 = not sig_1 and adx > 80 // DIRECCIÓN Y COLORES is_bull = (breakout_up) or (not breakout_down and (di_plus > di_minus)) is_bear = (breakout_down) or (not breakout_up and (di_minus > di_plus)) col_bull = color.green col_bear = color.red col_indec = color.new(#E1C600, 0) col_range = color.gray col_extreme = color.purple final_color = is_bull ? col_bull : col_bear if not (aroon_trend or is_breakout or adx > 25) final_color := col_indec // Coloreado de barras bar_color = sig_5 ? col_extreme : (sig_1 ? col_range : final_color) barcolor(bar_color) is_uptrend = not sig_1 and final_color == col_bull is_downtrend = not sig_1 and final_color == col_bear is_sideway_trend = not sig_1 and final_color == col_indec // --- CONTADORES Y LÓGICA DE ENTRADA (Idéntico a Estrategia) --- is_s2_bear = sig_2 and is_downtrend is_s3_bear = sig_3 and is_downtrend is_s4_bear = sig_4 and is_downtrend var int s2_bear_count = 0 var int s3_bear_count = 0 var int s4_bear_count = 0 s2_bear_count := is_s2_bear ? s2_bear_count[1] + 1 : 0 s3_bear_count := is_s3_bear ? s3_bear_count[1] + 1 : 0 s4_bear_count := is_s4_bear ? s4_bear_count[1] + 1 : 0 min_volatility = bb_width > ta.sma(bb_width, 100) * 0.5 entry_s2 = is_s2_bear and s2_bear_count >= min_bars_s2 and min_volatility entry_s3 = is_s3_bear and s3_bear_count >= min_bars_s3 and min_volatility entry_s4 = is_s4_bear and s4_bear_count >= min_bars_s4 and min_volatility short_condition = entry_s2 or entry_s3 or entry_s4 // --- SIMULACIÓN TRADING (Adaptada para Indicador) --- var bool in_short_pos = false var float entry_price = 0.0 var float tp_price = 0.0 var float sl_price = 0.0 bool open_short_trigger = false // Si hay condición de short y no estamos dentro, entramos if short_condition and not in_short_pos in_short_pos := true entry_price := close tp_price := entry_price * (1 - take_profit_pct) sl_price := entry_price * (1 + stop_loss_pct) open_short_trigger := true bool close_short_trigger = false // Si estamos dentro y no acabamos de entrar en esta misma vela if in_short_pos and not open_short_trigger bool hit_tp = low <= tp_price bool hit_sl = high >= sl_price if hit_tp or hit_sl in_short_pos := false close_short_trigger := true // --- VISUALIZACIÓN DE INDICADORES --- plot(adx, "ADX Fuerza", color=color.yellow, linewidth=2) plot(aroon_up, "Aroon Up", color=color.new(#00FF00, 0), linewidth=1) plot(aroon_down, "Aroon Down", color=color.new(#FF0000, 0), linewidth=1) hline(20, "Nivel Rango", color=color.gray, linestyle=hline.style_dashed) // --- LÓGICA DE VISUALIZACIÓN (Copiada de Estrategia para coincidir triggers) --- current_level = sig_5 ? 5 : (sig_4 ? 4 : (sig_3 ? 3 : (sig_2 ? 2 : 1))) prev_level = current_level[1] level_up = current_level > prev_level and current_level > 1 new_trend_start = current_level > 1 and prev_level == 1 direction_change = (is_uptrend and not is_uptrend[1]) or (is_downtrend and not is_downtrend[1]) show_signal = new_trend_start or level_up or direction_change show_range = sig_1 and not sig_1[1] // Definición de Triggers para Plotshapes y Alertas trig_neutral = show_signal and is_sideway_trend trig_range = show_range trig_s2_bull = show_signal and sig_2 and is_uptrend trig_s3_bull = show_signal and sig_3 and is_uptrend trig_s4_bull = show_signal and sig_4 and is_uptrend trig_s5_bull = show_signal and sig_5 and is_uptrend trig_s2_bear = show_signal and sig_2 and is_downtrend trig_s3_bear = show_signal and sig_3 and is_downtrend trig_s4_bear = show_signal and sig_4 and is_downtrend trig_s5_bear = show_signal and sig_5 and is_downtrend // --- PLOTSHAPES (Estilo Estrategia) --- // S1 RANGO (Círculo) - small plotshape(trig_range, title="Inicio Rango", style=shape.circle, location=location.abovebar, color=col_range, size=size.small, force_overlay=true, text="Rango", textcolor=col_range) // NEUTRAL / INDECISIÓN - small plotshape(trig_neutral, title="Indecisión", style=shape.diamond, location=location.belowbar, color=col_indec, size=size.small, force_overlay=true, text="Wait", textcolor=col_indec) // BULL SIGNALS plotshape(trig_s2_bull, title="S2 Bull", style=shape.square, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S2", textcolor=col_bull) plotshape(trig_s3_bull, title="S3 Bull", style=shape.triangleup, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S3", textcolor=col_bull) plotshape(trig_s4_bull, title="S4 Bull", style=shape.triangleup, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S4", textcolor=col_bull) plotshape(trig_s5_bull, title="S5 Bull", style=shape.diamond, location=location.belowbar, color=col_extreme, size=size.small, force_overlay=true, text="S5!", textcolor=col_extreme) // BEAR SIGNALS plotshape(trig_s2_bear, title="S2 Bear", style=shape.square, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S2", textcolor=col_bear) plotshape(trig_s3_bear, title="S3 Bear", style=shape.triangledown, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S3", textcolor=col_bear) plotshape(trig_s4_bear, title="S4 Bear", style=shape.triangledown, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S4", textcolor=col_bear) plotshape(trig_s5_bear, title="S5 Bear", style=shape.diamond, location=location.abovebar, color=col_extreme, size=size.small, force_overlay=true, text="S5!", textcolor=col_extreme) // MARCADORES DE OPERACIÓN (small + ICONOS NUEVOS DE ESTRATEGIA) // Bandera Azul Aqua para Entrada Short plotshape(open_short_trigger, title="Entrada Short", style=shape.flag, location=location.abovebar, color=color.aqua, size=size.normal, force_overlay=true, text="SHORT", textcolor=color.aqua) // Signo de Dólar Verde para Cierre de Orden plotchar(close_short_trigger, title="Cierre Orden", char="$", location=location.belowbar, color=color.green, size=size.normal, force_overlay=true, text="TP/SL", textcolor=color.green) // Bandas (Visual) p_upper = plot(bb_upper, "BB Upper", color=color.new(color.blue, 50), force_overlay=true) p_lower = plot(bb_lower, "BB Lower", color=color.new(color.blue, 50), force_overlay=true) plot(bb_middle, "BB Basis", color=bar_color, linewidth=2, force_overlay=true) fill(p_upper, p_lower, color=color.new(color.blue, 95), title="Relleno BB") // ========================================== // ALERTAS DE EJECUCIÓN (OPEN / CLOSE) // ========================================== alertcondition(open_short_trigger, title="OPEN_SHORT", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "OPEN_SHORT"}') alertcondition(close_short_trigger, title="CLOSE_SHORT", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "CLOSE_SHORT"}') // ========================================== // ALERTAS DE ESTADO (NEUTRAL / RANGE) // ========================================== alertcondition(trig_neutral, title="NEUTRAL", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "NEUTRAL"}') alertcondition(trig_range, title="RANGE", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "RANGE"}') // ========================================== // ALERTAS DE TENDENCIA BEAR (S2 - S5) // ========================================== alertcondition(trig_s2_bear, title="S2_BEAR", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S2_BEAR"}') alertcondition(trig_s3_bear, title="S3_BEAR", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S3_BEAR"}') alertcondition(trig_s4_bear, title="S4_BEAR", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S4_BEAR"}') // ========================================== // ALERTAS DE TENDENCIA BULL (S2 - S5) // ========================================== alertcondition(trig_s2_bull, title="S2_BULL", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S2_BULL"}') alertcondition(trig_s3_bull, title="S3_BULL", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S3_BULL"}') alertcondition(trig_s4_bull, title="S4_BULL", message='{"clave_usuario": "TU_CLAVE_AQUI", "signal": "S4_BULL"}')