Sure! Based on your requirements, I've updated your Pine Script code to add the new functionality. The code now:
- Adds a new signal: When a candle meets the Distance from EMA condition and all other conditions (Last N Candles Touching EMA, Overlap Check, Size Percentage Check), it will highlight those candles with an orange circle.
- Keeps the blue circle as same: Candles that only meet the Distance from EMA condition (but not all other conditions) will continue to be highlighted with a blue circle.
- Includes the new signal in the old alert condition: The alerts have been updated to reflect the new signals.
- Updates all variable names: Variable names have been updated to be more meaningful and understandable for future use.
Here's the updated code:
//@version=5
indicator("Candles Not Touching EMA 5 with Overlap and Size Check (Updated)", overlay=true)
// User Inputs
enable_last_n_check = input.bool(true, title="Require last N candles to have touched EMA?")
N_candles_touched_ema = input.int(3, minval=1, title="Number of candles to check")
enable_stop_loss_feature = input.bool(true, title="Enable Stop Loss Levels")
atr_percent = input.float(2.0, title="ATR Percentage for Stop Loss (%)", minval=0, step=0.1)
// Overlap Method Inputs
enable_overlap_check = input.bool(true, title="Require N% of Current Candle to Overlap with Last Candle?")
overlap_percentage_required = input.float(50.0, minval=0.0, maxval=100.0, title="Required Overlap Percentage (%)")
// Candle Size Inputs
enable_percentage_check = input.bool(true, title="Require candle to be N% smaller than last candle?")
percentage_smaller_required = input.float(20.0, minval=0.01, title="Percentage smaller (%)")
// Distance from EMA Check Inputs
enable_distance_check = input.bool(false, title="Enable Distance from EMA Check?")
distance_atr_percent = input.float(2.0, minval=0.0, step=0.1, title="ATR Percentage for Distance from EMA (%)")
// Inputs to Ignore Checks when Distance Condition is Met
ignore_last_n_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_percentage_check = input.bool(false, title="Ignore Size Percentage Check when Distance Condition is Met?")
// 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
did_last_N_candles_touch_ema(N) =>
result = true
for i = 1 to N
result := result and candle_touching_ema[i]
result
// Initialize condition for last N candles touching EMA
last_N_candles_touched_ema_condition = true
// Update condition if the feature is enabled
if enable_last_n_check
last_N_candles_touched_ema_condition := did_last_N_candles_touch_ema(N_candles_touched_ema)
// Overlap Method Logic
current_candle_overlaps_previous_by_N_percent_condition = true
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
current_candle_overlaps_previous_by_N_percent_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 percentage
current_candle_overlaps_previous_by_N_percent_condition := percentage_of_current_overlapping >= overlap_percentage_required
else
// If current candle range is zero, set condition to false
current_candle_overlaps_previous_by_N_percent_condition := false
// Candle Size Logic
current_candle_is_N_percent_smaller_condition = true
if enable_percentage_check
// Calculate candle sizes
prev_candle_size = high[1] - low[1]
current_candle_size = high - low
// Avoid division by zero
if prev_candle_size != 0
// Calculate size difference percentage
size_difference_percent = ((prev_candle_size - current_candle_size) / prev_candle_size) * 100.0
// Check if current candle is N% smaller than previous candle
current_candle_is_N_percent_smaller_condition := size_difference_percent >= percentage_smaller_required
else
// If previous candle size is zero, set condition to false
current_candle_is_N_percent_smaller_condition := false
// 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
atr_offset = (distance_atr_percent / 100.0) * daily_atr
// Initialize Distance Condition
distance_from_ema_condition_met = 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 >= atr_offset
distance_from_ema_condition_met := true
// Adjust conditions based on Distance Condition and user preferences
cond_last_n_check = true
if enable_last_n_check
cond_last_n_check := last_N_candles_touched_ema_condition
cond_overlap_check = true
if enable_overlap_check
cond_overlap_check := current_candle_overlaps_previous_by_N_percent_condition
cond_percentage_check = true
if enable_percentage_check
cond_percentage_check := current_candle_is_N_percent_smaller_condition
if distance_from_ema_condition_met
if ignore_last_n_check
cond_last_n_check := true
if ignore_overlap_check
cond_overlap_check := true
if ignore_percentage_check
cond_percentage_check := true
// Signal conditions (including all logic)
signal_above = candle_not_touching_ema and close > ema5 and cond_last_n_check and cond_overlap_check and cond_percentage_check
signal_below = candle_not_touching_ema and close < ema5 and cond_last_n_check and cond_overlap_check and cond_percentage_check
// Combined signal for any of the conditions
signal_any = signal_above or signal_below
// New Signal: All conditions met including Distance from EMA
distance_and_all_conditions_met_above = signal_above and distance_from_ema_condition_met
distance_and_all_conditions_met_below = signal_below and distance_from_ema_condition_met
distance_and_all_conditions_met = distance_and_all_conditions_met_above or distance_and_all_conditions_met_below
// Plot EMA 5 for reference
plot(ema5, color=color.orange, linewidth=2, title="EMA 5")
// Plot arrows to highlight signals
// Arrow Down for candles entirely above EMA 5
plotshape(signal_above, title="Above EMA Signal", style=shape.arrowdown, location=location.abovebar, color=color.green, size=size.tiny)
// Arrow Up for candles entirely below EMA 5
plotshape(signal_below, title="Below EMA Signal", style=shape.arrowup, location=location.belowbar, color=color.red, size=size.tiny)
// Plot small blue circle to highlight candles where only Distance Condition is met (but not all other conditions)
plotshape(distance_from_ema_condition_met and not distance_and_all_conditions_met and close > ema5, title="Distance Condition Met Above EMA", style=shape.circle, location=location.abovebar, color=color.blue, size=size.tiny)
plotshape(distance_from_ema_condition_met and not distance_and_all_conditions_met and close < ema5, title="Distance Condition Met Below EMA", style=shape.circle, location=location.belowbar, color=color.blue, size=size.tiny)
// Plot small orange circle to highlight candles where Distance Condition and all other conditions are met
plotshape(distance_and_all_conditions_met_above, title="All Conditions Met Above EMA", style=shape.circle, location=location.abovebar, color=color.orange, size=size.tiny)
plotshape(distance_and_all_conditions_met_below, title="All Conditions Met Below EMA", style=shape.circle, location=location.belowbar, color=color.orange, size=size.tiny)
// Alert Conditions
alertcondition(signal_any, title="Any Signal Alert", message="A signal has been triggered: 'Above EMA' or 'Below EMA'. Distance Condition might be met.")
alertcondition(signal_above, title="Above EMA Alert", message="Candle entirely above EMA 5, not touching it. Signal conditions met. Check if Distance Condition is also met.")
alertcondition(signal_below, title="Below EMA Alert", message="Candle entirely below EMA 5, not touching it. Signal conditions met. Check if Distance Condition is also met.")
alertcondition(distance_from_ema_condition_met, title="Distance Condition Met Alert", message="Candle met the Distance from EMA Condition.")
alertcondition(distance_and_all_conditions_met, title="All Conditions Met Alert", message="Candle met Distance Condition and all other conditions.")
// --- Existing 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
stop_loss_level := high + ((atr_percent / 100.0) * daily_atr)
stop_loss_color := color.red
else if signal_below
stop_loss_level := low - ((atr_percent / 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 Changes:
-
Variable Names Updated: All variable names have been updated to be more meaningful and understandable. For example:
not_touching ➔ candle_not_touching_ema
touching ➔ candle_touching_ema
condition_for_signal ➔ last_N_candles_touched_ema_condition
is_current_candle_N_percent_overlapping ➔ current_candle_overlaps_previous_by_N_percent_condition
is_smaller_by_N_percent ➔ current_candle_is_N_percent_smaller_condition
distance_condition ➔ distance_from_ema_condition_met
distance ➔ distance_from_ema
-
New Signal Added:
- A new condition
distance_and_all_conditions_met has been created to check if Distance from EMA condition and all other conditions are met.
- When this condition is true, an orange circle is plotted on the candle.
- The existing blue circle is plotted only when the Distance from EMA condition is met but not all other conditions.
-
Alerts Updated:
- A new alert condition
alertcondition(distance_and_all_conditions_met, title="All Conditions Met Alert", ...) has been added to notify when all conditions are met.
- Existing alerts have been updated to reflect the possibility that the Distance from EMA condition might also be met.
-
Code Structure:
- The code has been reorganized and commented for better readability and future maintenance.
Usage:
- Enable/Disable Checks: You can enable or disable the various checks (Last N Candles Touching EMA, Overlap Check, Size Percentage Check, Distance from EMA Check) using the input options.
- Customizable Parameters: Adjust the parameters like the number of candles to check (
N_candles_touched_ema), overlap percentage (overlap_percentage_required), size percentage (percentage_smaller_required), and ATR percentage for distance from EMA (distance_atr_percent) as per your strategy.
- Visual Indicators:
- Green/Red Arrows: Indicate signals where the basic conditions are met.
- Blue Circles: Highlight candles where only the Distance from EMA condition is met.
- Orange Circles: Highlight candles where all conditions including Distance from EMA are met.
Feel free to adjust the code further to suit your specific needs. Let me know if you have any questions or need further assistance!