To explain and explore the functionality of the box.set_rightbottom() function in Pine Script, we’ll delve into a custom Pine Script indicator. This indicator demonstrates how to dynamically update the dimensions of a graphical box on the chart to capture a specific area based on price action throughout the trading day.
Understanding the box.set_rightbottom()
Function
The box.set_rightbottom()
function in Pine Script is used to adjust the position of an existing graphical box’s right and bottom borders. This is crucial for dynamic chart visualizations that need to update in response to new data. The function signature is as follows:
box.set_rightbottom(identifier, newRight, newBottom)
identifier
: The unique ID of the box you want to modify. This ID is obtained from thebox.new()
function when the box is initially created.newRight
: The new coordinate on the time axis for the box’s right border. This can be a bar number (usingbar_index
) or a specific time value (using thetime
variable), depending on how the box was initially positioned.newBottom
: The new coordinate on the price axis for the box’s bottom border. This is a float value representing the price level.
Example
//@version=5 indicator(title="Dynamic Box Update", overlay=true) // Create persistent variables for tracking the day's low and the box's ID var dailyLowPrice = low var box dailyRangeBox = na // Check for the start of a new calendar day if dayofmonth != dayofmonth[1] // Initialize a box to represent the opening bar's range dailyRangeBox := box.new(left=bar_index, top=high, right=bar_index + 1, bottom=low, border_width=0, bgcolor=color.purple) // Reset the daily low to the current bar's low dailyLowPrice := low else // Update the box dimensions if a new daily low is observed if low < dailyLowPrice box.set_rightbottom(id=dailyRangeBox, right=bar_index, bottom=low) box.set_bgcolor(id=dailyRangeBox, color=color.new(color.red, 70)) // Maintain the lowest price encountered during the day dailyLowPrice := math.min(dailyLowPrice[1], low)
Walkthrough of Code
- Initialization of Variables:
var dailyLowPrice = low
: This line initializes a variabledailyLowPrice
to store the lowest price of the current day. Thevar
keyword ensures that the value is persistent across different bars (time periods on the chart), updating only when explicitly changed. Initially, it’s set to the low price of the current bar.var box dailyRangeBox = na
: This initializes abox
variable nameddailyRangeBox
but doesn’t assign it a box yet (na
stands for ‘not available’). This variable will later be used to store a reference to the box drawn on the chart to represent the day’s price range.
- Check for New Calendar Day:
- The
if dayofmonth != dayofmonth[1]
condition checks if the day has changed by comparing the current bar’s day of the month with the previous bar’s. If they differ, it signifies the start of a new day.
- The
- Box Initialization at the Start of a New Day:
- Within the
if
statement, a new box is created usingbox.new(...)
, representing the price range of the opening bar of the new day. The box is drawn from the current bar’s index (left=bar_index
) to one bar to the right (right=bar_index + 1
), with the top and bottom set to the current bar’s high and low prices, respectively. The box’s appearance is customized (e.g.,bgcolor=color.purple
for background color). dailyLowPrice := low
resets thedailyLowPrice
for the new day to the low of the current bar.
- Within the
- Updating the Box During the Day:
- If it’s not the start of a new day (
else
clause), the script checks if the current bar’s low is lower than thedailyLowPrice
stored. If a new low is found:- The box’s dimensions are updated using
box.set_rightbottom(...)
, extending its right side to the current bar and setting the bottom to the new lower price. - The box’s background color is changed to a semi-transparent red using
box.set_bgcolor(...)
.
- The box’s dimensions are updated using
- The lowest price encountered during the day is updated with
dailyLowPrice := math.min(dailyLowPrice[1], low)
, ensuring thatdailyLowPrice
always holds the lowest value seen so far in the day.
- If it’s not the start of a new day (
Key Takeaways
- The
box.set_rightbottom()
function is essential for dynamically updating the dimensions of a box in response to changing data, allowing for real-time visual analysis on the chart. - This function requires the box’s identifier, a new right border position (time axis), and a new bottom border position (price axis) to update the box correctly.
- The example script demonstrates a practical application: adjusting a box to represent the range from the opening price to the lowest price within a trading day, highlighting significant price movements visually.