Certainly! Below is a TradingView Pine Script indicator that highlights candles that do not touch the 5-period Exponential Moving Average (EMA). Candles that do not touch the EMA 5 will be colored yellow on your chart.
//@version=5
indicator("Highlight Candles That Do Not Touch EMA 5", overlay=true)
// Calculate the 5-period EMA
ema5 = ta.ema(close, 5)
// Determine if the candle does not touch the EMA
// This happens when the candle's high is below the EMA or the low is above the EMA
no_touch = (high < ema5) or (low > ema5)
// Highlight candles that do not touch the EMA by changing their color
barcolor(no_touch ? color.yellow : na)
// Plot the EMA 5 for reference
plot(ema5, color=color.blue, linewidth=2)
How the Indicator Works:
-
EMA Calculation: The script calculates the 5-period EMA of the closing prices using ta.ema(close, 5).
-
No-Touch Condition: It checks for candles where:
- The high is below the EMA (
high < ema5), meaning the entire candle is below the EMA.
- The low is above the EMA (
low > ema5), meaning the entire candle is above the EMA.
-
Highlighting Candles:
- If a candle meets the no-touch condition, it's colored yellow using
barcolor(color.yellow).
- If the candle does touch the EMA, its color remains unchanged.
-
Plotting EMA: The EMA is plotted on the chart in blue for visual reference.
How to Use the Indicator:
-
Adding the Indicator:
- Copy the script above.
- Open TradingView and go to the Pine Editor tab at the bottom of the chart.
- Paste the script into the Pine Editor.
- Click on Add to Chart.
-
Interpreting the Chart:
- Candles highlighted in yellow are those that do not touch the EMA 5.
- The blue line represents the EMA 5.
Example Scenario:
- If a candle's entire range is above the EMA 5 line (the lowest price is higher than the EMA), it will be highlighted.
- If a candle's entire range is below the EMA 5 line (the highest price is lower than the EMA), it will also be highlighted.
- Candles that touch or cross the EMA 5 will not be highlighted.
Customization:
-
Changing Highlight Color: To change the highlight color, modify color.yellow in the barcolor function to any color you prefer (e.g., color.red, color.green, or a custom color code).
barcolor(no_touch ? color.new(color.red, 0) : na)
-
Adjusting EMA Length: To use a different EMA length, change the number 5 in ta.ema(close, 5) to your desired period.
ema_length = input(5, minval=1, title="EMA Length")
ema5 = ta.ema(close, ema_length)
-
Adding Inputs: You can add user-defined inputs to make the EMA length or highlight color adjustable from the indicator settings.
// User inputs
ema_length = input(5, minval=1, title="EMA Length")
highlight_color = input(color.yellow, title="Highlight Color")
// Calculate EMA
ema_n = ta.ema(close, ema_length)
// Determine no-touch condition
no_touch = (high < ema_n) or (low > ema_n)
// Highlight candles
barcolor(no_touch ? highlight_color : na)
// Plot EMA
plot(ema_n, color=color.blue, linewidth=2)
Notes:
- This indicator is designed for use on any timeframe and any financial instrument supported by TradingView.
- Ensure that the chart's style supports bar coloring (e.g., Candles, Bars).
Feel free to adjust and customize the script to suit your trading preferences. If you have any questions or need further assistance, don't hesitate to ask!