This strategy is an advanced trading system based on the MACD (Moving Average Convergence Divergence) indicator, combining MACD signals with dynamic risk management to create a comprehensive trading solution. The strategy not only focuses on MACD line and signal line crossovers but also incorporates histogram confirmation and flexible stop-loss and take-profit settings to optimize trading performance. It offers fully parameterized configuration options to adapt to different market conditions and trading requirements.
The core logic is built on three main pillars:
This strategy creates a robust trading system by combining the classic MACD indicator with modern risk management methods. Its strengths lie in comprehensive signal confirmation, flexible risk management, and strong parameter adjustability, making it suitable for various market environments. Through the suggested optimization directions, the strategy has room for further improvement. However, users need to pay attention to risk control, avoid over-optimization, and make appropriate adjustments based on actual trading conditions.
/*backtest start: 2019-12-23 08:00:00 end: 2025-01-04 08:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("Estrategia MACD", overlay=true) // Parámetros entrada direccion = input.string("ambas", "Dirección de operaciones", options=["larga", "corta", "ambas"]) velas_sl = input.int(3, "Velas para calcular Stop Loss", minval=1) ratio = input.float(1.5, "Ratio Beneficio:Riesgo", minval=0.5) rapida = input.int(12, "Periodo Media Rápida") lenta = input.int(26, "Periodo Media Lenta") senal = input.int(9, "Periodo Señal") // Calcular MACD [macdLinea, senalLinea, histograma] = ta.macd(close, rapida, lenta, senal) // Señales senal_larga = ta.crossover(macdLinea, senalLinea) and histograma > 0 senal_corta = ta.crossunder(macdLinea, senalLinea) and histograma < 0 // Gestión de riesgo calcular_sl_largo() => ta.lowest(low, velas_sl) calcular_sl_corto() => ta.highest(high, velas_sl) calcular_tp(entrada, sl, es_larga) => distancia = math.abs(entrada - sl) es_larga ? entrada + (distancia * ratio) : entrada - (distancia * ratio) // Operaciones sl_largo = calcular_sl_largo() sl_corto = calcular_sl_corto() if (direccion != "corta" and senal_larga and strategy.position_size == 0) entrada = close tp = calcular_tp(entrada, sl_largo, true) strategy.entry("Larga", strategy.long) strategy.exit("Salida Larga", "Larga", stop=sl_largo, limit=tp) if (direccion != "larga" and senal_corta and strategy.position_size == 0) entrada = close tp = calcular_tp(entrada, sl_corto, false) strategy.entry("Corta", strategy.short) strategy.exit("Salida Corta", "Corta", stop=sl_corto, limit=tp) // Visualización plotshape(senal_larga and direccion != "corta", "Compra", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.normal) plotshape(senal_corta and direccion != "larga", "Venta", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.normal)