OPEN-SOURCE SCRIPT
macd sma20

### MACD_sma20 – Multi-Timeframe MACD Pullback & SMA20 Dashboard
This script is a complete trading toolkit built around a **MACD pullback strategy** combined with **multi-timeframe SMA20 filters**, volume analysis, and a compact information panel.
It is designed for traders who like to:
* Trade **MACD pullbacks above the moving average**
* Track **key SMA20 levels across multiple timeframes** (Daily, 3-Day, Weekly, Monthly)
* Quickly see whether **current price is above or below those reference levels**
* Use **clean visual signals** for entries and exits, instead of staring at raw indicator values
---
### Core Features
#### 1. MACD Pullback Long Signal (Green Triangle Up)
The script detects a **bullish MACD pullback** pattern:
* MACD line is still **above** the signal line
* Both MACD line and histogram **pull back** for several bars
* Then MACD turns back up again, with price trading **above the local SMA20**
When this “pullback and re-acceleration” is confirmed, a **green triangle below the bar** is plotted as a **long entry signal**.
There is also an optional filter:
* **Weekly SMA20 filter**:
If enabled, long signals are only triggered when **current price is above the Weekly SMA20**, helping you stay on the right side of the higher-timeframe trend.
---
#### 2. Bearish Pullback Confirmation Signal (Red Triangle Down)
On the short side, the script detects a **bearish pullback confirmation** based on:
* A recent **high-volume bearish candle** (large down bar with volume above a multiple of the 20-period volume average)
* At least a minimum number of **negative MACD histogram bars**
* MACD line moving closer to the signal line (loss of momentum)
* Price recovering back up near the **top of that high-volume bearish candle**, then starting to fall again while MACD stays positive
When all conditions align, the script prints a **red triangle above the bar**, indicating a **bearish pullback confirmation** – often a good area to take profits on longs or consider short/hedge setups.
---
#### 3. Signal History Tracking
For both long and short signals, the script internally tracks the **most recent three signals**:
* Timestamp of the signal
* Price at the signal
* Short-term percentage change into the signal
This is mainly for internal use and future expansion, but already gives you a structured signal history if you want to extend or connect the logic later.
---
### Multi-Timeframe SMA20 Dashboard (Bottom-Right Panel)
One of the most useful parts of this script is the **compact dashboard table** in the **bottom-right corner** of the chart. It updates in real time and shows:
1. **Current Price**
2. **Daily SMA20** – value + whether price is above/below
3. **3-Day SMA20** – value + whether price is above/below
4. **Weekly SMA20** – value + whether price is above/below
5. **Monthly SMA20** – value + whether price is above/below
6. **RSI** (current timeframe)
For each timeframe’s SMA20:
* If **price ≥ SMA20**, the status cell is **green** with a ✓
* If **price < SMA20**, the status cell is **red** with a ✗
This gives you, at a glance:
* Is the market in a **short-term uptrend or downtrend** (Daily SMA20)?
* Is the **swing / position trend** healthy (3D & Weekly SMA20)?
* Is the broader **macro structure** supportive (Monthly SMA20)?
You don’t need to manually switch timeframes or add multiple moving averages – the script does all of that for you automatically using `request.security`.
---
### Alerts
The script comes with two built-in alert conditions:
* **MACD回踩转多信号 (MACD pullback bullish signal)**
* **空头回抽确认信号 (Bearish pullback confirmation signal)**
You can attach TradingView alerts to these conditions to get notified whenever a new long or bearish-confirmation setup appears, even when you’re not watching the chart.
---
### How to Use It in Your Trading
1. **Choose your main trading timeframe**
* For intraday swing: 15m / 1h / 4h
* For swing / position: 4h / Daily
2. **Watch the bottom-right SMA20 panel**
* If most higher-timeframe SMA20 rows are **green**, you are trading **with the larger trend**.
* If they are **mixed or mostly red**, you’re either counter-trend or in a choppy transition zone.
3. **Use the green MACD pullback signals**
* Prefer long setups when:
* The **Weekly and Monthly SMA20 rows are green**, and
* The signal appears **above the Daily SMA20**
* This stacks multiple edges: trend + pullback + momentum re-acceleration.
4. **Use the red bearish confirmation signals for risk management**
* Take partial profits on longs when a red signal appears near resistance.
* Consider hedge/short opportunities if higher-timeframe SMA20 rows are already red or turning red.
5. **Use RSI as a context indicator**
* Combine with overbought/oversold zones or your own RSI thresholds for additional confirmation.
---
### Why This Script Is Useful
* **Trend awareness across timeframes**:
You always know where current price sits relative to the Daily / 3-Day / Weekly / Monthly SMA20 – without switching charts.
* **Clear, rule-based signals**:
The MACD logic is explicit and systematic, focused on **pullbacks within trends** rather than random crossovers.
* **Volume-aware bearish logic**:
High-volume bearish candles often mark important supply zones. The script builds this idea directly into the short-side confirmation logic.
* **Visual and intuitive**:
Green/Red triangles + Green/Red table cells make it easy to interpret even if you are not a heavy indicator user.
* **Flexible**:
All key parameters (MACD lengths, SMA length, volume threshold, lookback period, RSI length, weekly filter) are customizable, so you can adapt it to different markets (crypto, stocks, FX) and timeframes.
---
In short, this script is a **multi-timeframe MACD pullback system with an integrated SMA20 dashboard**, suitable for swing traders and position traders who want a structured, visually clean way to align entries with trend and momentum while keeping an eye on higher-timeframe levels.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.