Files

34 lines
1.1 KiB
Plaintext

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Ehlers 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)