Strategy vault / Mean reversion
MACD Multi Interval Dynamic Stop Loss and Take Profit Trading System
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…

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:
- MACD Calculation: Uses default periods of 12 and 26 days for fast and slow moving averages, with a 9-day signal line smoothing period.
- 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.
- 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
- Systematic Trading: Fully automated trading decision process, avoiding emotional interference.
- Multiple Risk Controls: Achieves comprehensive risk management through fixed stops, profit targets, and trailing stops.
- Adjustable Parameters: All key parameters can be optimized for different market conditions.
- Trend Following: Effectively captures market trend reversal points, improving trading success rate.
Strategy Risks
- Choppy Market Risk: May generate frequent false signals in sideways markets.
- Slippage Risk: Actual execution prices may deviate from ideal prices during high volatility.
- Parameter Sensitivity: Optimal parameters may vary significantly across different market environments.
- Systemic Risk: Sudden market changes may cause stop-loss failures.
Strategy Optimization Directions
- Add Market Environment Filters:
- Incorporate volatility indicators to screen trading opportunities
- Confirm signal validity with volume analysis
- Optimize Parameter Adaptation:
- Implement dynamic parameter adjustment mechanisms
- Automatically select optimal parameters based on market characteristics
- 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.
Source Pine Script
/*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)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-29. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/MACD-Multi-Interval-Dynamic-Stop-Loss-and-Take-Profit-Trading-System.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- 1 2 3 Pattern Quantitative Trading Strategy with EMAs MACD and 4th Candle ExtensionPine Script · Mean reversion · 1h
- 3 10 Oscillator Profile Flagging StrategyPine Script · Mean reversion · 1d
- ATR and EMA based Dynamic Take Profit and Stop Loss Adaptive StrategyPine Script · Mean reversion · 1h
- Advanced 15 Minute Chart Trading Signal StrategyPine Script · Mean reversion · 3h
- BONK Multi Factor Trading StrategyPine Script · Mean reversion · 1d
- Bearish Harami Reversal Backtest StrategyPine Script · Mean reversion · 15m