Strategy · Pine Script v5 · On the chart

ATR RSI Strategy v2

by hasnocool· code credited to liwei666

This strategy combines Average True Range (ATR) and the Relative Strength Index (RSI) to generate volatility-filtered momentum trades on the price chart.

View on GitHub

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code84
Settings7inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

84 lines of Pine Script v5 from ATR_RSI_Strategy v2 with no repaint [liwei666].pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

ATR_RSI_Strategy v2 with no repaint [liwei666].pine
// © liwei666
//@version=5
// # ========================================================================= #
// #                   |   Strategy  |
// # ========================================================================= #
strategy(
 title                = "ATR_RSI_Strategy v2[liwei666]",
 shorttitle           = "ATR_RSI_Strategy",
 overlay              =  true,
 max_lines_count                 =  500, 
 max_labels_count                =  500, 
 max_boxes_count                 =  500,
 max_bars_back = 5000,
 initial_capital = 10000,
 default_qty_type=strategy.percent_of_equity, 
 default_qty_value=10, commission_type=strategy.commission.percent, pyramiding=1, 
 commission_value=0.05
 )
// # ========================================================================= #
// #                   |   Strategy  |
// # ========================================================================= #
atr_length  = input.int(26, "atr_length", minval = 6, maxval = 100, step=1)
atr_ma_length = input.int(45, "atr_ma_length", minval = 6, maxval = 100, step=1)
rsi_length = input.int(15, "rsi_length", minval = 6, maxval = 100, step=1)
rsi_entry = input.int(10, "rsi_entry", minval = 6, maxval = 100, step=1)
atr_ma_norm_min = input.float(0.3, "atr_ma_norm_min", minval = 0.1, maxval = 0.5, step=0.1)
atr_ma_norm_max = input.float(0.7, "atr_ma_norm_max", minval = 0.5, maxval = 1, step=0.1)
trailing_percent= input.float(1.5, "trailing_stop_percent", minval = 0.1, maxval = 2, step=0.1)
var rsi_buy = 50 + rsi_entry
var rsi_sell = 50 - rsi_entry
sma_norm_h_45() => 
    source = high
    n = 45
    sma = ta.sma(source, n) 
    sma_norm = (sma - ta.lowest(sma, n)) / (ta.highest(sma,n) - ta.lowest(sma, n))
    sma_norm
atr_value = ta.atr(atr_length)
atr_ma = ta.sma(atr_value, atr_ma_length) 
rsi_value = ta.rsi(close, length = rsi_length) 
atr_ma_norm = atr_ma / close * 100
sma_norm = sma_norm_h_45()
var intra_trade_high = 0.0
var intra_trade_low = 0.0
var long_sl = 0.0
var long_tp = 0.0
var short_sl = 0.0
var short_tp = 0.0
if strategy.position_size == 0
    intra_trade_high := high
    intra_trade_low := low
    // when in living trading, enter after freq_once_per_bar_close
    if atr_ma_norm >= atr_ma_norm_min and atr_ma_norm <= atr_ma_norm_max
        if atr_value > atr_ma
            if rsi_value > rsi_buy
                strategy.entry("B1", strategy.long, limit = close + 5 )
                alert("Long B1", alert.freq_once_per_bar_close)
            else if rsi_value < rsi_sell
                strategy.entry("S1", strategy.short, limit = close - 5 )
                alert("Short S1", alert.freq_once_per_bar_close)
else if strategy.position_size > 0
    intra_trade_high := math.max(intra_trade_high, high)
    intra_trade_low := low
    long_sl := intra_trade_high * (1 - trailing_percent / 100)
    long_tp := strategy.position_avg_price * 1.03
    strategy.exit("Exit B1", from_entry="B1", stop = long_sl, limit = long_tp)
else if strategy.position_size < 0
    intra_trade_high := high
    intra_trade_low := math.min(intra_trade_low, low) 
    short_sl := intra_trade_low * (1 + trailing_percent / 100)
    short_tp := strategy.position_avg_price * 0.94
    strategy.exit("Exit S1", from_entry="S1", stop = short_sl, limit = short_tp)
// when in living trading, SL and TP should use freq_once_per_bar
// you can compare with trade_list of backtest result
// long Exit alert
if strategy.position_size > 0 and low < long_sl
    alert("B1 Stop_loss", alert.freq_once_per_bar)
else if strategy.position_size > 0 and high > long_tp
    alert("B1 Take_profit", alert.freq_once_per_bar) 
// short Exit alert
if strategy.position_size < 0 and high > short_sl
    alert("S1 Stop_loss", alert.freq_once_per_bar)
else if strategy.position_size > 0 and low < short_tp
    alert("S1 Take_profit", alert.freq_once_per_bar)
Expand (99 lines)

Overview

ATR RSI Strategy v2 checks whether a normalized ATR moving average falls within user-defined bounds and requires current ATR to exceed its moving average. Long trades enter when RSI moves above a designated high threshold, while short trades enter below a low threshold. Exits are managed using intra-trade trailing stop losses alongside fixed percentage take-profit targets.

Details

Published by

hasnocool on GitHub

Code credit

liwei666, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Oscillators & momentum, Volatility & bands, Moving averages

Markets

Crypto

Timeframes

M1-M5, M15-H1

Risk Notes

  • The strategy can add several entries in the same direction

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
atr_lengthint26atr_length
atr_ma_lengthint45atr_ma_length
rsi_lengthint15rsi_length
rsi_entryint10rsi_entry
atr_ma_norm_minfloat0.3atr_ma_norm_min
atr_ma_norm_maxfloat0.7atr_ma_norm_max
trailing_stop_percentfloat1.5trailing_percent
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

This strategy plots MACD line, signal line, Bollinger Bands, and RSI on the price chart. It signals long entries on MACD crossover above signal with RSI below 50 and price below BB basis, plus optional DMI filter. Short entries are available but disabled by de

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of MACD + BB + RSI [Alorse]

Take Profits & Stop Losses

by PythonForForex
64 stars

This educational strategy demonstrates moving average crossovers filtered by RSI momentum, incorporating ATR-based risk management. It calculates a 10-period SMA, a 30-period SMA, a 14-period RSI, and a 14-period ATR. Long entries trigger when the fast SMA cro

Strategy v5 Moving averagesOscillators & momentum
Updated 4 years ago · None stated GitHub repository of Take Profits & Stop Losses

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL

Bollinger bands examples

by tradingstrategy-ai
53 stars

This strategy plots exponential-moving-average Bollinger bands on the price chart and calculates RSI. It opens a long position when close crosses above the upper band while RSI is at or above a threshold, the date filter allows trading, and no position is open

Strategy v5 Volatility & bandsOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Bollinger bands examples

CombinedBinHAndClucV6

by viktors1982
36 stars

CombinedBinHAndClucV6 is a trading strategy based on the popular BinHV45 and ClucMay72018 algorithms. It calculates dual Bollinger Bands, Exponential Moving Averages, Relative Strength Index, Average True Range, and volume metrics across primary and one-hour h

Strategy v4 Volatility & bandsOscillators & momentum Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV6

BB Swing

by benso87
24 stars

This strategy calculates Bollinger Bands, a configurable moving average slope, Stochastic RSI, ATR, and ADX directly on the price chart. It evaluates trend bias and momentum conditions to generate long and short swing entries using percent-of-equity sizing. Th

Strategy v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BB Swing

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