本策略是一个基于技术指标的波动交易策略,结合了均线交叉、RSI超买超卖和ATR止损止盈等多重信号。策略的核心是通过短期EMA和长期SMA的交叉来捕捉市场趋势,同时利用RSI指标进行信号确认,并通过ATR动态设置止损和止盈位置。策略支持多空双向交易,并可以根据用户偏好灵活开启或关闭任一方向。
策略采用了多层技术指标组合的方式构建交易系统: 1. 趋势判断层:使用20周期EMA和50周期SMA的交叉来判断趋势方向,EMA上穿SMA视为做多信号,下穿则为做空信号。 2. 动量确认层:使用RSI指标进行超买超卖判断,RSI低于70时允许做多,高于30时允许做空。 3. 波动性计算层:使用14周期ATR来计算止损止盈位置,止损设置为1.5倍ATR,止盈设置为3倍ATR。 4. 仓位管理层:基于初始资金和每笔交易风险比例(默认1%)来动态计算开仓数量。
该策略通过多重技术指标的组合运用,构建了一个相对完整的交易系统。策略的优势在于信号确认的可靠性和风险管理的完整性,但也需要注意市场环境对策略表现的影响。通过建议的优化方向,策略还有较大的改进空间。在实盘应用时,建议进行充分的参数测试和回测验证。
/*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"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CryptoRonin84
//@version=5
strategy("Swing Trading Strategy with On/Off Long and Short", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Input for turning Long and Short trades ON/OFF
enable_long = input.bool(true, title="Enable Long Trades")
enable_short = input.bool(true, title="Enable Short Trades")
// Input parameters for strategy
sma_short_length = input.int(20, title="Short EMA Length", minval=1)
sma_long_length = input.int(50, title="Long SMA Length", minval=1)
sl_percentage = input.float(1.5, title="Stop Loss (%)", step=0.1, minval=0.1)
tp_percentage = input.float(3, title="Take Profit (%)", step=0.1, minval=0.1)
risk_per_trade = input.float(1, title="Risk Per Trade (%)", step=0.1, minval=0.1)
capital = input.float(10000, title="Initial Capital", step=100)
// Input for date range for backtesting
start_date = input(timestamp("2020-01-01 00:00"), title="Backtest Start Date")
end_date = input(timestamp("2024-12-31 23:59"), title="Backtest End Date")
inDateRange = true
// Moving averages
sma_short = ta.ema(close, sma_short_length)
sma_long = ta.sma(close, sma_long_length)
// RSI setup
rsi = ta.rsi(close, 14)
rsi_overbought = 70
rsi_oversold = 30
// ATR for volatility-based stop-loss calculation
atr = ta.atr(14)
stop_loss_level_long = strategy.position_avg_price - (1.5 * atr)
stop_loss_level_short = strategy.position_avg_price + (1.5 * atr)
take_profit_level_long = strategy.position_avg_price + (3 * atr)
take_profit_level_short = strategy.position_avg_price - (3 * atr)
// Position sizing based on risk per trade
risk_amount = capital * (risk_per_trade / 100)
position_size = risk_amount / (close * sl_percentage / 100)
// Long and Short conditions
long_condition = ta.crossover(sma_short, sma_long) and rsi < rsi_overbought
short_condition = ta.crossunder(sma_short, sma_long) and rsi > rsi_oversold
// Execute long trades
if (long_condition and inDateRange and enable_long)
strategy.entry("Long", strategy.long, qty=position_size)
strategy.exit("Take Profit/Stop Loss", "Long", stop=stop_loss_level_long, limit=take_profit_level_long)
// Execute short trades
if (short_condition and inDateRange and enable_short)
strategy.entry("Short", strategy.short, qty=position_size)
strategy.exit("Take Profit/Stop Loss", "Short", stop=stop_loss_level_short, limit=take_profit_level_short)
// Plot moving averages
plot(sma_short, title="Short EMA", color=color.blue)
plot(sma_long, title="Long SMA", color=color.red)
// Plot RSI on separate chart
hline(rsi_overbought, "Overbought", color=color.red)
hline(rsi_oversold, "Oversold", color=color.green)
plot(rsi, title="RSI", color=color.purple)
// Plot signals on chart
plotshape(series=long_condition and enable_long, title="Long Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=short_condition and enable_short, title="Short Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Background color for backtest range
bgcolor(inDateRange ? na : color.red, transp=90)