Center of Gravity (cog)
[[cog]] is an unbounded oscillator that measures the position of recent prices inside an observation window. You can use it to spot potential reversals, compare several strategy configurations, or build signals from crossovers with other series.
The COG reacts to the movement of the most recent prices. When its curve changes quickly, it reflects a marked shift in the balance of recent candles. When its variations are smaller, recent price behavior is more stable.
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 main symbol and timeframe defined in [backtest].
[[cog]]
id = "cog"
length = 10Finding 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"]
length = 10Dedicated timeframe
In this configuration, the COG runs on a dedicated daily timeframe, independently of the primary timeframe used by the rest of the strategy. See the Exchanges, Symbols and Timeframes page for alignment rules between this timeframe and the primary one.
[[cog]]
id = "cog"
length = 10
timeframe = "D"Specific symbol
This block uses a specific market symbol for the COG input prices. It reads its source data from BINANCE:ETHUSDT, while the rest of the strategy may run on another instrument.
[[cog]]
id = "cog"
length = 10
symbol = "BINANCE:ETHUSDT"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 Required | Look‑back window size; must be strictly greater than 0. Usage: • Fixed: length = 10• 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"
source = "close"
length = 10Then:
| 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
- 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.