趋势追踪移动平均RSI策略


创建日期: 2023-11-23 17:13:06 最后修改: 2023-11-23 17:13:06
复制: 0 点击次数: 626
avatar of ChaoZhang ChaoZhang
1
关注
1617
关注者

趋势追踪移动平均RSI策略

概述

趋势追踪移动平均RSI策略是一种同时利用趋势分析和超买超卖指标的股票自动交易策略。该策略运用简单移动平均线判断市场趋势方向,并结合相对强弱指数(RSI)指标发出交易信号,实现对趋势的判断与跟踪。

策略原理

该策略主要由三部分组成:

  1. 趋势判断:计算长期趋势的200日简单移动平均线,计算短期趋势的30日和50日简单移动平均线。当短期移动平均线上穿长期移动平均线时为看涨信号,下穿为看跌信号,判断市场长短期趋势。

  2. 超买超卖判断:计算14日RSI指标,RSI高于80为超买区,低于20为超卖区。当RSI指标从超买区下跌或从超卖区上涨时,发出交易信号。

  3. 入场与出场:当判断到超买超卖信号时,如果与趋势判断的信号方向一致,则入场做多/空。当短期与长期移动平均线发生黄金交叉时,判断趋势反转,此时平仓离场。

通过该策略,可以在股票价格出现反转时及时入场,同时结合趋势判断过滤掉部分噪音交易,在回撤控制方面相对比较优秀。

优势分析

该策略具有以下几点优势:

  1. 结合趋势判断和超买超卖指标,过滤噪音,识别行情反转点。
  2. 同时考量长短期两个时间段内的趋势方向,判断更为准确。
  3. 采用移动平均线作为止损方式,可以根据市场波动程度来设定止损点。
  4. 入场条件严格,可以有效避免假突破。

风险及解决方法

该策略也存在一些风险:

  1. 如果行情出现长期震荡,将打开大量无效交易。解决方法是加入更多过滤条件,避免无谓交易。
  2. 存在一定的时间滞后风险。解决方法是适当缩短移动平均线的周期参数。
  3. RSI指标发出信号的效果会受到股票和市场的影响。解决方法是结合K线形态等更多因素判断效果。

优化方向

该策略还可从以下几个方面进行优化:

  1. 加入更多滤波条件,如成交量,K线形态等,进一步提升信号的有效性。
  2. 优化移动平均线和RSI的参数周期,使其更符合不同股票的特征。
  3. 建立动态移动平均线,根据市场波动性和风险偏好自动调整参数。
  4. 采用机器学习等更为先进的技术判断行情趋势,提高判断准确率。

总结

趋势追踪移动平均RSI策略总体来说是一个非常实用的策略思路,同时结合趋势分析和超买超卖指标,在一定程度上过滤了市场噪音,使交易信号更加准确有效。通过不断优化手段和参数,该策略可以成为一个稳定盈利的长期交易系统。

策略源码
/*backtest
start: 2022-11-16 00:00:00
end: 2023-11-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mattehalen

// INPUT per TIMEFRAME
// 5min     = Legnth = 9, Source = ohlc4,MaxLoss = 1000 TrendMA = 200, ShortMA = 4, LongMA = 10
// 30min    = Legnth = 7, Source = ohlc4,MaxLoss = 1000 TrendMA = 200, ShortMA = 10, LongMA = 20

strategy("Mathias & Christer Timeframe RSI", shorttitle="M&C_RSI",overlay=true, process_orders_on_close = true, default_qty_type =  strategy.percent_of_equity, default_qty_value = 100)
len = input(9, title="Length", type=input.integer)
src = input(ohlc4, title="Source", type=input.source)
//show4h = input(true, title="show 4h", type=input.bool)
maxLoss = input(3000)

rsiCurrent = rsi(src, len)
//rsi4h = security(syminfo.ticker, "240", rsi(src, len))
rsi4h   = rsi(src, len)

//--------------------------------------------------
//MA
trendMAInput = input(200, title="trendMA", type=input.integer)
shortMAInput = input(30, title="shortMA", type=input.integer)
longMAInput = input(50, title="longMA", type=input.integer)

trendMA = ema(close,trendMAInput)
shortMA = ema(close,shortMAInput)
longMA  = ema(close,longMAInput)
plot(trendMA, color=color.black, linewidth=5)
plot(shortMA, color=color.red, linewidth=2)
plot(longMA, color=color.green, linewidth=2)
bgcolor(crossunder(shortMA,longMA) ? color.black : na, transp=10)

//--------------------------------------------------
//RSI
BuySignalBarssince = barssince(rsi4h[1]<rsi4h[0] and rsi4h[1]<20)
BuySignal       = (rsi4h[1]<rsi4h[0] and rsi4h[1]<20 and BuySignalBarssince[1]>10)
BuySignalOut   = crossunder(longMA[1],shortMA[1])
bgcolor(BuySignal ? color.green : na, transp=70)
bgcolor(BuySignalOut ? color.green : na, transp=10)



SellSignalBarssince = barssince(rsi4h[1]>rsi4h[0] and rsi4h[1]>80)
SellSignal      = (rsi4h[1]>rsi4h[0] and rsi4h[1]>80 and SellSignalBarssince[1]>10)
SellSignalOut   = crossunder(shortMA[1],longMA[1])
bgcolor(SellSignal ? color.red : na, transp=70)
bgcolor(SellSignalOut ? color.red : na, transp=10)


if BuySignal
    strategy.close("short", comment = "Exit short")
    strategy.entry("long", true)
    strategy.exit("Max Loss", "long", loss = maxLoss)

if BuySignalOut
    strategy.close("long", comment = "Exit Long")
if SellSignal
    // Enter trade and issue exit order on max loss.
    strategy.close("long", comment = "Exit Long")
    strategy.entry("short", false)
    strategy.exit("Max Loss", "short", loss = maxLoss)
if SellSignalOut
    // Force trade exit.
    strategy.close("short", comment = "Exit short")
    
//--------------------------------------------------
//ATR
MyAtr = atr(10)
AtrFactor = 10
mySLBuy  = close[BuySignalBarssince]
mySLSell = close[SellSignalBarssince]

plotchar(BuySignal, "BuySignal", "⬆", location.belowbar, color.lime,size =size.huge )
plotchar(BuySignalOut, "BuySignalOut", "█", location.belowbar, color.lime,size =size.small)
plotchar(SellSignal, "SellSignal", "⬇", location.abovebar ,color.red,size =size.huge)
plotchar(SellSignalOut, "SellSignalOut", "█", location.abovebar, color.red,size =size.small)