Skip to main content

Gann Fan and Williams Fractal indicator strategy

This strategy uses the Gann Fan and Williams Fractal indicators for entry and exit signals to identify potential trends in the price movement of a security.

The Gann Fan identifies potential support and resistance levels based on the theory that markets move in predictable geometric patterns, using diagonal lines to map possible trend lines.

The Williams Fractal indicator identifies potential reversal points using fractal geometry. It marks turning points where price changes direction, which can signal entry or exit opportunities.

Combined, the Gann Fan provides support/resistance context while Williams Fractals confirm reversals or trend continuation. Traders can enter long or short positions based on the indicator signals and exit when a reversal is signaled or when price reaches a Gann Fan support or resistance level.

Overall, this strategy aims to provide a structured approach to identifying trends and timing entries and exits.

tip

This example strategy is machine generated using Gunbot AI. Review its behavior carefully in a simulated bot instance before using parts of this code in production.

// initialize customStratStore within pairLedger object
gb.data.pairLedger.customStratStore = gb.data.pairLedger.customStratStore || {};

// forced wait time reduces risk of double orders
function checkTime() {
return !gb.data.pairLedger.customStratStore.timeCheck || typeof gb.data.pairLedger.customStratStore.timeCheck !== "number"
? (gb.data.pairLedger.customStratStore.timeCheck = Date.now(), false)
: (Date.now() - gb.data.pairLedger.customStratStore.timeCheck > 8000);
}
const enoughTimePassed = checkTime();

// set timestamp for checkTime in next round
const setTimestamp = () => gb.data.pairLedger.customStratStore.timeCheck = Date.now();

// calculate Gann Fan indicator
function calculateGannFan() {
const highs = gb.data.candlesHigh.slice(-9);
const lows = gb.data.candlesLow.slice(-9);
const fanLevels = [];
for (let i = 0; i < 9; i++) {
const fanLevel = (highs[i] + lows[i]) / 2;
fanLevels.push(fanLevel);
}
return fanLevels;
}

// calculate Williams Fractal indicator
function calculateWilliamsFractal() {
const highs = gb.data.candlesHigh.slice(-5);
const lows = gb.data.candlesLow.slice(-5);
const fractals = [];
for (let i = 2; i < 5; i++) {
if (highs[i] > highs[i - 2] && highs[i] > highs[i - 1] && highs[i] > highs[i + 1] && highs[i] > highs[i + 2]) {
fractals.push({ type: "high", value: highs[i] });
}
if (lows[i] < lows[i - 2] && lows[i] < lows[i - 1] && lows[i] < lows[i + 1] && lows[i] < lows[i + 2]) {
fractals.push({ type: "low", value: lows[i] });
}
}
return fractals;
}

// log indicators and trading conditions
console.log("Gann Fan:", calculateGannFan());
console.log("Williams Fractal:", calculateWilliamsFractal());
console.log("Stoch RSI:", gb.data.stochRsi);
console.log("Got Bag:", gb.data.gotBag);

if (enoughTimePassed) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
const gannFan = calculateGannFan();
const williamsFractal = calculateWilliamsFractal();
const buyConditions = gannFan[0] < gb.data.bid && williamsFractal.some(fractal => fractal.type === "low") && gb.data.stochRsi < 0.1 && !gb.data.gotBag;
const sellConditions = gannFan[8] > gb.data.ask && williamsFractal.some(fractal => fractal.type === "high") && gb.data.gotBag;

if (buyConditions) {
gb.method.buyMarket(buyAmount, gb.data.pairName);
setTimestamp();
console.log("Buy order placed");
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
setTimestamp();
console.log("Sell order placed");
}
}

// Code is machine generated, review it and run in simulator mode first