资源加载中... loading...

MACD Multi-Interval Dynamic Stop-Loss and Take-Profit Trading System

Author: ChaoZhang, Date: 2024-11-29 15:01:33
Tags: MACDMASMAEMA

img

Overview

This strategy is an automated trading system based on the MACD indicator, incorporating dynamic stop-loss and take-profit mechanisms. The core strategy determines trading signals through MACD line and signal line crossovers, while integrating percentage-based stop-loss, profit targets, and trailing stops for risk management. The strategy calculates the MACD indicator using the difference between fast and slow moving averages, identifying market trend reversal points through signal line crossovers to make corresponding trading decisions.

Strategy Principles

The core logic includes several key components:

  1. MACD Calculation: Uses default periods of 12 and 26 days for fast and slow moving averages, with a 9-day signal line smoothing period.
  2. Entry Signals: The system generates long signals when the MACD line crosses above the signal line; short signals are generated when the MACD line crosses below the signal line.
  3. Risk Management: Incorporates three protection mechanisms:
    • Fixed Stop Loss: 1% below entry price
    • Profit Target: 2% above entry price
    • Trailing Stop: 1.5% dynamic trailing stop distance

Strategy Advantages

  1. Systematic Trading: Fully automated trading decision process, avoiding emotional interference.
  2. Multiple Risk Controls: Achieves comprehensive risk management through fixed stops, profit targets, and trailing stops.
  3. Adjustable Parameters: All key parameters can be optimized for different market conditions.
  4. Trend Following: Effectively captures market trend reversal points, improving trading success rate.

Strategy Risks

  1. Choppy Market Risk: May generate frequent false signals in sideways markets.
  2. Slippage Risk: Actual execution prices may deviate from ideal prices during high volatility.
  3. Parameter Sensitivity: Optimal parameters may vary significantly across different market environments.
  4. Systemic Risk: Sudden market changes may cause stop-loss failures.

Strategy Optimization Directions

  1. Add Market Environment Filters:
    • Incorporate volatility indicators to screen trading opportunities
    • Confirm signal validity with volume analysis
  2. Optimize Parameter Adaptation:
    • Implement dynamic parameter adjustment mechanisms
    • Automatically select optimal parameters based on market characteristics
  3. Enhance Risk Control:
    • Add money management module
    • Develop more sophisticated stop-loss mechanisms

Summary

This strategy constructs a robust automated trading system through MACD crossover signals and comprehensive risk management. While there is room for optimization, the basic framework is already well-developed. Through continuous optimization and improvement, the strategy has the potential to maintain stable performance across different market environments. For live trading implementation, it is recommended to conduct thorough backtesting and adjust parameters according to specific market characteristics.


/*backtest
start: 2024-01-01 00:00:00
end: 2024-11-01 00:00:00
period: 12h
basePeriod: 12h
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/
// © traderhub


//@version=5
strategy("MACD Strategy with Settings", overlay=true)

// Параметры MACD в контрольной панели
fastLength = input.int(12, title="Fast Length", minval=1, maxval=50)
slowLength = input.int(26, title="Slow Length", minval=1, maxval=50)
signalSmoothing = input.int(9, title="Signal Smoothing", minval=1, maxval=50)

// Параметры риска
stopLossPerc = input.float(1, title="Stop Loss (%)", step=0.1) // Стоп-лосс в процентах
takeProfitPerc = input.float(2, title="Take Profit (%)", step=0.1) // Тейк-профит в процентах
trailStopPerc = input.float(1.5, title="Trailing Stop (%)", step=0.1) // Трейлинг-стоп в процентах

// Вычисляем MACD
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalSmoothing)

// Показываем MACD и сигнальную линию на графике
plot(macdLine, color=color.blue, title="MACD Line")
plot(signalLine, color=color.red, title="Signal Line")
hline(0, "Zero Line", color=color.gray)

// Условия для покупки и продажи
longCondition = ta.crossover(macdLine, signalLine) // Покупка при пересечении MACD вверх сигнальной линии
shortCondition = ta.crossunder(macdLine, signalLine) // Продажа при пересечении MACD вниз сигнальной линии

// Расчет стоп-лосса и тейк-профита
var float longStopLevel = na
var float longTakeProfitLevel = na

if (longCondition)
    longStopLevel := strategy.position_avg_price * (1 - stopLossPerc / 100)
    longTakeProfitLevel := strategy.position_avg_price * (1 + takeProfitPerc / 100)
    strategy.entry("Long", strategy.long)

if (strategy.position_size > 0)
    // Установка стоп-лосса и тейк-профита
    strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLevel, limit=longTakeProfitLevel, trail_offset=trailStopPerc)

// Закрытие позиции при медвежьем сигнале
if (shortCondition)
    strategy.close("Long")
    strategy.entry("Short", strategy.short)


Related

More