OPEN-SOURCE SCRIPT
Closest Candle to EMA (CCE)

🔍 Closest Candle to EMA (CCE)
The Closest Candle to EMA (CCE) indicator is a visual analytical tool designed to identify the historical price (candle close) that is closest to the current Exponential Moving Average (EMA) over a user-defined period. This allows traders to easily detect how price has interacted with the trend line recently, providing insights into potential mean reversion, support/resistance, and price convergence behavior.
📌 Key Features
✅ Highlights the candle with a closing price closest to the current EMA
✅ Customizable EMA length for various trading styles and timeframes
✅ Helps detect potential zones of trend interaction
✅ Supports analysis of price behavior near dynamic support/resistance
✅ Lightweight and non-intrusive visual overlay (red = closest price, blue = EMA)
🧠 How It Works
The script calculates the EMA using the user-defined length (default: 20).
It then scans the last N candles (equal to the EMA length) and finds the one whose closing price is closest to the current EMA value.
That close is highlighted in red, while the EMA is shown in blue.
This comparison helps traders understand the proximity of past price action to the current trend level.
💡 Use Cases
Mean Reversion Strategies – Spot when price historically reverts to the trend
Dynamic Support/Resistance Identification – Find levels where price respected or returned to the EMA
Consolidation Zone Analysis – Identify areas where price hovered around trend lines
Backtesting Trend Sensitivity – See how price reacted to EMA over time
⚙️ Settings
EMA Length – Set the number of periods used for EMA and comparison window (default: 20)
📊 Example Strategy Setup – EMA Touch with Reversal Candle
This indicator can be incorporated into a price-action strategy that combines candlestick patterns, EMA proximity, and volume confirmation. Here's a practical use case:
🔧 Note: This setup is designed specifically with the EMA length set to 9.
🔁 Bullish Setup – Hammer + EMA (in uptrend)
The market is in an uptrend, confirmed by EMA(9) sloping upward
A Hammer candlestick forms
The EMA (blue) must touch the lower shadow (wick) of the Hammer
It must not touch the candle body
Candle volume is above average
→ ✅ This may signal a bullish continuation opportunity
🔁 Bearish Setup – Shooting Star + EMA (in downtrend)
The market is in a downtrend, confirmed by EMA(9) sloping downward
A Shooting Star candlestick forms
The EMA (blue) must touch the upper shadow (wick) of the candle
It must not touch the candle body
Candle volume is above average
The Closest Candle to EMA (CCE) indicator is a visual analytical tool designed to identify the historical price (candle close) that is closest to the current Exponential Moving Average (EMA) over a user-defined period. This allows traders to easily detect how price has interacted with the trend line recently, providing insights into potential mean reversion, support/resistance, and price convergence behavior.
📌 Key Features
✅ Highlights the candle with a closing price closest to the current EMA
✅ Customizable EMA length for various trading styles and timeframes
✅ Helps detect potential zones of trend interaction
✅ Supports analysis of price behavior near dynamic support/resistance
✅ Lightweight and non-intrusive visual overlay (red = closest price, blue = EMA)
🧠 How It Works
The script calculates the EMA using the user-defined length (default: 20).
It then scans the last N candles (equal to the EMA length) and finds the one whose closing price is closest to the current EMA value.
That close is highlighted in red, while the EMA is shown in blue.
This comparison helps traders understand the proximity of past price action to the current trend level.
💡 Use Cases
Mean Reversion Strategies – Spot when price historically reverts to the trend
Dynamic Support/Resistance Identification – Find levels where price respected or returned to the EMA
Consolidation Zone Analysis – Identify areas where price hovered around trend lines
Backtesting Trend Sensitivity – See how price reacted to EMA over time
⚙️ Settings
EMA Length – Set the number of periods used for EMA and comparison window (default: 20)
📊 Example Strategy Setup – EMA Touch with Reversal Candle
This indicator can be incorporated into a price-action strategy that combines candlestick patterns, EMA proximity, and volume confirmation. Here's a practical use case:
🔧 Note: This setup is designed specifically with the EMA length set to 9.
🔁 Bullish Setup – Hammer + EMA (in uptrend)
The market is in an uptrend, confirmed by EMA(9) sloping upward
A Hammer candlestick forms
The EMA (blue) must touch the lower shadow (wick) of the Hammer
It must not touch the candle body
Candle volume is above average
→ ✅ This may signal a bullish continuation opportunity
🔁 Bearish Setup – Shooting Star + EMA (in downtrend)
The market is in a downtrend, confirmed by EMA(9) sloping downward
A Shooting Star candlestick forms
The EMA (blue) must touch the upper shadow (wick) of the candle
It must not touch the candle body
Candle volume is above average
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.
For quick access on a chart, add this script to your favorites — learn more here.
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.
For quick access on a chart, add this script to your favorites — learn more here.
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.