Strategy · Pine Script v5 · On the chart

RSI + Trailing Stoploss

by kritarth2121· code credited to Dynamictradingstrategies

This strategy uses a 14-period RSI with fixed thresholds.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code63
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago10 Dec 2023

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

63 lines of Pine Script v5 from RSI + TRAILING STOPLOSS PINESCRIPT..txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

RSI + TRAILING STOPLOSS PINESCRIPT..txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dynamictradingstrategies

//@version=5
strategy("RSI + TRAILING_STOPLOSS", overlay = true)

// INPUT CONTROLS 

// input controls 


RSI_L = input.int(title = "RSIL", defval = 14, minval = 1, maxval = 22, step = 1)


trailing_Stoploss_pb = input.float(title = "trailing_Stoploss_pb", defval = 0.02, minval = 0.01, maxval = 100.00, step = 0.01)
trailing_Stoploss_ps  = input.float(title = "trailing_Stoploss_ps", defval = 0.02, minval = 0.01, maxval = 100.00, step = 0.01)


/// trading indicators 

RSI = ta.rsi(close,RSI_L)

/// BUY AND SELL RULES 


BUY = ta.crossover(RSI,60)
SELL = ta.crossunder(RSI,40)


/////////////////// TRAILING STOPLOSSS 


LONGSTOPPRICE = 0.0
SHORTSTOPPRICE = 0.0


LONGSTOPPRICE :=  if (strategy.position_size > 0)
    stopvalue = close * (1 - trailing_Stoploss_pb )
    math.max(stopvalue, LONGSTOPPRICE[1]) 
    
else
    0 
    
    
SHORTSTOPPRICE := if (strategy.position_size < 0)
    stopvalue = close * (1 + trailing_Stoploss_ps )
    math.min(stopvalue, SHORTSTOPPRICE[1])

else 
    999999
    
    
    
strategy.entry("long", when = BUY, direction = strategy.long, comment = "B")
strategy.exit("long", from_entry = "long", stop = LONGSTOPPRICE, comment = "TRAILEXIT_B" )
    
    
    
strategy.entry("short", when = SELL, direction = strategy.short, comment = "S")
strategy.exit("short", from_entry = "short", stop = SHORTSTOPPRICE, comment = "TRAILEXIT_S" )

plot(strategy.position_size > 0 ? LONGSTOPPRICE : na, style = plot.style_circles, title = "TSLB", color = color.new(color.green,0))
plot(strategy.position_size < 0 ? SHORTSTOPPRICE : na, style = plot.style_circles, title = "TSLB", color = color.new(color.red,0))

Overview

RSI + Trailing Stoploss goes long when RSI crosses above 60 and short when it crosses below 40. Each side has a percentage trailing stop, two percent by default, that follows the close and only moves in the trade's favour; the stop is plotted as circles on the price chart. There is no profit target, and a trade reverses or exits only through the stop or an opposite entry signal.

Details

Published by

kritarth2121 on GitHub

Code credit

Dynamictradingstrategies, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
RSILint14RSI_L
trailing_Stoploss_pbfloat0.02trailing_Stoploss_pb
trailing_Stoploss_psfloat0.02trailing_Stoploss_ps
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

MACD Strategy

by TradersPost
228 stars

This overlay strategy is built around MACD inputs for a fast average, a slow average, and a signal line. Comments say the script keeps MACD values available and can draw a histogram and zero line in a separate pane, with an optional overlay on price. Settings

Strategy v6 Oscillators & momentumMoving averages
Updated 3 months ago · None stated GitHub repository of MACD Strategy

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 Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers