Home » Box Functions » Understanding the box.set_rightbottom() Function in Pine Script

Understanding the box.set_rightbottom() Function in Pine Script

Photo of author
Published on

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 the box.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 (using bar_index) or a specific time value (using the time 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.


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

  1. Initialization of Variables:
    • var dailyLowPrice = low: This line initializes a variable dailyLowPrice to store the lowest price of the current day. The var 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 a box variable named dailyRangeBox 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.
  2. 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.
  3. Box Initialization at the Start of a New Day:
    • Within the if statement, a new box is created using box.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 the dailyLowPrice for the new day to the low of the current bar.
  4. 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 the dailyLowPrice 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 lowest price encountered during the day is updated with dailyLowPrice := math.min(dailyLowPrice[1], low), ensuring that dailyLowPrice always holds the lowest value seen so far in the 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.

Leave a Comment