This is an automated trading strategy system based on dual moving average crossover. The system utilizes 9-period and 21-period Exponential Moving Averages (EMA) as core indicators, generating trading signals through their crossovers. It incorporates stop-loss and take-profit management, along with a visual interface that displays trading signals and key price levels.
The strategy employs a fast EMA (9-period) and a slow EMA (21-period) to construct the trading system. Long signals are generated when the fast EMA crosses above the slow EMA, while short signals occur when the fast EMA crosses below the slow EMA. The system automatically sets stop-loss and take-profit levels based on preset percentages for each trade. Position sizing uses a percentage-based approach, defaulting to 100% of account equity.
This is a well-designed, logically sound moving average crossover strategy system. By combining EMA crossover signals with risk management mechanisms, the strategy can capture profits in trending markets. While inherent risks exist, the suggested optimizations can further enhance the strategy’s stability and reliability. This strategy is particularly suitable for tracking medium to long-term trends and represents a solid choice for patient traders.
/*backtest start: 2019-12-23 08:00:00 end: 2024-12-04 00:00:00 period: 1d basePeriod: 1d exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 // // ██╗ █████╗ ██████╗ ██████╗ ██╗ ██╗ ██╗ // ██║ ██╔══██╗ ██╔═══██╗ ██╔══██╗ ██║ ██║ ██║ // ██║ ███████║ ██║ ██║ ██║ ██║ ██║ ██║ ██║ // ██║ ██╔══██║ ██║ ██║ ██║ ██║ ██║ ██║ ██║ // ███████╗ ██║ ██║ ╚██████╔╝ ██████╔╝ ╚██████╔╝ ██║ // ╚══════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ // // BTC-EMA做多策略(5分钟确认版) - 作者:LAODUI // 版本:2.0 // 最后更新:2024 // ═══════════════════════════════════════════════════════════════════════════ strategy("EMA Cross Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) // 添加策略参数设置 var showLabels = input.bool(true, "显示标签", group="显示设置") var stopLossPercent = input.float(5.0, "止损百分比", minval=0.1, maxval=20.0, step=0.1, group="风险管理") var takeProfitPercent = input.float(10.0, "止盈百分比", step=0.1, group="风险管理") // EMA参数设置 var emaFastLength = input.int(9, "快速EMA周期", minval=1, maxval=200, group="EMA设置") var emaSlowLength = input.int(21, "慢速EMA周期", minval=1, maxval=200, group="EMA设置") // 计算EMA ema_fast = ta.ema(close, emaFastLength) ema_slow = ta.ema(close, emaSlowLength) // 绘制EMA线 plot(ema_fast, "快速EMA", color=color.blue, linewidth=2) plot(ema_slow, "慢速EMA", color=color.red, linewidth=2) // 检测交叉 crossOver = ta.crossover(ema_fast, ema_slow) crossUnder = ta.crossunder(ema_fast, ema_slow) // 格式化时间显示 (UTC+8) utc8Time = time + 8 * 60 * 60 * 1000 timeStr = str.format("{0,date,MM-dd HH:mm}", utc8Time) // 计算止损止盈价格 longStopLoss = strategy.position_avg_price * (1 - stopLossPercent / 100) longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent / 100) shortStopLoss = strategy.position_avg_price * (1 + stopLossPercent / 100) shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent / 100) // 交易逻辑 if crossOver if strategy.position_size < 0 strategy.close("做空") strategy.entry("做多", strategy.long) if showLabels label.new(bar_index, high, text="做多入场\n" + timeStr + "\n入场价: " + str.tostring(close) + "\n止损价: " + str.tostring(longStopLoss) + "\n止盈价: " + str.tostring(longTakeProfit), color=color.green, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar) if crossUnder if strategy.position_size > 0 strategy.close("做多") strategy.entry("做空", strategy.short) if showLabels label.new(bar_index, low, text="做空入场\n" + timeStr + "\n入场价: " + str.tostring(close) + "\n止损价: " + str.tostring(shortStopLoss) + "\n止盈价: " + str.tostring(shortTakeProfit), color=color.red, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar) // 设置止损止盈 if strategy.position_size > 0 // 多仓止损止盈 strategy.exit("多仓止损止盈", "做多", stop=longStopLoss, limit=longTakeProfit) if strategy.position_size < 0 // 空仓止损止盈 strategy.exit("空仓止损止盈", "做空", stop=shortStopLoss, limit=shortTakeProfit)