Choppiness Index and DPO Trading Strategy
The 14-period Choppiness Index filters for directional markets. The 20-period Detrended Price Oscillator (DPO) helps time entries and exits.
- Buy when the Choppiness Index is below 55 and DPO is positive.
- Sell when DPO is negative or the Choppiness Index is above 61.8.
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.detrendedchoppinessOrderCandle === 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);
if (closes.length < 22 || highs.length !== closes.length || lows.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 PERIOD = 14;
const highest = Math.max(...highs.slice(-PERIOD));
const lowest = Math.min(...lows.slice(-PERIOD));
if (highest <= lowest) return;
let trueRangeSum = 0;
for (let i = closes.length - PERIOD; i < closes.length; i++) {
trueRangeSum += Math.max(highs[i] - lows[i],
Math.abs(highs[i] - closes[i - 1]), Math.abs(lows[i] - closes[i - 1]));
}
const choppiness = 100 * Math.log10(trueRangeSum / (highest - lowest)) / Math.log10(PERIOD);
const [dpo] = indicator("dpo", [closes], [20]);
const buySignal = choppiness < 55 && dpo.at(-1) > 0;
const sellSignal = choppiness > 61.8 || dpo.at(-1) < 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.detrendedchoppinessOrderCandle = 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.detrendedchoppinessOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

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