RSI指标背离交易策略


创建日期: 2023-10-25 16:47:14 最后修改: 2023-10-25 16:47:14
复制: 0 点击次数: 556
avatar of ChaoZhang ChaoZhang
1
关注
1241
关注者

RSI指标背离交易策略

概述

RSI指标背离交易策略通过识别RSI指标与价格走势之间的背离,给出买入和卖出信号。该策略同时具有止损、止盈、追踪止损等功能,可有效控制风险。

原理

该策略主要基于RSI指标的背离来识别交易机会。具体来说,策略首先计算一定周期内的RSI值,然后画出RSI指标的趋势线。同时,策略还画出价格的趋势线。当RSI线与价格线出现背离时,即RSI上涨而价格下跌或RSI下跌而价格上涨,策略判断可能出现转折,产生交易信号。

如果识别到RSI线低点上升且价格线高点下跌的情况,则给出买入信号。如果识别到RSI线高点下降且价格线低点上涨的情况,则给出卖出信号。一旦形成交易信号,策略即可按照RSI值的大小进行适量交易。

此外,策略还设置了止损、止盈和追踪止损功能。止损可以控制亏损风险,止盈可以锁定利润,追踪止损可以让利润继续运行。这些设置可以有效管理每笔交易的风险。

优势

这种RSI背离交易策略具有以下优势:

  1. 通过捕捉RSI指标背离,可以提早发现价格转折。

  2. RSI指标使用广泛,大多数交易软件都内置了RSI指标。该策略适用性强。

  3. RSI指标参数设置灵活,可根据市场调整观察周期,适应不同行情。

  4. 结合止损、止盈和追踪止损设置,可以有效控制每笔交易的风险。

  5. 策略交易信号频率适中,避免过于密集交易。

  6. 策略思路清晰易懂,方便计算机程序实现。

风险

该策略也存在一些风险:

  1. RSI背离不是百分之百可靠,可能会出现假信号。需要结合其他指标过滤信号。

  2. 趋势行情中,RSI背离信号可能失效,应该避开使用。

  3. RSI参数设置不当也会影响策略效果。周期设置过短则会增加交易频率和风险。

  4. 止损点设置过小,可能会过早止损;止损点过大,则无法有效控制风险。需要权衡设置。

  5. 追踪止损在价格剧烈波动时,可能会过早止损。需要结合市场波动率设定合理的追踪止损距离。

对应风险可以通过以下措施加以缓解:

  1. 增加其他指标,如MACD、布林线等进行信号过滤,减少假信号。

  2. 只在盘整震荡市中使用该策略,避开明显趋势行情。

  3. 优化RSI参数设置,选取最佳周期长度。同时测试不同市场的参数偏好。

  4. 根据历史回测数据设定合理的止损和止盈位置。

  5. 根据市场波动率和风险偏好调整追踪止损的幅度。

优化方向

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

  1. 增加其他指标判断来过滤交易信号,提高信号的可靠性。

  2. 利用机器学习技术来自动优化RSI参数设置。

  3. 根据不同市场行情型态,设计动态止损算法。如在震荡行情中扩大止损位,在趋势行情中缩小止损位。

  4. 设计动态仓位管理算法,根据市场波动率等因素来调整每次交易的仓位大小。

  5. 在追踪止损中引入波动率概念,根据价格波动强度来设定追踪止损距离。

  6. 尝试将策略部署至其他品种,如外汇和加密货币等市场。

  7. 构建量化交易系统,实现策略的自动化交易。

总结

该RSI背离交易策略通过捕捉RSI指标与价格走势之间的背离来产生交易信号。策略优势在于简单清晰,容易实现自动化。同时,止损、止盈和追踪止损设置也能有效控制风险。但策略也存在一些局限,需要多指标组合验证信号,并不适用于强趋势行情。策略可以从优化参数设置、增加信号过滤以及动态止损等方面进行改进。如果系统化实现,该策略可作为盘整震荡行情下的辅助策略之一。

