
该策略结合了扩散通道指标和MACD指标对趋势进行判断,属于典型的趋势跟踪策略。当价格突破上轨且MACD指标出现金叉时做多,当价格跌破下轨且MACD指标出现死叉时做空,使用ATR指标计算止损位。
计算MACD指标,包括快线、慢线和histogram。
计算上下扩散通道。上轨为N日内的最高价,下轨为N日内的最低价。
当价格突破上轨,且MACD快线向上突破慢线时,做多。
当价格跌破下轨,且MACD快线向下突破慢线时,做空。
使用ATR指标计算本策略的止损位,设定为价格到止损位距离为ATR的值乘以一个系数。
当价格出现反转信号时,平掉当前仓位。
该策略结合趋势判断指标和通道指标,可以有效跟踪趋势。MACD指标可以判断价格趋势和力度,扩散通道指标判断方向。ATR止损可以限制单笔损失。
优势如下:
策略参数简单,容易实现。
可以顺势开仓,及时捕捉趋势机会。
ATR止损可以控制风险。
回撤可以得到一定控制。
该策略也存在一些风险:
扩散通道参数设置不当可能造成虚假信号。
MACD参数设置不当也可能导致 Viticulture Administration System 提示信号滞后。
止损设置过大可能造成亏损扩大。
行情剧烈反转时,可能导致亏损。
该策略容易产生过度交易。
对应解决方法:
优化参数,谨慎选股。
严格止损,追踪止损。
适当调整仓位管理。
该策略可以从以下方面进行优化:
优化MACD参数,提高指标的灵敏度。
优化止损算法,使止损更贴近价格。
增加仓位管理机制,根据趋势强弱调整仓位。
增加过滤条件,避免虚假信号。
增加对交易品种的选择标准。
增加对交易时间段的判断。
该策略整体来说是一个典型的趋势跟踪策略。它融合了扩散通道指标判断趋势方向和MACD指标判断趋势力度。可以顺势而为,有效控制风险。通过优化参数设置、止损方式、仓位管理等方面,可以进一步增强策略的稳定性和收益率。该策略适合对趋势判断要求较高的投资者使用。
/*backtest
start: 2023-10-15 00:00:00
end: 2023-11-14 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Robrecht99
//@version=5
strategy("Trend Following with Donchian Channels and MACD", overlay=false, margin_long=100, margin_short=100, pyramiding=3)
// MACD //
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
col_macd = input(#2962FF, "MACD Line ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
// Donchian Channels //
Length1 = input.int(title="Length Upper Channel", defval=50, minval=1, group="Donchian Channels Inputs")
Length2 = input.int(title="Length Lower Channel", defval=50, minval=1, group="Donchian Channels Inputs")
h1 = ta.highest(high[1], Length1)
l1 = ta.lowest(low[1], Length2)
fillColor = input.color(color.new(color.purple, 95), title = "Fill Color", group = "Donchian Channels Inputs")
upperColor = input.color(color.new(color.orange, 0), title = " Color Upper Channel", group = "Donchian Channels Inputs", inline = "upper")
lowerColor = input.color(color.new(color.orange, 0), title = " Color Lower Channel", group = "Donchian Channels Inputs", inline = "lower")
u = plot(h1, "Upper", color=upperColor)
l = plot(l1, "Lower", color=upperColor)
fill(u, l, color=fillColor)
//ATR and Position Size //
strategy.initial_capital = 50000
length = input.int(title="ATR Period", defval=14, minval=1, group="ATR Inputs")
risk = input(title="Risk Per Trade", defval=0.01, group="ATR Inputs")
multiplier = input(title="ATR Multiplier", defval=2, group="ATR Inputs")
atr = ta.atr(length)
amount = (risk * strategy.initial_capital / (multiplier * atr))
// Buy and Sell Conditions //
entrycondition1 = ta.crossover(macd, signal)
entrycondition2 = macd > signal
entrycondition3 = macd and signal > hist
sellcondition1 = ta.crossover(signal, macd)
sellcondition2 = signal > macd
sellcondition3 = macd and signal < hist
// Buy and Sell Signals //
if (close > h1 and entrycondition2 and entrycondition3)
strategy.entry("long", strategy.long, qty=amount)
stoploss = close - atr * 4
strategy.exit("exit sl", stop=stoploss, trail_offset=stoploss)
if (sellcondition1 and sellcondition2 and sellcondition3)
strategy.close(id="long")
if (close < l1 and sellcondition2 and sellcondition3)
strategy.entry("short", strategy.short, qty=amount)
stoploss = close + atr * 4
strategy.exit("exit sl", stop=stoploss, trail_offset=stoploss)
if (entrycondition1 and entrycondition2 and entrycondition3)
strategy.close(id="short")