Detailed Guide
1. Indicator Overview
Purpose:
This script combines the Supertrend and MACD indicators to help you detect potential trend changes. It plots a Supertrend line (green for bullish, red for bearish) and marks the chart with shapes when a trend reversal is signaled by both indicators. In addition, it includes alert conditions so that you can be notified when a potential trend change occurs.
How It Works:
Supertrend: Uses the Average True Range (ATR) to determine dynamic support and resistance levels. When the price crosses these levels, it signals a possible change in trend.
MACD: Focuses on the crossover between the MACD line and the signal line. A bullish crossover (MACD line crossing above the signal line) suggests upward momentum, while a bearish crossover (MACD line crossing below the signal line) suggests downward momentum.
2. Supertrend Component
Key Parameters:
Factor:
Function: Multiplies the ATR to create an offset from the mid-price (hl2).
Adjustment Impact: Lower values make the indicator more sensitive (producing more frequent signals), while higher values result in fewer, more confirmed signals.
ATR Period:
Function: Sets the number of bars over which the ATR is calculated.
Adjustment Impact: A shorter period makes the ATR react more quickly to recent price changes (but can be noisy), whereas a longer period provides a smoother volatility measurement.
Trend Calculation:
The script compares the previous close with the dynamically calculated upper and lower bands. If the previous close is above the upper band, the trend is set to bullish (1); if it’s below the lower band, the trend is bearish (-1). The Supertrend line is then plotted in green for bullish trends and red for bearish trends.
3. MACD Component
Key Parameters:
Fast MA (Fast Moving Average):
Function: Represents a shorter-term average, making the MACD line more sensitive to recent price movements.
Slow MA (Slow Moving Average):
Function: Represents a longer-term average to smooth out the MACD line.
Signal Smoothing:
Function: Defines the period for the signal line, which is a smoothed version of the MACD line.
Crossover Logic:
The script uses the crossover() function to detect when the MACD line crosses above the signal line (bullish crossover) and crossunder() to detect when it crosses below (bearish crossover).
4. Combined Signal Logic
How Signals Are Combined:
Bullish Scenario:
When the MACD shows a bullish crossover (MACD line crosses above the signal line) and the Supertrend indicates a bullish trend (green line), a green upward triangle is plotted below the bar.
Bearish Scenario:
When the MACD shows a bearish crossover (MACD line crosses below the signal line) and the Supertrend indicates a bearish trend (red line), a red downward triangle is plotted above the bar.
Rationale:
By combining the signals from both indicators, you increase the likelihood that the detected trend change is reliable, filtering out some false signals.
5. Alert Functionality
Alert Setup in the Code:
The alertcondition() function is used to define conditions under which TradingView can trigger alerts.
There are two alert conditions:
Bullish Alert: Activated when there is a bullish MACD crossover and the Supertrend confirms an uptrend.
Bearish Alert: Activated when there is a bearish MACD crossover and the Supertrend confirms a downtrend.
What Happens When an Alert Triggers:
When one of these conditions is met, TradingView registers the alert condition. You can then create an alert in TradingView (using the alert dialog) and choose one of these alert conditions. Once set up, you’ll receive notifications (via pop-ups, email, or SMS, depending on your settings) whenever a trend change is signaled.
6. User Adjustments and Their Effects
Factor (Supertrend):
Adjustment: Lowering the factor increases sensitivity, resulting in more frequent signals; raising it will filter out some signals, making them potentially more reliable.
ATR Period (Supertrend):
Adjustment: A shorter ATR period makes the indicator more responsive to recent price movements (but can introduce noise), while a longer period smooths out the response.
MACD Parameters (Fast MA, Slow MA, and Signal Smoothing):
Adjustment:
Shortening the Fast MA increases sensitivity, generating earlier signals that might be less reliable.
Lengthening the Slow MA produces a smoother MACD line, reducing noise.
Adjusting the Signal Smoothing changes how quickly the signal line responds to changes in the MACD line.
7. Best Practices and Considerations
Multiple Confirmation:
Even if both indicators signal a trend change, consider confirming with additional analysis such as volume, price action, or other indicators.
Market Conditions:
These indicators tend to perform best in trending markets. In sideways or choppy conditions, you may experience more false alerts.
Backtesting:
Before applying the indicator in live trading, backtest your settings to ensure they suit your trading style and the market conditions.
Risk Management:
Always use proper risk management, including stop-loss orders and appropriate position sizing, as alerts may occasionally produce late or false signals.
Happy trading!