This article delves into the `ta.bb()`

function, exploring its syntax, usage, and a practical example to enhance your trading strategies with Pine Script.

## Syntax

The `ta.bb()`

function in Pine Script is designed to calculate the Bollinger Bands of a given price series. Its syntax is:

ta.bb(series, length, mult) → [series float, series float, series float]

### Arguments

**series**`(series int/float)`

: The data series to process, typically the closing prices of a financial instrument.**length**`(series int)`

: The number of bars to consider for the simple moving average (SMA) and standard deviation calculations.**mult**`(simple int/float)`

: The multiplier for the standard deviation to set the width of the bands.

## Example

Let’s explore a practical example of using the `ta.bb()`

function to plot Bollinger Bands:

//@version=5 indicator("Custom Bollinger Bands") [coreMiddle, coreUpper, coreLower] = ta.bb(close, 5, 4) plot(coreMiddle, color=color.yellow) plot(coreUpper, color=color.yellow) plot(coreLower, color=color.yellow)

## Walkthrough of Code

**Script Version Declaration**:`//@version=5`

: This line specifies that the script is written for version 5 of Pine Script. Each version has its own set of features and syntax rules, so it’s crucial to declare the correct version to ensure the script runs as expected.

**Indicator Declaration**:`indicator("Custom Bollinger Bands")`

: This function declares a new indicator with the name “Custom Bollinger Bands”. It makes the script visible in the TradingView platform as an indicator that users can add to their charts.

**Bollinger Bands Calculation**:`[coreMiddle, coreUpper, coreLower] = ta.bb(close, 5, 4)`

: This line calculates the Bollinger Bands using the built-in`ta.bb()`

function. The function takes three arguments:`close`

: The series of closing prices to calculate the bands on. In Pine Script,`close`

refers to the closing prices of the bars on the chart.`5`

: The length of the moving average and the standard deviation calculation, indicating that the last 5 bars are considered.`4`

: The multiplier for the standard deviation, setting the distance between the middle band and the upper and lower bands.

`coreMiddle`

,`coreUpper`

, and`coreLower`

respectively.**Plotting the Bollinger Bands**:`plot(coreMiddle, color=color.yellow)`

: This line plots the middle Bollinger Band on the chart, using a yellow color. The`plot()`

function takes a series of values to plot (here, the middle band) and an optional color argument.`plot(coreUpper, color=color.yellow)`

: Similarly, this line plots the upper Bollinger Band in yellow.`plot(coreLower, color=color.yellow)`

: This line plots the lower Bollinger Band in yellow.

`plot()`

function call creates a new line on the chart for the respective Bollinger Band component, allowing traders to visually assess volatility and potential price levels of interest.

## Alternative Implementation

The `ta.bb()`

function encapsulates the computation of Bollinger Bands. However, understanding its internal workings can be beneficial. Here’s how you might manually implement a similar function, `customBB()`

, in Pine Script:

f_customBB(src, bars, factor) => float basis = ta.sma(src, bars) float deviation = factor * ta.stdev(src, bars) [basis, basis + deviation, basis - deviation] [customMiddle, customUpper, customLower] = f_customBB(close, 5, 4) plot(customMiddle) plot(customUpper) plot(customLower)

This custom implementation, `f_customBB()`

, mirrors the functionality of `ta.bb()`

, utilizing the simple moving average (`ta.sma()`

) and the standard deviation (`ta.stdev()`

) for the calculations.

## Walkthrough of Code

**Custom Function Definition**:`f_customBB(src, bars, factor) =>`

: This line defines a custom function named`f_customBB`

that calculates Bollinger Bands. The function takes three parameters:`src`

: The data series to calculate the Bollinger Bands for, typically price data such as closing prices.`bars`

: The number of periods (bars) to use for the moving average and standard deviation calculations.`factor`

: The multiplier for the standard deviation to determine the distance of the upper and lower bands from the middle band.

**Calculating the Middle Band**:`float basis = ta.sma(src, bars)`

: Inside the function, this line calculates the simple moving average (SMA) of the provided data series (`src`

) over the specified number of bars (`bars`

). This SMA serves as the middle band of the Bollinger Bands.

**Calculating the Standard Deviation**:`float deviation = factor * ta.stdev(src, bars)`

: This line calculates the standard deviation of the`src`

data series over the specified`bars`

, then multiplies it by`factor`

. This product determines the distance that the upper and lower bands will be plotted from the middle band.

**Returning the Bollinger Bands**:`[basis, basis + deviation, basis - deviation]`

: The function returns a tuple containing three elements: the middle band (`basis`

), the upper band (`basis + deviation`

), and the lower band (`basis - deviation`

). This allows the function to output all three components of the Bollinger Bands in a single call.

**Function Call and Band Assignment**:`[customMiddle, customUpper, customLower] = f_customBB(close, 5, 4)`

: This line calls the`f_customBB`

function with the closing prices (`close`

) as the data series,`5`

as the number of bars, and`4`

as the standard deviation multiplier. The returned values are assigned to`customMiddle`

,`customUpper`

, and`customLower`

respectively, representing the middle, upper, and lower Bollinger Bands.

**Plotting the Bollinger Bands**:`plot(customMiddle)`

: Plots the middle Bollinger Band on the chart.`plot(customUpper)`

: Plots the upper Bollinger Band on the chart.`plot(customLower)`

: Plots the lower Bollinger Band on the chart.

`plot()`

calls visually represent the Bollinger Bands calculated by the`f_customBB`

function on the chart. No specific color or style is specified for these plots, so they will be displayed in the default color and style set by the TradingView platform or the user’s settings.

## Key Features and Takeaways

**Function Usability**: The`ta.bb()`

function simplifies the process of adding Bollinger Bands to your indicators or strategies, requiring only the series, length, and multiplier as inputs.**Syntax and Application**: Its straightforward syntax allows for easy adjustments to the Bollinger Bands’ sensitivity by altering the`length`

and`mult`

parameters.**Manual Calculation Insight**: Understanding the manual calculation of Bollinger Bands (via`f_customBB()`

) offers deeper insights into the indicators’ mechanics and potential for customization.

By incorporating the `ta.bb()`

function into your Pine Script toolbox, you can enhance your trading strategies with valuable insights into market volatility and price trends. Whether using the built-in function or exploring a custom implementation, Bollinger Bands remain a cornerstone of technical analysis in financial markets.