策略知识库.EN
策略

基于SMA和ATR的趋势追踪策略SMA-and-ATR-Based-Trend-Tracking-Strategy

ChaoZhang · PineScript · 趋势跟踪 · 2024-01-18 16:04:51

全部策略 →
过滤
仓位
—
出场
—

概述

本策略利用SMA指标判断价格趋势方向,并利用ATR指标设置止损位置来追踪趋势。当价格跌破上升趋势时做空,当价格突破下跌趋势时做多,实现趋势交易。

总结

本策略整体思路清晰,通过SMA判断趋势方向,并利用ATR进行趋势追踪,回撤控制能力良好,适合中长线趋势交易。但实盘中仍需要适当调整参数,并防范过优化的风险。

其他

本策略名称为基于SMA和ATR的趋势追踪策略。 (1)当收盘价上涨且高于SMA时,做多 (2)当收盘价下跌且低于SMA时,做空 利用ATR指标的数值乘以设置的止损倍数作为止损位置。 每根K线收盘后检查止损位置,并更新为更靠近当前价位的止损值。 价格触碰止损线后主动止损退出。 利用ATR指标的动态止损设定能够实现对趋势的自动追踪。 严格的止损规则有助于控制单笔交易的最大回撤。 只有3个参数,方便调整和优化。 如果止损倍数设置过大,可能导致止损位置过于宽松,从而增加回撤。 价格出现假突破时,可能导致错开趋势方向,应结合其他指标过滤信号。 过度依赖参数优化可能导致曲线优化。应谨慎评估参数稳定性。 可以测试其他型的止损算法,如移动止损、比例止损等。 可以加入其他指标过滤假突破。例如增加成交量条件等。 通过历史回测评估参数对不同品种和周期的适应性。

参数

参数默认值说明
v_input_int_1100SMA Length
v_input_int_210ATR Length
v_input_float_14Stop Offset Multiple
源码(82 行)
/*backtest
start: 2024-01-16 00:00:00
end: 2024-01-16 17:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © omererkan

//@version=5
strategy(title="SMA with ATR", overlay=true)

smaLen = input.int(100, title="SMA Length")

atrLen     = input.int(10, title="ATR Length")
stopOffset = input.float(4, title="Stop Offset Multiple", step=0.25)


smaValue  = ta.sma(close, smaLen)
stopValue = ta.atr(atrLen) * stopOffset


lowerCloses = close < close[1] and 
     close[1] < close[2] and
     close[2] < close[3]

enterLong = close > smaValue and 
     lowerCloses


longStop = 0.0
longStop := if enterLong and strategy.position_size < 1
    close - stopValue
else
    math.max(close - stopValue, longStop[1])


higherCloses = close > close[1] and 
     close[1] > close[2] and
     close[2] > close[3]

enterShort = close < smaValue and 
     higherCloses


shortStop = 0.0
shortStop := if enterShort and strategy.position_size > -1
    close + stopValue
else
    math.min(close + stopValue, shortStop[1])


plot(smaValue, color=#4169e1, linewidth=2, title="SMA")

plot(strategy.position_size > 0 ? longStop : na, color=color.lime,
     style=plot.style_linebr, title="Long stop", linewidth=2)

plot(strategy.position_size < 0 ? shortStop : na, color=color.red,
     style=plot.style_linebr, title="Short stop", linewidth=2)


if enterLong
    strategy.entry("EL", strategy.long)

if enterShort
    strategy.entry("ES", strategy.short)


if strategy.position_size > 0
    strategy.exit("SL Long", from_entry="EL", stop=longStop)

if strategy.position_size < 0
    strategy.exit("SL Short", from_entry="ES", stop=shortStop)


if enterLong
    strategy.cancel("Exit Short")

if enterShort
    strategy.cancel("Exit Long")