here .. make the stop loss, if the price goes under the EMA 50:

//@version=5
strategy("OFI-Based Long Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// === Input Parameters ===
lookback = input.int(14, title="OFI Lookback Period", minval=1)
threshold = input.float(0.7, title="Imbalance Threshold (0-1)", minval=0.0, maxval=1.0)
takeProfitPerc = input.float(1.0, title="Take Profit (%)", minval=0.1)
stopLossPerc = input.float(0.5, title="Stop Loss (%)", minval=0.1)

// === Calculate Buy and Sell Volume ===
// Simplistic proxies for buy and sell volumes based on candle direction
buyVolume = volume * (close > open ? 1 : 0)   // Buy volume when the current candle is bullish
sellVolume = volume * (close < open ? 1 : 0)  // Sell volume when the current candle is bearish

// === Calculate Cumulative OFI ===
cumulative_ofi = ta.cum(buyVolume - sellVolume)

// === Calculate Rolling OFI ===
// Retrieve the cumulative OFI from 'lookback' bars ago
// The 'ta.valuewhen' function fetches the value of 'cumulative_ofi' exactly 'lookback' bars ago
previous_cumulative_ofi = ta.valuewhen(bar_index >= lookback, cumulative_ofi, lookback)

// Calculate the difference to get the rolling sum over the lookback period
ofi = cumulative_ofi - previous_cumulative_ofi

// Handle NaN values for the initial 'lookback' bars
ofi := na(ofi) ? 0 : ofi

// === Calculate Imbalances ===
buyImbalance = ofi > 0 ? ofi : 0
sellImbalance = ofi < 0 ? math.abs(ofi) : 0
totalImbalance = buyImbalance + sellImbalance
buyStrength = totalImbalance != 0 ? buyImbalance / totalImbalance : 0
sellStrength = totalImbalance != 0 ? sellImbalance / totalImbalance : 0

// === Plot OFI and Imbalances ===
plot(ofi, title="OFI", color=color.blue)
hline(0, "Zero Line", color=color.gray)
plot(buyImbalance, title="Buy Imbalance", color=color.new(color.green, 50), style=plot.style_columns)
plot(sellImbalance, title="Sell Imbalance", color=color.new(color.red, 50), style=plot.style_columns)

// === Additional Indicators for Price Action Prediction ===
// Moving Average (50-period Simple Moving Average)
ma = ta.sma(close, 50)
// Relative Strength Index (14-period)
rsi = ta.rsi(close, 14)

// === Plot Additional Indicators ===
plot(ma, title="50 SMA", color=color.orange)
hline(70, "RSI Overbought", color=color.red)
hline(30, "RSI Oversold", color=color.green)
plot(rsi, title="RSI", color=color.purple)

// === Placeholder for OFI_Hawkes Inspired Metric ===
// Currently using a 3-period Exponential Moving Average (EMA) as a placeholder
// Replace this with actual Hawkes process logic when available
ofi_hawkes = ta.ema(ofi, 3)

// === Plot OFI_Hawkes ===
plot(ofi_hawkes, title="OFI Hawkes", color=color.purple)

// === Generate Long Signal with Enhanced Conditions ===
enterLongCondition = (buyStrength > threshold) and 
                     (buyImbalance > sellImbalance) and 
                     (ofi_hawkes > 0) and 
                     (close > ma) and 
                     (rsi < 70)

// === Execute Long Entry ===
if (enterLongCondition)
    strategy.entry("Long", strategy.long)

// === Risk Management: Take Profit and Stop Loss ===
if (strategy.position_size > 0)
    strategy.exit("Take Profit/Stop Loss", "Long", 
      limit=close * (1 + takeProfitPerc / 100), 
      stop=close * (1 - stopLossPerc / 100))

// === Display Buy and Sell Strength ===
plot(buyStrength, title="Buy Strength", color=color.green)
plot(sellStrength, title="Sell Strength", color=color.red)