Delete Demand Zone that are violated now: //@version=5
indicator("Demand Zone Detector (RBR and DBR)", overlay=true)
// ‣ Input Parameters
// Average body period for calculating average body size
avgBodyPeriod = input.int(title="Average Body Period", defval=10, minval=1)
// Big candle factor
bigCandleFactor = input.float(title="Big Candle Factor (Multiplier of Avg Body Size)", defval=1.5, minval=0.1)
// Max number of base candles
maxBaseCandles = input.int(title="Maximum Base Candles", defval=6, minval=1)
// Max base candle body size as percentage of big candle's body
maxBaseBodyPercent = input.float(title="Max Base Candle Body Size (% of Big Candle)", defval=50.0, minval=0.1, maxval=100.0)
// Option for drawing historical zones
drawHistoricalZones = input.bool(title="Draw Historical Demand Zones", defval=true)
// New input parameter to limit the number of recent demand zones displayed
maxZonesToDisplay = input.int(title="Number of Recent Demand Zones to Display", defval=5, minval=1)
// ‣ Calculations
// Calculate average body size
avgBodySize = ta.sma(math.abs(close - open), avgBodyPeriod)
// Identify candle types
bodySize = math.abs(close - open)
isBigGreenCandle = (close > open) and (bodySize >= bigCandleBodyThreshold)
isBigRedCandle = (close < open) and (bodySize >= bigCandleBodyThreshold)
isSmallBodyCandle = bodySize <= maxBaseCandleBodySize
isGreenCandle = close > open
// ‣ Variables for Patterns
// RBR Pattern Variables
var int rbr_state = 0 // 0: idle, 1: collecting base
var int rbr_baseCount = 0
var float rbr_highestBody = na
var float rbr_lowestLow = na
var int rbr_startIndex = na
// DBR Pattern Variables
var int dbr_state = 0 // 0: idle, 1: collecting base
var int dbr_baseCount = 0
var float dbr_highestBody = na
var float dbr_lowestLow = na
var int dbr_startIndex = na
// Arrays to store active demand zones
var box[] demandZones = array.new()
var float[] proximalLines = array.new()
var float[] distalLines = array.new()
var bool[] zoneIsRealtime = array.new() // True if zone created during real-time data
var int[] zoneTypes = array.new() // 1 for RBR, 2 for DBR
var bool[] zoneStatus = array.new() // True if active, False if violated
// ‣ RBR Pattern Detection
if rbr_state == 0
if isBigGreenCandle
// Leg-in detected
rbr_state := 1
rbr_baseCount := 0
rbr_startIndex := bar_index
rbr_highestBody := na
rbr_lowestLow := na
else if rbr_state == 1
if isSmallBodyCandle
// Base candle detected
rbr_baseCount += 1
// Update highest body (proximal line)
float candleHighBody = math.max(open, close)
rbr_highestBody := na(rbr_highestBody) ? candleHighBody : math.max(rbr_highestBody, candleHighBody)
// Update lowest low (distal line)
rbr_lowestLow := na(rbr_lowestLow) ? low : math.min(rbr_lowestLow, low)
else if isGreenCandle
// Leg-out detected
if rbr_baseCount >= 1
// Include leg-out candle in distal line
rbr_lowestLow := math.min(rbr_lowestLow, low)
// Draw the demand zone
box demandZone = box.new(left=rbr_startIndex, right=bar_index, top=rbr_highestBody, bottom=rbr_lowestLow,
border_color=color.green, bgcolor=color.new(color.green, 85))
box.set_extend(demandZone, extend.right)
// Store the zone
array.push(demandZones, demandZone)
array.push(proximalLines, rbr_highestBody)
array.push(distalLines, rbr_lowestLow)
array.push(zoneIsRealtime, barstate.isrealtime)
array.push(zoneTypes, 1) // 1 for RBR
array.push(zoneStatus, true) // Zone is active when created
Assistant
code
// Limit the number of active zones displayed
int activeZonesCount = 0
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
activeZonesCount += 1
// If number of active zones exceeds the limit, remove the oldest active zone
while activeZonesCount > maxZonesToDisplay
// Find the oldest active zone
int oldestActiveIndex = na
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
oldestActiveIndex := idx
break
if not na(oldestActiveIndex)
// Remove the oldest active zone
box dzBoxToRemove = array.get(demandZones, oldestActiveIndex)
box.delete(dzBoxToRemove)
// Remove from arrays
array.remove(demandZones, oldestActiveIndex)
array.remove(proximalLines, oldestActiveIndex)
array.remove(distalLines, oldestActiveIndex)
array.remove(zoneIsRealtime, oldestActiveIndex)
array.remove(zoneTypes, oldestActiveIndex)
array.remove(zoneStatus, oldestActiveIndex)
// Decrease activeZonesCount
activeZonesCount -= 1
else
// No active zones found
break
// Reset pattern
rbr_state := 0
rbr_baseCount := 0
else
// Pattern invalidated
rbr_state := 0
rbr_baseCount := 0
// ‣ DBR Pattern Detection
if dbr_state == 0
if isBigRedCandle
// Leg-in detected
dbr_state := 1
dbr_baseCount := 0
dbr_startIndex := bar_index
dbr_highestBody := na
dbr_lowestLow := low // Include leg-in candle's low
else if dbr_state == 1
if isSmallBodyCandle
// Base candle detected
dbr_baseCount += 1
// Update highest body (proximal line)
float candleHighBody = math.max(open, close)
dbr_highestBody := na(dbr_highestBody) ? candleHighBody : math.max(dbr_highestBody, candleHighBody)
// Update lowest low (distal line)
dbr_lowestLow := math.min(dbr_lowestLow, low)
else if isGreenCandle
// Leg-out detected
if dbr_baseCount >= 1
// Include leg-out candle's low
dbr_lowestLow := math.min(dbr_lowestLow, low)
// Draw the demand zone
box demandZone = box.new(left=dbr_startIndex, right=bar_index, top=dbr_highestBody, bottom=dbr_lowestLow,
border_color=color.new(color.blue, 0), bgcolor=color.new(color.blue, 85))
box.set_extend(demandZone, extend.right)
// Store the zone
array.push(demandZones, demandZone)
array.push(proximalLines, dbr_highestBody)
array.push(distalLines, dbr_lowestLow)
array.push(zoneIsRealtime, barstate.isrealtime)
array.push(zoneTypes, 2) // 2 for DBR
array.push(zoneStatus, true) // Zone is active when created
code
// Limit the number of active zones displayed
int activeZonesCount = 0
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
activeZonesCount += 1
// If number of active zones exceeds the limit, remove the oldest active zone
while activeZonesCount > maxZonesToDisplay
// Find the oldest active zone
int oldestActiveIndex = na
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
oldestActiveIndex := idx
break
if not na(oldestActiveIndex)
// Remove the oldest active zone
box dzBoxToRemove = array.get(demandZones, oldestActiveIndex)
box.delete(dzBoxToRemove)
// Remove from arrays
array.remove(demandZones, oldestActiveIndex)
array.remove(proximalLines, oldestActiveIndex)
array.remove(distalLines, oldestActiveIndex)
array.remove(zoneIsRealtime, oldestActiveIndex)
array.remove(zoneTypes, oldestActiveIndex)
array.remove(zoneStatus, oldestActiveIndex)
// Decrease activeZonesCount
activeZonesCount -= 1
else
// No active zones found
break
// Reset pattern
dbr_state := 0
dbr_baseCount := 0
else
// Pattern invalidated
dbr_state := 0
dbr_baseCount := 0
// ‣ Zone Violation Checking and Management
if array.size(demandZones) > 0
var int i = array.size(demandZones) - 1
while i >= 0
box dzBox = array.get(demandZones, i)
float proximalLine = array.get(proximalLines, i)
float distalLine = array.get(distalLines, i)
bool isRealtimeZone = array.get(zoneIsRealtime, i)
int zoneType = array.get(zoneTypes, i)
bool zoneIsActive = array.get(zoneStatus, i)
bool isZoneActive = (drawHistoricalZones or isRealtimeZone)
bool isViolated = close < distalLine // Using closing price
code
if isZoneActive
if isViolated and zoneIsActive
// Mark zone as violated
array.set(zoneStatus, i, false)
// Keep violated zone and color it gray
box.set_border_color(dzBox, color.gray)
box.set_bgcolor(dzBox, color.new(color.gray, 85))
else if not isViolated and not zoneIsActive
// Zone was previously violated but is no longer
array.set(zoneStatus, i, true)
// Restore original color based on zoneType
if zoneType == 1 // RBR
box.set_border_color(dzBox, color.green)
box.set_bgcolor(dzBox, color.new(color.green, 85))
else if zoneType == 2 // DBR
box.set_border_color(dzBox, color.new(color.blue, 0))
box.set_bgcolor(dzBox, color.new(color.blue, 85))
else
// Delete zone if it's historical and not set to be displayed
box.delete(dzBox)
array.remove(demandZones, i)
array.remove(proximalLines, i)
array.remove(distalLines, i)
array.remove(zoneIsRealtime, i)
array.remove(zoneTypes, i)
array.remove(zoneStatus, i)
i -= 1 // Decrement i
// ‣ Plot Markers (Optional)
// RBR Markers
plotshape(rbr_state == 1 and isBigGreenCandle and rbr_baseCount == 0, title="RBR Leg-in", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.small)
plotshape(rbr_state == 1 and isGreenCandle and rbr_baseCount >= 1 and not isBigGreenCandle, title="RBR Leg-out", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
// DBR Markers
plotshape(dbr_state == 1 and isBigRedCandle and dbr_baseCount == 0, title="DBR Leg-in", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(dbr_state == 1 and isGreenCandle and dbr_baseCount >= 1, title="DBR Leg-out", style=shape.triangleup, location=location.belowbar, color=color.blue, size=size.small)
To modify your Pine Script to delete demand zones that have been violated, you'll need to adjust the Zone Violation Checking and Management section of your script. Currently, the script marks violated zones by changing their color to gray. We'll change this behavior to remove the violated zones entirely from the chart and the underlying arrays that manage them.
Below is the updated script with the necessary modifications:
pinescript
//@version=5
indicator("Demand Zone Detector (RBR and DBR) with Deletion on Violation", overlay=true)
// ‣ Input Parameters
// Average body period for calculating average body size
avgBodyPeriod = input.int(title="Average Body Period", defval=10, minval=1)
// Big candle factor
bigCandleFactor = input.float(title="Big Candle Factor (Multiplier of Avg Body Size)", defval=1.5, minval=0.1)
// Max number of base candles
maxBaseCandles = input.int(title="Maximum Base Candles", defval=6, minval=1)
// Max base candle body size as percentage of big candle's body
maxBaseBodyPercent = input.float(title="Max Base Candle Body Size (% of Big Candle)", defval=50.0, minval=0.1, maxval=100.0)
// Option for drawing historical zones
drawHistoricalZones = input.bool(title="Draw Historical Demand Zones", defval=true)
// New input parameter to limit the number of recent demand zones displayed
maxZonesToDisplay = input.int(title="Number of Recent Demand Zones to Display", defval=5, minval=1)
// ‣ Calculations
// Calculate average body size
avgBodySize = ta.sma(math.abs(close - open), avgBodyPeriod)
// Define thresholds
bigCandleBodyThreshold = bigCandleFactor * avgBodySize
maxBaseCandleBodySize = (maxBaseBodyPercent / 100.0) * bigCandleBodyThreshold
// Identify candle types
bodySize = math.abs(close - open)
isBigGreenCandle = (close > open) and (bodySize >= bigCandleBodyThreshold)
isBigRedCandle = (close < open) and (bodySize >= bigCandleBodyThreshold)
isSmallBodyCandle = bodySize <= maxBaseCandleBodySize
isGreenCandle = close > open
// ‣ Variables for Patterns
// RBR Pattern Variables
var int rbr_state = 0 // 0: idle, 1: collecting base
var int rbr_baseCount = 0
var float rbr_highestBody = na
var float rbr_lowestLow = na
var int rbr_startIndex = na
// DBR Pattern Variables
var int dbr_state = 0 // 0: idle, 1: collecting base
var int dbr_baseCount = 0
var float dbr_highestBody = na
var float dbr_lowestLow = na
var int dbr_startIndex = na
// Arrays to store active demand zones
var box[] demandZones = array.new<box>()
var float[] proximalLines = array.new<float>()
var float[] distalLines = array.new<float>()
var bool[] zoneIsRealtime = array.new<bool>() // True if zone created during real-time data
var int[] zoneTypes = array.new<int>() // 1 for RBR, 2 for DBR
var bool[] zoneStatus = array.new<bool>() // True if active, False if violated
// ‣ RBR Pattern Detection
if rbr_state == 0
if isBigGreenCandle
// Leg-in detected
rbr_state := 1
rbr_baseCount := 0
rbr_startIndex := bar_index
rbr_highestBody := na
rbr_lowestLow := na
else if rbr_state == 1
if isSmallBodyCandle
// Base candle detected
rbr_baseCount += 1
// Update highest body (proximal line)
float candleHighBody = math.max(open, close)
rbr_highestBody := na(rbr_highestBody) ? candleHighBody : math.max(rbr_highestBody, candleHighBody)
// Update lowest low (distal line)
rbr_lowestLow := na(rbr_lowestLow) ? low : math.min(rbr_lowestLow, low)
else if isGreenCandle
// Leg-out detected
if rbr_baseCount >= 1
// Include leg-out candle in distal line
rbr_lowestLow := math.min(rbr_lowestLow, low)
// Draw the demand zone
box demandZone = box.new(left=rbr_startIndex, right=bar_index, top=rbr_highestBody, bottom=rbr_lowestLow,
border_color=color.green, bgcolor=color.new(color.green, 85))
box.set_extend(demandZone, extend.right)
// Store the zone
array.push(demandZones, demandZone)
array.push(proximalLines, rbr_highestBody)
array.push(distalLines, rbr_lowestLow)
array.push(zoneIsRealtime, barstate.isrealtime)
array.push(zoneTypes, 1) // 1 for RBR
array.push(zoneStatus, true) // Zone is active when created
// Limit the number of active zones displayed
int activeZonesCount = 0
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
activeZonesCount += 1
// If number of active zones exceeds the limit, remove the oldest active zone
while activeZonesCount > maxZonesToDisplay
// Find the oldest active zone
int oldestActiveIndex = na
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
oldestActiveIndex := idx
break
if not na(oldestActiveIndex)
// Remove the oldest active zone
box dzBoxToRemove = array.get(demandZones, oldestActiveIndex)
box.delete(dzBoxToRemove)
// Remove from arrays
array.remove(demandZones, oldestActiveIndex)
array.remove(proximalLines, oldestActiveIndex)
array.remove(distalLines, oldestActiveIndex)
array.remove(zoneIsRealtime, oldestActiveIndex)
array.remove(zoneTypes, oldestActiveIndex)
array.remove(zoneStatus, oldestActiveIndex)
// Decrease activeZonesCount
activeZonesCount -= 1
else
// No active zones found
break
// Reset pattern
rbr_state := 0
rbr_baseCount := 0
else
// Pattern invalidated
rbr_state := 0
rbr_baseCount := 0
// ‣ DBR Pattern Detection
if dbr_state == 0
if isBigRedCandle
// Leg-in detected
dbr_state := 1
dbr_baseCount := 0
dbr_startIndex := bar_index
dbr_highestBody := na
dbr_lowestLow := low // Include leg-in candle's low
else if dbr_state == 1
if isSmallBodyCandle
// Base candle detected
dbr_baseCount += 1
// Update highest body (proximal line)
float candleHighBody = math.max(open, close)
dbr_highestBody := na(dbr_highestBody) ? candleHighBody : math.max(dbr_highestBody, candleHighBody)
// Update lowest low (distal line)
dbr_lowestLow := math.min(dbr_lowestLow, low)
else if isGreenCandle
// Leg-out detected
if dbr_baseCount >= 1
// Include leg-out candle's low
dbr_lowestLow := math.min(dbr_lowestLow, low)
// Draw the demand zone
box demandZone = box.new(left=dbr_startIndex, right=bar_index, top=dbr_highestBody, bottom=dbr_lowestLow,
border_color=color.new(color.blue, 0), bgcolor=color.new(color.blue, 85))
box.set_extend(demandZone, extend.right)
// Store the zone
array.push(demandZones, demandZone)
array.push(proximalLines, dbr_highestBody)
array.push(distalLines, dbr_lowestLow)
array.push(zoneIsRealtime, barstate.isrealtime)
array.push(zoneTypes, 2) // 2 for DBR
array.push(zoneStatus, true) // Zone is active when created
// Limit the number of active zones displayed
int activeZonesCount = 0
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
activeZonesCount += 1
// If number of active zones exceeds the limit, remove the oldest active zone
while activeZonesCount > maxZonesToDisplay
// Find the oldest active zone
int oldestActiveIndex = na
for int idx = 0 to array.size(zoneStatus) - 1
if array.get(zoneStatus, idx)
oldestActiveIndex := idx
break
if not na(oldestActiveIndex)
// Remove the oldest active zone
box dzBoxToRemove = array.get(demandZones, oldestActiveIndex)
box.delete(dzBoxToRemove)
// Remove from arrays
array.remove(demandZones, oldestActiveIndex)
array.remove(proximalLines, oldestActiveIndex)
array.remove(distalLines, oldestActiveIndex)
array.remove(zoneIsRealtime, oldestActiveIndex)
array.remove(zoneTypes, oldestActiveIndex)
array.remove(zoneStatus, oldestActiveIndex)
// Decrease activeZonesCount
activeZonesCount -= 1
else
// No active zones found
break
// Reset pattern
dbr_state := 0
dbr_baseCount := 0
else
// Pattern invalidated
dbr_state := 0
dbr_baseCount := 0
// ‣ Zone Violation Checking and Management
if array.size(demandZones) > 0
// Iterate from the last index to handle deletions properly
for i = array.size(demandZones) - 1 to 0 by -1
box dzBox = array.get(demandZones, i)
float proximalLine = array.get(proximalLines, i)
float distalLine = array.get(distalLines, i)
bool isRealtimeZone = array.get(zoneIsRealtime, i)
int zoneType = array.get(zoneTypes, i)
bool zoneIsActive = array.get(zoneStatus, i)
bool isZoneActive = (drawHistoricalZones or isRealtimeZone)
bool isViolated
// Determine violation based on zone type
if zoneType == 1 // RBR
isViolated := close < distalLine
else if zoneType == 2 // DBR
isViolated := close > proximalLine
else
isViolated := false
if isZoneActive
if isViolated and zoneIsActive
// Delete the violated zone
box.delete(dzBox)
// Remove from all arrays
array.remove(demandZones, i)
array.remove(proximalLines, i)
array.remove(distalLines, i)
array.remove(zoneIsRealtime, i)
array.remove(zoneTypes, i)
array.remove(zoneStatus, i)
// No else clause needed since we're deleting violated zones
else
if not isZoneActive and not drawHistoricalZones
// Delete historical zone if not drawing historical zones
box.delete(dzBox)
// Remove from all arrays
array.remove(demandZones, i)
array.remove(proximalLines, i)
array.remove(distalLines, i)
array.remove(zoneIsRealtime, i)
array.remove(zoneTypes, i)
array.remove(zoneStatus, i)
// ‣ Plot Markers (Optional)
// RBR Markers
plotshape(rbr_state == 1 and isBigGreenCandle and rbr_baseCount == 0, title="RBR Leg-in", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.small)
plotshape(rbr_state == 1 and isGreenCandle and rbr_baseCount >= 1 and not isBigGreenCandle, title="RBR Leg-out", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
// DBR Markers
plotshape(dbr_state == 1 and isBigRedCandle and dbr_baseCount == 0, title="DBR Leg-in", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(dbr_state == 1 and isGreenCandle and dbr_baseCount >= 1, title="DBR Leg-out", style=shape.triangleup, location=location.belowbar, color=color.blue, size=size.small)
Key Modifications Explained
Deletion Instead of Marking Violation:
Original Behavior: When a zone is violated, it changes the zone's color to gray but keeps it on the chart.
Modified Behavior: When a zone is violated, the script deletes the zone from the chart and removes its reference from all managing arrays.
Violation Logic Adjustments:
The violation condition now depends on the zone type:
RBR (Rising Base Reversal): A zone is violated if the close price drops below the distalLine (support level).
DBR (Dropping Base Reversal): A zone is violated if the close price climbs above the proximalLine (resistance level).
Loop Adjustment for Safe Deletion:
Iteration is done backwards (for i = array.size(demandZones) - 1 to 0 by -1) to safely remove items from arrays without skipping any elements.
This ensures that removing an element does not affect the loop's indexing.
Removed Zone Status Updates:
Since violated zones are now deleted, the zoneStatus array is updated by removing the element instead of setting it to false.
Historical Zones Handling:
Zones that are no longer active and are not set to display historical zones are also deleted to keep the chart clean.
Additional Recommendations
Testing: After implementing the changes, test the script on historical data to ensure that zones are being deleted correctly upon violation.
Performance Optimization: If you observe any performance issues, consider optimizing the loop or array operations, especially if dealing with a large number of zones.
Customization: Feel free to adjust the violation conditions or parameters based on your specific trading strategy and requirements.
Conclusion
By implementing these changes, your script will now automatically delete demand zones once they are violated, maintaining a cleaner and more relevant chart. This ensures that only active and potentially actionable demand zones are displayed, enhancing the effectiveness of your trading analysis.