Skip to main content

Gann-Style Support and Williams Fractal Trading Strategy

This example projects a Gann-style support line from the latest confirmed Williams low fractal in the last 24 candles. The slope is fixed at 10% of the 14-period average true range at the pivot, per candle. A newer confirmed low fractal replaces the line.

  • Buy when the close crosses above that support line.
  • Sell when the close falls below support or a new Williams high fractal is confirmed.

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.gannwilliamsOrderCandle === 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 < 34 || 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 ATR_PERIOD = 14;
const [atr] = indicator("atr", [highs, lows, closes], [ATR_PERIOD]);
const end = closes.length - 1;

let buySignal = false;
let sellSignal = highs.at(-3) > Math.max(highs.at(-5), highs.at(-4), highs.at(-2), highs.at(-1));
const firstPivot = Math.max(ATR_PERIOD, end - 23);
for (let pivot = end - 2; pivot >= firstPivot; pivot--) {
if (lows[pivot] >= Math.min(lows[pivot - 2], lows[pivot - 1], lows[pivot + 1], lows[pivot + 2])) continue;

const slope = atr.at(pivot - closes.length) * 0.1;
const support = lows[pivot] + (end - pivot) * slope;
buySignal = closes.at(-2) <= support - slope && closes.at(-1) > support;
sellSignal ||= closes.at(-1) < support;
break;
}

// Spot market order amounts are in the asset being traded (BTC in USDT-BTC).
if (gb.data.gotBag && sellSignal && gb.data.quoteBalance > 0) {
store.gannwilliamsOrderCandle = 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.gannwilliamsOrderCandle = candleTime;
await gb.method.buyMarket(amount, gb.data.pairName, gb.data.exchangeName);
}

Gunbot Live backtest result for gann williams, showing 49 buys, 49 sells and a final wallet value of 1,002.00 USDT.

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