该策略是一个基于均线、RSI指标和跟踪止损的量化交易系统。它结合了技术分析中的趋势跟踪和动量指标,通过设定严格的入场和出场条件,实现风险可控的交易。策略的核心逻辑是在上升趋势中寻找超卖机会入场,并使用跟踪止损保护盈利。
策略使用200日简单移动平均线(SMA)作为趋势判断的基准线,结合相对强弱指标(RSI)进行交易信号的生成。具体来说: 1. 使用200日均线判断大趋势,只有价格在均线之上才考虑做多 2. 当RSI低于预设阈值(默认40)时,认为出现超卖信号 3. 同时满足以上两个条件且距离上次平仓超过等待期(默认10天)时,触发做多信号 4. 持仓期间通过跟踪止损(默认5%)动态保护利润 5. 当价格跌破跟踪止损价格或跌破200日均线时,平仓出场
这是一个结构完整、逻辑清晰的量化交易策略。它通过结合多个技术指标,在控制风险的同时追求稳定收益。虽然存在一定的优化空间,但基本框架具有良好的实用性和扩展性。策略适合中长期投资者使用,对于不同市场环境都有较好的适应性。
/*backtest
start: 2025-01-09 00:00:00
end: 2025-01-16 00:00:00
period: 15m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=5
strategy("200 SMA Crossover Strategy", overlay=false)
// Define inputs
smaLength = input.int(200, title="SMA Length")
rsiLength = input.int(14, title="RSI Length")
rsiThreshold = input.float(40, title="RSI Threshold")
trailStopPercent = input.float(5.0, title="Trailing Stop Loss (%)")
waitingPeriod = input.int(10, title="Waiting Period (Days)")
// Calculate 200 SMA
sma200 = ta.sma(close, smaLength)
// Calculate RSI
rsi = ta.rsi(close, rsiLength)
// Plot the 200 SMA and RSI
plot(sma200, color=color.blue, linewidth=2, title="200 SMA")
plot(rsi, color=color.purple, title="RSI", display=display.none)
// Define buy and sell conditions
var isLong = false
var float lastExitTime = na
var float trailStopPrice = na
// Explicitly declare timeSinceExit as float
float timeSinceExit = na(lastExitTime) ? na : (time - lastExitTime) / (24 * 60 * 60 * 1000)
canEnter = na(lastExitTime) or timeSinceExit > waitingPeriod
buyCondition = close > sma200 and rsi < rsiThreshold and canEnter
if (buyCondition and not isLong)
strategy.entry("Buy", strategy.long)
trailStopPrice := na
isLong := true
// Update trailing stop loss if long
if (isLong)
trailStopPrice := na(trailStopPrice) ? close * (1 - trailStopPercent / 100) : math.max(trailStopPrice, close * (1 - trailStopPercent / 100))
// Check for trailing stop loss or sell condition
if (isLong and (close < trailStopPrice or close < sma200))
strategy.close("Buy")
lastExitTime := time
isLong := false
// Plot buy and sell signals
plotshape(series=buyCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=(isLong and close < trailStopPrice) or close < sma200, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")