
趋势追踪移动平均RSI策略是一种同时利用趋势分析和超买超卖指标的股票自动交易策略。该策略运用简单移动平均线判断市场趋势方向,并结合相对强弱指数(RSI)指标发出交易信号,实现对趋势的判断与跟踪。
该策略主要由三部分组成:
趋势判断:计算长期趋势的200日简单移动平均线,计算短期趋势的30日和50日简单移动平均线。当短期移动平均线上穿长期移动平均线时为看涨信号,下穿为看跌信号,判断市场长短期趋势。
超买超卖判断:计算14日RSI指标,RSI高于80为超买区,低于20为超卖区。当RSI指标从超买区下跌或从超卖区上涨时,发出交易信号。
入场与出场:当判断到超买超卖信号时,如果与趋势判断的信号方向一致,则入场做多/空。当短期与长期移动平均线发生黄金交叉时,判断趋势反转,此时平仓离场。
通过该策略,可以在股票价格出现反转时及时入场,同时结合趋势判断过滤掉部分噪音交易,在回撤控制方面相对比较优秀。
该策略具有以下几点优势:
该策略也存在一些风险:
该策略还可从以下几个方面进行优化:
趋势追踪移动平均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)