Skip to main content

Time Series Forecast and Balance of Power Trading Strategy

This example compares the close to a 10-period Time Series Forecast (TSF). A three-candle average of Balance of Power (BOP) checks whether closes are moving above or below their opens.

  • Buy when the close is above TSF and average BOP is positive.
  • Sell when the close is below TSF and average BOP is negative.

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

const opens = gb.data.candlesOpen.slice(0, -1);
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 < 16 || opens.length !== closes.length || 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 [forecast] = indicator("tsf", [closes], [10]);
const [power] = indicator("bop", [opens, highs, lows, closes], []);
const powerSum = power.slice(-3).reduce((sum, value) => sum + value, 0);
const close = closes.at(-1);
const buySignal = close > forecast.at(-1) && powerSum > 0;
const sellSignal = close < forecast.at(-1) && powerSum < 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.timeseriesbalanceofpowerOrderCandle = 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.timeseriesbalanceofpowerOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

Gunbot Live backtest result for time series balance of power, showing 226 buys, 226 sells and a final wallet value of 963.23 USDT.

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