
This strategy is a trend-following trading system that combines MACD (Moving Average Convergence Divergence) and Parabolic SAR (Stop and Reverse) indicators. By integrating momentum and trend indicators, it quantifies trend strength while identifying market direction, thereby capturing higher quality trading opportunities. The strategy uses MACD line crossovers to confirm trend momentum while utilizing SAR points to confirm trend direction and set trailing stops.
The core logic consists of two components: 1. MACD Component: Calculates MACD line using 12-period and 26-period exponential moving averages, with a 9-period moving average as the signal line. MACD line crossing above the signal line indicates a bullish signal, while crossing below indicates a bearish signal. 2. SAR Component: Calculates SAR points using default parameters (start 0.02, increment 0.02, maximum 0.2). Confirms uptrend when price is above SAR points and downtrend when below.
Entry Rules: - Long Condition: MACD line above signal line and price above SAR points - Short Condition: MACD line below signal line and price below SAR points
Exit Rules: - Long Positions: Exit when short signal appears - Short Positions: Exit when long signal appears
Add Market Environment Filtering: Incorporate volatility indicators (like ATR) to assess market conditions, reducing trading frequency or pausing during low volatility periods.
Enhance Stop-Loss Mechanism: Implement a combination of fixed percentage and trailing stops alongside SAR stops to improve risk control stability.
Optimize Parameter Selection: Utilize machine learning methods to automatically optimize MACD and SAR parameter combinations for different market cycles.
Incorporate Volume Analysis: Include volume indicators to confirm trend strength and improve signal reliability.
This strategy creates a comprehensive trend-following trading system by combining MACD and Parabolic SAR. It offers clear signals, controllable risk, and strong adaptability, but also has limitations such as trend dependency and signal lag. Through improvements in market environment filtering and stop-loss optimization, the strategy’s stability and practicality can be further enhanced. It is suitable for traders focusing on medium to long-term trends, with recommended thorough parameter optimization and backtesting before live implementation.
/*backtest
start: 2024-02-21 00:00:00
end: 2024-11-25 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("MACD + Parabolic SAR Strategy", shorttitle="MACD+SAR", overlay=true)
//========== User Inputs ==========//
// MACD parameters
fastLength = input.int(12, "MACD Fast Length")
slowLength = input.int(26, "MACD Slow Length")
signalLength = input.int(9, "MACD Signal Length")
// SAR parameters (start, step, maximum)
afStart = input.float(0.02, "SAR Start")
afIncrement = input.float(0.02, "SAR Increment")
afMax = input.float(0.2, "SAR Max")
//========== MACD Calculation ==========//
[macdLine, signalLine, histLine] = ta.macd(close, fastLength, slowLength, signalLength)
//========== Parabolic SAR Calculation ==========//
sarValue = ta.sar(afStart, afIncrement, afMax)
//========== Entry Conditions ==========//
// Long: MACD > Signal + close > SAR
longCondition = (macdLine > signalLine) and (close > sarValue)
// Short: MACD < Signal + close < SAR
shortCondition = (macdLine < signalLine) and (close < sarValue)
//========== Enter Positions ==========//
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.entry("Short", strategy.short)
//========== Exit Positions on Opposite Signal ==========//
if strategy.position_size > 0 and shortCondition
strategy.close("Long", comment="Exit Long")
if strategy.position_size < 0 and longCondition
strategy.close("Short", comment="Exit Short")