Skip to content

Latest commit

 

History

History
188 lines (122 loc) · 8.12 KB

基于MACD的多时框架交易策略Multi-Timeframe-Trading-Strategy-Based-on-MACD.md

File metadata and controls

188 lines (122 loc) · 8.12 KB

Name

基于MACD的多时框架交易策略Multi-Timeframe-Trading-Strategy-Based-on-MACD

Author

ChaoZhang

Strategy Description

IMG

[trans]

概述:该策略利用MACD指标在多个时间框架上生成交易信号,实现趋势跟踪。其基本思路是在高周期时间框架确认趋势方向,然后在低周期时间框架寻找具体的入场时机。

策略原理: 该策略使用MACD指标的差离线和信号线的交叉来判断趋势方向。具体而言,它在高周期时间框架(默认60分钟)计算MACD差离线和信号线。当差离线上穿信号线时产生买入信号,下穿信号线时产生卖出信号,用于确认总体趋势方向。

然后策略会在低周期时间框架(当前周期)对MACD进行计算,当差离线和信号线发生交叉时,进入相应的仓位。所以高周期用于判断趋势方向,低周期用于寻找具体的入场点位。

该策略还使用柱状图的颜色变化来辅助判断趋势,绿色柱表示正在上涨,红色柱表示正在下跌。

优势分析:

  1. 多时框架设计,高周期确定趋势方向,低周期寻找入场点位,系统性好。

  2. 利用MACD指标的交叉来判断买卖时机,指标参数经过优化,信号比较可靠。

  3. 柱状图颜色辅助判断目前趋势状态,形成多重验证,提高决策的准确性。

  4. 自动追踪趋势运行,无需过多人工干预,减少情绪化判断错误。

风险分析:

  1. MACD作为跟踪中长期趋势的指标,在短期内可能产生错误信号导致不必要的损失。

  2. 多时框架策略由于需要同时考量多个周期,参数优化和测试难度较大。

  3. 策略没有设置止损,可能带来较大亏损。

优化方向:

  1. 优化MACD的参数,寻找最佳的参数组合。

  2. 增加止损机制,限制最大损失。

  3. 评估是否需要加入其它指标进行信号过滤,提高信号质量。

  4. 测试不同的时框架组合,找到最优时框架匹配。

总结: 本策略整体设计系统性好,同时结合MACD指标的多重优点,能够有效跟踪中长线趋势。但由于未设置止损,难以避免短期损失扩大的风险,这是需要进一步优化的方向。总的来说,该策略以其较强的趋势跟踪能力,为量化交易提供了一个高质量的选股和决策框架。通过不断优化参数和模型,有望进一步扩大获利空间,提高策略的稳定性。

||

Overview: This strategy uses the MACD indicator to generate trading signals across multiple time frames to track trends. The core idea is to confirm the trend direction in higher time frames and then look for specific entry opportunities in lower time frames.

Strategy Principle: The strategy uses the crossovers between the MACD difference line and signal line to determine the trend direction. Specifically, it calculates the MACD difference and signal lines in higher time frames (default 60min). When the difference line crosses above the signal line, a buy signal is generated. When crossing below, a sell signal is generated to confirm the overall trend direction.

The strategy then calculates the MACD in lower time frames (current period) and enters positions when crossovers happen between the difference and signal lines. So higher time frames are used to judge trend direction and lower ones are used to find specific entry points.

The strategy also uses the color change of the MACD histogram to assist in judging the trend. Green bars indicate an uptrend while red bars indicate a downtrend.

Advantage Analysis:

  1. Multi-timeframe design confirms trend in higher TF and finds entries in lower TF, improving systemacity.

  2. Uses MACD crossovers to determine entries and exits, parameters optimized for reliable signals.

  3. Histogram color assists in determining current trend status, improving decision accuracy.

  4. Automatically tracks trends, reduces emotional errors.

Risk Analysis:

  1. As a trend-following indicator for medium-long term trends, MACD can produce false signals in the short term leading to unnecessary losses.

  2. Multi-timeframe strategies are harder to optimize and test as multiple periods need to be considered simultaneously.

  3. No stop loss is set which poses the risk of large losses.

Optimization Directions:

  1. Optimize MACD parameters to find best combinations.

  2. Add stop loss to limit max loss.

  3. Evaluate other filters to improve signal quality.

  4. Test different time frame combinations to find optimal matches.

Summary: The strategy is well designed systemactically and combines multiple strengths of the MACD indicator to effectively track medium-long term trends. However, the lack of a stop loss mechanism means short term losses can easily expand, which needs to be improved. Overall, with strong trend following capabilities, the strategy provides a high-quality framework for stock picking and decision making in quantitative trading. Further optimizations in parameters and models can expand profit potential and improve stability.

[/trans]

Strategy Arguments

Argument Default Description
v_input_1 true Use Current Chart Resolution?
v_input_2 60 Use Different Timeframe? Uncheck Box Above
v_input_3 true Show MacD & Signal Line? Also Turn Off Dots Below
v_input_4 true Show Dots When MacD Crosses Signal Line?
v_input_5 true Show Histogram?
v_input_6 true Change MacD Line Color-Signal Line Cross?
v_input_7 true MacD Histogram 4 Colors?
v_input_8 12 fastLength
v_input_9 26 slowLength
v_input_10 9 signalLength

Source (PineScript)

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

//@author : SudeepBisht
//@version=2
strategy(title="SB_CM_MacD_Ult_MTF", shorttitle="SB_CM_Ult_MacD_MTF")
source = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above",  defval="60")
smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
sh = input(true, title="Show Histogram?")
macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
hist_colorChange = input(true,title="MacD Histogram 4 Colors?")

res = useCurrentRes ? timeframe.period : resCustom

fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)

macd = fastMA - slowMA
signal = sma(macd, signalLength)
hist = macd - signal

outMacD = request.security(syminfo.tickerid, res, macd)
outSignal = request.security(syminfo.tickerid, res, signal)
outHist = request.security(syminfo.tickerid, res, hist)

histA_IsUp = outHist > outHist[1] and outHist > 0
histA_IsDown = outHist < outHist[1] and outHist > 0
histB_IsDown = outHist < outHist[1] and outHist <= 0
histB_IsUp = outHist > outHist[1] and outHist <= 0

//MacD Color Definitions
macd_IsAbove = outMacD >= outSignal
macd_IsBelow = outMacD < outSignal

plot_color = hist_colorChange ? histA_IsUp ? aqua : histA_IsDown ? blue : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
macd_color = macd_colorChange ? macd_IsAbove ? lime : red : red
signal_color = macd_colorChange ? macd_IsAbove ? yellow : yellow : lime

circleYPosition = outSignal
 
plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=4)
plot(smd and outSignal ? outSignal : na, title="Signal Line", color=signal_color, style=line ,linewidth=2)
plot(sh and outHist ? outHist : na, title="Histogram", color=plot_color, style=histogram, linewidth=4)
plot(sd and cross(outMacD, outSignal) ? circleYPosition : na, title="Cross", style=circles, linewidth=4, color=macd_color)
// hline(0, '0 Line', linestyle=solid, linewidth=2, color=white)

macd_chk=smd and outMacD ? outMacD : na
checker=smd and outSignal ? outSignal : na
if (crossover(macd_chk,checker))
    strategy.entry("BBandLE", strategy.long, comment="BBandLE")

if (crossunder(macd_chk, checker))
    strategy.entry("BBandSE", strategy.short, comment="BBandSE")

Detail

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

Last Modified

2024-01-12 11:46:59