Home » Box Functions » box.set_lefttop Function in Pine Script

box.set_lefttop Function in Pine Script

Photo of author
Published on

Introduction to box.set_lefttop Function in Pine Script

Pine Script provides a rich set of built-in functions and types, and among them, one of the most interesting is box.set_lefttop.

Understanding box.set_lefttop Function

The box.set_lefttop function in Pine Script is used to set the left and top coordinates of a box. It is a part of the box namespace in Pine Script which is used to manipulate box objects. The syntax of the box.set_lefttop function is as follows:

box.set_lefttop(id, left, top) → void

Here,

  • id is a series box object, which is the box that you want to modify.
  • left is a series int that represents the bar index or bar time of the left border.
  • top is a series int/float which is the price value of the top border.

The function returns nothing (void), meaning it modifies the box object in place.

How to Use box.set_lefttop

//@version=5
indicator('Last 50 bars price ranges', overlay = true)
RANGE_PERIOD = 50
maxPrice = ta.highest(RANGE_PERIOD)
minPrice = ta.lowest(RANGE_PERIOD)
if barstate.islastconfirmedhistory
    var BoxCurrentRange = box.new(bar_index[RANGE_PERIOD], maxPrice, bar_index, minPrice, bgcolor = color.new(color.green, 80))
    var BoxPreviousRange = box.copy(BoxCurrentRange)
    box.set_lefttop(BoxPreviousRange, bar_index[RANGE_PERIOD * 2], maxPrice[50])
    box.set_rightbottom(BoxPreviousRange, bar_index[RANGE_PERIOD], minPrice[50])
    box.set_bgcolor(BoxPreviousRange, color.new(color.red, 80))

box.set_lefttop Function

Here’s what each line does:

  1. //@version=5: This line indicates that we are using version 5 of Pine Script.
  2. indicator('Last 50 bars price ranges', overlay = true): This line defines a new study (or indicator) with the title “Last 50 bars price ranges”, which will be overlayed onto the price chart.
  3. RANGE_PERIOD = 50: Here, we declare a constant RANGE_PERIOD and set its value to 50.
  4. maxPrice = ta.highest(RANGE_PERIOD): We calculate the highest price over the last RANGE_PERIOD bars.
  5. minPrice = ta.lowest(RANGE_PERIOD): We calculate the lowest price over the last RANGE_PERIOD bars.
  6. if barstate.islastconfirmedhistory: This if statement checks whether the current bar is the last confirmed historical bar.
  7. var BoxCurrentRange = box.new(bar_index[RANGE_PERIOD], maxPrice, bar_index, minPrice, bgcolor = color.new(color.green, 80)): If the if statement is true, we create a new box from the current bar back to the RANGE_PERIOD bars in the past, from the highest price to the lowest price within that period. The box has a green background color.
  8. var BoxPreviousRange = box.copy(BoxCurrentRange): We create a copy of the BoxCurrentRange.
  9. box.set_lefttop(BoxPreviousRange, bar_index[RANGE_PERIOD * 2], maxPrice[50]): We move the left and top borders of the BoxPreviousRange to 100 bars ago and the highest price of 50 bars ago, respectively.
  10. box.set_rightbottom(BoxPreviousRange, bar_index[RANGE_PERIOD], minPrice[50]): We move the right and bottom borders of the BoxPreviousRange to 50 bars ago and the lowest price of 50 bars ago, respectively.
  11. box.set_bgcolor(BoxPreviousRange, color.new(color.red, 80)): We set the background color of the BoxPreviousRange to red.

Key Takeaways

  • box.set_lefttop and box.set_rightbottom functions in Pine Script provide an easy way to manipulate the position and dimensions of a box on a chart, essentially allowing us to visually represent price ranges or other relevant information.
  • These functions offer a dynamic way to highlight different areas on the chart based on the conditions defined in our script.
  • Coupled with other box functions like box.set_bgcolor and box.set_border_color, you can create visually impactful and informative markers on your trading charts.
  • It’s important to carefully manage the values passed to these functions to ensure the boxes accurately represent your intended analysis.

Conclusion

To wrap it up, the box.set_lefttop function and its counterparts in Pine Script open up a wide range of possibilities for advanced chart annotations. They allow you to visually emphasize important periods or price levels, which can help in understanding market behavior and patterns.

Leave a Comment