策略源码
/*backtest
start: 2023-09-24 00:00:00
end: 2023-10-24 00:00:00
period: 4h
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/
// © faytterro

//@version=5
// strategy("RSI Divergence Strategy", overlay=true, scale = scale.none)
rsilen=input.int(14, title="rsi length")
rsisrc=input(close, title="source")
x=ta.rsi(rsisrc,rsilen)
len=input.int(14, title="RSI Divergence length", maxval=500)
tpb = input.float(25, title="take profit", group = "buy", step = 0.5)
sb = input.float(5, title="stop", group = "buy", step = 0.5)
tsb = input.float(0.25, title="trailing stop", group = "buy", step = 0.5)
tps = input.float(25, title="take profit", group = "sell", step = 0.5)
ss =input.float(5, title="stop", group = "sell", step = 0.5)
tss = input.float(0.25, title="trailing stop", group = "sell", step = 0.5)
src=close
extrapolation=0
zoom=input.int(0, title="zoom", maxval=27, minval=-27)
hline(300-zoom*10, color=color.rgb(54, 58, 69, 100))
hline(10, color=color.rgb(54, 58, 69, 100))
// for ax+b
xo=0.0
yo=0.0
xyo=0.0
xxo=0.0
for i=0 to len-1
    xo:= xo + i/(len)
    yo:= yo + x[len-1-i]/(len)
    xyo:= xyo + i*x[len-1-i]/(len)
    xxo:= xxo + i*i/(len)
dnm=ta.lowest(low,200)
dizi=array.new_float(len*2+1+extrapolation)
// linedizi=array.new_line()
a=(xo*yo-xyo)/(xo*xo-xxo)
b=yo-a*xo
for i=0 to len-1+extrapolation
    array.set(dizi,i,a*i+b)
//// for src
// for ax+b
xo2=0.0
yo2=0.0
xyo2=0.0
xxo2=0.0
for i=0 to len-1
    xo2:= xo2 + i/(len)
    yo2:= yo2 + src[len-1-i]/(len)
    xyo2:= xyo2 + i*src[len-1-i]/(len)
    xxo2:= xxo2 + i*i/(len)

dizi2=array.new_float(len*2+1+extrapolation)
// linedizi2=array.new_line()
a2=(xo2*yo2-xyo2)/(xo2*xo2-xxo2)
b2=yo2-a*xo2
for i=0 to len-1+extrapolation
    array.set(dizi2,i,a2*i+b2)
ttk=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))? 1 : 
 ((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))? -1 : 0
cg=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)))// and ta.highest(ttk[1],len/2)<1)
cr=((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)))// and ta.lowest(ttk[1],len/2)>-1)
bgcolor(color=(cg and ta.highest(ttk[1],len/2)<1)? color.rgb(76, 175, 79, 50) : 
 (cr and ta.lowest(ttk[1],len/2)>-1)? color.rgb(255, 82, 82, 50) : na, offset=0, display=display.none)
plot(x)

// for ax+b
xo3=0.0
yo3=0.0
xyo3=0.0
xxo3=0.0
for i=0 to len-1
    xo3:= xo3 + i/(len)
    yo3:= yo3 + x[len-1-i+(ta.barssince(cg))]/(len)
    xyo3:= xyo3 + i*x[len-1-i+(ta.barssince(cg))]/(len)
    xxo3:= xxo3 + i*i/(len)

dizi3=array.new_float(len*2+1+extrapolation)
// linedizi3=array.new_line()
a3=(xo3*yo3-xyo3)/(xo3*xo3-xxo3)
b3=yo3-a3*xo3
for i=0 to len-1+extrapolation
    array.set(dizi3,i,a3*i+b3)

// for ax+b
xo4=0.0
yo4=0.0
xyo4=0.0
xxo4=0.0
for i=0 to len-1
    xo4:= xo4 + i/(len)
    yo4:= yo4 + x[len-1-i+(ta.barssince(cr))]/(len)
    xyo4:= xyo4 + i*x[len-1-i+(ta.barssince(cr))]/(len)
    xxo4:= xxo4 + i*i/(len)

dizi4=array.new_float(len*2+1+extrapolation)
// linedizi4=array.new_line()
a4=(xo4*yo4-xyo4)/(xo4*xo4-xxo4)
b4=yo4-a4*xo4
for i=0 to len-1+extrapolation
    array.set(dizi4,i,a4*i+b4)

// line=line.new((last_bar_index-ta.barssince(cg)-len),
//  array.get(dizi3,0), 
//  last_bar_index-ta.barssince(cg),
//  array.get(dizi3,len-1), color=color.rgb(0,255,0), width=2)
// line2=line.new((last_bar_index-ta.barssince(cr)-len),
//  array.get(dizi4,0), 
//  last_bar_index-ta.barssince(cr),
//  array.get(dizi4,len-1), color=color.rgb(255, 0, 0, 0), width=2)
// line.delete(line[1])
// line.delete(line2[1])

alert=((array.get(dizi,0)<array.get(dizi,1)) and (array.get(dizi2,0)>array.get(dizi2,1)) and ta.highest(ttk[1],len/2)<1)
 or ((array.get(dizi,0)>array.get(dizi,1)) and (array.get(dizi2,0)<array.get(dizi2,1)) and ta.lowest(ttk[1],len/2)>-1)
alertcondition(alert)
hline(50)
rs=hline(30)
rss=hline(70)
fill(rs, rss, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

longCondition = cg and ta.highest(ttk[1],len/2)<1 
if (longCondition)
    strategy.entry("Long", strategy.long)
    strategy.exit("exit long", "Long", limit = close*(100+tpb)/100 , stop =close*(100-sb)/100 , trail_price = close , trail_offset = close*tsb)

shortCondition = cr and ta.lowest(ttk[1],len/2)>-1 
if (shortCondition)
    strategy.entry("Short", strategy.short)
    strategy.exit("exit short", "Short", limit = close*(100-tps)/100, stop = close*(100+ss)/100, trail_price = close , trail_offset = close*tss)