This tutorial will focus on using the box.set_right
function, a crucial feature for manipulating box objects on TradingView charts.
Creating a Box using box.new
Before we delve into the box.set_right
function, let’s understand how to create a box object in Pine Script. A box object is created using the box.new
function:
box_id = box.new(left, top, right, bottom, border_color, border_width, border_style, extend, xloc, bgcolor, text, text_size, text_color, text_halign, text_valign, text_wrap, text_font_family)
In the function above, left
, top
, right
, and bottom
define the box’s dimensions. Other parameters define attributes like border properties, color, text, and more. The function returns a box
object, which we can further manipulate using functions like box.set_right
.
Understanding the box.set_right Function
The box.set_right
function is used to set the right coordinate of a box. The syntax is as follows:
box.set_right(id, right)
id
is abox
object.right
is an integer denoting the bar index or bar time of the right border.
Note that objects positioned using xloc.bar_index
cannot be drawn more than 500 bars into the future.
Box.set_right Use Case Example
Now let’s look at an example that uses both box.new
and box.set_right
functions:
First let’s take a look at this code without set.right command.
//@version=5 indicator("My Box Script", overlay = true) box_id = box.new(bar_index - 8 ,ta.highest(5), bar_index -3, ta.lowest(5)) //box.set_right(box_id, bar_index ) box.set_bgcolor(box_id, color.new(color.green, 90)) box.delete(box_id[1])
Now let’s remove the comment command from box.set_right function.
//version=5 indicator("My Box Script", overlay = true) box_id = box.new(bar_index - 8 ,ta.highest(5), bar_index -3, ta.lowest(5)) box.set_right(box_id, bar_index ) box.set_bgcolor(box_id, color.new(color.green, 90)) box.delete(box_id[1])
The code snippet above first declares an indicator with the indicator
function. The overlay = true
argument allows the indicator to be drawn directly on the price chart.
Next, a box object is created using box.new
. The left and right coordinates are set relative to the current bar (bar_index - 8
and bar_index -3
), while the top and bottom coordinates are determined by the highest and lowest prices over the past 5 bars, respectively.
Then box.set_right
function is used to reset the right coordinate of the box to the current bar_index
, effectively moving the right border of the box to the present time.
The color of the box is set to a semi-transparent green with the box.set_bgcolor
function.
Lastly, box.delete(box_id[1])
is used to delete the previous box, ensuring that only the current box is displayed on the chart.
Key Takeaway
The box.set_right
function is a versatile tool in Pine Script that allows for dynamic adjustments of box objects on a TradingView chart. With it, you can create flexible visualizations and illustrations that evolve with new data points.
Conclusion
In conclusion, the box.set_right
function in Pine Script allows us to manipulate the right coordinate of a box object, thereby providing flexibility in visualizing data and marking areas on the chart. Understanding how to use it is essential for any trader aiming to create dynamic and customizable indicators and strategies on Trading.