Skip to content

RSI mean reversion ​

Buys oversold RSI with a price limit, sells when RSI recovers.

Kind strategy · Resolution 15m

Buys an oversold bounce and sells when RSI recovers.

Entry. RSI crosses back up through Buy below: it was oversold and is turning. The strategy buys Size % equity with a 1 % price limit. Entry bars are shaded and marked.

Exit. RSI crosses above Exit above, or the price falls Stop % below the average entry price. The stop is checked at every bar's close in onCandle, and on every block in onBlock where blocks are delivered (precise, 1B, live), so it reacts sooner there.

Plots. RSI goes to its own pane (pane: 1) with the two levels as dashed lines.

What to try. Compare a quick and a precise run: the difference is the block-level stop.

Try it ​

On the chart, press /, open Strategies and choose RSI mean reversion. Or copy the source below into the AlphaScript editor and click Add to chart.

Inputs ​

InputTypeDefault
RSI lengthint14
Buy belowfloat30
Exit abovefloat60
Stop %float4
Size % equityfloat30

Source ​

From packages/alphascript/examples/rsi_mean_reversion.as.js.

js
strategy({ name: "RSI mean reversion", version: "0.1", market: "ALPHA:61", resolution: "15m",
           capital: { tao: 100 }, actOn: "confirmed", risk: { maxPriceImpactPct: 0.5, maxPositionTao: 60 } });

const len     = input.int("RSI length", 14, { min: 2, max: 100 });
const buyLvl  = input.float("Buy below", 30, { min: 5, max: 50 });
const exitLvl = input.float("Exit above", 60, { min: 50, max: 95 });
const stopPct = input.float("Stop %", 4, { min: 0.5, max: 30, step: 0.5 });
const sizePct = input.float("Size % equity", 30, { min: 1, max: 100 });

const rsi = ta.rsi(candle.close, len);
const entry = ta.crossover(rsi, buyLvl);
const exit  = ta.crossover(rsi, exitLvl);

plot(rsi, { title: "RSI", color: "#8b98a8", pane: 1 });
hline(buyLvl,  { color: "#2bb889", style: "dashed", pane: 1 });
hline(exitLvl, { color: "#e35c70", style: "dashed", pane: 1 });
plotShape(entry, { shape: "triangleUp", location: "belowBar", color: "#2bb889" });
bgcolor(entry, { color: "rgba(43,184,137,0.10)" });

function stopHit(price = 0) {
  const p = account.position();
  return p.alpha > 0 && price < p.avgPrice * (1 - stopPct / 100);
}

event.onCandle((bar) => {
  const pos = account.position();
  if (pos.alpha === 0 && entry.value) {
    const size = account.equity() * sizePct / 100;
    trade.buy({ tao: size, maxPrice: pool.price() * 1.01, ttlBlocks: 3, reason: "rsi-bounce" });
  } else if (pos.alpha > 0 && (exit.value || stopHit(bar.close))) {
    trade.closePosition({ reason: exit.value ? "rsi-recovered" : "stop" });
  }
});
event.onBlock((b) => { if (stopHit(b.price)) trade.closePosition({ reason: "stop-block" }); });
event.onFill((f) => log(f.side, f.alpha.toFixed(2), "α @", f.price.toFixed(7), `impact ${f.impactBps.toFixed(1)}bps`));

AlphaScript language v0.1 · runtime 0.2.0