RSI and EMA Pullback Trading Strategy
This example uses 20- and 50-period EMAs to identify an uptrend, then uses RSI to look for a pullback within it.
- Buy when the 20-period EMA is above the 50-period EMA and 14-period RSI moves from below 40 to at least 40.
- Sell when RSI is at or above 65 or the faster EMA falls to or below the slower 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.rsimaOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 80) 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 [fast] = indicator("ema", [closes], [20]);
const [slow] = indicator("ema", [closes], [50]);
const [rsi] = indicator("rsi", [closes], [14]);
const uptrend = fast.at(-1) > slow.at(-1);
const buySignal = uptrend && rsi.at(-2) < 40 && rsi.at(-1) >= 40;
const sellSignal = rsi.at(-1) >= 65 || !uptrend;
// Spot market order amounts are in the asset being traded (BTC in USDT-BTC).
if (gb.data.gotBag && sellSignal && gb.data.quoteBalance > 0) {
store.rsimaOrderCandle = 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.rsimaOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

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