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 aseries box
object, which is the box that you want to modify.left
is aseries int
that represents the bar index or bar time of the left border.top
is aseries 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))
Here’s what each line does:
//@version=5
: This line indicates that we are using version 5 of Pine Script.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.RANGE_PERIOD = 50
: Here, we declare a constantRANGE_PERIOD
and set its value to 50.maxPrice = ta.highest(RANGE_PERIOD)
: We calculate the highest price over the lastRANGE_PERIOD
bars.minPrice = ta.lowest(RANGE_PERIOD)
: We calculate the lowest price over the lastRANGE_PERIOD
bars.if barstate.islastconfirmedhistory
: Thisif
statement checks whether the current bar is the last confirmed historical bar.var BoxCurrentRange = box.new(bar_index[RANGE_PERIOD], maxPrice, bar_index, minPrice, bgcolor = color.new(color.green, 80))
: If theif
statement is true, we create a new box from the current bar back to theRANGE_PERIOD
bars in the past, from the highest price to the lowest price within that period. The box has a green background color.var BoxPreviousRange = box.copy(BoxCurrentRange)
: We create a copy of theBoxCurrentRange
.box.set_lefttop(BoxPreviousRange, bar_index[RANGE_PERIOD * 2], maxPrice[50])
: We move the left and top borders of theBoxPreviousRange
to 100 bars ago and the highest price of 50 bars ago, respectively.box.set_rightbottom(BoxPreviousRange, bar_index[RANGE_PERIOD], minPrice[50])
: We move the right and bottom borders of theBoxPreviousRange
to 50 bars ago and the lowest price of 50 bars ago, respectively.box.set_bgcolor(BoxPreviousRange, color.new(color.red, 80))
: We set the background color of theBoxPreviousRange
to red.
Key Takeaways
box.set_lefttop
andbox.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
andbox.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.