Indicator · Pine Script v6 · Own pane

Center Of Gravity Oscillator

by rubbermetal· code credited to veryfid — enhanced by claygrace74

A separate-pane oscillator based on the centre of gravity calculation with an adjustable length, an optional RMA smoothing, and a trigger line made with an ALMA filter.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code175
Settings19inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

175 lines of Pine Script v6 from Center of Gravity.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

Center of Gravity.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © veryfid — enhanced by claygrace74

//@version=6
indicator("Center Of Gravity Oscillator", shorttitle="COG", precision=4)

// ==================== COG Settings ====================
var cog_group = "COG Settings"
src     = input.source(close, "Source", group=cog_group)
length  = input.int(9, "COG Length", minval=1, group=cog_group)
smooth  = input.string("NONE", "Smoothing", options=["RMA", "NONE"], group=cog_group)
length5 = input.int(2, "Smoothing Length", minval=1, group=cog_group)

// ==================== Trigger Settings ====================
var trig_group = "Trigger Settings"
windowsize = input.int(3, "Window Size", group=trig_group)
trigOffset = input.float(0.85, "Offset", group=trig_group)
sigma      = input.float(6.0, "Sigma", group=trig_group)

// ==================== Theme & Display ====================
var theme_group = "Theme & Display"
theme   = input.string("CUSTOM", "Theme", options=["CUSTOM", "CLEAN", "FIBONACCI", "FIBONACCI 2"], group=theme_group)
pline   = input.bool(true, "Show Price Line", group=theme_group)
showcol = input.bool(false, "Dual Colored Trigger", group=theme_group)
showhl  = input.bool(false, "Show High/Low Levels", group=theme_group)

// ==================== LSMA & Range ====================
var lsma_group = "LSMA & Range"
length2 = input.int(20, "Previous High/Low Length", minval=1, group=lsma_group)
length3 = input.int(200, "LSMA Length", minval=1, group=lsma_group)

// ==================== Fibonacci Settings ====================
var fib_group = "Fibonacci Settings"
fib     = input.bool(false, "Show Fibonacci Levels", group=fib_group)
length4 = input.int(1000, "Fibonacci Lookback", minval=1, group=fib_group)
col1    = input.color(color.white, "Fibonacci Color", group=fib_group)

// ==================== Signal Settings ====================
var sig_group = "Signal Settings"
i_showSignals    = input.bool(true, "Show Buy/Sell Signals", group=sig_group)
i_showDivergence = input.bool(false, "Show Divergence Signals", group=sig_group)
i_divLookback    = input.int(5, "Divergence Lookback", minval=3, group=sig_group)

// ==================================================================================
// Theme Logic
// ==================================================================================
bool useFib    = fib
int  fibLen    = length4
bool useDualCol = showcol

if theme == "FIBONACCI"
    useFib  := true
    fibLen  := 50
if theme == "FIBONACCI 2"
    useFib    := true
    fibLen    := 30
    useDualCol := true

// ==================================================================================
// Core Calculations
// ==================================================================================
COG1   = ta.cog(src, length)
cogRma = ta.rma(COG1, length5)

COG = smooth == "RMA" ? cogRma : COG1
if theme == "CLEAN"
    COG := ta.rma(COG1, 9)

trigger = ta.alma(COG, windowsize, trigOffset, sigma)
lsma    = ta.linreg(COG, length3, 0)

// Momentum coloring — acceleration based
cogChange = ta.change(COG)
cogAccel  = ta.change(cogChange)
cogColor  = cogAccel > 0 ? (cogChange > 0 ? color.green : color.new(color.orange, 30)) : (cogChange > 0 ? color.new(color.green, 50) : color.red)

// Previous high/low
cogHigh = ta.highest(COG, length2)
cogLow  = ta.lowest(COG, length2)

// Fibonacci levels
h = ta.highest(COG, fibLen)
l = ta.lowest(COG, fibLen)
cogRange = h - l

l1 = h - cogRange * 0.236
l2 = h - cogRange * 0.382
l3 = h - cogRange * 0.618
l4 = h - cogRange * 0.764
l5 = h - cogRange * 0.5

// ==================================================================================
// Signals
// ==================================================================================
enter = ta.crossover(COG, trigger)
exit  = ta.crossunder(COG, trigger)

// Divergence detection (simplified)
priceHigh    = high > ta.highest(high, i_divLookback)[1]
cogNotHigh   = COG < ta.highest(COG, i_divLookback)[1]
bearDiv      = priceHigh and cogNotHigh

priceLow     = low < ta.lowest(low, i_divLookback)[1]
cogNotLow    = COG > ta.lowest(COG, i_divLookback)[1]
bullDiv      = priceLow and cogNotLow

// ==================================================================================
// Plots
// ==================================================================================

// Reference line
plot(-5, color=color.new(color.black, 50), style=plot.style_line, linewidth=1, title="Reference")

// Previous high/low (hidden for fib themes)
isFibTheme = theme == "FIBONACCI" or theme == "FIBONACCI 2"
p1 = plot(not isFibTheme ? cogLow : na,  color=color.new(color.green, 100), title="Previous Low")
p2 = plot(not isFibTheme ? cogHigh : na, color=color.new(color.red, 100),   title="Previous High")

