这是一个结合均线交叉和相对强弱指标(RSI)的趋势跟踪策略。该策略通过短期和长期移动平均线的交叉来确定市场趋势方向,同时使用RSI作为动量过滤器来确认趋势的强度,从而提高交易信号的可靠性。策略还包含了百分比止损和止盈来进行风险管理。
策略使用9周期和21周期的简单移动平均线(SMA)作为主要趋势指标。当短期均线向上穿越长期均线且RSI大于50时,系统产生做多信号;当短期均线向下穿越长期均线且RSI小于50时,系统产生做空信号。这种设计确保了交易方向与市场趋势和动量保持一致。系统通过设置1%的止损和2%的止盈来控制每笔交易的风险回报比。
这是一个结构完整、逻辑清晰的趋势跟踪策略。通过均线交叉提供基本的趋势方向,RSI提供动量确认,再配合风险管理机制,形成了一个完整的交易系统。虽然存在一些固有的局限性,但通过持续优化和调整,该策略有望在不同市场环境下保持稳定的表现。策略的成功关键在于参数优化和风险控制的执行。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-10 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Moving Average Crossover + RSI Strategy", overlay=true, shorttitle="MA RSI Strategy")
// --- Input Parameters ---
shortMA = input.int(9, title="Short MA Period", minval=1)
longMA = input.int(21, title="Long MA Period", minval=1)
rsiLength = input.int(14, title="RSI Length", minval=1)
rsiOverbought = input.int(70, title="RSI Overbought Level", minval=50, maxval=100)
rsiOversold = input.int(30, title="RSI Oversold Level", minval=0, maxval=50)
stopLossPercent = input.float(1, title="Stop Loss Percentage", minval=0.1, maxval=10.0) / 100
takeProfitPercent = input.float(2, title="Take Profit Percentage", minval=0.1, maxval=10.0) / 100
// --- Calculate Moving Averages ---
shortMA_value = ta.sma(close, shortMA)
longMA_value = ta.sma(close, longMA)
// --- Calculate RSI ---
rsi_value = ta.rsi(close, rsiLength)
// --- Buy and Sell Conditions ---
longCondition = ta.crossover(shortMA_value, longMA_value) and rsi_value > 50
shortCondition = ta.crossunder(shortMA_value, longMA_value) and rsi_value < 50
// --- Plot Moving Averages ---
plot(shortMA_value, color=color.blue, linewidth=2, title="Short MA")
plot(longMA_value, color=color.red, linewidth=2, title="Long MA")
// --- Plot RSI (Optional) ---
hline(rsiOverbought, "Overbought", color=color.red)
hline(rsiOversold, "Oversold", color=color.green)
plot(rsi_value, color=color.purple, title="RSI")
// --- Strategy Execution ---
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// --- Risk Management (Stop Loss and Take Profit) ---
longStopLoss = close * (1 - stopLossPercent)
longTakeProfit = close * (1 + takeProfitPercent)
shortStopLoss = close * (1 + stopLossPercent)
shortTakeProfit = close * (1 - takeProfitPercent)
// Set the stop loss and take profit for long and short positions
strategy.exit("Long Exit", from_entry="Long", stop=longStopLoss, limit=longTakeProfit)
strategy.exit("Short Exit", from_entry="Short", stop=shortStopLoss, limit=shortTakeProfit)