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

Understanding the ta.kc() Function in Pine Script

Photo of author
Published on

This article delves into the syntax, usage, and practical examples of the ta.kc() function, aiming to provide a clear understanding of how it can be leveraged in trading analysis.

Keltner Channels

Keltner Channels are a type of technical analysis indicator consisting of three lines: a central moving average (the middle line) and two channel lines located above and below the central line. These channels are designed to help traders identify potential buy and sell signals based on the position of the price relative to the channels.

Syntax and Overloads

Pine Script’s ta.kc() function can be called with different sets of arguments, known as overloads. The basic syntax is as follows:

Basic Syntax

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

Extended Syntax with True Range

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

Arguments

  • series (series int/float): The input data series (e.g., closing prices).
  • length (simple int): The number of bars to calculate the moving average.
  • mult (simple int/float): The multiplier for the channel width.
  • useTrueRange (optional bool): A boolean to indicate whether to use the true range for channel width calculation.

Practical Example

Consider the following example to understand how to implement and plot Keltner Channels using Pine Script:

//@version=5
indicator("Custom KC Example")

[coreLine, bandUpper, bandLower] = ta.kc(close, 5, 4)
plot(coreLine, color=color.blue)
plot(bandUpper, color=color.green)
plot(bandLower, color=color.red)
Example

This script creates a custom indicator named “Custom KC Example” that calculates the Keltner Channels based on the close prices over 5 bars with a multiplier of 4 for the channel width. The central line is plotted in blue, the upper channel in green, and the lower channel in red.

Code Explanation

  1. Version Declaration: //@version=5 – Specifies the version of Pine Script used for the script. This ensures compatibility with the platform’s features and syntax specific to version 5.
  2. Indicator Declaration: indicator("Custom KC Example") – Declares a new indicator with the name “Custom KC Example”. This name appears in the indicator’s window on TradingView charts.
  3. Keltner Channel Calculation:
    • [coreLine, bandUpper, bandLower] = ta.kc(close, 5, 4) – This line computes the Keltner Channels using the ta.kc() function.
      • close refers to the closing prices of the bars/candles, which is the series of values the Keltner Channels are calculated on.
      • 5 is the length parameter, indicating the number of bars to calculate the exponential moving average (EMA) for the middle line.
      • 4 is the multiplier for the channel width, determining how far the upper and lower bands are from the central line.
  4. Plotting the Channels:
    • plot(coreLine, color=color.blue) – Plots the central line (coreLine) of the Keltner Channels in blue. This line is typically an EMA of the closing prices.
    • plot(bandUpper, color=color.green) – Plots the upper band (bandUpper) of the Keltner Channels in green. This band is positioned above the central line by a distance calculated using the average true range (ATR) multiplied by the specified multiplier (4 in this case).
    • plot(bandLower, color=color.red) – Plots the lower band (bandLower) of the Keltner Channels in red. Similar to the upper band, this band is positioned below the central line by the same distance.

Custom Function Variant

To demonstrate flexibility, let’s create a custom variant of the Keltner Channel calculation that allows for the option of using the true range in the channel width computation:

f_customKC(src, period, factor, useTR) =>
    float basis = ta.ema(src, period)
    float span = useTR ? ta.tr : (high - low)
    float rangeEma = ta.ema(span, period)
    [basis, basis + rangeEma * factor, basis - rangeEma * factor]

[customMid, customUpper, customLower] = f_customKC(close, 5, 4, true)

plot(customMid, color=color.purple)
plot(customUpper, color=color.orange)
plot(customLower, color=color.red)
Custom Function Variant

Code Explanation

  1. Custom Function Definition: f_customKC(src, period, factor, useTR) =>
    • This line starts the definition of a custom function named f_customKC which takes four parameters:
      • src: The data series to calculate the channels on (e.g., close prices).
      • period: The number of bars to use for the exponential moving average (EMA) calculation.
      • factor: The multiplier used to determine the width of the channels.
      • useTR: A boolean that determines whether to use the true range (ta.tr) or the high-low difference for the channel width calculation.
  2. Central Line Calculation: float basis = ta.ema(src, period)
    • Calculates the EMA of the provided data series (src) over the specified period. This value serves as the central line of the Keltner Channels.
  3. Span Calculation: float span = useTR ? ta.tr : (high - low)
    • Determines the range to use for the channel width. If useTR is true, it uses the true range of the bars. Otherwise, it calculates the difference between the high and low prices of each bar.
  4. Range EMA Calculation: float rangeEma = ta.ema(span, period)
    • Calculates the EMA of the selected span (ta.tr or high-low) over the specified period. This averaged range is then used to calculate the upper and lower bands of the channels.
  5. Channels Calculation:
    • [basis, basis + rangeEma * factor, basis - rangeEma * factor]
    • The function returns a tuple with three elements:
      • The central line (basis).
      • The upper band (basis + rangeEma * factor).
      • The lower band (basis - rangeEma * factor).
  6. Function Call and Channel Plotting:
    • [customMid, customUpper, customLower] = f_customKC(close, 5, 4, true)
      • Calls the f_customKC function with the closing price as src, a period of 5, a factor of 4, and true for using the true range. The results are assigned to customMid (central line), customUpper (upper band), and customLower (lower band).
    • plot(customMid, color=color.purple)
      • Plots the central line of the Keltner Channels in purple.
    • plot(customUpper, color=color.orange)
      • Plots the upper band of the Keltner Channels in orange.
    • plot(customLower, color=color.red)
      • Plots the lower band of the Keltner Channels in red.

Key Features and Takeaways

  • Function Flexibility: The ta.kc() function and its custom variants offer flexibility in technical analysis, allowing users to adjust parameters according to their trading strategy.
  • Syntax Variability: Understanding the syntax and how to manipulate arguments such as length, mult, and useTrueRange is crucial for accurate indicator representation.
  • Customizability: Pine Script allows for the creation of custom functions (like f_customKC in our example), enabling traders to tailor indicators to their specific needs.

By mastering functions such as ta.kc() in Pine Script, traders can enhance their technical analysis toolkit, aiding in better decision-making based on the dynamics of Keltner Channels.

Leave a Comment