Filter Those Candles also that Below EMA and does not EMA Line from N Candles:…
- User
Filter Those Candles also that Below EMA and does not EMA Line from N Candles: //@version=5 indicator("EMA5 Break Strategy with Corrected Previous Candles Condition", overlay=true)
// Parameters ema_length = input.int(5, minval=1, title="EMA Length") atr_length = input.int(14, minval=1, title="ATR Length")
// Risk-Reward Ratio Input risk_reward_ratio = input.float(2.5, minval=0.1, step=0.1, title="Risk-Reward Ratio")
// Time Filter Settings use_time_filter = input.bool(true, title="Use Time Filter?")
session1 = input.session("0930-1600", "Session 1 Time", group="Time Filter Settings") session2 = input.session("2000-2300", "Session 2 Time", group="Time Filter Settings") timezone = "America/New_York"
// New Feature Inputs use_preceding_candles_condition = input.bool(true, title="Require All Previous Candles Touch EMA?") preceding_candles_count = input.int(5, minval=1, title="Number of Previous Candles (N)", group="Additional Conditions")
// Compute EMA ema5 = ta.ema(close, ema_length)
// Compute Daily ATR daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(atr_length))
// Time filter logic var bool is_in_time = true
if use_time_filter is_in_session1 = not na(time(timeframe.period, session1, timezone)) is_in_session2 = not na(time(timeframe.period, session2, timezone)) is_in_time := is_in_session1 or is_in_session2 else is_in_time := true
// Conditions // First candle not touching EMA: Previous candle's low is above EMA, and the candle before that touches or crosses EMA first_candle_not_touching_ema = low[1] > ema5[1] and low[2] <= ema5[2]
// Price is above EMA (on the previous candle) price_above_ema = close[1] > ema5[1]
// Current candle breaks the low of the previous candle (the first candle not touching EMA) breaks_prev_low = low < low[1]
// New Condition: All of the previous N candles must have touched the EMA (their lows are at or below EMA) all_previous_candles_touched_ema = true
if use_preceding_candles_condition for i = 2 to preceding_candles_count + 1 by 1 if low[i] > ema5[i] all_previous_candles_touched_ema := false break // Exit loop early if any candle does not meet the condition else all_previous_candles_touched_ema := true // Condition passes if feature is disabled
// Signal when all conditions are met signal = first_candle_not_touching_ema and price_above_ema and breaks_prev_low and is_in_time and all_previous_candles_touched_ema
// Entry, Stop Loss, and Take Profit Prices entry_price = low[1] stop_loss_price = high[1] + (daily_atr * 0.02) risk = stop_loss_price - entry_price take_profit_price = entry_price - (risk * risk_reward_ratio)
// Variables to track trade status var bool in_trade = false var bool trade_won = false var float trade_entry_price = na var float trade_stop_loss = na var float trade_take_profit = na var int trade_entry_bar = na var int trade_exit_bar = na var bool trade_closed_on_this_bar = false
- Assistant