Skip to content

Latest commit

 

History

History
251 lines (184 loc) · 10.5 KB

单指数平滑移动平均线附带尾随止损的趋势跟踪策略Single-Exponential-Smoothed-Moving-Average-with-Trailing-Stop-Loss-Trend-Following-Strategy.md

File metadata and controls

251 lines (184 loc) · 10.5 KB

Name

单指数平滑移动平均线附带尾随止损的趋势跟踪策略Single-Exponential-Smoothed-Moving-Average-with-Trailing-Stop-Loss-Trend-Following-Strategy

Author

ChaoZhang

Strategy Description

IMG [trans]

概述

该策略结合使用了单指数平滑移动平均线(SESMA)和附带尾随止损的唐奇阶梯围绕机制,形成一个非常稳定和高效的趋势跟踪策略。SESMA作为主线,用于识别价格趋势方向。尾随止损机制则可有效降低策略风险,同时保护策略利润。

策略原理

该策略由两个核心指标组成:

  1. 单指数平滑移动平均线(SESMA):SESMA借鉴了EMA的思想,同时改进了参数,使得曲线更加光滑,延迟降低。通过SESMA的方向和价位关系来判断价格趋势。

  2. 尾随止损机制:结合最高价、最低价以及ATR指标,实时计算出多头和空头的止损线。这是一个动态调整的止损机制,可根据市场波动性和趋势调整止损幅度。止损线同价位的关系用于判断平仓退出的时机。

该策略的入场依据是价格突破SESMA。而出场信号则由止损线来触发。可设定是否显示标记。

策略优势

  1. SESMA计算方法改进,可有效减少延迟,提高顺势捕获能力。
  2. 尾随止损机制可根据实时波动调整止损幅度,避免止损过于宽松或过于紧挨。
  3. 附带视觉辅助判断 Entry 和 Exit 时机的标记。
  4. 可自定义参数,适用于不同品种和参数优化。

风险及优化方向

  1. 在趋势反转时,可能出现止损被触发导致过早退出。可适当放宽止损幅度。
  2. SESMA 参数可进行优化,找到最佳长度。
  3. ATR 参数也可测试不同的周期长度。
  4. 测试是否显示标记的效果。

总结

该策略整合了趋势判断与风险控制指标,形成一个较为稳健的趋势跟踪策略。相比于简单的移动平均线策略,该策略可更加灵活地捕捉趋势,同时可减少回撤。通过参数优化,可使策略在不同市场中达到更好的效果。

||

Overview

This strategy combines Single Exponential Smoothed Moving Average (SESMA) and a trailing stop loss mechanism with the Chandelier Exit to form a very stable and efficient trend following strategy. SESMA serves as the main line to identify the trend direction of prices. The trailing stop loss mechanism can effectively reduce the risk of the strategy while protecting profits.

Strategy Logic

The strategy consists of two core indicators:

  1. Single Exponential Smoothed Moving Average (SESMA): SESMA draws on the idea of EMA and improves the parameters to make the curve smoother and reduce lag. It uses the direction and level of SESMA to judge price trends.

  2. Trailing stop loss mechanism: Combined with highest price, lowest price and ATR indicator to calculate long and short stop loss lines in real time. It is a dynamic adjustable stop loss mechanism that can adjust stop loss range based on market volatility and trends. The relationship between stop loss line and price level is used to determine the timing of exit orders.

The entry signal of this strategy is triggered when price crosses over SESMA. The exit signal is generated by the stop loss lines. Option to show entry/exit markings.

Advantages of the Strategy

  1. The SESMA calculation method is improved and can effectively reduce lag and improve trend-following ability.
  2. The trailing stop loss mechanism can adjust the stop loss range according to real-time fluctuations to avoid excessively wide or too tight stop loss.
  3. Comes with visual assistances to mark Entry and Exit signals.
  4. Customizable parameters suitable for different products and parameter optimizations.

Risks and Optimization Directions

  1. Stop loss may be triggered prematurely during trend reversals. Can appropriately loosen the stop loss range.
  2. SESMA parameters can be optimized to find the best length.
  3. The period parameter of ATR can also be tested.
  4. Test the effects of showing markings.

Conclusion

This strategy integrates trend judging and risk control indicators to form a relatively robust trend following strategy. Compared to simple moving average strategies, this strategy can capture trends more flexibly while reducing drawdowns. Through parameter optimization, the strategy can achieve better results in different markets.

[/trans]

Strategy Arguments

Argument Default Description
v_input_1 true ATR Period
v_input_float_1 2 ATR Multiplier
v_input_2 false Show Buy/Sell Labels ?
v_input_3 true Show Signal Labels ?
v_input_4 true Use Close Price for Extrema ?
v_input_5 false Enable Rising/Decreasing Highlightning
v_input_6 50 ZLSMA Length
v_input_7 false Offset

Source (PineScript)

