// 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 strategy("[H32] Detector de Tendencia Estrategia V4.3", shorttitle = '[H32] EsTrend', overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1) // --- 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 Estrategia" 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 --- [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 --- 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 --- 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 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 // --- IDENTIFICAR SEÑALES BEAR --- is_s2_bear = sig_2 and is_downtrend is_s3_bear = sig_3 and is_downtrend is_s4_bear = sig_4 and is_downtrend // --- CONTADORES DE DURACIÓN --- 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 // --- CONDICIONES DE ENTRADA MEJORADAS --- 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 // --- EJECUCIÓN DE ESTRATEGIA --- if short_condition and strategy.position_size == 0 strategy.entry("Short", strategy.short, comment="SHORT") // Detectar cierre de posición is_position_closed = strategy.position_size[1] < 0 and strategy.position_size == 0 // --- SALIDAS (TP + SL FIJO) --- if strategy.position_size < 0 tp_level = strategy.position_avg_price * (1 - take_profit_pct) sl_level = strategy.position_avg_price * (1 + stop_loss_pct) strategy.exit("Salida", from_entry="Short", limit=tp_level, stop=sl_level, comment_profit="TP", comment_loss="SL") // --- DETECCIÓN DE EVENTOS PARA VISUALIZACIÓN --- 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] // --- VISUALIZACIÓN COMPLETA (SIZE small) --- 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) // S1 RANGO (Círculo) - small plotshape(show_range, title="Inicio Rango", style=shape.circle, location=location.abovebar, color=col_range, size=size.small, force_overlay=true, text="Rango", textcolor=col_range) // S2 (Cuadrado) - small plotshape(show_signal and sig_2 and is_uptrend, title="S2 Bull", style=shape.square, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S2", textcolor=col_bull) plotshape(show_signal and sig_2 and is_downtrend, title="S2 Bear", style=shape.square, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S2", textcolor=col_bear) // S3 (Triángulo) - small plotshape(show_signal and sig_3 and is_uptrend, title="S3 Bull", style=shape.triangleup, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S3", textcolor=col_bull) plotshape(show_signal and sig_3 and is_downtrend, title="S3 Bear", style=shape.triangledown, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S3", textcolor=col_bear) // S4 (Triángulo) - small plotshape(show_signal and sig_4 and is_uptrend, title="S4 Bull", style=shape.triangleup, location=location.belowbar, color=col_bull, size=size.small, force_overlay=true, text="S4", textcolor=col_bull) plotshape(show_signal and sig_4 and is_downtrend, title="S4 Bear", style=shape.triangledown, location=location.abovebar, color=col_bear, size=size.small, force_overlay=true, text="S4", textcolor=col_bear) // INDECISIÓN - small plotshape(show_signal and is_sideway_trend, title="Indecisión", style=shape.diamond, location=location.belowbar, color=col_indec, size=size.small, force_overlay=true, text="Wait", textcolor=col_indec) // S5 EXTREMA - small plotshape(show_signal and sig_5, title="S5 Extrema", style=shape.diamond, location=location.abovebar, color=col_extreme, size=size.small, force_overlay=true, text="S5!", textcolor=col_extreme) // Bandas 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") // --- MARCADORES DE OPERACIÓN (small + ICONOS NUEVOS) --- // Bandera Azul Aqua para Entrada Short plotshape(short_condition and strategy.position_size == 0, 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(is_position_closed, title="Cierre Orden", char="$", location=location.belowbar, color=color.green, size=size.normal, force_overlay=true, text="TP", textcolor=color.green)