该策略基于相对强弱指数(RSI)指标设计,利用RSI指标的超买超卖原理进行双向突破操作。当RSI指标上穿设定的超买线时做多,当RSI指标下穿设定的超卖线时做空,属于典型的反转交易策略。
根据用户输入设置计算RSI指标的参数,包括RSI周期长度、超买线阈值、超卖线阈值。
根据RSI曲线相对于超买线和超卖线的位置关系,判断所处的超买区还是超卖区。
当RSI指标从超卖区突破对应阈值线时,进行相反方向的开仓操作。例如从超买区突破超买线时,认为行情反转,此时开仓做多;从超卖区突破超卖线时,认为行情反转,此时开仓做空。
开仓后,设置止损止盈线。跟踪止损止盈情况,满足条件时进行平仓。
该策略还提供了利用EMA作为过滤器的可选功能。只有在RSI指标做多做空信号的同时,价格也要突破EMA才开仓。
策略还提供了只在特定交易时段交易的功能。用户可以设置只在某个时间段交易,超过时间后平仓离场。
风险解决:
该策略可以从以下几个方面进行优化:
优化RSI参数,寻找不同品种最佳参数组合。可以通过遍历回测找到最佳超买超卖阈值。
尝试不同的指标替代或结合RSI,形成更强势的判断信号。例如MACD,KD,布林带等。
优化止损止盈策略,提高策略稳定性。可以根据市场波动率设定游离止损,或者带有追踪止损功能的策略。
优化EMA过滤器参数或试验其他指标过滤器,进一步避免被套。
增加趋势判断模块,避免反向做空多头行情,或反向做多空头行情。
测试不同的交易时段参数,判断哪些时段适合该策略,哪些时段应避免。
该RSI双向突破策略整体思路清晰,利用经典的RSI超买超卖原理进行反转交易。既可以抓取超买超卖区的反转机会,又可通过EMA过滤和止损止盈来控制风险。通过参数优化和模块优化空间较大,可以将其打造成较为稳定可靠的反转策略。值得进一步测试优化后实际应用。
/*backtest
start: 2023-10-08 00:00:00
end: 2023-11-07 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/
// © REV0LUTI0N
//@version=4
strategy("RSI Strategy", overlay=true, initial_capital = 10000, default_qty_value = 10000, default_qty_type = strategy.cash)
// Strategy Backtesting
startDate = input(timestamp("2021-10-01T00:00:00"), type = input.time, title='Backtesting Start Date')
finishDate = input(timestamp("9999-12-31T00:00:00"), type = input.time, title='Backtesting End Date')
time_cond = true
// Strategy
Length = input(12, minval=1)
src = input(close, title="Source")
overbought = input(70, minval=1)
oversold = input(30, minval=1)
xRSI = rsi(src, Length)
rsinormal = input(true, title="Overbought Go Long & Oversold Go Short")
rsiflipped = input(false, title="Overbought Go Short & Oversold Go Long")
// EMA Filter
noemafilter = input(true, title="No EMA Filter")
useemafilter = input(false, title="Use EMA Filter")
ema_length = input(defval=15, minval=1, title="EMA Length")
emasrc = input(close, title="Source")
ema = ema(emasrc, ema_length)
plot(ema, "EMA", style=plot.style_linebr, color=#cad850, linewidth=2)
//Time Restriction Settings
startendtime = input("", title='Time Frame To Enter Trades')
enableclose = input(false, title='Enable Close Trade At End Of Time Frame')
timetobuy = (time(timeframe.period, startendtime))
timetoclose = na(time(timeframe.period, startendtime))
// Stop Loss & Take Profit % Based
enablesl = input(false, title='Enable Stop Loss')
enabletp = input(false, title='Enable Take Profit')
stopTick = input(5.0, title='Stop Loss %', type=input.float, step=0.1) / 100
takeTick = input(10.0, title='Take Profit %', type=input.float, step=0.1) / 100
longStop = strategy.position_avg_price * (1 - stopTick)
shortStop = strategy.position_avg_price * (1 + stopTick)
shortTake = strategy.position_avg_price * (1 - takeTick)
longTake = strategy.position_avg_price * (1 + takeTick)
plot(strategy.position_size > 0 and enablesl ? longStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Fixed SL")
plot(strategy.position_size < 0 and enablesl ? shortStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Short Fixed SL")
plot(strategy.position_size > 0 and enabletp ? longTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Long Take Profit")
plot(strategy.position_size < 0 and enabletp ? shortTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Short Take Profit")
// Alert messages
message_enterlong = input("", title="Long Entry message")
message_entershort = input("", title="Short Entry message")
message_closelong = input("", title="Close Long message")
message_closeshort = input("", title="Close Short message")
// Strategy Execution
if (xRSI > overbought and close > ema and time_cond and timetobuy and rsinormal and useemafilter)
strategy.entry("Long", strategy.long, alert_message = message_enterlong)
if (xRSI < oversold and close < ema and time_cond and timetobuy and rsinormal and useemafilter)
strategy.entry("Short", strategy.short, alert_message = message_entershort)
if (xRSI < oversold and close > ema and time_cond and timetobuy and rsiflipped and useemafilter)
strategy.entry("Long", strategy.long, alert_message = message_enterlong)
if (xRSI > overbought and close < ema and time_cond and timetobuy and rsiflipped and useemafilter)
strategy.entry("Short", strategy.short, alert_message = message_entershort)
if (xRSI > overbought and time_cond and timetobuy and rsinormal and noemafilter)
strategy.entry("Long", strategy.long, alert_message = message_enterlong)
if (xRSI < oversold and time_cond and timetobuy and rsinormal and noemafilter)
strategy.entry("Short", strategy.short, alert_message = message_entershort)
if (xRSI < oversold and time_cond and timetobuy and rsiflipped and noemafilter)
strategy.entry("Long", strategy.long, alert_message = message_enterlong)
if (xRSI > overbought and time_cond and timetobuy and rsiflipped and noemafilter)
strategy.entry("Short", strategy.short, alert_message = message_entershort)
if strategy.position_size > 0 and timetoclose and enableclose
strategy.close_all(alert_message = message_closelong)
if strategy.position_size < 0 and timetoclose and enableclose
strategy.close_all(alert_message = message_closeshort)
if strategy.position_size > 0 and enablesl and time_cond
strategy.exit(id="Close Long", stop=longStop, limit=longTake, alert_message = message_closelong)
if strategy.position_size < 0 and enablesl and time_cond
strategy.exit(id="Close Short", stop=shortStop, limit=shortTake, alert_message = message_closeshort)
if strategy.position_size > 0 and enabletp and time_cond
strategy.exit(id="Close Long", stop=longStop, limit=longTake, alert_message = message_closelong)
if strategy.position_size < 0 and enabletp and time_cond
strategy.exit(id="Close Short", stop=shortStop, limit=shortTake, alert_message = message_closeshort)