RSI 多空自动交易策略

Author: ChaoZhang, Date: 2023-10-30 17:13:24
Tags:

RSI 多空自动交易策略

概述

该策略基于相对强弱指数(RSI)指标设计了一个多空自动交易系统。它可以在RSI达到超买超卖区域时,自动发出做多做空信号,进行自动交易。

策略原理

该策略通过计算一定周期内的股票涨跌幅,得到0-100区间的RSI值。当RSI低于30时为超卖状态,高于70时为超买状态。策略根据这一规则,在RSI达到超卖区时自动做多,达到超买区时自动做空。

具体来说,策略首先计算15周期的RSI值。当RSI下跌到低于20时,认为处于超卖状态,此时在突破200日移动均线上方时,做多入场。当RSI上涨到超过80时,认为处于超买状态,此时做空入场。做多做空后,均设置止盈止损离场。

此外,策略还在价格发生信号时,画出相关的标记线和标签,使交易信号更加直观。

策略优势

  • 策略思路清晰简单,容易理解实现
  • 基于RSI指标,对超买超卖情况判断准确
  • 完全自动交易,不需要人工干预
  • 设置止盈止损,有效控制风险
  • 交易信号直观,易于监控

策略风险

  • RSI指标具有一定滞后性,可能出现误判
  • 固定的超买超卖阈值不适用于所有品种
  • 止损点设置不恰当可能造成较大亏损
  • 趋势市场中随大趋势做多做空可能亏损

风险控制措施包括:优化RSI参数,调整超买超卖阈值适应不同品种,合理设置止损点,结合趋势指标避免逆势交易。

策略优化方向

  • 优化RSI参数,提高对超买超卖的判断准确性
  • 结合其他指标确认交易信号,例如KDJ,MACD等
  • 根据市场情况,优化止损点的设置
  • 增加趋势判断,避免逆势操作
  • 设定权益曲线跟踪止损
  • 开发风控模块,控制单笔和总体风险

总结

该策略overall是一个利用RSI指标判断超买超卖的自动交易策略。它在RSI达到超买超卖极端区域时发出交易信号,可以自动进行买卖操作。策略思路简单清晰,容易实现,适合作为自动交易的基础策略。但RSI指标存在一定滞后性,所以建议结合其他指标进行优化,提高信号准确性。此外还需要关注风险控制,优化止损机制,开发风控模块,以减少交易风险。如果在实盘中经过优化验证,该策略可以成为一个有效的多空自动交易系统。


/*backtest
start: 2023-10-22 00:00:00
end: 2023-10-29 00:00:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("RSI Improved strategy", overlay=true)
higherTF1 = input.timeframe('15' , "Resolution", options = ['5', '15', '1H', 'D', 'W', 'M'])
dailyopen = request.security(syminfo.tickerid, higherTF1, close)

Reward = input(1600)
Risk = input(1600)

length = input( 5 )
overSold = input( 30 )
overBought = input( 70 )
EMA = input(200)
price = close

vrsi = ta.rsi(price, length)

RSIlowest =  vrsi[1] > vrsi ? true : false
RSIhighest = vrsi[1] < vrsi ? true : false

//ro = ta.crossunder(vrsi, 20)
//ru = ta.crossover(vrsi, 80)

co = ta.crossunder(vrsi, overSold)
cu = ta.crossunder(vrsi, overBought)

plot(ta.ema(close, EMA))
plot(ta.ema(close, 50), color = color.orange)

UponEMA = close > ta.ema(close, EMA) ? true : false
belowEMA = close < ta.ema(close, EMA) ? true : false
//transfer 'float' to 'int' to 'string'
r = int(vrsi)
value = str.tostring(r)

m = int(strategy.openprofit)
money = str.tostring(m)
if (not na(vrsi))
	//when price stand up on 200ema and rsi is at oversold area, open long position 
//	if (co and UponEMA)
  //      strategy.order("Rsi long", strategy.long, 1 , comment = "Rsi long")
        
    if(vrsi < 20 and RSIlowest)
        // line1 = line.new(x1=bar_index, y1=dailyopen, x2=bar_index+1, y2=dailyopen, xloc=xloc.bar_index, style=line.style_solid,extend=extend.right, color=color.aqua, width = 2)
        // line.delete(line1[1])  // remove the previous line when new bar appears
        // label1 = label.new(x=bar_index, y=dailyopen,yloc=yloc.belowbar, text = value,textcolor = color.white, color = color.green, style = label.style_label_up)
        // label.delete(label1[1])
        strategy.order("Rsi long", strategy.long, 1 , comment = "Rsi long")
        strategy.exit("exit", "Rsi long", profit = Reward, loss = Risk, comment = "Rsi long exit")
//strategy.close("Rsi short", comment = "Rsi close")

	
	

	if(vrsi > 80 and RSIhighest)
        // line2 = line.new(x1=bar_index, y1=dailyopen, x2=bar_index+1, y2=dailyopen, xloc=xloc.bar_index, style=line.style_solid,extend=extend.right, color = #e65100, width = 2)
        // line.delete(line2[1])  // remove the previous line when new bar appears
        // label2 = label.new(x=bar_index, y=dailyopen,yloc=yloc.abovebar, text = value, textcolor = color.white, color = color.red)            
        // label.delete(label2[1])
        strategy.order("Rsi short",strategy.short, 1,  comment = "Rsi short ")
        strategy.exit("exit", "Rsi short", profit = Reward,loss = Risk, comment = "Rsi short exit")
//	if(UponEMA)
//        strategy.close("Rsi short", comment = "Rsi short close")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_cross)
//plotshape(confirmPH, title="Label",offset = 1,text="Bull",style=shape.labeldown,location=location.abovebar,color=color.green,textcolor=color.green)




//when Rsi reaches overbought, draw a Horizontal Ray to close prices, similarly when it comes to oversold.(accomplished)
//detects when there is more lower/higher RSI values, adjust horizontal Ray and label to new posistion.(accomplished)

更多内容