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

Understanding box.set_bottom() Function in Pine Script

Photo of author
Published on

In this tutorial, we delve into the box.set_bottom() function in Pine Script. We’ll break down a full script example that showcases the use of box.set_bottom() adjusting a box’s bottom border in real-time based on market conditions, specifically tracking the lowest price point within a week.

Overview of box.set_bottom() Function

The box.set_bottom() function is instrumental in modifying the bottom border of a box drawing on the chart. It allows for dynamic adjustments based on changing data, such as price movements. Here’s the syntax:

box.set_bottom(id, bottom)
  • id: The identifier of the box, which is obtained from box.new() function at the time of the box’s creation.
  • bottom: A floating-point number representing the new bottom border’s price axis coordinate.

Example Script Explained

Our script example employs box.set_bottom() to dynamically adjust a box’s bottom to the lowest price point of the current week. We start by initializing the necessary variables and proceed with a step-by-step walkthrough.

Script Setup

indicator(title="Weekly Low Tracker", overlay=true)

var float weekLowPrice = low
var box boxWeekLow = na
  • weekLowPrice stores the week’s lowest price, initially set to the current bar’s low.
  • boxWeekLow is a placeholder for the box’s identifier, initially set to na (not available).

Weekly Box Creation and Bottom Adjustment

if weekofyear != weekofyear[1]
    boxWeekLow := box.new(left=bar_index, top=high, right=bar_index + 1, bottom=low, bgcolor=color.new(color.red, 85), border_color=color.orange)
    weekLowPrice := low
    if low < weekLowPrice
        box.set_bottom(id=boxWeekLow, bottom=low)
    box.set_right(id=boxWeekLow, right=bar_index)
    weekLowPrice := math.min(weekLowPrice[1], low)
  • Checks for a new week; if true, creates a new box at the week’s start with the opening bar’s high and low as the top and bottom borders, respectively.
  • If the current bar’s low is below weekLowPrice, box.set_bottom() updates the box’s bottom border to this new low.
  • Regardless of price movements, the box’s right border extends with each new bar to encompass the entire week.
  • weekLowPrice is updated to reflect the lowest price encountered during the week.

Key Features and Takeaways

  • Function Usability: box.set_bottom() offers a versatile way to dynamically adjust box drawings in response to data changes, crucial for visualizing specific market conditions or strategies.
  • Syntax and Application: The function requires an identifier and a new bottom value, demonstrating its straightforward implementation for real-time chart modifications.
  • Practical Example: Our script illustrates how to leverage box.set_bottom() to visualize weekly lows, enhancing analysis and decision-making processes for traders.

By understanding and applying the box.set_bottom() function, Pine Script users can significantly enrich their trading charts on TradingView, making it an essential tool in the arsenal of any trader or developer looking to incorporate dynamic visual elements into their technical analysis or trading strategies.

Leave a Comment