// Signal Generation with Mutual Exclusivity var int lastSignal = 0 // -1 = Sell active, 0 = Neutral, 1 = Buy active var float entryPrice = na var float slPrice = na var float tpPrice = na var float sl2Price = na
Buy_Signal = false Sell_Signal = false
// Check for new signals if Buy_OB and valid_candle and lastSignal <= 0 Buy_Signal := true lastSignal := 1 entryPrice := high slPrice := low tpPrice := entryPrice * (1 + tp_percent) sl2Price := slPrice * (1 - sl2_percent) else if Sell_OB and valid_candle and lastSignal >= 0 Sell_Signal := true lastSignal := -1 entryPrice := low slPrice := high tpPrice := entryPrice * (1 - tp_percent) sl2Price := slPrice * (1 + sl2_percent)
// Check for TP/SL hit to reset signals if lastSignal == 1 and (high >= tpPrice or low <= slPrice) lastSignal := 0 entryPrice := na slPrice := na tpPrice := na sl2Price := na else if lastSignal == -1 and (low <= tpPrice or high >= slPrice) lastSignal := 0 entryPrice := na slPrice := na tpPrice := na sl2Price := na