基于MACD和RSI的趋势追踪反转策略


创建日期: 2023-12-18 17:53:38 最后修改: 2023-12-18 17:53:38
复制: 0 点击次数: 416
avatar of ChaoZhang ChaoZhang
1
关注
1241
关注者

基于MACD和RSI的趋势追踪反转策略

概述

该策略综合运用MACD,EMA和RSI三种指标,实现趋势追踪和反转交易。当MACD向上通过信号线且收盘价高于EMA均线时生成买入信号;当MACD向下跌破信号线且收盘价低于EMA均线时生成卖出信号,从而捕捉趋势;同时,当RSI达到超买超卖区域时,进行反转交易。

策略原理

  1. 计算MACDdiffs和EMA。

    fastMA = ema(close, fast)  
    slowMA = ema(close, slow)
    macd = fastMA - slowMA
    signal = sma(macd, 9)
    ema = ema(close, input(200))
    
  2. 生成买入信号:MACD差值(macd-signal)上穿0轴且收盘价高于EMA均线。

    delta = macd - signal 
    buy_entry= close>ema and delta > 0
    
  3. 生成卖出信号:MACD差值下破0轴且收盘价低于EMA均线。

    sell_entry = close<ema and delta<0 
    
  4. 当RSI进入超买超卖区域时,进行反转交易。

    if (rsi > 70 or rsi < 30)
        reversal := true
    

优势分析

  1. 结合趋势跟踪和反转交易,既可以追踪主要趋势,也可在反转点位获利。
  2. 利用MACD判断主要趋势方向,避免虚假突破。
  3. 借助EMA过滤掉部分噪音。
  4. RSI指标判断反转点位,增强策略获利空间。

风险分析

  1. 大趋势市场中,反转交易可能招致损失。
  2. 参数设置不当,会增大交易频率和滑点成本。
  3. 反转信号可能发生延迟,错过最佳入场时机。

解决方法:

  1. 优化参数,找到最佳参数组合。
  2. 适当调整反转交易的RSI阈值。
  3. 考虑加入止损以控制损失。

优化方向

  1. 测试不同长度的EMA均线参数。
  2. 优化MACD参数,找到最佳参数组合。
  3. 测试不同的反转RSI阈值。
  4. 考虑加入其他指标进行combinatio组合。

总结

本策略综合运用MACD,EMA和RSI指标,实现了趋势追踪和反转交易的有机结合。MACD判断主要趋势方向,EMA滤波噪音,RSI指标捕捉反转点位。这种多指标组合能更准确判断市场走势,在降低误交易的同时,提高获利概率。当然,参数优化和止损管理等还需进一步完善,以减少不必要损失,使策略更稳健。总的来说,该策略框架合理,有望取得稳定收益。

策略源码
/*backtest
start: 2023-11-17 00:00:00
end: 2023-12-17 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/
// © mbuthiacharles4

//Good with trending markets
//@version=4
strategy("CHARL MACD EMA RSI")

fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)

ema = ema(close, input(200))

rsi = rsi(close, input(14))
//when delta > 0  and close above ema buy

delta = macd - signal

buy_entry= close>ema and delta > 0
sell_entry = close<ema and delta<0 
var bought = false
var sold = false
var reversal = false
if (buy_entry and bought == false and rsi <= 70) 
    strategy.entry("Buy",true , when=buy_entry)
    bought := true
    
strategy.close("Buy",when= delta<0 or rsi > 70)
if (delta<0 and bought==true)
    bought := false

//handle sells

if (sell_entry and sold == false and rsi >= 30)
    strategy.entry("Sell",false , when=sell_entry)
    sold := true

strategy.close("Sell",when= delta>0 or rsi < 30)
if (delta>0 and sold==true)
    sold := false
    
if (rsi > 70 or rsi < 30)
    reversal := true
    placing = rsi > 70 ? high :low
    label.new(bar_index, placing, style=label.style_flag, color=color.blue, size=size.tiny)
if (reversal == true)
    if (rsi < 70 and sold == false and delta < 0)
        strategy.entry("Sell",false , when= delta < 0)
        sold := true
        reversal := false
    else if (rsi > 30 and bought == false and delta > 0)
        strategy.entry("Buy",true , when= delta > 0)
        bought := true
        reversal := false