Skip to main content

Relative Strength Index (RSI)

This strategy involves using technical analysis and the Relative Strength Index (RSI) as tools for making decisions on when to enter and exit cryptocurrency trades.

The RSI is a momentum oscillator that measures the speed and change of price movements. Traders often use the RSI to identify overbought and oversold conditions in the market. An RSI reading above 70 is typically considered overbought, while a reading below 30 is considered oversold.

Overall, this strategy involves a systematic and data-driven approach to cryptocurrency trading, with a focus on using technical analysis and custom-built indicators to inform trading decisions.

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 RSI
function calculateRSI(period) {
const closes = gb.data.candlesClose.slice(-period);
const changes = closes.map((close, i) => i === 0 ? 0 : close - closes[i - 1]);
const gains = changes.map(change => change > 0 ? change : 0);
const losses = changes.map(change => change < 0 ? Math.abs(change) : 0);
const avgGain = gains.reduce((sum, gain) => sum + gain, 0) / period;
const avgLoss = losses.reduce((sum, loss) => sum + loss, 0) / period;
const RS = avgGain / avgLoss;
const RSI = 100 - (100 / (1 + RS));
return RSI;
}

// calculate moving average
function calculateMA(period, data) {
const values = data.slice(-period);
const sum = values.reduce((total, value) => total + value, 0);
const MA = sum / period;
return MA;
}

// calculate Bollinger Bands
function calculateBB(period, stdDev, data) {
const values = data.slice(-period);
const MA = calculateMA(period, data);
const variance = values.reduce((total, value) => total + Math.pow(value - MA, 2), 0) / period;
const stdDeviation = Math.sqrt(variance);
const upperBand = MA + (stdDev * stdDeviation);
const lowerBand = MA - (stdDev * stdDeviation);
return { upperBand, lowerBand };
}

// calculate indicators
const RSI = calculateRSI(14);
const { upperBand, lowerBand } = calculateBB(20, 2, gb.data.candlesClose);

// log indicators and trading conditions
console.log(`RSI: ${RSI}`);
console.log(`Upper Bollinger Band: ${upperBand}`);
console.log(`Lower Bollinger Band: ${lowerBand}`);
console.log(`Got Bag: ${gb.data.gotBag}`);

// set buy and sell conditions
const buyConditions = RSI < 30 && !gb.data.gotBag;
const sellConditions = RSI > 70 && gb.data.gotBag;

if (enoughTimePassed) {
if (buyConditions) {
const buyAmount = gb.data.baseBalance * 0.95;
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