
La estrategia es un sistema de seguimiento de tendencias basado en múltiples cruces de medias, que combina los indicadores SMA y EMA para capturar las tendencias del mercado. La estrategia utiliza una combinación de promedios móviles simples (SMA) y dos promedios móviles de índices (EMA) de períodos personalizados para construir un sistema de comercio de seguimiento de tendencias completo. Al mismo tiempo, integra un mecanismo de gestión de objetivos de pérdidas y ganancias dinámicas para controlar el riesgo y bloquear los beneficios de manera efectiva.
La estrategia toma decisiones de negociación basadas principalmente en la relación dinámica de las tres líneas uniformes. El sistema determina la dirección de la tendencia mediante el monitoreo de la posición relativa del precio con el SMA y la intersección del EMA rápido con el EMA lento. Las señales de entrada se dividen en dos formas de activación: una, cuando el precio está por encima de la SMA y por encima de la EMA rápida y por debajo de la EMA lenta; la segunda, cuando el precio rompe la SMA y el precio previo permanece por encima de la SMA y por debajo de la misma.
La estrategia utiliza el uso combinado de múltiples líneas de medias para construir un sistema completo de seguimiento de tendencias, con un diseño de reglas detallado en los aspectos de entrada, salida y gestión de riesgos. La ventaja de la estrategia reside en la capacidad de identificar y seguir las tendencias de manera efectiva, mientras que los beneficios se protegen a través de un mecanismo de parada de pérdidas dinámico. Aunque existen algunos riesgos inherentes, la orientación de optimización propuesta puede mejorar aún más la estabilidad y adaptabilidad de la estrategia.
/*backtest
start: 2025-02-17 17:00:00
end: 2025-02-20 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("交易策略(自定义EMA/SMA参数)", overlay=true, initial_capital=100000, currency=currency.EUR, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// 输入参数:可调的 SMA 和 EMA 周期
smaLength = input.int(120, "SMA Length", minval=1, step=1)
emaFastPeriod = input.int(13, "EMA Fast Period", minval=1, step=1)
emaSlowPeriod = input.int(21, "EMA Slow Period", minval=1, step=1)
// 计算均线
smaVal = ta.sma(close, smaLength)
emaFast = ta.ema(close, emaFastPeriod)
emaSlow = ta.ema(close, emaSlowPeriod)
// 绘制均线
plot(smaVal, color=color.orange, title="SMA")
plot(emaFast, color=color.blue, title="EMA Fast")
plot(emaSlow, color=color.red, title="EMA Slow")
// 入场条件 - 做多
// 条件1:收盘价高于SMA 且 EMA Fast 向上穿越 EMA Slow
longTrigger1 = (close > smaVal) and ta.crossover(emaFast, emaSlow)
// 条件2:收盘价上穿SMA 且前5根K线的最低价均高于各自的SMA
longTrigger2 = ta.crossover(close, smaVal) and (low[1] > smaVal[1] and low[2] > smaVal[2] and low[3] > smaVal[3] and low[4] > smaVal[4] and low[5] > smaVal[5])
longCondition = longTrigger1 or longTrigger2
// 入场条件 - 做空
// 条件1:收盘价低于SMA 且 EMA Fast 向下穿越 EMA Slow
shortTrigger1 = (close < smaVal) and ta.crossunder(emaFast, emaSlow)
// 条件2:收盘价下穿SMA 且前5根K线的最高价均低于各自的SMA
shortTrigger2 = ta.crossunder(close, smaVal) and (high[1] < smaVal[1] and high[2] < smaVal[2] and high[3] < smaVal[3] and high[4] < smaVal[4] and high[5] < smaVal[5])
shortCondition = shortTrigger1 or shortTrigger2
// 定义变量记录入场时的价格与EMA Fast值,用于计算止损
var float entryPriceLong = na
var float entryEMA_Fast_Long = na
var float entryPriceShort = na
var float entryEMA_Fast_Short = na
// 入场与初始止盈止损设置 - 做多
// 止损取“开仓时的EMA Fast价格”与“0.2%止损”中较大者;止盈为止损的5倍
if (longCondition and strategy.position_size == 0)
entryPriceLong := close
entryEMA_Fast_Long := emaFast
strategy.entry("Long", strategy.long)
stopPercLong = math.max(0.002, (entryPriceLong - entryEMA_Fast_Long) / entryPriceLong)
stopLong = entryPriceLong * (1 - stopPercLong)
tpLong = entryPriceLong * (1 + 5 * stopPercLong)
strategy.exit("LongExit", "Long", stop=stopLong, limit=tpLong)
// 入场与初始止盈止损设置 - 做空
// 止损取“开仓时的EMA Fast价格”与“0.2%止损”中较大者;止盈为止损的5倍
if (shortCondition and strategy.position_size == 0)
entryPriceShort := close
entryEMA_Fast_Short := emaFast
strategy.entry("Short", strategy.short)
stopPercShort = math.max(0.002, (entryEMA_Fast_Short - entryPriceShort) / entryPriceShort)
stopShort = entryPriceShort * (1 + stopPercShort)
tpShort = entryPriceShort * (1 - 5 * stopPercShort)
strategy.exit("ShortExit", "Short", stop=stopShort, limit=tpShort)
// 移动止损逻辑
// 当持仓盈利达到0.8%时更新止损和止盈,保持止盈为止损的5倍
var float longHighest = na
if (strategy.position_size > 0)
longHighest := na(longHighest) ? high : math.max(longHighest, high)
if (high >= entryPriceLong * 1.008)
newLongStop = longHighest * (1 - 0.003)
newPerc = (entryPriceLong - newLongStop) / entryPriceLong
newLongTP = entryPriceLong * (1 + 5 * newPerc)
strategy.exit("LongExit", "Long", stop=newLongStop, limit=newLongTP)
else
longHighest := na
var float shortLowest = na
if (strategy.position_size < 0)
shortLowest := na(shortLowest) ? low : math.min(shortLowest, low)
if (low <= entryPriceShort * 0.992)
newShortStop = shortLowest * (1 + 0.003)
newPercShort = (newShortStop - entryPriceShort) / entryPriceShort
newShortTP = entryPriceShort * (1 - 5 * newPercShort)
strategy.exit("ShortExit", "Short", stop=newShortStop, limit=newShortTP)
else
shortLowest := na
// 额外平仓条件
// 如果持多仓时EMA Fast下穿EMA Slow,则立即平多
if (strategy.position_size > 0 and ta.crossunder(emaFast, emaSlow))
strategy.close("Long", comment="EMA下穿平多")
// 如果持空仓时EMA Fast上穿EMA Slow,则立即平空
if (strategy.position_size < 0 and ta.crossover(emaFast, emaSlow))
strategy.close("Short", comment="EMA上穿平空")