Skip to main content

Fibonacci

This strategy is a day trading strategy that combines two technical analysis tools: Fibonacci retracements and Stochastic oscillator.

Fibonacci retracements are used to identify potential levels of support and resistance by plotting horizontal lines on a chart based on key Fibonacci ratios. The trader will look for price to bounce off these levels or break through them, which can signal a potential entry or exit point.

The Stochastic oscillator is a momentum indicator that compares a security's closing price to its price range over a given period of time. The trader will look for signals generated by the oscillator, such as when the indicator crosses above or below a certain threshold, which can indicate a potential entry or exit point.

The strategy also includes calculating these indicators without using built-in methods and logging every indicator and trading condition. This means that the strategy will calculate the indicators itself rather than relying on pre-built methods.

Overall, this strategy is a technical analysis-based day trading strategy that uses Fibonacci retracements and Stochastic oscillator to identify potential entry and exit points.

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