Indicator · Pine Script v4 · Own pane

Preprosessed & Standardized RSI

by capissimo

This indicator standardizes price inputs using z-score normalization before calculating a relative strength index (RSI).

View on GitHub

Repaints

GitHub stars114on the repository
Pine versionv4older, still runs
Lines of code161
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Nov 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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

161 lines of Pine Script v4 from scripts/103-Preprosessed-and-Standardized-RSI.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

103-Preprosessed-and-Standardized-RSI.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capissimo

//@version=4
study('Preprosessed & Standardized RSI', 'PreRSI', false)

// Description:

// This is a preprocessed and standardized version of RSI indicator.
// You can replace RSI with any other indicator of your choice. 
// Use this code as a framework for your ideas.
// Time Threshold parameter is useful in handling a repainting issue.
// By setting it to higher values you can effectively eliminate repainting!
// Cumulative Return, Win/Loss and Win Rate are quick & dirty performance measures 
// helpful in tuning parameters.

// Enjoy!

//-------------------- Inputs

ptype     = input('Close', 'Price Type',             options=['Open','High','Low','Close','HL2','OC2','OHL3','HLC3','OHLC4'])
reso      = input('',      'Resolution',             input.resolution)
p         = input(2,       'Lookback Window',        minval=2,   maxval=1440)
ftype     = input('Both',  'Filter Signals by',      options=['Volatility','Volume','Both','None'])
vmin      = input(10,      'Min Volatility Period',  minval=1,   maxval=1440)
vmax      = input(40,      'Max Volatility Period',  minval=2,   maxval=1440)
tthres    = input(0.999,   'Time Threshold',         minval=0.001, maxval=100, step=0.001)
holding_p = input(1,       'Holding Period |1..n|',  minval=1,   maxval=240)

//-------------------- System Variables

var BUY = -1,   var SELL = 1,    var HOLD = 0
var MIN = -2,   var MAX  = 2,    var MID  = 0

//-------------------- Dynamic Arrays

var int   signal     = HOLD
var int   hp_counter = 0
var label lbl        = na

//-------------------- Custom Functions

volumeBreak(thres) =>  
    rsivol   = rsi(volume, 14)  
    osc      = hma(rsivol, 10)  
    osc > thres                     

volatilityBreak(volmin, volmax) => atr(volmin) > atr(volmax)

meanfn(x)  => cum(x) / bar_index
varfn(x)   => cum(pow(x - meanfn(x), 2)) / (bar_index-1)
stdfn(x)   => sqrt(varfn(x))
znormfn(x) => (x - meanfn(x)) / stdfn(x)

//-------------------- Logic

tbase   = (time - time[1]) / 1000
tcurr   = (timenow - time_close[1]) / 1000
barlife = tcurr / tbase

o  = security('', reso, (open  - sma(open,  p)) / stdev(open,  p)) / log(2)
h  = security('', reso, (high  - sma(high,  p)) / stdev(high,  p)) / log(2)
l  = security('', reso, (low   - sma(low,   p)) / stdev(low,   p)) / log(2)
c  = security('', reso, (close - sma(close, p)) / stdev(close, p)) / log(2)

base =  ptype=='Open'  ? o 
      : ptype=='High'  ? h 
      : ptype=='Low'   ? l 
      : ptype=='Close' ? c 
      : ptype=='OC2'   ? (o+c)/2 
      : ptype=='HL2'   ? (h+l)/2 
      : ptype=='OHL3'  ? (o+h+l)/3 
      : ptype=='HLC3'  ? (h+l+c)/3 
      :                  (o+h+l+c)/4

feature = rsi(base, p)     // use your own here
f       = znormfn(feature)

filter =   ftype=='Volatility' ? volatilityBreak(vmin, vmax) 
         : ftype=='Volume'     ? volumeBreak(49) 
         : ftype=='Both'       ? volatilityBreak(vmin, vmax) and volumeBreak(49)
         :                       true

signal :=  crossover(c, f) and filter ? BUY : crossunder(o, f) and filter ? SELL : nz(signal[1])

changed = change(signal)

hp_counter := changed ? 0 : hp_counter + 1

startLongTrade  = barlife > tthres and changed and signal==BUY    
startShortTrade = barlife > tthres and changed and signal==SELL   
endLongTrade    = (signal==BUY  and hp_counter==holding_p and not changed) or (changed and signal==SELL)
endShortTrade   = (signal==SELL and hp_counter==holding_p and not changed) or (changed and signal==BUY)

//-------------------- Rendering

plot(MID,  '', color.orange)
plot(SELL, '', color.gray)
plot(BUY,  '', color.gray)
plot(MAX,  '', color.red,   9, transp=50)
plot(MIN,  '', color.green, 9, transp=50)

plot(f,    '')

plotshape(startLongTrade  ? MIN+.2 : na, 'Buy',  shape.labelup,   location.absolute, color.blue, size=size.tiny) 
plotshape(startShortTrade ? MAX-.2 : na, 'Sell', shape.labeldown, location.absolute, color.red,  size=size.tiny) 
plot(endLongTrade         ? HOLD   : na, 'StopBuy',  color.blue, 1, plot.style_cross)
plot(endShortTrade        ? HOLD   : na, 'StopSell', color.red,  1, plot.style_cross)

