本策略基于简单移动平均线(SMA)的斜率来识别上升趋势,并在满足特定条件时开仓做多。同时,引入了可选的跟踪止损机制,通过动态调整止损价格来保护利润。此外,该策略还设置了止损后重新进场的条件,以防止在价格过高时重新建仓。通过这些功能,该策略能够有效捕捉上升趋势,控制风险,并实现纪律化的交易。
该策略利用SMA趋势跟踪、跟踪止损和纪律化重新进场等机制,在捕捉上升趋势的同时控制风险。通过优化参数设置、增强风险管理、支持双向交易和多时间框架确认等方法,可进一步提升策略的适应性和稳健性。
/*backtest start: 2023-05-28 00:00:00 end: 2024-06-02 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("MA Incline Strategy with Optional Trailing Stop-Loss", overlay=true, calc_on_every_tick=true) // Input parameters windowSize = input.int(20, title="Window Size") maLength = input.int(150, title="Moving Average Length") minSlope = input.float(0.1, title="Minimum Slope") useTrailingStop = input.bool(true, title="Use Trailing Stop-Loss") trailingStopPercentage = input.float(2.8, title="Trailing Stop Percentage (%)") / 100 // Calculate the moving average ma = ta.sma(close, maLength) // Calculate the slope of the moving average over the window size previousMa = ta.sma(close[windowSize], maLength) slopeMa = (ma - previousMa) / windowSize // Check conditions isAboveMinSlope = slopeMa > minSlope isAboveMa = close > ma // Buy condition buyCondition = isAboveMinSlope and isAboveMa // Execute strategy if (buyCondition and strategy.opentrades == 0) strategy.entry("Long", strategy.long) // Trailing stop-loss (optional) if (strategy.opentrades == 1 and useTrailingStop and isAboveMa) // Calculate the trailing stop price trailPrice = close * (1 - trailingStopPercentage) // Use the built-in strategy.exit function with the trailing stop strategy.exit("Trail Stop", "Long", stop=trailPrice) // Exit condition sellCondition = ta.crossover(ma, close) if (sellCondition and strategy.opentrades == 1) strategy.close("Long")