Skip to main content

RSI & moving averages strategy

This strategy uses technical analysis to identify entry and exit signals for a position trade. It relies on moving averages and the Relative Strength Index (RSI) to generate those signals.

Moving averages are used to identify trend direction and support/resistance levels by comparing a selected period average. The RSI measures momentum on a 0–100 scale to flag overbought and oversold conditions: above 70 is overbought and below 30 is oversold.

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 indicators
const ema1 = gb.data.ema1;
const ema2 = gb.data.ema2;
const rsi = gb.data.rsi;

console.log(`EMA1: ${ema1}, EMA2: ${ema2}, RSI: ${rsi}`);

if (enoughTimePassed) {
// calculate buy and sell conditions
const buyConditions = ema1 > ema2 && rsi < 30 && !gb.data.gotBag;
const sellConditions = ema1 < ema2 && rsi > 70 && gb.data.gotBag;

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

// fire orders when conditions are met
if (buyConditions) {
const buyAmount = parseFloat(gb.data.pairLedger.whatstrat.TRADING_LIMIT) / gb.data.bid;
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