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

Understanding box.set_border_style() Function in Pine Script

Photo of author
Published on

Pine Script’s box.set_border_style() function is a powerful tool that allows traders and developers to customize the appearance of box drawings on charts. By adjusting the border style of a box, one can visually distinguish between different market conditions or highlight specific analytical conclusions. For example, a dashed border might indicate uncertainty or volatility, making it a useful visual aid in trading strategies.

Syntax

box.set_border_style(box_id, new_style)
  • box_id is the unique identifier for the box you wish to modify. This ID is obtained from the box.new() function, which initially creates the box.
  • new_style defines the desired border style of the box. Possible values include:
  • line.style_solid for a solid border,
  • line.style_dotted for a dotted border, and
  • line.style_dashed for a dashed border.

Example

Below is a script that utilizes the box.set_border_style() function. It dynamically creates a box to predict future price ranges, using different border styles to indicate the level of volatility associated with the asset being analyzed.

//@version=5
indicator(title="Enhanced Box Visualization Example", overlay=true)

// Determine the midpoint of the highest and lowest prices over the last 10 bars
midPriceRange = (ta.highest(high, 10) - ta.lowest(low, 10)) / 2

// Create a box to predict future price movement on the last confirmed historical bar
if barstate.islastconfirmedhistory
    futurePriceBox = box.new(left=bar_index, top=close + midPriceRange,
         right=bar_index + 10, bottom=close - midPriceRange, bgcolor=na,
         border_width=2, border_color=color.purple)

    // For highly volatile assets like cryptocurrencies, use a dotted border for the box
    if syminfo.type == "crypto"
        box.set_border_style(id=futurePriceBox, style=line.style_dotted)
Example

Walkthrough

  • The indicator() function sets up the script with a title and overlays it on the chart.
  • midPriceRange is calculated using the highest and lowest prices within the last 10 bars, providing a measure of recent volatility.
  • A conditional statement checks for the last confirmed historical bar (barstate.islastconfirmedhistory), ensuring the box is drawn only once.
  • box.new() creates the predictive box, with futurePriceBox storing its ID. The box extends from the current bar to 10 bars into the future, with its top and bottom borders based on the calculated midPriceRange.
  • A nested conditional statement then adjusts the box’s border style to dotted (line.style_dotted) if the asset is a cryptocurrency, highlighting the greater uncertainty in these markets.

Key Features and Takeaways

  • Functionality: The box.set_border_style() function customizes the visual representation of boxes, enhancing chart analysis.
  • Syntax: Requires two arguments, the box ID and the desired new style, allowing for dynamic adjustments based on data.
  • Application: Especially useful in scripts analyzing volatility or predicting future price ranges, where visual distinction can aid interpretation.

This modified example and detailed explanation aim to provide a comprehensive understanding of how to use the box.set_border_style() function in Pine Script to create visually distinctive analytical tools on trading charts.

Leave a Comment