Sure, I've updated your Pine Script code to include the new logic as you requested. The code now:
- Implements the new condition where candles that are above the EMA but not touching it and are within a low percentage of the Daily ATR away from the EMA, or candles below the EMA but not touching it and are a high percentage away from the EMA.
- Includes all other existing logic, such as requiring the last N candles to have touched the EMA, overlap checks, and size percentage checks.
- Highlights these new conditions with orange circles, while keeping the existing blue circles unchanged.
- Adds the new signal into the old alert conditions.
- Updates all variable names to be more meaningful and understandable for future use.
Here is the updated code with explanations:
//@version=5
indicator("Candles Not Touching EMA 5 with Overlap and Size Check (Updated)", overlay=true)
// —————————————— User Inputs ——————————————
// Last N Candles Touching EMA Check
enable_last_n_touching_check = input.bool(true, title="Require last N candles to have touched EMA?")
number_of_candles_to_check = input.int(3, minval=1, title="Number of candles to check")
// Stop Loss Feature
enable_stop_loss_feature = input.bool(true, title="Enable Stop Loss Levels")
stop_loss_atr_percentage = input.float(2.0, title="ATR Percentage for Stop Loss (%)", minval=0, step=0.1)
// Overlap Check
enable_overlap_check = input.bool(true, title="Require N% of Current Candle to Overlap with Last Candle?")
required_overlap_percentage = input.float(50.0, minval=0.0, maxval=100.0, title="Required Overlap Percentage N%")
// Candle Size Percentage Check
enable_size_percentage_check = input.bool(true, title="Require candle to be N% smaller than last candle?")
size_percentage_smaller = input.float(20.0, minval=0.01, title="Percentage smaller (%)")
// Existing Distance from EMA Check
enable_distance_check = input.bool(false, title="Enable Distance from EMA Check?")
distance_atr_percentage = input.float(2.0, minval=0.0, step=0.1, title="ATR Percentage for Distance from EMA (%)")
// Inputs to select which logic to ignore when Distance Condition is Met
ignore_last_n_touching_check = input.bool(false, title="Ignore Last N Candles Touching EMA Check when Distance Condition is Met?")
ignore_overlap_check = input.bool(false, title="Ignore Overlap Check when Distance Condition is Met?")
ignore_size_percentage_check = input.bool(false, title="Ignore Size Percentage Check when Distance Condition is Met?")
// New ATR Percentage Thresholds for New Conditions
low_atr_percentage_threshold_above = input.float(20.0, title="Low ATR Percentage Threshold Above EMA (%)", minval=0.0, step=0.1)
high_atr_percentage_threshold_below = input.float(80.0, title="High ATR Percentage Threshold Below EMA (%)", minval=0.0, step=0.1)
// —————————————— Calculations ——————————————
// Calculate EMA 5
ema5 = ta.ema(close, 5)
// Identify candles that do not touch EMA 5
candle_not_touching_ema = (low > ema5) or (high < ema5)
// Identify candles that touch EMA 5
candle_touching_ema = not candle_not_touching_ema
// Function to check if all of the last N candles touched the EMA
all_last_n_candles_touching_ema(n) =>
result = true
for i = 1 to n
result := result and candle_touching_ema[i]
result
// Initialize last_n_touching_condition
last_n_touching_condition = true
// Update last_n_touching_condition if the feature is enabled
if enable_last_n_touching_check
// Condition: all of the last N candles touched the EMA
last_n_touching_condition := all_last_n_candles_touching_ema(number_of_candles_to_check)
// —————————— Overlap Check Logic ——————————
// Initialize overlap_condition
overlap_condition = true
// Compute the overlap_condition if the feature is enabled
if enable_overlap_check
// Calculate current candle's range
current_candle_range = high - low
// Avoid division by zero
if current_candle_range != 0
// Calculate overlap between current candle and previous candle
overlap_high = math.min(high, high[1])
overlap_low = math.max(low, low[1])
overlap_range = overlap_high - overlap_low
// If overlap_range is negative or zero, set condition to false
if overlap_range <= 0
overlap_condition := false
else
// Calculate percentage of current candle's range that overlaps with previous candle's range
percentage_of_current_overlapping = (overlap_range / current_candle_range) * 100.0
// Check if percentage is greater than or equal to required_overlap_percentage
overlap_condition := percentage_of_current_overlapping >= required_overlap_percentage
else
// If current candle range is zero, set condition to false
overlap_condition := false
// —————— Candle Size Percentage Check Logic ——————
// Initialize size_smaller_condition
size_smaller_condition = true
// Update size_smaller_condition if the feature is enabled
if enable_size_percentage_check
// Calculate candle sizes
previous_candle_size = high[1] - low[1]
current_candle_size = high - low
// Avoid division by zero
if previous_candle_size != 0
// Calculate size difference percentage
size_difference_percentage = ((previous_candle_size - current_candle_size) / previous_candle_size) * 100.0
// Check if current candle is N% smaller than previous candle
size_smaller_condition := size_difference_percentage >= size_percentage_smaller
else
// If previous candle size is zero, set condition to false
size_smaller_condition := false
// ————— Existing Distance from EMA Condition —————
// Calculate Daily ATR
daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(14))
// Calculate ATR offset based on user-defined percentage
distance_atr_offset = (distance_atr_percentage / 100.0) * daily_atr
// Initialize Distance Condition
distance_condition = false
distance_from_ema = 0.0
if enable_distance_check and candle_not_touching_ema
if close > ema5
distance_from_ema := low - ema5
else if close < ema5
distance_from_ema := ema5 - high
else
distance_from_ema := 0
if distance_from_ema >= distance_atr_offset
distance_condition := true
// Adjust conditions based on Distance Condition and user preferences
last_n_touching_condition_adj = last_n_touching_condition
overlap_condition_adj = overlap_condition
size_smaller_condition_adj = size_smaller_condition
if distance_condition
if ignore_last_n_touching_check
last_n_touching_condition_adj := true
if ignore_overlap_check
overlap_condition_adj := true
if ignore_size_percentage_check
size_smaller_condition_adj := true
// —————————— Signal Conditions ———————————
// Existing signal conditions (including all logic)
signal_above_ema = candle_not_touching_ema and close > ema5 and last_n_touching_condition_adj and overlap_condition_adj and size_smaller_condition_adj
signal_below_ema = candle_not_touching_ema and close < ema5 and last_n_touching_condition_adj and overlap_condition_adj and size_smaller_condition_adj
// —————— New Conditions Based on ATR Percentage Thresholds ——————
// Calculate percentage distance from EMA for candles above and below EMA
percentage_distance_above = ((low - ema5) / daily_atr) * 100.0
percentage_distance_below = ((ema5 - high) / daily_atr) * 100.0
// New Conditions
new_condition_above = candle_not_touching_ema and close > ema5 and (percentage_distance_above <= low_atr_percentage_threshold_above)
new_condition_below = candle_not_touching_ema and close < ema5 and (percentage_distance_below >= high_atr_percentage_threshold_below)
// Incorporate adjusted other logic conditions
new_signal_above = new_condition_above and last_n_touching_condition_adj and overlap_condition_adj and size_smaller_condition_adj
new_signal_below = new_condition_below and last_n_touching_condition_adj and overlap_condition_adj and size_smaller_condition_adj
// Combined signal for any of the conditions
signal_any = signal_above_ema or signal_below_ema or new_signal_above or new_signal_below
// —————————— Plotting ———————————
// Plot EMA 5 for reference
plot(ema5, color=color.orange, linewidth=2, title="EMA 5")
// Plot arrows to highlight existing signals
// Arrow Down for candles entirely above EMA 5
plotshape(signal_above_ema, title="Signal Above EMA", style=shape.arrowdown, location=location.abovebar, color=color.green, size=size.tiny)
// Arrow Up for candles entirely below EMA 5
plotshape(signal_below_ema, title="Signal Below EMA", style=shape.arrowup, location=location.belowbar, color=color.red, size=size.tiny)
// Plot blue circles to highlight candles where Distance Condition is met
plotshape(distance_condition and (close > ema5), title="Distance Condition Met Above EMA", style=shape.circle, location=location.abovebar, color=color.blue, size=size.tiny)
plotshape(distance_condition and (close < ema5), title="Distance Condition Met Below EMA", style=shape.circle, location=location.belowbar, color=color.blue, size=size.tiny)
// Plot orange circles to highlight candles where new conditions are met
plotshape(new_signal_above, title="New Condition Met Above EMA", style=shape.circle, location=location.abovebar, color=color.orange, size=size.tiny)
plotshape(new_signal_below, title="New Condition Met Below EMA", style=shape.circle, location=location.belowbar, color=color.orange, size=size.tiny)
// —————————— Alert Conditions ———————————
// Alert Conditions
alertcondition(signal_any, title="Any Signal Alert", message="A signal has been triggered: either 'Above EMA' or 'Below EMA'.")
alertcondition(signal_above_ema, title="Above EMA Alert", message="Candle entirely above EMA 5, not touching it. Signal conditions met.")
alertcondition(signal_below_ema, title="Below EMA Alert", message="Candle entirely below EMA 5, not touching it. Signal conditions met.")
alertcondition(new_signal_above, title="New Above EMA Alert", message="Candle above EMA 5, new conditions met.")
alertcondition(new_signal_below, title="New Below EMA Alert", message="Candle below EMA 5, new conditions met.")
// —————————— Stop Loss Feature ———————————
// Initialize variables for stop loss levels
float stop_loss_level = na
color stop_loss_color = na
// Compute and plot stop loss levels when the feature is enabled
if enable_stop_loss_feature
if signal_above_ema
stop_loss_level := high + ((stop_loss_atr_percentage / 100.0) * daily_atr)
stop_loss_color := color.red
else if signal_below_ema
stop_loss_level := low - ((stop_loss_atr_percentage / 100.0) * daily_atr)
stop_loss_color := color.green
else
stop_loss_level := na
stop_loss_color := na
// Plot stop loss level
plot(stop_loss_level, title="Stop Loss Level", color=stop_loss_color, linewidth=2)
Explanation of the Updates:
-
Variable Names Updated: All variable names have been updated to be more meaningful and understandable. For example, condition_for_signal is now last_n_touching_condition, is_current_candle_N_percent_overlapping is now overlap_condition, and so on.
-
New User Inputs:
low_atr_percentage_threshold_above: The low ATR percentage threshold for candles above the EMA.
high_atr_percentage_threshold_below: The high ATR percentage threshold for candles below the EMA.
-
New Conditions Implemented:
- For Candles Above EMA: Check if the percentage distance from EMA is less than or equal to the
low_atr_percentage_threshold_above.
- For Candles Below EMA: Check if the percentage distance from EMA is greater than or equal to the
high_atr_percentage_threshold_below.
-
Integration with Existing Logic: The new conditions (new_signal_above and new_signal_below) also require all other logic to be met, including the last N candles touching EMA, overlap check, and size percentage check.
-
Plotting Orange Circles: Candles that meet the new conditions are highlighted with orange circles, while the existing blue circles remain unchanged for the distance condition.
-
Alert Conditions Updated: The new signals are added to the old alert conditions, so you will receive alerts when the new conditions are met.
Notes:
- Blue Circles: The existing blue circles represent candles where the distance condition is met based on your previous logic.
- Orange Circles: The new orange circles highlight candles that meet the new ATR percentage threshold conditions along with all other enabled checks.
- Alerts: Ensure that you set up alerts in TradingView using the updated alert conditions to get notifications for both existing and new signals.
Feel free to adjust the input values to suit your trading strategy. Let me know if you need further assistance or additional modifications!