/*backtest
start: 2023-12-31 00:00:00
end: 2024-01-07 00:00:00
period: 10m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/


//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © simwai
strategy('Chandelier Exit ZLSMA Strategy', shorttitle='CE_ZLSMA', overlay = true, initial_capital = 1000, default_qty_value = 10, default_qty_type = strategy.percent_of_equity, calc_on_every_tick = false, process_orders_on_close = true, commission_value = 0.075)

// -- Colors --
color maximumYellowRed = color.rgb(255, 203, 98) // yellow
color rajah = color.rgb(242, 166, 84) // orange
color magicMint = color.rgb(171, 237, 198)
color languidLavender = color.rgb(232, 215, 255)
color maximumBluePurple = color.rgb(181, 161, 226)
color skyBlue = color.rgb(144, 226, 244)
color lightGray = color.rgb(214, 214, 214)
color quickSilver = color.rgb(163, 163, 163)
color mediumAquamarine = color.rgb(104, 223, 153)
color carrotOrange = color.rgb(239, 146, 46)

// -- Inputs --
length = input(title='ATR Period', defval=1)
mult = input.float(title='ATR Multiplier', step=0.1, defval=2)
showLabels = input(title='Show Buy/Sell Labels ?', tooltip='Created by Chandelier Exit (CE)', defval=false)
isSignalLabelEnabled = input(title='Show Signal Labels ?', defval=true)
useClose = input(title='Use Close Price for Extrema ?', defval=true)
zcolorchange = input(title='Enable Rising/Decreasing Highlightning', defval=false)
zlsmaLength = input(title='ZLSMA Length', defval=50)
offset = input(title='Offset', defval=0)

// -- CE - Credits to @everget --
float haClose = float(1) / 4 * (open[1] + high[1] + low[1] + close[1])
atr = mult * ta.atr(length)[1]

longStop = (useClose ? ta.highest(haClose, length) : ta.highest(haClose, length)) - atr
longStopPrev = nz(longStop[1], longStop)
longStop := haClose > longStopPrev ? math.max(longStop, longStopPrev) : longStop

shortStop = (useClose ? ta.lowest(haClose, length) : ta.lowest(haClose, length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := haClose < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop

var int dir = 1
dir := haClose > shortStopPrev ? 1 : haClose < longStopPrev ? -1 : dir

buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal and showLabels ? longStop : na, title='Buy Label', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=mediumAquamarine, textcolor=color.white)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal and showLabels ? shortStop : na, title='Sell Label', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=carrotOrange, textcolor=color.white)

changeCond = dir != dir[1]

// -- ZLSMA - Credits to @netweaver2011 --
lsma = ta.linreg(haClose, zlsmaLength, offset)
lsma2 = ta.linreg(lsma, zlsmaLength, offset)
eq = lsma - lsma2
zlsma = lsma + eq

zColor = zcolorchange ? zlsma > zlsma[1] ? magicMint : rajah : languidLavender
plot(zlsma, title='ZLSMA', linewidth=2, color=zColor)

// -- Signals --
var string isTradeOpen = ''
var string signalCache = ''

bool enterLong = buySignal and ta.crossover(haClose, zlsma) 
bool exitLong = ta.crossunder(haClose, zlsma) 
bool enterShort = sellSignal and ta.crossunder(haClose, zlsma)
bool exitShort = ta.crossover(haClose, zlsma)

if (signalCache == 'long entry')
    signalCache := ''
    enterLong := true
else if (signalCache == 'short entry')
    signalCache := ''
    enterShort := true

if (isTradeOpen == '')
    if (exitShort and (not enterLong))
        exitShort := false
    if (exitLong and (not enterShort))
        exitLong := false   
    if (enterLong and exitShort)
        isTradeOpen := 'long'
        exitShort := false
    else if (enterShort and exitLong)
        isTradeOpen := 'short'
        exitLong := false
    else if (enterLong)
        isTradeOpen := 'long'
    else if (enterShort)
        isTradeOpen := 'short'
else if (isTradeOpen == 'long')
    if (exitShort)
        exitShort := false
    if (enterLong)
        enterLong := false
    if (enterShort and exitLong)
        enterShort := false
        signalCache := 'short entry'
    if (exitLong)
        isTradeOpen := ''
else if (isTradeOpen == 'short')
    if (exitLong)
        exitLong := false
    if (enterShort)
        enterShort := false
    if (enterLong and exitShort)
        enterLong := false
        signalCache := 'long entry'
    if (exitShort)
        isTradeOpen := ''

plotshape((isSignalLabelEnabled and enterLong) ? zlsma : na, title='LONG', text='L', style=shape.labelup, color=mediumAquamarine, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and enterShort) ? zlsma : na, title='SHORT', text='S', style=shape.labeldown, color=carrotOrange, textcolor=color.white, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitLong) ? zlsma : na, title='LONG EXIT', style=shape.circle, color=magicMint, size=size.tiny, location=location.absolute)
plotshape((isSignalLabelEnabled and exitShort) ? zlsma : na, title='SHORT EXIT', style=shape.circle, color=rajah, size=size.tiny, location=location.absolute)

barcolor(color=isTradeOpen == 'long' ? mediumAquamarine : isTradeOpen == 'short' ? carrotOrange : na)

// -- Long Exits --
if (exitLong and strategy.position_size > 0)
    strategy.close('long', comment='EXIT_LONG')

// -- Short Exits --
if (exitShort and strategy.position_size < 0)
    strategy.close('short', comment='EXIT_SHORT')

// -- Long Entries --
if (enterLong)
    strategy.entry('long', strategy.long, comment='ENTER_LONG')

// -- Short Entries --
if (enterShort)
    strategy.entry('short', strategy.short, comment='ENTER_SHORT')

Detail

https://www.fmz.com/strategy/438023

Last Modified

2024-01-08 11:37:44