SpecialPine Script v6

ta.cog()Center of Gravity

The Center of Gravity oscillator is the negative ratio of a bar-position-weighted price sum to the unweighted price sum over the lookback. It is not a price level.

TradingView language reference

Syntax

Syntax
ta.cog(source, length) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars in the rolling window.

Returns

Center of Gravity.

Remarks

na values in the source series are ignored.

Code Examples

Pine Script v6 Example
//@version=6
indicator("ta.cog", overlay=true)
plot(ta.cog(close, 10))

// the same on pine
pine_cog(source, length) =>
    sum = math.sum(source, length)
    num = 0.0
    for i = 0 to length - 1
        price = source[i]
        num := num + price * (i + 1)
    -num / sum

plot(pine_cog(close, 10))

Trading Applications

Inspect shifts in the price-weighted center across a rolling window

Compare oscillator turns with price; no universal overbought threshold is implied

Build mean-reversion strategies

Test oscillator-based hypotheses without treating turns as forecasts

Related Functions

Frequently Asked Questions

ta.cog() returns a normalized, negative weighted-position measure. In the example, lag i receives weight i + 1, and the sum is divided by the sum of source values. Its scale differs from price.

It treats prices in the window like mass along a beam and solves for the balance point; the result tracks where the “mass” of price sits over the lookback.

It uses the current and past source values. The formula does not observe future prices, and a turn does not establish a future reversal or a profitable entry.

Generate ta.cog() Code with AI

Draft Pine Script using ta.cog() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.