This ProBuilder code snippet demonstrates how to draw and style a rectangle on a chart, using specific color and border attributes. The rectangle is defined between the lowest and highest points over the last 10 bars.
defparam DRAWONLASTBARONLY = true
if barindex>10 then
lowbar = max(1,lowestBars[10])
highbar = max(1,highestBars[10])
drawrectangle(barindex[lowbar], lowest[10], barindex[highbar], highest[10])
coloured("crimson",50)
bordercolor("orchid",200)
style(dottedline4,3)
endif
return
Explanation of the Code:
- defparam DRAWONLASTBARONLY = true: This line ensures that the drawing is only displayed on the last bar of the chart, keeping the chart uncluttered.
- if barindex > 10 then: The code inside this conditional block executes only if there are more than 10 bars on the chart, ensuring there’s enough data to calculate the rectangle dimensions.
- lowbar and highbar: These variables store the indices of the lowest and highest bars within the last 10 bars, respectively. The max function ensures that the indices are at least 1, avoiding any indexing errors.
- drawrectangle(…): This function draws a rectangle from the lowest point to the highest point between the calculated bars. The coordinates are determined by barindex[lowbar], lowest[10], barindex[highbar], and highest[10].
- coloured(“crimson”,50): Sets the fill color of the rectangle to crimson with 50% opacity.
- bordercolor(“orchid”,200): Sets the border color of the rectangle to orchid with full opacity (200).
- style(dottedline4,3): Applies a dotted line style with a thickness of 3 to the border of the rectangle.
This snippet is useful for visually highlighting specific ranges on a chart, with customizable aesthetic options to enhance readability and focus.