The resource loading... loading...

EMA-MACD High-Frequency Quantitative Strategy with Smart Risk Management

Author: ChaoZhang, Date: 2024-12-05 14:54:01
Tags: EMAMACDATR

img

Overview

This strategy is a high-frequency quantitative trading system based on EMA and MACD indicators, combined with ATR dynamic stop-loss and intelligent position management. The strategy uses 9-period and 21-period EMA crossovers as primary entry signals, confirmed by MACD indicator, and calculates stop-loss and profit targets dynamically through ATR, achieving a complete trading loop and risk control system.

Strategy Principle

The strategy employs multiple technical indicators to identify trading opportunities. First, it uses short-period (9) and long-period (21) EMA crossovers as preliminary signals, generating long signals when the short-term moving average crosses above the long-term moving average, and vice versa. Second, it uses an optimized MACD indicator (6,13,4) for signal confirmation, requiring the MACD line and signal line relationship to align with the EMA cross direction. For risk control, the strategy uses the ATR indicator to dynamically calculate stop-loss distances while maintaining a 1:2 risk-reward ratio for profit targets. Additionally, the strategy implements percentage-based risk management based on account size, limiting each trade’s risk to 1% of the account.

Strategy Advantages

  1. Signal system uses multiple confirmation mechanisms, improving trading accuracy
  2. Dynamic ATR stop-loss settings adapt to different market environments
  3. Strict risk control system, including fixed risk and dynamic position management
  4. Complete trade automation, including entry, stop-loss, and profit target execution
  5. Visualized trade management, including real-time display of stop-loss and profit levels
  6. Optimized indicator parameters suitable for short-term high-frequency trading

Strategy Risks

  1. High-frequency trading may face slippage and commission erosion
  2. EMA and MACD may generate false signals in ranging markets
  3. ATR stops may trigger premature exits during extreme volatility
  4. Fixed risk-reward ratio may need adjustment in different market environments
  5. System stability and latency issues need consideration

Optimization Directions

  1. Introduce market environment filters, such as volatility indicators or trend strength indicators
  2. Optimize MACD parameters, considering dynamic adjustment based on different timeframes
  3. Improve stop-loss mechanism, possibly adding trailing stops or support-based stops
  4. Add volume analysis to optimize entry timing
  5. Develop a more sophisticated money management system, such as dynamic risk percentage adjustment

Summary

The strategy combines classical technical indicators with modern risk management methods to build a complete high-frequency trading system. The core advantages lie in multiple signal confirmation and strict risk control, though it still requires thorough testing and optimization in live trading environments. Through continuous improvement and risk management refinement, the strategy shows promise for maintaining stable performance across different market conditions.


/*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
strategy("High-Frequency Trade Script with EMA, MACD, and ATR-based TP/SL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=2, initial_capital=100000)

// إعداد المؤشرات
emaBuy = ta.ema(close, 9)       // EMA بفترة قصيرة للشراء
emaSell = ta.ema(close, 21)     // EMA بفترة أطول للبيع
[macdLine, signalLine, _] = ta.macd(close, 6, 13, 4) // MACD بفترات قصيرة
atr = ta.atr(14)  // حساب مؤشر ATR

// إعداد نسبة وقف الخسارة وجني الأرباح
stopLossATRMultiplier = 1.5  // تقليل وقف الخسارة لـ 1.5 * ATR
riskToRewardRatio = 2.0  // نسبة العائد إلى المخاطرة 1:2

// إعداد إدارة المخاطر
riskPercentage = 1.0  // المخاطرة كـ 1% من رأس المال
capital = strategy.equity  // إجمالي رأس المال
riskAmount = capital * (riskPercentage / 100)  // مقدار المخاطرة

// شروط إشارات الشراء: تقاطع EMA القصير فوق الطويل و MACD أعلى من Signal
longCondition = ta.crossover(emaBuy, emaSell) and macdLine > signalLine

// شروط إشارات البيع: تقاطع EMA القصير تحت الطويل و MACD أسفل Signal
shortCondition = ta.crossunder(emaBuy, emaSell) and macdLine < signalLine

// --- تنفيذ أوامر الشراء والبيع تلقائيًا مع وقف الخسارة وجني الأرباح --- //
// تعريف خطوط وقف الخسارة وجني الأرباح
var line longStopLossLine = na
var line longTakeProfitLine = na
var line shortStopLossLine = na
var line shortTakeProfitLine = na

if (longCondition)
    longEntryPrice = close  // سعر الدخول للشراء
    longStopLoss = longEntryPrice - (atr * stopLossATRMultiplier)  // وقف الخسارة بناءً على ATR
    longTakeProfit = longEntryPrice + ((longEntryPrice - longStopLoss) * riskToRewardRatio)  // جني الأرباح بنسبة 1:2

    // حساب حجم الصفقة بناءً على مقدار المخاطرة
    positionSize = riskAmount / (longEntryPrice - longStopLoss)  // حجم العقد

    // إدخال أمر الشراء
    strategy.entry("Buy", strategy.long, qty=positionSize)
    
    // إعداد أوامر وقف الخسارة وجني الأرباح
    strategy.exit("Take Profit/Stop Loss", "Buy", stop=longStopLoss, limit=longTakeProfit)

    // رسم الخطوط لجني الأرباح ووقف الخسارة
    // longStopLossLine := line.new(bar_index, longStopLoss, bar_index + 1, longStopLoss, color=color.red, width=1, style=line.style_dashed)  // خط وقف الخسارة
    // longTakeProfitLine := line.new(bar_index, longTakeProfit, bar_index + 1, longTakeProfit, color=color.green, width=1, style=line.style_dashed)  // خط جني الأرباح

if (shortCondition)
    shortEntryPrice = close  // سعر الدخول للبيع
    shortStopLoss = shortEntryPrice + (atr * stopLossATRMultiplier)  // وقف الخسارة بناءً على ATR
    shortTakeProfit = shortEntryPrice - ((shortStopLoss - shortEntryPrice) * riskToRewardRatio)  // جني الأرباح بنسبة 1:2

    // حساب حجم الصفقة بناءً على مقدار المخاطرة
    positionSize = riskAmount / (shortStopLoss - shortEntryPrice)  // حجم العقد

    // إدخال أمر البيع
    strategy.entry("Sell", strategy.short, qty=positionSize)
    
    // إعداد أوامر وقف الخسارة وجني الأرباح
    strategy.exit("Take Profit/Stop Loss", "Sell", stop=shortStopLoss, limit=shortTakeProfit)

    // رسم الخطوط لجني الأرباح ووقف الخسارة
    // shortStopLossLine := line.new(bar_index, shortStopLoss, bar_index + 1, shortStopLoss, color=color.red, width=1, style=line.style_dashed)  // خط وقف الخسارة
    // shortTakeProfitLine := line.new(bar_index, shortTakeProfit, bar_index + 1, shortTakeProfit, color=color.green, width=1, style=line.style_dashed)  // خط جني الأرباح

// --- رسم مؤشرات منفصلة --- //
plot(emaBuy, title="EMA Buy (9)", color=color.green, linewidth=2)   // EMA الشراء
plot(emaSell, title="EMA Sell (21)", color=color.red, linewidth=2)  // EMA البيع
plot(macdLine, title="MACD Line", color=color.blue, linewidth=1)    // MACD Line
plot(signalLine, title="Signal Line", color=color.orange, linewidth=1)  // Signal Line

Related

More