本策略是一个基于MACD和RSI双重指标过滤的趋势跟踪系统,集成了动态止损机制。该策略主要通过MACD的交叉信号产生交易机会,并使用RSI作为二次确认,同时引入了百分比止损来控制风险。策略的核心在于通过技术指标的配合使用来提高交易信号的可靠性,并通过动态止损来保护盈利。
策略采用MACD(12,26,9)和RSI(14)作为主要指标。入场信号需同时满足两个条件:MACD金叉且RSI处于超卖区域(默认40以下)时做多,MACD死叉且RSI处于超买区域(默认59以上)时做空。系统还设置了3%的动态止损,当价格向不利方向移动超过设定百分比时,会自动平仓以控制风险。此外,策略还包含了时间过滤器,允许用户设定特定的交易时间范围。
这是一个结构完整、逻辑清晰的趋势跟踪策略。通过MACD和RSI的组合使用,有效提高了交易信号的质量。动态止损的设计帮助控制风险,使策略具有良好的风险管理特性。该策略适合在趋势明确的市场中使用,但需要根据具体市场特征调整参数设置。通过建议的优化方向,策略的稳定性和可靠性还可以进一步提升。
/*backtest
start: 2025-02-13 10:00:00
end: 2025-02-19 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Binance","currency":"BNB_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © eagle916
//@version=5
strategy("EAG MACD + RSI Strategy",overlay=true, initial_capital = 300, default_qty_value = 10, default_qty_type = "percent_of_equity", commission_type=strategy.commission.percent, commission_value=0.1)
// Input para el RSI
rsi_length = input.int(14, title="RSI Length", minval=1)
rsi_overbought = input.int(59, title="RSI Overbought Level", minval=1, maxval=100)
rsi_oversold = input.int(40, title="RSI Oversold Level", minval=1, maxval=100)
// Input para el MACD
macd_length = input.int(12, title="MACD Length", minval=1)
macd_overbought = input.int(26, title="MACD Overbought Level", minval=1, maxval=100)
macd_signal = input.int(9, title="MACD Signal Level", minval=1, maxval=100)
// Input para el porcentaje de pérdida (stop loss)
stop_loss_percent = input.float(3.0, title="Porcentaje de Stop Loss (%)", minval=0.1, step=0.1)
// Calcular RSI
rsi_value = ta.rsi(close, rsi_length)
// Calcular MACD
[macdLine, signalLine, _] = ta.macd(close, macd_length, macd_overbought, macd_signal)
macd_crossup = ta.crossover(macdLine, signalLine) // Cruce al alza del MACD
macd_crossdown = ta.crossunder(macdLine, signalLine) // Cruce a la baja del MACD
// Condiciones de compra y venta
buy_condition = macd_crossup and rsi_value <= rsi_oversold
sell_condition = macd_crossdown and rsi_value >= rsi_overbought
// Registrar precio de entrada
var float entry_price = na
if strategy.position_size == 0
entry_price := na
// Mostrar señales de compra y venta en la gráfica principal
plotshape(series=buy_condition, title="Señal de Compra", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY") // Compra debajo de la vela
plotshape(series=sell_condition, title="Señal de Venta", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL") // Venta encima de la vela
// Órdenes de estrategia
if buy_condition
strategy.entry("Compra", strategy.long)
entry_price := close
if sell_condition
strategy.entry("Venta", strategy.short)
entry_price := close
// Calcular el precio de stop loss
long_stop_loss = entry_price * (1 - stop_loss_percent / 100)
short_stop_loss = entry_price * (1 + stop_loss_percent / 100)
// Cerrar posición si el precio va en contra el porcentaje definido por el usuario
if strategy.position_size > 0 and close < long_stop_loss
strategy.close("Compra", comment="Stop Loss Compra")
if strategy.position_size < 0 and close > short_stop_loss
strategy.close("Venta", comment="Stop Loss Venta")