Skip to main content

Fibonacci strategy

This day trading strategy combines Fibonacci retracements with the Stochastic oscillator to look for entry and exit points. It uses retracement levels as potential support/resistance and Stochastic thresholds for momentum signals. The script computes both indicators without built-in methods and logs the indicator values and trade conditions for review.

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 Fibonacci retracements
function fibRetracement(high, low, close) {
const diff = high - low;
const levels = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1];
const retracements = levels.map(level => close + diff * level);
return retracements;
}

// calculate Stochastic oscillator
function stochasticOscillator(highs, lows, closes, period, smooth) {
const kPeriod = period;
const dPeriod = smooth;
const kValues = [];
const dValues = [];
for (let i = kPeriod - 1; i < highs.length; i++) {
const highSlice = highs.slice(i - kPeriod + 1, i + 1);
const lowSlice = lows.slice(i - kPeriod + 1, i + 1);
const closeSlice = closes.slice(i - kPeriod + 1, i + 1);
const high = Math.max(...highSlice);
const low = Math.min(...lowSlice);
const close = closeSlice[closeSlice.length - 1];
const k = ((close - low) / (high - low)) * 100;
kValues.push(k);
}
for (let i = dPeriod - 1; i < kValues.length; i++) {
const dSlice = kValues.slice(i - dPeriod + 1, i + 1);
const d = dSlice.reduce((a, b) => a + b, 0) / dPeriod;
dValues.push(d);
}
const k = kValues[kValues.length - 1];
const d = dValues[dValues.length - 1];
return { k, d };
}

// calculate indicators
const closes = gb.data.candlesClose;
const highs = gb.data.candlesHigh;
const lows = gb.data.candlesLow;
const fibRetracements = fibRetracement(Math.max(...highs), Math.min(...lows), closes[closes.length - 1]);
const { k, d } = stochasticOscillator(highs, lows, closes, 14, 3);

// log indicators
console.log(`Fibonacci retracements: ${fibRetracements}`);
console.log(`Stochastic oscillator K: ${k}`);
console.log(`Stochastic oscillator D: ${d}`);

if (enoughTimePassed) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
const buyConditions = k < 20 && !gb.data.gotBag;
const sellConditions = k > 80 && gb.data.gotBag;

// log trading conditions
console.log(`Buy conditions: ${buyConditions}`);
console.log(`Sell conditions: ${sellConditions}`);

if (buyConditions) {
gb.method.buyMarket(buyAmount, gb.data.pairName);
setTimestamp();
} else if (sellConditions) {
gb.method.sellMarket(gb.data.quoteBalance, gb.data.pairName);
setTimestamp();
}
}

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