Center of Gravity (cog)
The Center of Gravity (COG) describes where the values of a source are positioned in time within a window of length candles. When the source values share the same sign, especially with a positive price source, each value can be treated as a weight attached to the age of its candle. The output then represents the weighted average age within the window.
Under this conventional interpretation, the scale runs from -1 when the weights are concentrated toward the most recent candles to -length when they are concentrated toward the oldest ones. A uniform distribution lies near the middle of that scale. COG therefore represents neither a price nor a change, but the relative position of those weights within the window. When source mixes positive and negative values, this conventional interpretation no longer applies directly.
Block declaration
A strategy can contain multiple [[cog]] blocks. Each [[cog]] block produces one numeric series identified by its id field.
Examples
Minimal setup
This block computes COG from the close source, using the default length 10 and the main symbol and timeframe defined in [backtest].
[[cog]]
id = "cog"Custom fixed setup
This block uses a custom value for length.
[[cog]]
id = "cog"
length = 21Finding the optimal length
This block explores a range of values for length to identify the best-performing length.
[[cog]]
id = "cog"
length.start = 5
length.stop = 20Testing close and hlc3 as sources
This block tests COG with two input sources, close and hlc3 (average of high, low, close). One combination is generated for each source. The block still uses the main symbol and timeframe.
[[cog]]
id = "cog"
source = ["close", "hlc3"]Dedicated symbol and timeframe
This block computes the COG from the daily series of BINANCE:ETHUSDT, independently of the primary symbol and timeframe used by the rest of the strategy. See Exchanges, Symbols and Timeframes for alignment rules.
[[cog]]
id = "cog"
symbol = "BINANCE:ETHUSDT"
timeframe = "D"Parameters
| Parameter | Description |
|---|---|
idString Required | Unique name identifying the COG series. |
sourceString or Array Optional | Input series used for the calculation. Accepted forms: source = "hl2" or source = ["close", "hl2"].Each value can be either a standard price source ( open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume) or the id of another indicator.Default value: "close" |
lengthInteger Optional | Look‑back window size; must be strictly greater than 0 when provided. If length is omitted, the default value is 10.Usage: • Fixed: length = value• Grid search: – length.start = min_value– length.stop = max_value– length.step = value (optional, default 1) |
symbolString or Array Optional | Market symbol(s) that provide the source series when source contains standard prices (open, close, high, low, hl2, hlc3, ohlc4, hlcc4, volume).If source mixes standard prices and indicator ids, symbol is applied only to combinations based on standard prices.If source contains only indicator ids, symbol is ignored.For format and alignment details, see Exchanges, Symbols and Timeframes. |
timeframeString or Array Optional | Timeframe on which this indicator is computed. If omitted, the grid’s main timeframe is used. For valid formats and cross‑timeframe behavior, see Exchanges, Symbols and Timeframes. |
Available variables
The COG block exposes one numeric series, its configuration parameters, and metadata about the input source, symbol, and timeframe. You can use these identifiers directly inside your expressions.
Assume the block is configured as:
[[cog]]
id = "cog"Then:
| Variable | Description |
|---|---|
cog or cog[0]Decimal | Current COG value. |
cog[n]Decimal | COG value from n bars ago. |
cog.lengthDecimal | Effective look‑back length in use. |
cog.sourceString | Name of the input source series. |
cog.symbolString | Symbol from which the input data is read. |
cog.timeframeString | Timeframe on which the COG is computed. |
Notes
- If the sum of the
sourcevalues in the window is zero, the COG value is unavailable (NaN). - If
sourcemixes positive and negative values, the result can fall outside the conventional range from-lengthto-1. - Numeric variables can be combined freely with arithmetic, comparison, and logical operators.
- Text variables are meant for equality/inequality checks or for logging and export purposes.