Elder-Ray and Swing Pivot Trading Strategy
This strategy combines Elder-Ray bull and bear power with confirmed swing pivots. Bull and bear power measure how far the candle high and low are from a 13-period EMA. A pivot is confirmed after two more candles close.
- Buy when a swing low is confirmed, bear power is higher than on the previous candle, and bull power is positive.
- Sell when a swing high is confirmed and bull power is lower than on the previous candle, or the close drops below the 13-period EMA.
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.elderrayOrderCandle === 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 < 32 || 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 [average] = indicator("ema", [closes], [13]);
const pivotLow = lows.at(-3) < Math.min(lows.at(-5), lows.at(-4), lows.at(-2), lows.at(-1));
const pivotHigh = highs.at(-3) > Math.max(highs.at(-5), highs.at(-4), highs.at(-2), highs.at(-1));
const bearPower = lows.at(-1) - average.at(-1);
const priorBearPower = lows.at(-2) - average.at(-2);
const bullPower = highs.at(-1) - average.at(-1);
const priorBullPower = highs.at(-2) - average.at(-2);
const buySignal = pivotLow && bearPower > priorBearPower && bullPower > 0;
const sellSignal = (pivotHigh && bullPower < priorBullPower) || closes.at(-1) < 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.elderrayOrderCandle = 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.elderrayOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

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