Skip to main content

Ergodic Oscillator & Center of Gravity

The Ergodic Oscillator is a technical indicator used to identify trend reversals in financial markets. It's calculated by taking the ratio between two moving averages of the price, and then smoothing the result with an additional moving average.

The Center of Gravity indicator is another technical indicator that attempts to identify potential turning points in the market. It uses a weighted moving average to determine the center of gravity for a given period, and then plots the difference between price and the center of gravity.

By combining these two indicators, a trading strategy can be designed to generate entry and exit signals.

tip

This example strategy is machine generated using Gunbot AI. Review its behavior carefully in a simulated bot instance before using parts of this code in production.

// initialize customStratStore within pairLedger object
gb.data.pairLedger.customStratStore = gb.data.pairLedger.customStratStore || {};

// forced wait time reduces risk of double orders
function checkTime() {
return !gb.data.pairLedger.customStratStore.timeCheck || typeof gb.data.pairLedger.customStratStore.timeCheck !== "number"
? (gb.data.pairLedger.customStratStore.timeCheck = Date.now(), false)
: (Date.now() - gb.data.pairLedger.customStratStore.timeCheck > 8000);
}
const enoughTimePassed = checkTime();

// set timestamp for checkTime in next round
const setTimestamp = () => gb.data.pairLedger.customStratStore.timeCheck = Date.now();

// calculate Ergodic Oscillator
const eo = (fastPeriod, slowPeriod, signalPeriod) => {
const fastEma = gb.data.ema1(fastPeriod);
const slowEma = gb.data.ema1(slowPeriod);
const emaDiff = fastEma - slowEma;
const signal = gb.data.ema1(signalPeriod, emaDiff);
const eo = emaDiff - signal;
return eo;
}

// calculate Center of Gravity
const cog = (period) => {
const sum = gb.data.candlesClose.slice(-period).reduce((acc, val, i) => acc + val * (i + 1), 0);
const denominator = gb.data.candlesClose.slice(-period).reduce((acc, val, i) => acc + (i + 1), 0);
const cog = sum / denominator;
return cog;
}

// define buy and sell conditions
const buyConditions = eo(10, 30, 9) < 0 && cog(20) < gb.data.candlesClose.slice(-1)[0] && !gb.data.gotBag;
const sellConditions = eo(10, 30, 9) > 0 && cog(20) > gb.data.candlesClose.slice(-1)[0] && gb.data.gotBag;

// log indicators and trading conditions
console.log(`Ergodic Oscillator: ${eo(10, 30, 9)}`);
console.log(`Center of Gravity: ${cog(20)}`);
console.log(`Got Bag: ${gb.data.gotBag}`);
console.log(`Buy Conditions: ${buyConditions}`);
console.log(`Sell Conditions: ${sellConditions}`);

if (enoughTimePassed) {
if (buyConditions) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
gb.method.buyMarket(buyAmount, gb.data.pairName);
setTimestamp();
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
setTimestamp();
}
}

// Code is machine generated, review it and run in simulator mode first