Ergodic Oscillator & Center of Gravity strategy
The Ergodic Oscillator identifies trend reversals by comparing fast and slow moving averages and smoothing the result.
The Center of Gravity indicator estimates turning points by weighting recent prices and comparing the current price to that weighted center.
This example combines both indicators to set 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