// COG price line (momentum colored)
c2 = plot(pline ? COG : na, color=cogChange > 0 ? color.new(#00ffaa, 0) : color.new(color.red, 0), linewidth=theme == "FIBONACCI 2" ? 3 : 1, title="COG Trend Line", trackprice=true)

// COG main line
c = plot(theme != "FIBONACCI 2" and theme != "CLEAN" ? COG : na, color=cogColor, linewidth=2, title="COG")

// Fills for previous high/low
fill(p1, c, title="Previous Low Fill")
fill(p2, c, color=color.new(color.red, 80), title="Previous High Fill")

// Trigger line
trigColor = useDualCol ? (cogChange > 0 ? color.green : color.red) : color.blue
plot(not isFibTheme and theme != "CLEAN" ? trigger : na, title="Trigger", color=trigColor)

// LSMA
plot(not isFibTheme ? lsma : na, color=color.red, title="LSMA")

// Clean theme specific
plot(theme == "CLEAN" ? COG : na,          color=color.blue,  linewidth=3, title="COG Clean")
plot(theme == "CLEAN" ? ta.sma(COG, 2) : na, color=color.white, linewidth=2, title="COG Clean SMA")

// Fibonacci levels
f1 = plot(useFib ? l1 : na, color=color.new(col1, 60), title="0.236")
f2 = plot(useFib ? l2 : na, color=color.new(col1, 60), title="0.382")
f3 = plot(useFib ? l3 : na, color=color.new(col1, 60), title="0.618")
f4 = plot(useFib ? l4 : na, color=color.new(col1, 60), title="0.764")
f5 = plot(useFib ? l5 : na, color=theme == "FIBONACCI 2" ? color.new(#ffe0b2, 20) : color.new(col1, 60), title="0.5")

// High/low boundary lines
p3 = plot(showhl or theme == "FIBONACCI" ? h : na, title="High")
p4 = plot(showhl or theme == "FIBONACCI" ? l : na, title="Low")
p5 = plot(theme == "FIBONACCI 2" ? h : na, title="High F2", color=color.red)
p6 = plot(theme == "FIBONACCI 2" ? l : na, title="Low F2",  color=color.green)

// Fibonacci 2 fills
fill(p5, f1, color=color.new(color.red, 80))
fill(p5, f2, color=color.new(color.red, 80))
fill(p6, f4, color=color.new(color.green, 80))
fill(p6, f3, color=color.new(color.green, 80))

// ==================================================================================
// Signal Shapes
// ==================================================================================
plotshape(i_showSignals and enter, style=shape.triangleup,   location=location.bottom, color=color.green, text="Buy",  textcolor=color.green, size=size.tiny, title="Buy Signal")
plotshape(i_showSignals and exit,  style=shape.triangledown, location=location.top,    color=color.red,   text="Sell", textcolor=color.red,   size=size.tiny, title="Sell Signal")

// Divergence background highlights
bgcolor(i_showDivergence and bearDiv ? color.new(color.orange, 85) : na, title="Bearish Divergence")
bgcolor(i_showDivergence and bullDiv ? color.new(color.teal, 85)   : na, title="Bullish Divergence")

// ==================================================================================
// Alert Conditions
// ==================================================================================
alertcondition(enter,    "COG Buy Signal",        "COG crossed above trigger line")
alertcondition(exit,     "COG Sell Signal",        "COG crossed below trigger line")
alertcondition(bullDiv,  "Bullish Divergence",     "Price making new low but COG is not — potential reversal")
alertcondition(bearDiv,  "Bearish Divergence",     "Price making new high but COG is not — potential reversal")

Overview

Center Of Gravity Oscillator also computes a linear regression line, previous high and low levels of the oscillator and optional Fibonacci levels. Several themes change the look, and the line colour reflects acceleration. Buy and sell markers appear when the oscillator and trigger cross, and divergence signals can be switched on. All lengths and offsets are adjustable.

Details

Published by

rubbermetal on GitHub

Code credit

veryfid — enhanced by claygrace74, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Divergence

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosesrc
COG Lengthint9length
SmoothingstringNONEsmooth
Smoothing Lengthint2length5
Window Sizeint3windowsize
Offsetfloat0.85trigOffset
Sigmafloat6.0sigma
ThemestringCUSTOMtheme
Show Price Linebooltruepline
Dual Colored Triggerboolfalseshowcol
Show High/Low Levelsboolfalseshowhl
Previous High/Low Lengthint20length2
Show all 19 settings
SettingTypeDefaultVariable
LSMA Lengthint200length3
Show Fibonacci Levelsboolfalsefib
Fibonacci Lookbackint1000length4
Fibonacci Colorcolorcolor.whitecol1
Show Buy/Sell Signalsbooltruei_showSignals
Show Divergence Signalsboolfalsei_showDivergence
Divergence Lookbackint5i_divLookback
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
330 stars

This indicator calculates the MACD oscillator from a configurable source with fast and slow lengths, then plots the histogram, MACD line, and signal line in a separate pane. It detects regular bullish divergences (price lower lows with oscillator higher lows),

Indicator v5 Oscillators & momentumDivergence
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]
218 stars

This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe RSI with Divergence

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

RSI [SystemAlpha]

by iamc1oud
56 stars

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills. It identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and be

Indicator v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of RSI [SystemAlpha]
55 stars

This indicator calculates a 14-period RSI from close and plots it in a separate pane with dotted overbought and dashed oversold lines at 70 and 30. It marks bullish divergence when a five-bar pivot low in price is lower than the price twenty bars earlier while

Indicator v6 Oscillators & momentumDivergence
Updated 6 months ago · Apache-2.0 GitHub repository of RSI Divergence Detector

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers