OPEN-SOURCE SCRIPT

Moving Average Convergence Divergence

165
This script is written in Pine Script (version 6) for TradingView and implements the **Moving Average Convergence Divergence (MACD)** indicator. The MACD is a popular momentum oscillator used to identify trend direction, strength, and potential reversals. This version includes customizable inputs, visual enhancements (like crossover markers), and alerts for key events. Below is a detailed explanation of the script:

---

### **1. Purpose**
- The script calculates and displays the MACD line, signal line, and histogram.
- It highlights key events such as MACD/signal line crossovers and zero-line crosses with shapes and colors.
- It provides alerts for changes in the histogram's direction (rising to falling or vice versa).

---

### **2. User Inputs**
- **Fast Length**: Period for the fast moving average (default: 12).
- **Slow Length**: Period for the slow moving average (default: 26).
- **Source**: Data input for calculation (default: closing price, `close`).
- **Signal Smoothing**: Period for the signal line (default: 9, range: 1–50).
- **Oscillator MA Type**: Type of moving average for MACD calculation (options: SMA or EMA, default: EMA).
- **Signal Line MA Type**: Type of moving average for the signal line (options: SMA or EMA, default: EMA).

---

### **3. MACD Calculation**
The MACD is calculated in three parts:
1. **MACD Line**: Difference between the fast and slow moving averages.
- Fast MA: Either SMA or EMA of the source over `fast_length`.
- Slow MA: Either SMA or EMA of the source over `slow_length`.
- Formula: `macd = fast_ma - slow_ma`.
2. **Signal Line**: A moving average (SMA or EMA) of the MACD line over `signal_length`.
- Formula: `signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)`.
3. **Histogram**: Difference between the MACD line and the signal line.
- Formula: `hist = macd - signal`.

---

### **4. Key Events Detection**
#### **MACD/Signal Line Crossovers**
- **Bullish Cross**: MACD crosses above the signal line (`ta.crossover(macd, signal)`).
- **Bearish Cross**: MACD crosses below the signal line (`ta.crossunder(macd, signal)`).

#### **Zero Line Crosses**
- **Cross Above Zero**: MACD crosses above 0 (`ta.crossover(macd, 0)`).
- **Cross Below Zero**: MACD crosses below 0 (`ta.crossunder(macd, 0)`).

---

### **5. Colors**
- **MACD Line**: Green (#089981) if MACD > signal (bullish), red (#f23645) if MACD < signal (bearish).
- **Signal Line**: White (`color.white`).
- **Histogram**:
- Positive (MACD > signal): Light green (#B2DFDB) if decreasing, darker green (#26A69A) if increasing.
- Negative (MACD < signal): Light red (#FFCDD2) if increasing in magnitude, darker red (#FF5252) if decreasing in magnitude.
- **Zero Line**: Gray with 50% transparency (`color.new(#787B86, 50)`).

---

### **6. Visual Outputs**
#### **Plotted Lines**
- **MACD Line**: Plotted with dynamic coloring based on its position relative to the signal line.
- **Signal Line**: Plotted in white.
- **Histogram**: Displayed as columns, with colors indicating direction and momentum.
- **Zero Line**: Horizontal line at 0 for reference.

#### **Shapes for Key Events**
- **Bullish Cross Below Zero**: Green circle on the MACD line when MACD crosses above the signal line while still below zero.
- **Bearish Cross Above Zero**: Red circle on the MACD line when MACD crosses below the signal line while still above zero.
- **Cross Above Zero**: Green upward label at the zero line when MACD crosses above 0.
- **Cross Below Zero**: Red downward label at the zero line when MACD crosses below 0.

---

### **7. Alerts**
- **Rising to Falling**: Triggers when the histogram switches from positive (or zero) to negative.
- Condition: `hist[1] >= 0 and hist < 0`.
- Message: "MACD histogram switched from rising to falling".
- **Falling to Rising**: Triggers when the histogram switches from negative (or zero) to positive.
- Condition: `hist[1] <= 0 and hist > 0`.
- Message: "MACD histogram switched from falling to rising".

---

### **8. How It Works**
1. **Trend Direction**:
- MACD above signal line (green) suggests bullish momentum.
- MACD below signal line (red) suggests bearish momentum.
2. **Momentum Strength**:
- Histogram height shows the strength of the momentum (larger bars = stronger momentum).
- Histogram color changes indicate whether momentum is increasing or decreasing.
3. **Reversal Signals**:
- Crossovers between MACD and signal lines often signal potential trend changes.
- Zero-line crosses indicate shifts between bullish (above 0) and bearish (below 0) territory.

---

### **9. How to Use**
1. Add the script to TradingView.
2. Adjust inputs (e.g., fast/slow lengths, MA types) to suit your trading style.
3. Monitor the chart:
- Green MACD and upward histogram bars suggest bullish conditions.
- Red MACD and downward histogram bars suggest bearish conditions.
- Watch for circles (crossovers) and labels (zero-line crosses) for trade signals.
4. Set up alerts to notify you of histogram direction changes.

---

### **10. Key Features**
- **Customization**: Flexible MA types and periods.
- **Visual Clarity**: Dynamic colors and shapes highlight key events.
- **Alerts**: Notifies users of momentum shifts via histogram changes.
- **Intuitive**: Combines all MACD components (line, signal, histogram) in one indicator.

This script is ideal for traders who rely on MACD for momentum analysis and want clear visual cues and alerts for decision-making.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.