Certainly! Based on your request, you want to highlight candles with an orange circle when the following conditions are met:
- The candle's high or low is N% of the Daily ATR away from EMA 5.
- All other logic is also met, meaning all your current conditions (
not_touching, cond_last_n_check, cond_overlap_check, cond_percentage_check) are true.
To achieve this, we'll modify your script by adding a new condition that checks when both the distance condition and all other conditions are met. We'll then use plotshape to draw an orange circle on these candles.
Below is the updated script with the necessary changes:
//@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 = 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_N = input.float(50.0, minval=0.0, maxval=100.0, title="Required Overlap Percentage N%")
// Candle Size Inputs
enable_percentage_check = input.bool(true, title="Require candle to be N% smaller than last candle?")
percentage_N = input.float(20.0, minval=0.01, title="Percentage smaller (%)")
// New 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 select which logic to ignore 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
not_touching = (low > ema5) or (high < ema5)
// Identify candles that touch EMA 5
touching = not not_touching
// Function to check if all of the last N candles touched the EMA
all_last_N_touching(N) =>
result = true
for i = 1 to N
result := result and touching[i]
result
// Initialize condition_for_signal
condition_for_signal = true
// If the feature is enabled, update condition_for_signal
if enable_last_n_check
// Condition for signal: all of the last N candles touched the EMA
condition_for_signal := all_last_N_touching(N)
// Overlap Method Logic
// Initialize is_current_candle_N_percent_overlapping
is_current_candle_N_percent_overlapping = true
// If the feature is enabled, compute the condition
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
is_current_candle_N_percent_overlapping := 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 N%
is_current_candle_N_percent_overlapping := percentage_of_current_overlapping >= overlap_percentage_N
else
// If current candle range is zero, set condition to false
is_current_candle_N_percent_overlapping := false
// Candle Size Logic
// Initialize is_smaller_by_N_percent
is_smaller_by_N_percent = true
// If the feature is enabled, update is_smaller_by_N_percent
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
is_smaller_by_N_percent := size_difference_percent >= percentage_N
else
// If previous candle size is zero, set condition to false
is_smaller_by_N_percent := false
// --- New 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_condition = false
distance = 0.0
if enable_distance_check and not_touching
if close > ema5
distance := low - ema5
else if close < ema5
distance := ema5 - high
else
distance := 0
if distance >= atr_offset
distance_condition := true
// Adjust conditions based on Distance Condition and user preferences
cond_last_n_check = true
if enable_last_n_check
cond_last_n_check := condition_for_signal
cond_overlap_check = true
if enable_overlap_check
cond_overlap_check := is_current_candle_N_percent_overlapping
cond_percentage_check = true
if enable_percentage_check
cond_percentage_check := is_smaller_by_N_percent
if distance_condition
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 = not_touching and close > ema5 and cond_last_n_check and cond_overlap_check and cond_percentage_check
signal_below = not_touching 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 Code: Signal with Distance Condition Met ---
// Create signals where both distance condition and all other conditions are met
signal_with_distance_above = signal_above and distance_condition
signal_with_distance_below = signal_below and distance_condition
// 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 circle to highlight candles where Distance Condition is met (original code)
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 Circle when Both Distance and All Other Conditions are Met ---
// Plot orange circle when the Distance Condition and all other logic are met
plotshape(signal_with_distance_above, title="Signal Above with Distance", style=shape.circle, location=location.abovebar, color=color.orange, size=size.small)
plotshape(signal_with_distance_below, title="Signal Below with Distance", style=shape.circle, location=location.belowbar, color=color.orange, size=size.small)
// Alert Conditions
alertcondition(signal_any, title="Any Signal Alert", message="A signal has been triggered: either 'Above EMA' or 'Below EMA'.")
alertcondition(signal_above, title="Above EMA Alert", message="Candle entirely above EMA 5, not touching it. Signal conditions met.")
alertcondition(signal_below, title="Below EMA Alert", message="Candle entirely below EMA 5, not touching it. Signal conditions met.")
// --- 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:
-
Added New Signals for Combined Conditions:
-
Plotted Orange Circles on Candles Meeting All Conditions:
-
Used plotshape to draw an orange circle above/below the candles where both the distance condition and all other logic are met:
plotshape(signal_with_distance_above, title="Signal Above with Distance", style=shape.circle, location=location.abovebar, color=color.orange, size=size.small)
plotshape(signal_with_distance_below, title="Signal Below with Distance", style=shape.circle, location=location.belowbar, color=color.orange, size=size.small)
-
Set the size to size.small for better visibility.
-
Left the Original Distance Condition Plotting:
- Kept the original blue circles that indicate when only the distance condition is met, regardless of other conditions.
- This way, you can distinguish between candles where only the distance condition is true (blue circles) and candles where both the distance and all other conditions are true (orange circles).
How to Use:
Note:
- The alerts and stop loss levels remain unchanged. If you want alerts specifically for the new combined condition (distance condition plus all other logic), you can add new
alertcondition statements similarly.
Optional: Add Alerts for the New Combined Condition
If you would like to receive alerts specifically when the orange circle signals occur, you can add the following alert conditions:
// Alert Conditions for Combined Signals
alertcondition(signal_with_distance_above, title="Above EMA with Distance Alert", message="Candle entirely above EMA 5, not touching it, with distance condition met. Signal conditions met.")
alertcondition(signal_with_distance_below, title="Below EMA with Distance Alert", message="Candle entirely below EMA 5, not touching it, with distance condition met. Signal conditions met.")
In Summary:
- The updated script will now highlight candles with an orange circle when both the distance condition and all your other specified conditions are met.
- This provides a clear visual indication on your chart for these specific scenarios.
- Feel free to adjust the colors, shapes, or sizes to better suit your preferences.
Let me know if you need any further adjustments or explanations!