本策略是一个结合了相对强弱指标(RSI)和简单移动平均线(SMA)的趋势跟踪交易系统。该策略通过移动平均线判断市场趋势方向,同时利用RSI指标确认动量,从而在趋势与动量共振时进行交易。策略设计了完整的止盈止损机制,能够有效控制风险。
策略的核心逻辑基于两个技术指标的配合使用: 1. 移动平均线(MA):用于确定总体趋势。当价格位于MA之上时判定为上升趋势,反之为下降趋势。 2. 相对强弱指标(RSI):用于确认价格动量。当RSI高于设定阈值(如55)时确认上涨动量,低于阈值(如45)时确认下跌动量。
交易信号生成逻辑: - 做多条件:价格位于MA之上且RSI大于买入阈值 - 做空条件:价格位于MA之下且RSI小于卖出阈值
风险控制采用百分比止损和止盈方式,分别设置为入场价格的固定百分比。
该策略通过结合趋势和动量指标,构建了一个逻辑清晰、风险可控的交易系统。虽然存在一些固有风险,但通过合理的参数设置和风险控制,策略展现出较好的实用性。后续优化方向主要围绕参数动态调整、市场环境识别和信号质量提升展开,有望进一步提高策略的稳定性和盈利能力。
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-04 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © raiford87
//@version=6
strategy("RSI + MA Trend Strategy (v6)",
shorttitle="RSI_MA_Trend_v6",
overlay=true,
initial_capital=50000,
default_qty_type=strategy.fixed,
default_qty_value=1)
// ─────────────────────────────────────────────────────────────────────────────────────
// 1. USER INPUTS
// ─────────────────────────────────────────────────────────────────────────────────────
maLength = input.int(50, "Moving Average Length")
rsiLength = input.int(14, "RSI Length")
rsiBuyLevel = input.int(55, "RSI > X for Buy", minval=1, maxval=99)
rsiSellLevel = input.int(45, "RSI < X for Sell", minval=1, maxval=99)
stopLossPerc = input.float(1.0, "Stop Loss %", minval=0.1)
takeProfitPerc = input.float(2.0, "Take Profit %", minval=0.1)
// ─────────────────────────────────────────────────────────────────────────────────────
// 2. INDICATOR CALCULATIONS
// ─────────────────────────────────────────────────────────────────────────────────────
maValue = ta.sma(close, maLength)
rsiVal = ta.rsi(close, rsiLength)
// Trend conditions
bullTrend = close > maValue
bearTrend = close < maValue
// RSI conditions
rsiBull = rsiVal > rsiBuyLevel
rsiBear = rsiVal < rsiSellLevel
// ─────────────────────────────────────────────────────────────────────────────────────
// 3. ENTRY CONDITIONS
// ─────────────────────────────────────────────────────────────────────────────────────
longCondition = bullTrend and rsiBull
shortCondition = bearTrend and rsiBear
if longCondition
strategy.entry("RSI MA Long", strategy.long)
if shortCondition
strategy.entry("RSI MA Short", strategy.short)
// ─────────────────────────────────────────────────────────────────────────────────────
// 4. STOP LOSS & TAKE PROFIT
// ─────────────────────────────────────────────────────────────────────────────────────
stopLossLevel = stopLossPerc * 0.01
takeProfitLevel = takeProfitPerc * 0.01
if strategy.position_size > 0
stopPriceLong = strategy.position_avg_price * (1 - stopLossLevel)
tpPriceLong = strategy.position_avg_price * (1 + takeProfitLevel)
strategy.exit("Exit Long", from_entry="RSI MA Long", stop=stopPriceLong, limit=tpPriceLong)
if strategy.position_size < 0
stopPriceShort = strategy.position_avg_price * (1 + stopLossLevel)
tpPriceShort = strategy.position_avg_price * (1 - takeProfitLevel)
strategy.exit("Exit Short", from_entry="RSI MA Short", stop=stopPriceShort, limit=tpPriceShort)
// ─────────────────────────────────────────────────────────────────────────────────────
// 5. PLOT SIGNALS & LEVELS
// ─────────────────────────────────────────────────────────────────────────────────────
plot(maValue, color=color.yellow, linewidth=2, title="Moving Average")
plotchar(longCondition, title="Long Signal", char='▲', location=location.belowbar, color=color.green, size=size.tiny)
plotchar(shortCondition, title="Short Signal", char='▼', location=location.abovebar, color=color.red, size=size.tiny)
// Plot Stop & TP lines
posIsLong = strategy.position_size > 0
posIsShort = strategy.position_size < 0
plotStopLong = posIsLong ? strategy.position_avg_price * (1 - stopLossLevel) : na
plotTpLong = posIsLong ? strategy.position_avg_price * (1 + takeProfitLevel): na
plotStopShort= posIsShort? strategy.position_avg_price * (1 + stopLossLevel) : na
plotTpShort = posIsShort? strategy.position_avg_price * (1 - takeProfitLevel): na
plot(plotStopLong, color=color.red, linewidth=2, style=plot.style_line, title="Stop Loss Long")
plot(plotTpLong, color=color.green, linewidth=2, style=plot.style_line, title="Take Profit Long")
plot(plotStopShort, color=color.red, linewidth=2, style=plot.style_line, title="Stop Loss Short")
plot(plotTpShort, color=color.green, linewidth=2, style=plot.style_line, title="Take Profit Short")