Skip to main content

Chaikin Oscillator Trading Strategy

This example combines a band 1% below the 20-period simple moving average with the Chaikin Oscillator, which compares 3- and 10-period exponential moving averages of the accumulation/distribution line.

  • Buy when the close is below the band and the oscillator is higher than on the previous candle.
  • Sell when the close is at or above the moving average.

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.chaikinOrderCandle === candleTime || gb.data.openOrders.length > 0) return;

const highs = gb.data.candlesHigh.slice(0, -1);
const lows = gb.data.candlesLow.slice(0, -1);
const closes = gb.data.candlesClose.slice(0, -1);
const volumes = gb.data.candlesVolume.slice(0, -1);
if (closes.length < 30 || highs.length !== closes.length || lows.length !== closes.length || volumes.length !== closes.length) 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 [average] = indicator("sma", [closes], [20]);
const [chaikin] = indicator("adosc", [highs, lows, closes, volumes], [3, 10]);
const close = closes.at(-1);
const buySignal = close < average.at(-1) * 0.99 && chaikin.at(-1) > chaikin.at(-2);
const sellSignal = close >= average.at(-1);

// Spot market order amounts are in the asset being traded (BTC in USDT-BTC).
if (gb.data.gotBag && sellSignal && gb.data.quoteBalance > 0) {
store.chaikinOrderCandle = 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.chaikinOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

Gunbot Live backtest result for chaikin, showing 25 buys, 25 sells and a final wallet value of 1,006.80 USDT.

This Binance spot backtest filled 25 buys and 25 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.