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

Understanding the ta.cmo() Function in Pine Script

Photo of author
Published on

In this article, we will delve into the ta.cmo() function in Pine Script, which stands for Chande Momentum Oscillator. This oscillator is a technical indicator designed to capture momentum in a given financial instrument’s price. By understanding and applying this function, traders and analysts can gain insights into potential reversals, strength, or weakness in the market trend. Let’s break down its syntax, usage, and application in Pine Script.


The syntax for the ta.cmo() function in Pine Script is as follows:

ta.cmo(series, length) → series float


  • series (series int/float): This is the series of values that the function will process. Typically, this is a price series such as close, open, high, or low.
  • length (series int): The number of bars, or the period over which the calculation is performed. This defines the sensitivity of the oscillator to market movements.


Below is a simple example of how to use the ta.cmo() function in Pine Script:

indicator("Chande Momentum Oscillator Example")
plot(ta.cmo(close, 5), color=color.yellow)

This script creates an indicator that plots the Chande Momentum Oscillator based on the closing price over a period of 5 bars. The oscillator is displayed in yellow.

Custom Implementation of ta.cmo()

For a deeper understanding, let’s explore a custom implementation of the ta.cmo() function:

indicator("Custom CMO Implementation")
f_cmo(source, period) =>
    momentum = ta.change(source)
    sum_positive = math.sum((momentum >= 0) ? momentum : 0.0, period)
    sum_negative = math.sum((momentum >= 0) ? 0.0 : -momentum, period)
    100 * (sum_positive - sum_negative) / (sum_positive + sum_negative)

plot(f_cmo(close, 5))
Custom Implementation

In this custom version, f_cmo is a user-defined function that calculates the Chande Momentum Oscillator. Here’s a breakdown of each step in the function:

  1. Calculate Momentum: momentum = ta.change(source) computes the change in price from one period to the next.
  2. Sum of Positive and Negative Movements: The function then calculates the sum of positive movements (sum_positive) and the sum of negative movements (sum_negative) over the specified period.
  3. CMO Calculation: Finally, the Chande Momentum Oscillator is calculated by subtracting sum_negative from sum_positive, dividing the result by the sum of sum_positive and sum_negative, and then multiplying by 100 to get a percentage.

Key Features and Takeaways

  • Function Usability: The ta.cmo() function is a versatile tool in Pine Script for measuring market momentum over a specified period.
  • Syntax and Application: With its straightforward syntax, it’s easy to apply to various financial instruments and time frames to gauge market sentiment.
  • Custom Implementation: Understanding the underlying calculation through a custom implementation like f_cmo allows for greater flexibility and the potential for customization in trading strategies.

In conclusion, the ta.cmo() function is a powerful indicator for analyzing market momentum. Whether using Pine Script’s built-in function or a custom implementation, understanding how to apply the Chande Momentum Oscillator can enhance your trading strategy and decision-making process.

Leave a Comment