add New Logic along side old logic: Candles (Candles that does not Touch EMA ) Should be…
- User
add New Logic along side old logic: Candles (Candles that does not Touch EMA ) Should be N% Lower than Last Candle, Allow User to Turn of this Feature and Change the Number of Percentage: //@version=5 indicator("Candles Not Touching EMA 5 with Arrows and Alerts", overlay=true)
// User inputs enable_last_n_check = input.bool(true, title="Require last N candles to have touched EMA?") N = input.int(5, 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)
// 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)
// Signal conditions signal_above = not_touching and close > ema5 and condition_for_signal signal_below = not_touching and close < ema5 and condition_for_signal
// 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", 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", style=shape.arrowup, location=location.belowbar, color=color.red, size=size.tiny)
// Alert Conditions alertcondition(signal_above, title="Above EMA Alert", message="Candle entirely above EMA 5 and not touching it.") alertcondition(signal_below, title="Below EMA Alert", message="Candle entirely below EMA 5 and not touching it.")
// --- Added Code Starts Here ---
// Calculate Daily ATR daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(14))
// Calculate ATR offset based on user-defined percentage atr_offset = (atr_percent / 100.0) * daily_atr
// 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_offset stop_loss_color := color.red else if signal_below stop_loss_level := low - atr_offset 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)
- Assistant