Add Trend Detection Funtion in this indicator and Allow user to change option when they want to trade like up trend, down trend and sideway market: //@version=5
indicator("EMA5 Break Strategy with Correct Trade Outcome Highlighting", 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"

// 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
// Current candle's low is above EMA5 and the previous candle's low was at or below EMA5
first_candle_not_touching_ema = low[1] > ema5[1] and low[2] <= ema5[2]

// Price is above EMA5
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]

// Signal when the conditions are met and time is within session
signal = first_candle_not_touching_ema and price_above_ema and breaks_prev_low and is_in_time

// 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

// Variables to track trade statistics
var int total_trades = 0
var int wins = 0
var int losses = 0

if signal and not in_trade
    in_trade := true
    trade_won := false
    trade_entry_price := entry_price
    trade_stop_loss := stop_loss_price
    trade_take_profit := take_profit_price
    trade_entry_bar := bar_index
    trade_exit_bar := na
    trade_closed_on_this_bar := false

if in_trade
    // Check if the trade is exited on this bar
    if high >= trade_stop_loss and low <= trade_take_profit
        // Both levels were hit in the same bar; we need to decide which was hit first
        // Since intrabar price movement is unknown, we'll assume worse case (stop loss hit first)
        trade_won := false
        in_trade := false
        trade_exit_bar := bar_index
        trade_closed_on_this_bar := true
    else if high >= trade_stop_loss
        // Stop loss was hit
        trade_won := false
        in_trade := false
        trade_exit_bar := bar_index
        trade_closed_on_this_bar := true
    else if low <= trade_take_profit
        // Take profit was hit
        trade_won := true
        in_trade := false
        trade_exit_bar := bar_index
        trade_closed_on_this_bar := true
    // Optional: Exit trade after a maximum number of bars
    else if bar_index - trade_entry_bar >= 100
        in_trade := false
        trade_won := false
        trade_exit_bar := bar_index
        trade_closed_on_this_bar := true
    else
        trade_closed_on_this_bar := false
else
    trade_closed_on_this_bar := false

// Update trade statistics
if trade_closed_on_this_bar
    total_trades += 1
    if trade_won
        wins += 1
    else
        losses += 1

// Plot EMA
plot(ema5, color=color.blue, linewidth=2)

// Plot signals
plotshape(signal, title="Short Entry", location=location.abovebar, color=color.red, size=size.small, style=shape.triangledown, text="Short", textcolor=color.white)

// Plot stop loss and take profit lines on the chart when trade is active
plot(in_trade or trade_closed_on_this_bar ? trade_stop_loss : na, title="Stop Loss", color=color.red, linewidth=1, style=plot.style_linebr)
plot(in_trade or trade_closed_on_this_bar ? trade_take_profit : na, title="Take Profit", color=color.green, linewidth=1, style=plot.style_linebr)

// Plot trade outcome marker
if trade_closed_on_this_bar
    if trade_won
        label.new(bar_index, low, text="Win", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.tiny)
    else
        label.new(bar_index, high, text="Loss", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.tiny)

// Create and update the table
var table trade_stats_table = table.new(position.top_right, 2, 3, border_width=1)

// Update the table on every bar
table.cell(trade_stats_table, 0, 0, "Total Trades", text_color=color.white, bgcolor=color.blue)
table.cell(trade_stats_table, 1, 0, str.tostring(total_trades), text_color=color.white, bgcolor=color.blue)

table.cell(trade_stats_table, 0, 1, "Wins", text_color=color.white, bgcolor=color.green)
table.cell(trade_stats_table, 1, 1, str.tostring(wins), text_color=color.white, bgcolor=color.green)

table.cell(trade_stats_table, 0, 2, "Losses", text_color=color.white, bgcolor=color.red)
table.cell(trade_stats_table, 1, 2, str.tostring(losses), text_color=color.white, bgcolor=color.red)