OPEN-SOURCE SCRIPT

Order Block Signals

138
//version=5
indicator("Order Block Signals", overlay=true, max_lines_count=500, max_labels_count=500)

// Inputs
OB_Threshold = input.float(0.001, "OB Threshold (%)", step=0.001) / 100
Lookback = input.int(200, "Lookback Period")

min_diff = input.float(1.0, "Min Candle %", step=0.1)
max_diff = input.float(1.2, "Max Candle %", step=0.1)
tp_percent = input.float(1.3, "Take Profit %", step=0.1) / 100
sl2_percent = input.float(1.3, "Second SL %", step=0.1) / 100

// Order Block Detection
var float highRange = na
var float lowRange = na
highRange := ta.highest(high, Lookback)
lowRange := ta.lowest(low, Lookback)

Buy_OB = low >= lowRange * (1 - OB_Threshold) and low <= lowRange * (1 + OB_Threshold)
Sell_OB = high >= highRange * (1 - OB_Threshold) and high <= highRange * (1 + OB_Threshold)

// Candle Filter
candle_diff = ((high - low) / low) * 100
valid_candle = candle_diff >= min_diff and candle_diff <= max_diff

// Signal Generation
Buy_Signal = Buy_OB and valid_candle
Sell_Signal = Sell_OB and valid_candle

// Remove Excess Signals
var int lastSignal = 0
Buy_Signal := Buy_Signal and lastSignal <= 0
Sell_Signal := Sell_Signal and lastSignal >= 0

if Buy_Signal
lastSignal := 1
else if Sell_Signal
lastSignal := -1

// Price Levels
var float entryPrice = na
var float slPrice = na
var float tpPrice = na
var float sl2Price = na

if Buy_Signal
entryPrice := high
slPrice := low
tpPrice := entryPrice * (1 + tp_percent)
sl2Price := slPrice * (1 - sl2_percent)
else if Sell_Signal
entryPrice := low
slPrice := high
tpPrice := entryPrice * (1 - tp_percent)
sl2Price := slPrice * (1 + sl2_percent)

// Plotting
plot(entryPrice, "Entry", color.new(color.green, 0), 2)
plot(slPrice, "SL", color.new(color.red, 0), 2)
plot(tpPrice, "TP", color.new(color.blue, 0), 2)
plot(sl2Price, "SL2", color.new(color.orange, 0), 2)

// Enhanced Signal Markers
plotshape(Buy_Signal, style=shape.square, location=location.belowbar,
color=color.new(color.green, 0), size=size.tiny, offset=-40)
plotshape(Buy_Signal, style=shape.square, location=location.belowbar,
color=color.new(color.lime, 0), size=size.tiny, offset=-50)
plotshape(Buy_Signal, style=shape.triangleup, location=location.belowbar,
color=color.new(color.white, 0), size=size.small, offset=-45)

plotshape(Sell_Signal, style=shape.square, location=location.abovebar,
color=color.new(color.red, 0), size=size.tiny, offset=40)
plotshape(Sell_Signal, style=shape.square, location=location.abovebar,
color=color.new(color.orange, 0), size=size.tiny, offset=50)
plotshape(Sell_Signal, style=shape.triangledown, location=location.abovebar,
color=color.new(color.white, 0), size=size.small, offset=-45)

// Order Block Zones
bgcolor(Buy_OB ? color.new(color.green, 90) : na)
bgcolor(Sell_OB ? color.new(color.red, 90) : na)

// Dynamic Lines
var line entryLine = na
var line slLine = na
var line tpLine = na
var line sl2Line = na

if Buy_Signal or Sell_Signal
entryLine := line.new(bar_index, entryPrice, bar_index+1, entryPrice,
color=color.green, width=2)
slLine := line.new(bar_index, slPrice, bar_index+1, slPrice,
color=color.red, width=2)
tpLine := line.new(bar_index, tpPrice, bar_index+1, tpPrice,
color=color.blue, width=2)
sl2Line := line.new(bar_index, sl2Price, bar_index+1, sl2Price,
color=color.orange, width=2)





//version=5
indicator("Order Block Signals - Exclusive & Dynamic", overlay=true, max_lines_count=500)

// Inputs

Lookback = input.int(200, "Lookback Period")

min_diff = input.float(1.0, "Min Candle %", step=0.1)
max_diff = input.float(1.2, "Max Candle %", step=0.1)
tp_percent = input.float(1.3, "Take Profit %", step=0.1) / 100
sl2_percent = input.float(1.3, "Second SL %", step=0.1) / 100

// Order Block Detection
var float highRange = na
var float lowRange = na
highRange := ta.highest(high, Lookback)
lowRange := ta.lowest(low, Lookback)

Buy_OB = low >= lowRange * (1 - OB_Threshold) and low <= lowRange * (1 + OB_Threshold)
Sell_OB = high >= highRange * (1 - OB_Threshold) and high <= highRange * (1 + OB_Threshold)

// Candle Filter
candle_diff = ((high - low) / low) * 100
valid_candle = candle_diff >= min_diff and candle_diff <= max_diff

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

// Plotting
plot(entryPrice, "Entry", color.new(color.green, 0), 2, linewidth=2)
plot(slPrice, "SL", color.new(color.red, 0), 2, linewidth=2)
plot(tpPrice, "TP", color.new(color.blue, 0), 2, linewidth=2)
plot(sl2Price, "SL2", color.new(color.orange, 0), 2, linewidth=2)

// Signal Markers
plotshape(Buy_Signal, style=shape.triangleup, location=location.belowbar,
color=color.new(color.green, 0), size=size.normal, text="BUY", textcolor=color.white)

plotshape(Sell_Signal, style=shape.triangledown, location=location.abovebar,
color=color.new(color.red, 0), size=size.normal, text="SELL", textcolor=color.white)

// Order Block Zones
bgcolor(Buy_OB ? color.new(color.green, 90) : na, title="Buy OB Zone")
bgcolor(Sell_OB ? color.new(color.red, 90) : na, title="Sell OB Zone")

// Dynamic Lines for TP/SL
var line entryLine = na
var line slLine = na
var line tpLine = na
var line sl2Line = na

if Buy_Signal or Sell_Signal
entryLine := line.new(bar_index, entryPrice, bar_index + 1, entryPrice,
color=color.new(color.green, 0), width=2)
slLine := line.new(bar_index, slPrice, bar_index + 1, slPrice,
color=color.new(color.red, 0), width=2)
tpLine := line.new(bar_index, tpPrice, bar_index + 1, tpPrice,
color=color.new(color.blue, 0), width=2)
sl2Line := line.new(bar_index, sl2Price, bar_index + 1, sl2Price,
color=color.new(color.orange, 0), width=2)

// Clear lines when position is closed
if lastSignal == 0
line.delete(entryLine)
line.delete(slLine)
line.delete(tpLine)
line.delete(sl2Line)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.