这是一个基于Supertrend指标的趋势追踪策略,结合了自适应的跟踪止损机制。该策略主要通过Supertrend指标识别市场趋势方向,并利用动态调整的跟踪止损来管理风险和优化出场时机。策略支持多种止损方式,包括百分比止损、ATR止损和固定点数止损,能够根据不同市场环境灵活调整。
策略的核心逻辑基于以下几个关键要素: 1. 使用Supertrend指标作为趋势判断的主要依据,该指标结合了ATR(平均真实波幅)来衡量市场波动性 2. 入场信号由Supertrend方向变化触发,支持做多、做空或双向交易 3. 止损机制采用自适应的跟踪止损,可以根据市场波动自动调整止损位置 4. 交易管理系统包含仓位管理(默认为账户15%仓位)和时间过滤机制
这是一个设计合理、风险可控的趋势追踪策略。通过结合Supertrend指标和灵活的止损机制,策略能够在保持较高盈利能力的同时有效控制风险。策略的可配置性强,适合在不同市场环境下使用,但需要经过充分的参数优化和回测验证。未来可以通过添加更多的技术分析工具和风险控制手段来进一步提升策略的稳定性和盈利能力。
/*backtest start: 2019-12-23 08:00:00 end: 2024-12-18 08:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=6 strategy("Supertrend Strategy with Adjustable Trailing Stop [Bips]", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=15) // Inputs atrPeriod = input(10, "ATR Länge", "Average True Range „wahre durchschnittliche Schwankungsbreite“ und stammt aus der technischen Analyse. Die ATR misst die Volatilität eines Instruments oder eines Marktes. Mit ihr kann die Wahrscheinlichkeit für einen Trendwechsel bestimmt werden.", group="Supertrend Settings") factor = input.float(3.0, "Faktor", step=0.1, group="Supertrend Settings") tradeDirection = input.string("Long", "Trade Direction", options=["Both", "Long", "Short"], group="Supertrend Settings") sl_type = input.string("%", "SL Type", options=["%", "ATR", "Absolute"]) // Parameter für ST nur für einstieg -> Beim Ausstieg fragen ob der bool WWert true ist -> Für weniger und längere Trädes sl_perc = input.float(4.0, "% SL", group="Stop Loss Einstellung") atr_length = input.int(10, "ATR Length", group="Stop Loss Einstellung") atr_mult = input.float(2.0, "ATR Mult", group="Stop Loss Einstellung") sl_absol = input.float(10.0, "Absolute SL", group="Stop Loss Einstellung") //-------------------------// // BACKTESTING RANGE fromDay = input.int(defval=1, title="From Day", minval=1, maxval=31, group="Backtesting Einstellung") fromMonth = input.int(defval=1, title="From Month", minval=1, maxval=12, group="Backtesting Einstellung") fromYear = input.int(defval=2016, title="From Year", minval=1970, group="Backtesting Einstellung") toDay = input.int(defval=1, title="To Day", minval=1, maxval=31, group="Backtesting Einstellung") toMonth = input.int(defval=1, title="To Month", minval=1, maxval=12, group="Backtesting Einstellung") toYear = input.int(defval=2100, title="To Year", minval=1970, group="Backtesting Einstellung") startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00) finishDate = timestamp(toYear, toMonth, toDay, 00, 00) time_cond = time >= startDate and time <= finishDate //-------------------------// // Supertrend calculation [_, direction] = ta.supertrend(factor, atrPeriod) // SL values sl_val = sl_type == "ATR" ? atr_mult * ta.atr(atr_length) : sl_type == "Absolute" ? sl_absol : close * sl_perc / 100 // Init Variables var pos = 0 var float trailing_sl = 0.0 // Signals long_signal = nz(pos[1]) != 1 and high > nz(trailing_sl[1]) short_signal = nz(pos[1]) != -1 and low < nz(trailing_sl[1]) // Calculate SL trailing_sl := short_signal ? high + sl_val : long_signal ? low - sl_val : nz(pos[1]) == 1 ? math.max(low - sl_val, nz(trailing_sl[1])) : nz(pos[1]) == -1 ? math.min(high + sl_val, nz(trailing_sl[1])) : nz(trailing_sl[1]) // Position var pos := long_signal ? 1 : short_signal ? -1 : nz(pos[1]) // Entry logic if ta.change(direction) < 0 and time_cond if tradeDirection == "Both" or tradeDirection == "Long" strategy.entry("Long", strategy.long, stop=trailing_sl) else strategy.close_all("Stop Short") if ta.change(direction) > 0 and time_cond if tradeDirection == "Both" or tradeDirection == "Short" strategy.entry("Short", strategy.short, stop=trailing_sl) else strategy.close_all("Stop Long") // Exit logic: Trailing Stop and Supertrend //if strategy.position_size > 0 and not na(trailing_sl) //strategy.exit("SL-Exit Long", from_entry="Long", stop=trailing_sl) //if strategy.position_size < 0 and not na(trailing_sl) //strategy.exit("SL-Exit Short", from_entry="Short", stop=trailing_sl) // Trailing Stop visualization plot(trailing_sl, linewidth = 2, color = pos == 1 ? color.green : color.red) //plot(not na(trailing_sl) ? trailing_sl : na, color=pos == 1 ? color.green : color.red, linewidth=2, title="Trailing Stop")