Skip to main content

RSI + MACD Strategy

This strategy combines two popular technical indicators to generate trading signals:

  • RSI to identify overbought/oversold conditions
  • MACD to confirm trend direction and momentum

The strategy buys when:

  • RSI is below 30 (oversold condition)
  • MACD line crosses above its signal line (bullish momentum)

It sells when:

  • RSI is above 70 (overbought condition)
  • MACD line crosses below its signal line (bearish momentum)
// Register indicators
registerIndicator("RSI", "BUILTIN",
{ period: 14 },
"RSI_14"
);

registerIndicator("MACD", "BUILTIN", {
fastPeriod: 12,
slowPeriod: 26,
signalPeriod: 9,
field: "close"
}, "MACD_12_26_9");

function onBar(data, indicators, positions, trades) {
const rowCount = data.close.length;
const signals = new Array(rowCount).fill("HOLD");

for (let i = 0; i < rowCount; i++) {
const token = data.token_address[i];
const rsi = indicators.getValue("RSI_14", token);
const macd = indicators.getValue("MACD_12_26_9", token);
const signal = indicators.getSignal("MACD_12_26_9", token);

// Buy: RSI oversold + MACD crosses above signal
if (rsi < 30 && macd > signal) {
signals[i] = "BUY";
}
// Sell: RSI overbought + MACD crosses below signal
else if (rsi > 70 && macd < signal) {
signals[i] = "SELL";
}
}

return signals;
}