//-------------------- Alerting

alertcondition(startLongTrade,  'Buy',  'Go long!')
alertcondition(startShortTrade, 'Sell', 'Go short!') 
alertcondition(startLongTrade or startShortTrade, 'General Alert', 'Deal Time!')

//-------------------- Backtesting

show_info = input(true,  'Display information?')
lot_size  = input(0.01,  'Lot Size', options=[0.01,0.1,0.2,0.3,0.5,1,2,3,5,10,20,30,50,100,1000]) 

bidask = (open+high+low)/3

var float start_long_trade  = bidask  
var float long_trades       = 0.
var float start_short_trade = bidask  
var float short_trades      = 0.
var int   wins              = 0
var int   trade_count       = 0

if startLongTrade 
    start_long_trade := bidask 
if endLongTrade
    ldiff         = (bidask - start_long_trade)
    wins         := ldiff > 0 ? 1 : 0
    long_trades  := ldiff * lot_size   
    trade_count  := 1
if startShortTrade 
    start_short_trade := bidask
if endShortTrade
    sdiff         = (start_short_trade - bidask)
    wins         := sdiff > 0 ? 1 : 0
    short_trades := sdiff * lot_size 
    trade_count  := 1
    
cumreturn   = cum(long_trades) + cum(short_trades)  //-- cumulative return 
totaltrades = cum(trade_count)
totalwins   = cum(wins)  
totallosses = totaltrades - totalwins == 0 ? 1 : totaltrades - totalwins

//------------------- Information

info =  '\nCR='        + tostring(cumreturn,             '#.#') 
      + '\nTrades: '   + tostring(totaltrades,           '#')
      + '\nWin/Loss: ' + tostring(totalwins/totallosses, '#.##') 
      + '\nWinrate: '  + tostring(totalwins/totaltrades, '#.#%')       
      + '\nBar Time: ' + tostring(barlife,               '#.#%')

if show_info and barstate.islast  
    lbl := label.new(bar_index, HOLD, info, xloc.bar_index, yloc.price, 
                     color.new(color.blue, 100), label.style_label_left, color.black, size.small, text.align_left)
    label.delete(lbl[1])

Overview

Preprosessed & Standardized RSI generates trade signals when standardized price components cross over or under the standardized RSI feature. Signals can be filtered by volatility ATR comparisons, volume RSI conditions, or both. It incorporates a real-time bar life time threshold filter to manage live signal execution. The script plots in a separate pane.

Details

Published by

capissimo on GitHub

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Volatility & bands, Volume

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
Price TypestringCloseptype
Resolutiontimeframereso
Lookback Windowint2p
Filter Signals bystringBothftype
Min Volatility Periodint10vmin
Max Volatility Periodint40vmax
Time Thresholdfloat0.999tthres
Holding Period |1..n|int1holding_p
Display information?booltrueshow_info
Lot Sizefloat0.01lot_size
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

PPO ATR Bands

by cryptorife
75 stars

PPO ATR Bands plots a percentage price oscillator in a separate pane. It compares a Hull average of typical price with a volume-weighted exponential average of Parabolic SAR, then scales three true-range bands around the slow average into matching oscillator l

Indicator v4 Oscillators & momentumMoving averages
Updated 3 years ago · GPL-3.0 GitHub repository of PPO ATR Bands

This indicator calculates the Relative Strength Index (RSI) alongside a simple moving average to generate directional momentum signals in a separate pane. It supports Break Flip and Break Continuous scanning modes for long and short setups. A wide array of opt

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of Momentum Setup: RSI Direction Confirmation

NFI v6

by koradiyakaushal
12 stars

This Pine v4 study ports early NostalgiaForInfinity V6 conditions onto the price chart. It computes two Bollinger setups, several EMAs and SMAs, RSI, MFI, a percent SMA spread labeled EWO, a 14-period choppiness value, and short and longer volume medians. Fixe

Indicator v4 Moving averagesVolatility & bands
Updated 5 years ago · MIT GitHub repository of NFI v6

The script calculates a variant of the Fear & Greed Index using rate of return, accumulation distribution money flow weighted by volume, and volatility from VIX. It offers display options for the raw index value or price candles scaled by the index level. Smoo

Indicator v5 Oscillators & momentumVolatility & bands Repaints
Updated 4 years ago · MPL-2.0 GitHub repository of Trading Psychology - Fear & Greed Index by DGT

ACE Squeeze

by JasonTeixeira
9 stars

ACE Squeeze plots a cycle-based squeeze oscillator in a separate pane. It builds a histogram and an optional cycle phase line from a base cycle length, sensitivity, and price source, then can adjust the reading with volatility, volume delta, relative range exp

Indicator v6 Oscillators & momentumVolatility & bands
Updated 12 days ago · Custom licence GitHub repository of ACE Squeeze

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers