Ergodic Oscillator and Center of Gravity Trading Strategy
The Ergodic Oscillator uses 20- and 5-period EMA smoothing and a 5-period signal line. A 10-period Center of Gravity oscillator gives more weight to recent closes.
- Buy when the Ergodic Oscillator is positive and Center of Gravity is higher than on the previous candle.
- Sell when the Ergodic Oscillator is below zero.
Follow the backtest setup to run this example on a spot pair.
// Read completed candles. The newest candle can still be forming.
const candleTime = Number(gb.data.candlesTimestamp.at(-2));
if (!(candleTime > 0) || !(gb.data.ask > 0) || !(gb.data.bid > 0)) return;
const store = gb.data.pairLedger.customStratStore ??= {};
if (store.ergodicOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 80) return;
// Use the same synchronous Tulind callback as the editor's spot starter.
function indicator(name, inputs, options) {
let series = [];
gb.method.tulind.indicators[name].indicator(inputs, options, (error, results) => {
if (error) throw error;
series = results;
});
return series;
}
const prices = closes.slice(-80);
const changes = prices.slice(1).map((price, i) => price - prices[i]);
const [momentumSlow] = indicator("ema", [changes], [20]);
const [momentum] = indicator("ema", [momentumSlow], [5]);
const [absoluteSlow] = indicator("ema", [changes.map(Math.abs)], [20]);
const [absolute] = indicator("ema", [absoluteSlow], [5]);
const tsi = momentum.map((value, i) => absolute[i] > 0 ? 100 * value / absolute[i] : 0);
const [signal] = indicator("ema", [tsi], [5]);
function centerOfGravity(window) {
const total = window.reduce((sum, price) => sum + price, 0);
const weighted = window.reduce((sum, price, i) => sum + (i + 1) * price, 0);
return total > 0 ? 5.5 - weighted / total : 0;
}
const ergodic = tsi.at(-1) - signal.at(-1);
const cog = centerOfGravity(prices.slice(-10));
const priorCog = centerOfGravity(prices.slice(-11, -1));
const buySignal = ergodic > 0 && cog > priorCog;
const sellSignal = ergodic < 0;
// Spot market order amounts are in the asset being traded (BTC in USDT-BTC).
if (gb.data.gotBag && sellSignal && gb.data.quoteBalance > 0) {
store.ergodicOrderCandle = candleTime;
await gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName, gb.data.exchangeName);
} else if (!gb.data.gotBag && buySignal) {
const budget = Number(gb.data.pairLedger.whatstrat.TRADING_LIMIT);
if (!Number.isFinite(budget) || budget <= 0 || gb.data.baseBalance <= budget) return;
const amount = budget / gb.data.ask;
store.ergodicOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

This Binance spot backtest filled 101 buys and 100 sells on 1-hour USDT-BTC candles from January through March 2024, using a 1,000 USDT starting balance, a 100 USDT buy budget and 0.1% trading fees.