Custom Strategy Code Examples
These examples show how to read completed candles, calculate indicators, and place market orders for a spot pair. Use the backtest setup to run a snippet in Gunbot Live Backtesting.
Examples to Learn From
Example 1: Basic Buy Low, Sell High
This example uses one 20-period simple moving average to set the entry and exit levels.
- Buy when the close is at least 1% below the average.
- Sell when the close is at or above the average.
// 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.basicOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 20) 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 close = closes.at(-1);
const buySignal = close <= average.at(-1) * 0.99;
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.basicOrderCandle = 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.basicOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

This Binance spot backtest filled 39 buys and 39 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.
Example 2: Adding Custom Indicator (HMA)
This example shows how to read a Hull Moving Average (HMA) from a Tulind callback and detect a cross between the close and the average.
- Buy when the close crosses above the 50-period HMA.
- Sell when the close crosses below the HMA.
// 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.hmaOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 60) 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 [hma] = indicator("hma", [closes], [50]);
const buySignal = closes.at(-2) <= hma.at(-2) && closes.at(-1) > hma.at(-1);
const sellSignal = closes.at(-2) >= hma.at(-2) && closes.at(-1) < hma.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.hmaOrderCandle = 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.hmaOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

This Binance spot backtest filled 134 buys and 133 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.
Example 3: EMA Crossover Strategy
Compare the last two values from two exponential moving averages to find a crossover.
- Buy when the 9-period EMA crosses above the 21-period EMA.
- Sell when the 9-period EMA crosses below the 21-period EMA.
// 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.emacrossOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 42) 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], [9]);
const [slow] = indicator("ema", [closes], [21]);
const buySignal = fast.at(-2) <= slow.at(-2) && fast.at(-1) > slow.at(-1);
const sellSignal = fast.at(-2) >= slow.at(-2) && fast.at(-1) < slow.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.emacrossOrderCandle = 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.emacrossOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

This Binance spot backtest filled 52 buys and 51 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.
Strategy Examples
Keltner Crossover
The Keltner Channel Breakout uses ATR to set channel width and on-balance volume to confirm the breakout.
Dual RSI
Compare 10- and 20-period RSI to spot a change in short-term momentum.
- Buy when 10-period RSI crosses above 20-period RSI.
- Sell when 10-period RSI crosses below 20-period RSI.
// 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.dualrsiOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 42) 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("rsi", [closes], [10]);
const [slow] = indicator("rsi", [closes], [20]);
const buySignal = fast.at(-2) <= slow.at(-2) && fast.at(-1) > slow.at(-1);
const sellSignal = fast.at(-2) >= slow.at(-2) && fast.at(-1) < slow.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.dualrsiOrderCandle = 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.dualrsiOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

This Binance spot backtest filled 171 buys and 170 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.
Trail Price After StochRSI Target
Once 14-period Stochastic RSI reaches 0.8 on its 0–1 scale, the script tracks the highest completed close. The trailing exit uses completed closes. The separate 3% exit uses the pair’s Gunbot break-even price (gb.data.breakEven) and can trigger before trailing starts.
- Buy when 14-period Stochastic RSI crosses back above 0.2.
- Sell when a close is at least 1% below the stored peak, or at least 3% below break-even.
// 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.trailrsiOrderCandle === candleTime || gb.data.openOrders.length > 0) return;
const closes = gb.data.candlesClose.slice(0, -1);
if (closes.length < 35) 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 [stochRsi] = indicator("stochrsi", [closes], [14]);
const close = closes.at(-1);
const buySignal = stochRsi.at(-2) <= 0.2 && stochRsi.at(-1) > 0.2;
if (!gb.data.gotBag) {
delete store.stochRsiPeak;
} else if (stochRsi.at(-1) >= 0.8 || store.stochRsiPeak > 0) {
store.stochRsiPeak = Math.max(store.stochRsiPeak ?? 0, close);
}
const trailingExit = store.stochRsiPeak > 0 && close <= store.stochRsiPeak * 0.99;
const stop = gb.data.breakEven > 0 && close <= gb.data.breakEven * 0.97;
const sellSignal = trailingExit || stop;
// Spot market order amounts are in the asset being traded (BTC in USDT-BTC).
if (gb.data.gotBag && sellSignal && gb.data.quoteBalance > 0) {
store.trailrsiOrderCandle = 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.trailrsiOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

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