双均线交叉趋势跟踪策略是一种结合了技术分析与全面风险管理的量化交易系统。该策略核心利用快速简单移动平均线(Fast SMA)和慢速简单移动平均线(Slow SMA)的交叉信号来识别市场趋势变化,并通过多重风险控制机制确保资金安全。策略在Pine Script平台上实现,适用于多种交易品种的趋势跟踪交易。
该策略基于两条简单移动平均线之间的交互关系进行交易决策:
信号生成机制:
执行时机控制: 策略在K线收盘时执行所有交易决策,避免前瞻性偏差(look-ahead bias),确保回测结果的可靠性和真实性。
资金管理系统:
多层次风险控制:
该策略通过均线交叉捕捉趋势,并使用全面的风险管理措施确保交易的安全性和可持续性。
稳健的趋势识别机制:
精确的资金管理:
多层次的风险防护:
交易执行的时序控制:
process_orders_on_close=true
参数确保订单处理符合真实交易环境自适应的追踪止损系统:
趋势识别滞后性:
固定参数适应性问题:
追踪止损激活时机:
资金管理风险:
技术实现局限性:
信号生成机制优化:
风险管理系统增强:
入场优化:
回测与评估框架:
技术实现提升:
双均线交叉趋势跟踪策略是一个将经典技术分析方法与现代风险管理理念相结合的完整交易系统。其核心优势在于简洁明确的趋势识别机制与多层次的风险控制体系,特别是其精细的资金管理和高级追踪止损机制为策略提供了良好的风险调整回报潜力。
然而,该策略也面临移动平均线固有的滞后性和参数适应性等挑战。通过引入自适应参数、增强信号过滤机制以及完善风险管理系统,策略性能有望得到进一步提升。
总体而言,这是一个结构完善、逻辑清晰的量化策略框架,适合作为中长期趋势跟踪系统的基础,尤其适用于具有明显趋势特性的市场。对于交易者而言,理解并掌握其风险管理理念比简单复制策略参数更为重要,这也是该策略最有价值的部分。
/*backtest
start: 2025-06-04 00:00:00
end: 2025-06-11 00:00:00
period: 5m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title="Dual SMA Crossover Strategy", overlay=true, calc_on_every_tick=false, process_orders_on_close=true)
// --- Inputs ---
// SMA Lengths
fast_length = input.int(24, title="Fast SMA Length", minval=1)
slow_length = input.int(48, title="Slow SMA Length", minval=1)
// Risk Management
risk_per_trade_percent = input.float(2.0, title="Risk Per Trade (%)", minval=0.1, maxval=10.0, step=0.1) // % of equity to risk per trade
stop_loss_percent = input.float(0.8, title="Stop Loss (%)", minval=0.1, step=0.1) // % from entry price
risk_reward_ratio = input.float(2.0, title="Risk-Reward Ratio", minval=0.5, step=0.1) // 2.0 = 2R, 3.0 = 3R etc.
// Advanced Trailing Stop Loss
trailing_start_percent = input.float(1.0, title="Trailing Stop Start (%)", minval=0.1, step=0.1) // % profit to activate TSL
trailing_stop_percent = input.float(0.5, title="Trailing Stop Trail (%)", minval=0.1, step=0.1) // % to trail by once activated
// --- Calculations ---
// Calculate SMAs
fast_sma = ta.sma(close, fast_length)
slow_sma = ta.sma(close, slow_length)
// Plot SMAs on chart
plot(fast_sma, color=color.blue, title="Fast SMA")
plot(slow_sma, color=color.red, title="Slow SMA")
// Crossover conditions (calculated on previous bar to prevent look-ahead bias)
long_condition = ta.crossover(fast_sma[1], slow_sma[1])
short_condition = ta.crossunder(fast_sma[1], slow_sma[1])
// --- Money Management and Position Sizing ---
// Calculate account equity and risk amount
account_equity = strategy.initial_capital + strategy.netprofit
risk_amount = account_equity * (risk_per_trade_percent / 100)
// Calculate Stop Loss price based on entry and SL percentage
var float long_stop_price = na
var float short_stop_price = na
var float long_take_profit_price = na
var float short_take_profit_price = na
// --- Trailing Stop Loss Variables ---
var float trailing_long_activated_price = na // Price at which TSL is activated for long
var float trailing_short_activated_price = na // Price at which TSL is activated for short
var float current_trailing_stop_long = na
var float current_trailing_stop_short = na
var bool is_long_trailing_active = false
var bool is_short_trailing_active = false
// --- Strategy Entry and Exit Orders ---
if long_condition
// Reset TSL variables for a new entry
trailing_long_activated_price := na
current_trailing_stop_long := na
is_long_trailing_active := false
// Calculate SL, TP for long entry
long_stop_price := close * (1 - stop_loss_percent / 100) // SL below entry
long_take_profit_price := close * (1 + (stop_loss_percent * risk_reward_ratio) / 100) // TP above entry based on RRR
// Calculate position size for long entry
price_change_per_unit = close * (stop_loss_percent / 100)
if price_change_per_unit > 0
long_quantity = risk_amount / price_change_per_unit
strategy.entry("Long", strategy.long, qty=long_quantity, comment="Buy Signal")
else
strategy.entry("Long", strategy.long, comment="Buy Signal (Risk calculation skipped)") // Fallback if SL is 0 or negative
if short_condition
// Reset TSL variables for a new entry
trailing_short_activated_price := na
current_trailing_stop_short := na
is_short_trailing_active := false
// Calculate SL, TP for short entry
short_stop_price := close * (1 + stop_loss_percent / 100) // SL above entry
short_take_profit_price := close * (1 - (stop_loss_percent * risk_reward_ratio) / 100) // TP below entry based on RRR
// Calculate position size for short entry
price_change_per_unit = close * (stop_loss_percent / 100)
if price_change_per_unit > 0
short_quantity = risk_amount / price_change_per_unit
strategy.entry("Short", strategy.short, qty=short_quantity, comment="Sell Signal")
else
strategy.entry("Short", strategy.short, comment="Sell Signal (Risk calculation skipped)") // Fallback if SL is 0 or negative
// --- Stop Loss, Take Profit, Trailing Stop Logic ---
// Long position management
if strategy.position_size > 0 // We are in a long position
entry_price = strategy.opentrades.entry_price(0)
current_profit_percent = ((close - entry_price) / entry_price) * 100
// Initial SL and TP set at entry
strategy.exit("Exit Long", from_entry="Long", stop=long_stop_price, limit=long_take_profit_price, comment="TP/SL Long")
// Check for Trailing Stop activation
if not is_long_trailing_active and current_profit_percent >= trailing_start_percent
is_long_trailing_active := true
// Set initial trailing stop when activated
trailing_long_activated_price := high // Or close, depending on preference
current_trailing_stop_long := high * (1 - trailing_stop_percent / 100)
// If trailing stop is active, update it
if is_long_trailing_active
// Only move the trailing stop up (for long positions)
potential_new_stop = high * (1 - trailing_stop_percent / 100)
current_trailing_stop_long := math.max(current_trailing_stop_long, potential_new_stop)
// Ensure trailing stop is not below the initial long_stop_price
// This prevents the trailing stop from being less protective than the initial SL if the price drops after activation.
current_trailing_stop_long := math.max(current_trailing_stop_long, long_stop_price)
strategy.exit("Trailing Exit Long", from_entry="Long", stop=current_trailing_stop_long, comment="Trailing SL Long")
// Short position management
if strategy.position_size < 0 // We are in a short position
entry_price = strategy.opentrades.entry_price(0)
current_profit_percent = ((entry_price - close) / entry_price) * 100
// Initial SL and TP set at entry
strategy.exit("Exit Short", from_entry="Short", stop=short_stop_price, limit=short_take_profit_price, comment="TP/SL Short")
// Check for Trailing Stop activation
if not is_short_trailing_active and current_profit_percent >= trailing_start_percent
is_short_trailing_active := true
// Set initial trailing stop when activated
trailing_short_activated_price := low // Or close, depending on preference
current_trailing_stop_short := low * (1 + trailing_stop_percent / 100)
// If trailing stop is active, update it
if is_short_trailing_active
// Only move the trailing stop down (for short positions)
potential_new_stop = low * (1 + trailing_stop_percent / 100)
current_trailing_stop_short := math.min(current_trailing_stop_short, potential_new_stop)
// Ensure trailing stop is not above the initial short_stop_price
current_trailing_stop_short := math.min(current_trailing_stop_short, short_stop_price)
strategy.exit("Trailing Exit Short", from_entry="Short", stop=current_trailing_stop_short, comment="Trailing SL Short")
// Plot background color to indicate active position (optional)
bgcolor(strategy.position_size > 0 ? color.new(color.green, 90) : na, title="Long Position Background")
bgcolor(strategy.position_size < 0 ? color.new(color.red, 90) : na, title="Short Position Background")