RSI均线交叉策略是一种应用于加密货币交易的策略。该策略将移动平均线应用于RSI指标,并根据RSI与其移动平均线的交叉情况发出买入和卖出信号。
该策略首先计算RSI指标。RSI指标基于一定时间周期内的涨跌变化,反映价格的强弱。RSI大于70时为超买区,小于30时为超卖区。
然后,该策略在RSI指标的基础上应用移动平均线。移动平均线能够过滤随机波动,判断趋势方向。这里设置了10周期的RSI移动平均线。
当RSI上穿其移动平均线时,视为买入信号;当RSI下穿其移动平均线时,视为卖出信号。根据这两个信号进行交易。
代码中,首先计算 length 周期的RSI指标。然后计算 10 周期 RSI 的移动平均线 ma。当 ma 上穿 rsi 时,买入;当 ma 下穿 rsi 时,卖出。
此外,代码还绘制了 rsi、ma 的线形图,以及 rsi-ma 的柱状图。绘制了 rsi=70、rsi=30 的分界线。并在买入卖出时,在图上标记了相应的信号箭头。
针对风险,可以调整参数优化指标效果,适当缩短仓位,设置止损线,并配合趋势分析来过滤信号。
RSI均线交叉策略结合趋势指标和过滤指标的优点,相对成熟可靠。该策略逻辑简单易懂,代码实现也较完整,总体而言是一个较好的加密货币交易策略。但任何策略都有需要优化的地方,需要不断测试调整,并辅以趋势判断,以取得更好的策略效果。
/*backtest
start: 2022-10-31 00:00:00
end: 2023-11-06 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("RSI w MA Strategy", shorttitle="RSI w MA Strategy", overlay=false, initial_capital=10000, currency='USD',process_orders_on_close=true)
//TIME FRAME AND BACKGROUND CONTROL/////////////////////////////////////////////
testStartYear = input(2019, "Backtest Start Year")
testStartMonth = input(01, "Backtest Start Month")
testStartDay = input(01, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)
testStopYear = input(2022, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(1, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
testPeriodBackground = input(title="Color Background?", type=input.bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and time >= testPeriodStart and time <= testPeriodStop ?
color.teal : na
//bgcolor(testPeriodBackgroundColor, transp=50)
testPeriod() => true
////////////////////////////////////////////////////////////////////////////////
src = close, len = input(27, minval=1, title="Length")
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
window = input(10, "RSI MA Window")
ma = sma(rsi,window)
plot(rsi, color=color.orange)
colorr= ma > rsi ? color.red : color.green
plot(ma,color=colorr)
band1 = hline(70)
band0 = hline(30)
fill(band1, band0, color=color.purple, transp=90)
diff = rsi - ma
plot(diff,style= plot.style_columns,transp=50,color = colorr)
plotshape(crossunder(rsi,ma)?rsi:na,title="top",style=shape.triangledown,location=location.absolute,size=size.tiny,color=color.red,transp=0)
plotshape(crossover(rsi,ma)?rsi:na,title="bottom",style=shape.triangleup,location=location.absolute,size=size.tiny,color=color.lime,transp=0)
buySignal = crossover(rsi,ma)
sellSignal = crossunder(rsi,ma)
//TRADE CONTROL/////////////////////////////////////////////////////////////////
if testPeriod()
if buySignal
strategy.close("Short", qty_percent = 100, comment = "Close Short")
strategy.entry("Long", strategy.long, qty=.1)
if sellSignal
strategy.close("Long", qty_percent = 100, comment = "Close Long")
strategy.entry("Short", strategy.short, qty=.1)
////////////////////////////////////////////////////////////////////////////////