Home » Technical Analysis Functions » Understanding ta.bbw() Function in Pine Script

Understanding ta.bbw() Function in Pine Script

Photo of author
Published on

Bollinger Bands Width (BBW) is a technical analysis tool that measures the difference between the upper and lower Bollinger Bands, normalized by the middle band. This indicator provides insights into market volatility, with wider bands indicating higher volatility and narrower bands suggesting lower volatility. In Pine Script, the ta.bbw() function simplifies the computation of BBW, facilitating its integration into custom trading strategies or indicators.

Syntax

ta.bbw(series, length, mult) → series float
  • series (series int/float): The data series (such as price data) on which the BBW calculation is based.
  • length (series int): The number of bars to include in the Bollinger Bands calculation.
  • mult (simple int/float): The standard deviation multiplier for the upper and lower bands.

Example

The provided example demonstrates how to use the ta.bbw() function within a custom indicator script in Pine Script.

//@version=5
indicator("Custom BBW")

plot(ta.bbw(close, 5, 4), color=color.yellow)
Example
  • indicator("Custom BBW"): Defines a new indicator with the title “Custom BBW”.
  • plot(ta.bbw(close, 5, 4), color=color.yellow): Plots the BBW of the close price series over the last 5 bars with a standard deviation multiplier of 4, colored yellow.

Additionally, an equivalent custom function f_bbw() is shown to illustrate how BBW can be manually calculated.

f_bbw(src, period, mult) =>
    float mean = ta.sma(src, period)
    float deviation = mult * ta.stdev(src, period)
    ((mean + deviation) - (mean - deviation)) / mean

plot(f_bbw(close, 5, 4))
Example
  • mean: The simple moving average (SMA) serves as the middle Bollinger Band.
  • deviation: The product of the standard deviation of the series and the multiplier mult gives the distance from the mean to each band.
  • ((mean + deviation) - (mean - deviation)) / mean: This formula calculates the width of the Bollinger Bands normalized by the middle band.

Key Features

  • Function Usability and Syntax: ta.bbw() streamlines the BBW calculation, making it accessible with just one line of code. This function is versatile, accepting any series of integer or float values for volatility analysis.
  • Application: BBW is primarily used to assess market volatility. A sudden expansion or contraction in the BBW can signal potential market trend changes or periods of high uncertainty.
  • Remarks: Non-applicable (na) values in the input series are ignored. The function calculates based on the number of non-na values specified by the length argument.

Takeaways

  • ta.bbw() provides a concise and efficient way to calculate Bollinger Bands Width in Pine Script.
  • By analyzing BBW, traders can gain insights into market volatility and potential trend changes.
  • The custom function f_bbw() offers an alternative method for users who prefer more control over the calculation process, demonstrating the flexibility of Pine Script in creating customized indicators.

Leave a Comment