该策略综合运用MACD,EMA和RSI三种指标,实现趋势追踪和反转交易。当MACD向上通过信号线且收盘价高于EMA均线时生成买入信号;当MACD向下跌破信号线且收盘价低于EMA均线时生成卖出信号,从而捕捉趋势;同时,当RSI达到超买超卖区域时,进行反转交易。
计算MACDdiffs和EMA。
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)
ema = ema(close, input(200))
生成买入信号:MACD差值(macd-signal)上穿0轴且收盘价高于EMA均线。
delta = macd - signal
buy_entry= close>ema and delta > 0
生成卖出信号:MACD差值下破0轴且收盘价低于EMA均线。
sell_entry = close<ema and delta<0
当RSI进入超买超卖区域时,进行反转交易。
if (rsi > 70 or rsi < 30)
reversal := true
解决方法:
本策略综合运用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