这是一个结合了相对强弱指数(RSI)和多种移动平均线的量化交易策略。该策略主要通过监测RSI指标上的不同类型移动平均线(包括SMA、EMA、WMA和SMMA)的交叉信号来判断市场趋势,同时结合RSI指标本身的超买超卖区间作为辅助判断依据,从而确定交易时机。
策略主要包含以下几个关键计算步骤:
1. 计算14周期的RSI指标,设定超买区域为70,超卖区域为30
2. 在RSI曲线上分别计算3条不同参数的移动平均线:
- MA1: 20周期,可选SMA/EMA/WMA/SMMA
- MA2: 50周期,可选SMA/EMA/WMA/SMMA
- MA3: 100周期,可选SMA/EMA/WMA/SMMA
3. 交易信号生成规则:
- 买入信号:当MA2向上穿越MA3时
- 卖出信号:当MA2向下穿越MA3时
4. 同时检测RSI指标的背离情况,为交易决策提供辅助参考
该策略通过结合RSI和多重移动平均线,构建了一个具有较强适应性的交易系统。策略的核心优势在于多重技术指标的交叉验证和灵活的参数配置,但同时需要注意移动平均线的滞后性以及市场条件对策略表现的影响。通过持续优化和风险控制,该策略有望在实际交易中取得稳定表现。
/*backtest
start: 2024-01-17 00:00:00
end: 2025-01-16 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=6
strategy(title="Relative Strength Index with MA Strategy", shorttitle="RSI-MA Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=200)
// RSI Inputs
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
calculateDivergence = input.bool(false, title="Calculate Divergence", group="RSI Settings", tooltip="Calculating divergences is needed in order for divergence alerts to fire.")
// RSI Calculation
change_rsi = ta.change(rsiSourceInput)
up = ta.rma(math.max(change_rsi, 0), rsiLengthInput)
down = ta.rma(-math.min(change_rsi, 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
// RSI Plot
plot(rsi, "RSI", color=#7E57C2)
hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
hline(30, "RSI Lower Band", color=#787B86)
fill(hline(70), hline(30), color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
// RSI-based MA Inputs
grpRSIMovingAverages = "RSI Moving Averages"
ma1Length = input.int(20, title="MA1 Length", group=grpRSIMovingAverages)
ma2Length = input.int(50, title="MA2 Length", group=grpRSIMovingAverages)
ma3Length = input.int(100, title="MA3 Length", group=grpRSIMovingAverages)
ma1Type = input.string("SMA", title="MA1 Type", options=["SMA", "EMA", "WMA", "SMMA"], group=grpRSIMovingAverages)
ma2Type = input.string("EMA", title="MA2 Type", options=["SMA", "EMA", "WMA", "SMMA"], group=grpRSIMovingAverages)
ma3Type = input.string("WMA", title="MA3 Type", options=["SMA", "EMA", "WMA", "SMMA"], group=grpRSIMovingAverages)
// MA Calculation Function
calcMA(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"WMA" => ta.wma(source, length)
"SMMA" => ta.rma(source, length)
// MA Calculations
ma1 = calcMA(rsi, ma1Length, ma1Type)
ma2 = calcMA(rsi, ma2Length, ma2Type)
ma3 = calcMA(rsi, ma3Length, ma3Type)
// MA Plots
plot(ma1, title="RSI MA1", color=color.blue)
plot(ma2, title="RSI MA2", color=color.green)
plot(ma3, title="RSI MA3", color=color.red)
// Divergence (Retained from original script)
lookbackRight = 5
lookbackLeft = 5
rangeUpper = 60
rangeLower = 5
bearColor = color.red
bullColor = color.green
textColor = color.white
noneColor = color.new(color.white, 100)
_inRange(bool cond) =>
bars = ta.barssince(cond)
rangeLower <= bars and bars <= rangeUpper
plFound = false
phFound = false
bullCond = false
bearCond = false
rsiLBR = rsi[lookbackRight]
if calculateDivergence
// Regular Bullish
plFound := not na(ta.pivotlow(rsi, lookbackLeft, lookbackRight))
rsiHL = rsiLBR > ta.valuewhen(plFound, rsiLBR, 1) and _inRange(plFound[1])
lowLBR = low[lookbackRight]
priceLL = lowLBR < ta.valuewhen(plFound, lowLBR, 1)
bullCond := priceLL and rsiHL and plFound
// Regular Bearish
phFound := not na(ta.pivothigh(rsi, lookbackLeft, lookbackRight))
rsiLH = rsiLBR < ta.valuewhen(phFound, rsiLBR, 1) and _inRange(phFound[1])
highLBR = high[lookbackRight]
priceHH = highLBR > ta.valuewhen(phFound, highLBR, 1)
bearCond := priceHH and rsiLH and phFound
// plot(
// plFound ? rsiLBR : na,
// offset=-lookbackRight,
// title="Regular Bullish",
// linewidth=2,
// color=(bullCond ? bullColor : noneColor),
// display = display.pane
// )
plotshape(
bullCond ? rsiLBR : na,
offset=-lookbackRight,
title="Regular Bullish Label",
text=" Bull ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor
)
// plot(
// phFound ? rsiLBR : na,
// offset=-lookbackRight,
// title="Regular Bearish",
// linewidth=2,
// color=(bearCond ? bearColor : noneColor),
// display = display.pane
// )
plotshape(
bearCond ? rsiLBR : na,
offset=-lookbackRight,
title="Regular Bearish Label",
text=" Bear ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor
)
alertcondition(bullCond, title='Regular Bullish Divergence', message="Found a new Regular Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.")
alertcondition(bearCond, title='Regular Bearish Divergence', message='Found a new Regular Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.')
// ----- MUA/BÁN -----
// Điều kiện Mua: MA2 cắt lên MA3 và MA3 < 55
buyCondition = ta.crossover(ma2, ma3)
// Điều kiện Bán: MA2 cắt xuống MA3 và MA3 > 40
sellCondition = ta.crossunder(ma2, ma3)
// Thực hiện lệnh Mua/Bán
if (buyCondition)
strategy.entry("Buy", strategy.long, comment="Buy Signal")
if (sellCondition)
strategy.close("Buy", comment="Sell Signal")
// ----- KẾT THÚC -----