Files
QuanTAlib/quantower/lib/cg.pine
T
Miha Kralj 86fe32a682 SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
Co-authored-by: Warp <agent@warp.dev>
2026-01-18 19:02:03 -08:00

34 lines
1.1 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Center of Gravity (CG)", "CG", overlay=false)
//@function Calculates Ehlers' Center of Gravity indicator
//@param src Series to calculate Center of Gravity from
//@param length Period for the Center of Gravity calculation
//@returns Center of Gravity value identifying cycle turning points
//@optimized for performance and dirty data
cg(series float src, simple int length) =>
if length <= 0
runtime.error("Length must be greater than 0")
float num = 0.0, float den = 0.0
for count = 1 to length
float price = nz(src[count - 1])
num += count * price
den += price
float result = den != 0 ? num / den : (length + 1) / 2.0
result - (length + 1) / 2.0
// ---------- Main loop ----------
// Inputs
i_length = input.int(10, "Length", minval=1, tooltip="Period for Center of Gravity calculation")
i_source = input.source(close, "Source")
// Calculation
cg_value = cg(i_source, i_length)
// Plot
plot(cg_value, "CG", color=color.yellow, linewidth=2)
hline(0, "Zero Line", color.gray, linestyle=hline.style_dashed)