Strategy · Pine Script v5 · On the chart

RSI Strategy

by suyons· code credited to syoungs

Calculates relative strength index values across up to four configurable timeframes to compare short-term and long-term momentum.

View on GitHub

Repaints

GitHub stars7on the repository
Pine versionv5runs as is
Lines of code80
Settings15inputs
LicenceNone statedfile: MPL-2.0
Last update4 months ago21 Jun 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

80 lines of Pine Script v5 from relative-strength-index/04-rsi-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

04-rsi-strategy.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syoungs

//@version=5
strategy('RSI Strategy', 'RSI Strategy', true, pyramiding = 100, default_qty_type = strategy.fixed, default_qty_value = 5000, initial_capital = 1000, currency = currency.USD, commission_type = strategy.commission.cash_per_contract, commission_value = 0.0002, margin_long = 0.2, margin_short = 0.2)

// Input
tf_1 = input.timeframe('5', '', inline = '1', group = 'RSI: TIMEFRAME')
tf_2 = input.timeframe('15', '', inline = '1', group = 'RSI: TIMEFRAME')
tf_3 = input.timeframe('60', '', inline = '1', group = 'RSI: TIMEFRAME')
tf_4 = input.timeframe('240', '', inline = '1', group = 'RSI: TIMEFRAME')

val_high = input.int(65, '', inline = '1', group = 'RSI: LINE')
val_mid = input.int(50, '', inline = '1', group = 'RSI: LINE')
val_low = input.int(35, '', inline = '1', group = 'RSI: LINE')

rsi_len = input.int(21, 'Length', inline = '1', group = 'RSI: CONFIG')
rsi_src = input.source(close, 'Source', inline = '1', group = 'RSI: CONFIG')
rsi_tf1 = input.int(1, 'Small TF', 1, 4, 1, inline = '2', group = 'RSI: CONFIG')
rsi_tf2 = input.int(5, 'Large TF (5=avg)', 1, 5, 1, inline = '2', group = 'RSI: CONFIG')

sig_lookback = input.int(10, 'Lookback', inline = '1', group = 'SIGNAL: CONFIG')
sig_length = input.int(100, 'Length', inline = '1', group = 'SIGNAL: CONFIG')
sig_src = input.bool(true, 'T (Market @ close) | F (Limit @ wick)', inline = '2', group = 'SIGNAL: CONFIG')
sig_strategy = input.bool(false, 'T (Trend-following) | F (Trend-reversal)', inline = '3', group = 'SIGNAL: CONFIG')

// Fetch
rsi_1 = request.security(syminfo.tickerid, tf_1, ta.rsi(rsi_src, rsi_len))
rsi_2 = request.security(syminfo.tickerid, tf_2, ta.rsi(rsi_src, rsi_len))
rsi_3 = request.security(syminfo.tickerid, tf_3, ta.rsi(rsi_src, rsi_len))
rsi_4 = request.security(syminfo.tickerid, tf_4, ta.rsi(rsi_src, rsi_len))
rsi_avg = math.avg(rsi_2, rsi_3, rsi_4)

rsi_small = switch rsi_tf1
    1 => rsi_1
    2 => rsi_2
    3 => rsi_3
    4 => rsi_4
    => na

rsi_large = switch rsi_tf2
    1 => rsi_1
    2 => rsi_2
    3 => rsi_3
    4 => rsi_4
    5 => rsi_avg
    => na

// Condition
buy = ta.crossunder(rsi_small, val_low) and (sig_strategy ? rsi_large > val_mid : rsi_large < val_low)
sell = ta.crossover(rsi_small, val_high) and (sig_strategy ? rsi_large < val_mid : rsi_large > val_high)

lookback(side, len) =>
    bool result = false
    for i = 1 to len
        if side[i]
            result := true
    result

buy_condition = buy and not lookback(buy, sig_lookback)
sell_condition = sell and not lookback(sell, sig_lookback)

// Strategy
if buy_condition
    strategy.entry('Buy', strategy.long)

if sell_condition
    strategy.entry('Sell', strategy.short)

// Plot
lowest = ta.lowest(low, sig_lookback)
highest = ta.highest(high, sig_lookback)

if buy_condition
    buy_src = sig_src ? close : lowest
    line.new(x1 = bar_index, y1 = buy_src, x2 = bar_index + sig_length, y2 = buy_src, color = color.new(color.green, 30), style = line.style_solid, width = 4)

if sell_condition
    sell_src = sig_src ? close : highest
    line.new(x1 = bar_index, y1 = sell_src, x2 = bar_index + sig_length, y2 = sell_src, color = color.new(color.red, 30), style = line.style_solid, width = 4)

Overview

Generates long and short entry signals when lower-timeframe RSI crosses key thresholds, conditioned on higher-timeframe RSI levels. Offers toggles between trend-following and trend-reversal modes alongside a lookback filter to suppress rapid duplicate signals. Draws entry level lines directly on the price chart. This strategy includes no stop loss or profit target exits, permits multiple pyramiding entries, and repaints on live developing higher-timeframe bars.

Details

Published by

suyons on GitHub

Code credit

syoungs, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Oscillators & momentum, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
tf_1timeframe5tf_1
tf_2timeframe15tf_2
tf_3timeframe60tf_3
tf_4timeframe240tf_4
val_highint65val_high
val_midint50val_mid
val_lowint35val_low
Lengthint21rsi_len
Sourcesourceclosersi_src
Small TFint1rsi_tf1
Large TF (5=avg)int5rsi_tf2
Lookbackint10sig_lookback
Show all 15 settings
SettingTypeDefaultVariable
Lengthint100sig_length
T (Market @ close) | F (Limit @ wick)booltruesig_src
T (Trend-following) | F (Trend-reversal)boolfalsesig_strategy
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy
218 stars

This strategy generates trading signals based on a selectable momentum engine, supporting MACD, RSI, Stochastic, or Rate of Change. Entries trigger on signal crosses, zero-line crosses, or threshold crosses, with an optional moving average trend filter. Risk m

Strategy v6 Oscillators & momentumTrend
Updated 3 months ago · None stated GitHub repository of Momentum Strategy Template

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers