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

Understanding the ta.kcw() Function in Pine Script

Photo of author
Published on

Let’s dive into the details of how to use the ta.kcw() function, its syntax, and a practical example to better understand its application.

Syntax and Overloads

The ta.kcw() function calculates the width of the Keltner Channels, a type of volatility indicator. It does this by measuring the difference between the upper and lower Keltner Channels and dividing it by the middle channel. The function can be overloaded in two ways:

  1. ta.kcw(series, length, mult) → series float
  2. ta.kcw(series, length, mult, useTrueRange) → series float

Arguments

  • series (series int/float): The series of values to process. This is typically the price data.
  • length (simple int): The number of bars to include in the calculation. This determines the period of the EMA (Exponential Moving Average) used in the channel’s basis and range calculations.
  • mult (simple int/float): The standard deviation factor. This multiplier determines how wide the Keltner Channels are set.
  • useTrueRange (bool, optional): A boolean that decides whether to use the true range (ta.tr) instead of the high minus low for the span calculation. This can affect the width calculation, making it more sensitive to gaps in price data.

Example

Let’s look at an example to understand how ta.kcw() can be implemented in Pine Script:

//@version=5
indicator("Custom KCW Indicator")

// Using ta.kcw() built-in function
plot(ta.kcw(close, 5, 4), color=color.yellow)

// Custom implementation of Keltner Channel Width
customKCWidth(src, period, multiplier, useTR) =>
    float basis = ta.ema(src, period)
    float span = (useTR) ? ta.tr : (high - low)
    float rangeEma = ta.ema(span, period)
    ((basis + rangeEma * multiplier) - (basis - rangeEma * multiplier)) / basis

// Plotting the custom Keltner Channel Width
plot(customKCWidth(close, 5, 4, true))
Example

Breakdown

  • Indicator Declaration: The script begins with //@version=5 to specify it uses version 5 of Pine Script. The indicator() function declares a new indicator named “Custom KCW Indicator”.
  • Plotting with Built-in Function:
    • The plot() function is used to draw on the chart.
    • Inside plot(), the ta.kcw() function calculates the Keltner Channels Width using the closing price (close), with a length of 5 bars and a multiplier of 4.
    • The color of the plotted line is set to yellow.
  • Custom Function Definition:
    • customKCWidth is a user-defined function that calculates the width of the Keltner Channels in a custom way.
    • It accepts four parameters: src (source price series), period (number of bars for the EMA calculation), multiplier (to adjust the width of the channels), and useTR (a boolean to decide whether to use the true range or the difference between high and low prices for the span calculation).
  • Calculating Basis and Span:
    • Inside the customKCWidth function, basis is calculated as the Exponential Moving Average (EMA) of the source price series over the specified period.
    • span is determined by whether useTR is true or false. If true, the true range (ta.tr) is used; otherwise, the difference between the high and low prices is used.
  • Calculating Range EMA and Channel Width:
    • The EMA of the span over the specified period is stored in rangeEma.
    • The width of the Keltner Channels is then calculated by taking the difference between the upper and lower channels (defined as basis + rangeEma * multiplier and basis - rangeEma * multiplier, respectively) and dividing it by basis.
  • Plotting the Custom KCW:
    • Another plot() function is used to plot the result of the customKCWidth function on the chart.
    • This plot represents the custom calculation of the Keltner Channels Width using the closing price, with the same length of 5 bars, a multiplier of 4, and true range for the span calculation.

Returns

The ta.kcw() function returns a series of float values representing the width of the Keltner Channels over time. This can be plotted on a chart to visualize volatility and potential breakout points.

Remarks

When using the ta.kcw() function, it’s important to note that na (not available) values in the source series are ignored. The function calculates based on the length quantity of non-na values, ensuring continuity in the indicator even with missing data points.

Key Features and Takeaways

  • The ta.kcw() function is used to calculate the width of the Keltner Channels, offering insights into market volatility.
  • It supports two overloads, with and without the useTrueRange boolean, providing flexibility in how the width is calculated.
  • Customizing the function, as shown in the example, allows for additional control over its calculation and presentation.
  • The function’s ability to ignore na values ensures that it can provide consistent outputs even in datasets with gaps.

Understanding and utilizing the ta.kcw() function in Pine Script can significantly enhance trading strategies by providing a dynamic measure of market volatility through the Keltner Channels Width.

Leave a Comment