Backtest an RSI threshold strategy
Build an RSI 14 strategy with explicit entry and exit crossings, then check warm-up, individual trades and costs.
Before you start
This example calculates RSI 14, enters long when RSI crosses above 30 and closes when RSI crosses below 70. It uses crossing events, not continuous entries on every bar below or above a threshold. It is a code example, not a claim that these thresholds are profitable.
Follow the workflow
- Open the Pine editor, review any unsaved work and paste the complete strategy below instead of assuming the initial SMA code is the RSI example.
- Compile it, choose an available market/date window and allow enough RSI history. Check missing-value and warm-up Notes.
- Run Backtest and inspect a crossing, the following entry/exit fill and applicable fees.
- Record the result, then change one threshold or the RSI length and compare the same window. Use another window for independent evaluation.
Worked example
The full pasteable Pine strategy follows. The launch button opens the Pine editor; paste this code there before testing it.
//@version=6
strategy("RSI 30/70", initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
r = ta.rsi(close, 14)
if ta.crossover(r, 30)
strategy.entry("Long", strategy.long)
if ta.crossunder(r, 70)
strategy.close("Long")Limits and interpretation
RSI may remain high or low during a trend. Repeatedly tuning thresholds on one history can select noise. An unavailable indicator value is not a signal, and the selected window may contain no crossing. This example does not add a live broker connection, stop or target; protective rules and robot delivery are separate configuration tasks.