
Chiến lược này là một hệ thống giao dịch tiên tiến dựa trên chỉ báo MACD (Moving Average Convergence Divergence). Nó kết hợp các tín hiệu MACD với quản lý rủi ro động để đạt được giải pháp giao dịch toàn diện. Chiến lược này không chỉ tập trung vào sự giao nhau của đường MACD và đường tín hiệu mà còn kết hợp xác nhận biểu đồ và tối ưu hóa kết quả giao dịch thông qua các thiết lập dừng lỗ và chốt lời linh hoạt. Chiến lược này cung cấp đầy đủ các cấu hình tham số hóa, cho phép thích ứng với các môi trường thị trường và nhu cầu giao dịch khác nhau.
Logic cốt lõi của chiến lược này được xây dựng dựa trên ba trụ cột chính:
Chiến lược này tạo ra một hệ thống giao dịch mạnh mẽ bằng cách kết hợp chỉ báo MACD cổ điển với các phương pháp quản lý rủi ro hiện đại. Ưu điểm của nó nằm ở cơ chế xác nhận tín hiệu hoàn hảo, quản lý rủi ro linh hoạt và khả năng điều chỉnh thông số mạnh mẽ, khiến nó phù hợp với nhiều môi trường thị trường khác nhau. Thông qua các hướng tối ưu hóa được đề xuất, chiến lược vẫn còn có thể được cải thiện thêm. Tuy nhiên, người dùng cần chú ý kiểm soát rủi ro, tránh tối ưu hóa quá mức và thực hiện các điều chỉnh phù hợp dựa trên môi trường giao dịch thực tế.
/*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)