Benso RSI PMARP.pine
Copy
//@version=5
//
// Author: benso87
// Credit to The_Caretaker for CT RSI referenced to make this
//
indicator(title='Benso RSI + PMARP', shorttitle='RSI + PMARP', format=format.price, precision=2, timeframe='', timeframe_gaps=true)
ma(source, length, type) =>
switch type
'SMA' => ta.sma(source, length)
'EMA' => ta.ema(source, length)
'SMMA (RMA)' => ta.rma(source, length)
'WMA' => ta.wma(source, length)
'VWMA' => ta.vwma(source, length)
// Variables
rsiPeriod = input.int(14, minval=1, title='RSI Period', group='RSI Settings', inline='1')
rsiSource = input.source(close, 'Source', group='RSI Settings', inline='1')
signalPeriod = input.int(12, title='Signal Period', group='RSI Settings', inline='2')
signalType = input.string('EMA', title='Type', options=['SMA', 'EMA'], group='RSI Settings', inline='2')
scaleHigh = input.int(100, title='Scale High', group='Levels and Zones')
bullCrit = input.int(80, title='Bull Critical Level', group='Levels and Zones')
bullControl = input.int(62, title='Bull Control Level', group='Levels and Zones', inline='1')
bearControl = input.int(38, title='Bear Control Level', group='Levels and Zones', inline='2')
bearCrit = input.int(20, title='Bear Critical Level', group='Levels and Zones')
scaleLow = input.int(0, title='Scale Low', group='Levels and Zones')
showDiv = input.bool(true, title='Show Regular Divergences', group='Divergence', inline='1')
showHDiv = input.bool(true, title='Show Hidden Divergences', group='Divergence', inline='2')
arrowOffset = input.int(3, title='Arrow Offset', minval=0, maxval=100, group='Divergence', inline='3')
useAlertHigh = input.bool(false, title='High Alert', group='User Alerts', inline='1')
alertHigh = input.int(62, title='', group='User Alerts', inline='1')
alertHighBgColor = input.color(color.new(#4caf50, 70), title='', group='User Alerts', inline='1')
useAlertLow = input.bool(false, title='Low Alert', group='User Alerts', inline='2')
alertLow = input.int(38, title='', group='User Alerts', inline='2')
alertLowBgColor = input.color(color.new(#ff5252, 70), title='', group='User Alerts', inline='2')
// Function declarations
f_top_fractal(_src) =>
_src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and _src[2] > _src[0]
f_bot_fractal(_src) =>
_src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and _src[2] < _src[0]
f_fractalize(_src) =>
f_bot_fractal__1 = f_bot_fractal(_src)
f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0
// Calculations
up = ta.rma(math.max(ta.change(rsiSource), 0), rsiPeriod)
down = ta.rma(-math.min(ta.change(rsiSource), 0), rsiPeriod)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, signalPeriod, signalType)
// Divergence calculations
fractal_top = f_fractalize(rsi) > 0 ? rsi[2] : na
fractal_bot = f_fractalize(rsi) < 0 ? rsi[2] : na
high_prev = ta.valuewhen(fractal_top, rsi[2], 0)[2]
high_price = ta.valuewhen(fractal_top, high[2], 0)[2]
low_prev = ta.valuewhen(fractal_bot, rsi[2], 0)[2]
low_price = ta.valuewhen(fractal_bot, low[2], 0)[2]
rDivBear = fractal_top and high[2] > high_price and rsi[2] < high_prev
hDivBear = fractal_top and high[2] < high_price and rsi[2] > high_prev
rDivBull = fractal_bot and low[2] < low_price and rsi[2] > low_prev
hDivBull = fractal_bot and low[2] > low_price and rsi[2] < low_prev
col1 = rDivBear ? color.red : hDivBear and showHDiv ? color.orange : na
col2 = rDivBull ? color.green : hDivBull and showHDiv ? color.blue : na
col3 = rDivBear ? color.red : hDivBear and showHDiv ? color.orange : na
col4 = rDivBull ? color.green : hDivBull and showHDiv ? color.blue : na
var float arrowOffsetBear = rsi + arrowOffset
var float arrowOffsetBull = rsi - arrowOffset
//////////////////////////////////////////////////////////////
// Plots
plot(rsi, 'RSI', color=#00ffff, linewidth=2)
plot(rsiMA, 'RSI-based MA', color=color.white, linewidth=2)
// Scale lines
rsiscaleHigh = hline(scaleHigh, 'RSI Scale High', color=#00ffff, linestyle=hline.style_solid)
rsiscaleLow = hline(scaleLow, 'RSI Scale Low', color=#ff00ff, linestyle=hline.style_solid)
rsiMidLine = hline(50, 'RSI Mid Line', color=#aaaaaa, linestyle=hline.style_dashed)
// Zones
rsiBullCrit = hline(bullCrit, 'Critical Bull Level', color=#00ffff, linestyle=hline.style_dashed)
rsiBullControl = hline(bullControl, 'Control Bull Level', color=#00ffff, linestyle=hline.style_dotted)
rsiBearControl = hline(bearControl, 'Control Bear Level', color=#ff00ff, linestyle=hline.style_dotted)
rsiBearCrit = hline(bearCrit, 'Critical Bear Level', color=#ff00ff, linestyle=hline.style_dashed)
fill(rsiBullCrit, rsiBullControl, color.new(#00ffff, 90))
fill(rsiBearCrit, rsiBearControl, color.new(#ff00ff, 90))
rsiAlertHigh = hline(alertHigh, 'User Alert High', color=color.yellow, linestyle=hline.style_dotted)
rsiAlertLow = hline(alertLow, 'User Alert Low', color=color.yellow, linestyle=hline.style_dotted)
plot(title='H F', series=showDiv and fractal_top ? rsi[2] : na, color=col1, linewidth=2, offset=-2)
plot(title='L F', series=showDiv and fractal_bot ? rsi[2] : na, color=col2, linewidth=2, offset=-2)
plot(title='H D', series=showDiv and fractal_top ? rsi[2] : na, style=plot.style_circles, color=col3, linewidth=3, offset=-2)
plot(title='L D', series=showDiv and fractal_bot ? rsi[2] : na, style=plot.style_circles, color=col4, linewidth=3, offset=-2)
plotshape(title='+RBD', series=showDiv and rDivBear ? rsi[2] : na, style=shape.triangledown, size=size.tiny, location=location.absolute, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), offset=-2)
plotshape(title='+HBD', series=showDiv and hDivBear and showHDiv ? rsi[2] : na, style=shape.triangledown, size=size.tiny, location=location.absolute, color=color.new(color.orange, 0), textcolor=color.new(color.white, 0), offset=-2)
plotshape(title='-RBD', series=showDiv and rDivBull ? rsi[2] : na, style=shape.triangleup, size=size.tiny, location=location.absolute, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), offset=-2)
plotshape(title='-HBD', series=showDiv and hDivBull and showHDiv ? rsi[2] : na, style=shape.triangleup, size=size.tiny, location=location.absolute, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0), offset=-2)
// Plots
//////////////////////////////////////////////////////////////
// Alert background colors
backgroundColor = if ((rsi >= alertHigh) and useAlertHigh)
alertHighBgColor
else if ((rsi <= alertLow) and useAlertLow)
alertLowBgColor
else
color(na)
bgcolor(backgroundColor)
// Alert conditions
alertcondition(rsi >= bullControl, title='Bull Control', message='RSI in bull control zone')
alertcondition(rsi >= bullCrit, title='Bull Critical', message='RSI in bull ctricial zone')
alertcondition(rsi >= alertHigh, title='Custom Alert High', message='RSI above custom alert level')
alertcondition(rsi <= bearControl, title='Bear Control', message='RSI in bear control zone')
alertcondition(rsi <= bearCrit, title='Bear Critical', message='RSI in bear critical zone')
alertcondition(rsi <= alertLow, title='Custom Alert Low', message='RSI below custom alert level')
///////////////////////////////////////////////////////////////////////////////
// Input default variable declarations
var string s_pmarp = 'Price Moving Average Ratio Percentile'
var string s_pmar = 'Price Moving Average Ratio'
var string s_BGR = 'Blue Green Red'
var string s_BR = 'Blue Red'
var float pmarHigh = 0.0
///////////////////////////////////////////////////////////////////////////////
// Inputs
i_src_price = input.source ( close, 'Price source', inline='1', group='Main Properties')
i_p_type_line = input.string ( s_pmarp, 'Indicator....', options=[ s_pmar, s_pmarp ], inline='2', group='Main Properties')
i_ma_len = input.int ( 20, 'PMAR length', minval=1, inline='3', group='Main Properties')
i_ma_typ = input.string ( 'VWMA', 'MA type...', options=[ 'SMA', 'EMA', 'HMA', 'RMA', 'VWMA' ], inline='3', group='Main Properties')
i_c_typ_line = input.string ( 'Spectrum', 'Color type', options=[ 'Spectrum', 'Solid' ], inline='1', group='Line plot settings')
i_c_typ_spctrm_line = input.string ( s_BGR, 'Spectrum', options=[ s_BR, s_BGR ], inline='1', group='Line plot settings')
i_c_solid_line = input.color ( #FFFF00, 'Solid color', inline='1', group='Line plot settings')
i_p_width_line = input.int ( 1, 'Line width', minval=1, maxval=4, inline='2', group='Line plot settings')
i_pmarp_lookback = input.int ( 350, 'Lookback', minval=1, maxval=1900, inline='2', group='Line plot settings')
i_show_signal_ma = input.bool ( true, 'Signal MA', inline='0', group='Signal MA Settings')
i_signal_ma_Len = input.int ( 20, 'MA length', minval=1, inline='1', group='Signal MA Settings')
i_signal_ma_typ = input.string ( 'SMA', 'MA type', options=[ 'SMA', 'EMA', 'HMA', 'RMA', 'VWMA' ], inline='1', group='Signal MA Settings')
i_c_typ_sig = input.string ( 'Solid', 'Color type', options=[ 'Spectrum', 'Solid' ], inline='2', group='Signal MA Settings')
i_c_typ_spctrm_sig = input.string ( s_BGR, 'Spectrum', options=[ s_BR, s_BGR ], inline='2', group='Signal MA Settings')
i_c_solid_sig = input.color ( color.white, 'Solid color', inline='2', group='Signal MA Settings')
i_p_width_sig = input.int ( 1, 'Line width', minval=1, maxval=4, inline='3', group='Signal MA Settings')
i_hi_alert_pmarp = input.float ( 99, 'Hi PMARP alert level', minval=0.1, maxval=100, step=0.01, inline='6', group='Visual Alert Level Settings')
i_lo_alert_pmarp = input.float ( 1, 'Lo PMARP alert level', minval=0.1, maxval=100, step=0.01, inline='6', group='Visual Alert Level Settings')
i_hi_alert_pmar = input.float ( 2.7, 'Hi PMAR alert level.', minval=0.1, maxval=100, step=0.01, inline='7', group='Visual Alert Level Settings')
i_lo_alert_pmar = input.float ( 0.7, 'Lo PMAR alert level.', minval=0.1, maxval=100, step=0.01, inline='7', group='Visual Alert Level Settings')
i_hi_alert_line_on = input.bool ( true, 'Hi alert line.', inline='8', group='Visual Alert Level Settings')
i_lo_alert_line_on = input.bool ( true, 'Lo alert line.', inline='8', group='Visual Alert Level Settings')
i_bg_hi_signal_on = input.bool ( true, 'Hi signal bars', inline='9', group='Visual Alert Level Settings')
i_bg_lo_signal_on = input.bool ( true, 'Lo signal bars', inline='9', group='Visual Alert Level Settings')
///////////////////////////////////////////////////////////////////////////////
// variable declarations
var c_prcntSpctrm1 = array.new_color ( na )
if barstate.isfirst
array.push ( c_prcntSpctrm1, #0000FF ),
array.push ( c_prcntSpctrm1, #000AFF ), array.push ( c_prcntSpctrm1, #0014FF ), array.push ( c_prcntSpctrm1, #001FFF ), array.push ( c_prcntSpctrm1, #0029FF ), array.push ( c_prcntSpctrm1, #0033FF ),
array.push ( c_prcntSpctrm1, #003DFF ), array.push ( c_prcntSpctrm1, #0047FF ), array.push ( c_prcntSpctrm1, #0052FF ), array.push ( c_prcntSpctrm1, #005CFF ), array.push ( c_prcntSpctrm1, #0066FF ),
array.push ( c_prcntSpctrm1, #0070FF ), array.push ( c_prcntSpctrm1, #007AFF ), array.push ( c_prcntSpctrm1, #0085FF ), array.push ( c_prcntSpctrm1, #008FFF ), array.push ( c_prcntSpctrm1, #0099FF ),
array.push ( c_prcntSpctrm1, #00A3FF ), array.push ( c_prcntSpctrm1, #00ADFF ), array.push ( c_prcntSpctrm1, #00B8FF ), array.push ( c_prcntSpctrm1, #00C2FF ), array.push ( c_prcntSpctrm1, #00CCFF ),
array.push ( c_prcntSpctrm1, #00D6FF ), array.push ( c_prcntSpctrm1, #00E0FF ), array.push ( c_prcntSpctrm1, #00EBFF ), array.push ( c_prcntSpctrm1, #00F5FF ), array.push ( c_prcntSpctrm1, #00FFFF ),
array.push ( c_prcntSpctrm1, #00FFF5 ), array.push ( c_prcntSpctrm1, #00FFEB ), array.push ( c_prcntSpctrm1, #00FFE0 ), array.push ( c_prcntSpctrm1, #00FFD6 ), array.push ( c_prcntSpctrm1, #00FFCC ),
array.push ( c_prcntSpctrm1, #00FFC2 ), array.push ( c_prcntSpctrm1, #00FFB8 ), array.push ( c_prcntSpctrm1, #00FFAD ), array.push ( c_prcntSpctrm1, #00FFA3 ), array.push ( c_prcntSpctrm1, #00FF99 ),
array.push ( c_prcntSpctrm1, #00FF8F ), array.push ( c_prcntSpctrm1, #00FF85 ), array.push ( c_prcntSpctrm1, #00FF7A ), array.push ( c_prcntSpctrm1, #00FF70 ), array.push ( c_prcntSpctrm1, #00FF66 ),
array.push ( c_prcntSpctrm1, #00FF5C ), array.push ( c_prcntSpctrm1, #00FF52 ), array.push ( c_prcntSpctrm1, #00FF47 ), array.push ( c_prcntSpctrm1, #00FF3D ), array.push ( c_prcntSpctrm1, #00FF33 ),
array.push ( c_prcntSpctrm1, #00FF29 ), array.push ( c_prcntSpctrm1, #00FF1F ), array.push ( c_prcntSpctrm1, #00FF14 ), array.push ( c_prcntSpctrm1, #00FF0A ), array.push ( c_prcntSpctrm1, #00FF00 ),
array.push ( c_prcntSpctrm1, #0AFF00 ), array.push ( c_prcntSpctrm1, #14FF00 ), array.push ( c_prcntSpctrm1, #1FFF00 ), array.push ( c_prcntSpctrm1, #29FF00 ), array.push ( c_prcntSpctrm1, #33FF00 ),
array.push ( c_prcntSpctrm1, #3DFF00 ), array.push ( c_prcntSpctrm1, #47FF00 ), array.push ( c_prcntSpctrm1, #52FF00 ), array.push ( c_prcntSpctrm1, #5CFF00 ), array.push ( c_prcntSpctrm1, #66FF00 ),
array.push ( c_prcntSpctrm1, #70FF00 ), array.push ( c_prcntSpctrm1, #7AFF00 ), array.push ( c_prcntSpctrm1, #85FF00 ), array.push ( c_prcntSpctrm1, #8FFF00 ), array.push ( c_prcntSpctrm1, #99FF00 ),
array.push ( c_prcntSpctrm1, #A3FF00 ), array.push ( c_prcntSpctrm1, #ADFF00 ), array.push ( c_prcntSpctrm1, #B8FF00 ), array.push ( c_prcntSpctrm1, #C2FF00 ), array.push ( c_prcntSpctrm1, #CCFF00 ),
array.push ( c_prcntSpctrm1, #D6FF00 ), array.push ( c_prcntSpctrm1, #E0FF00 ), array.push ( c_prcntSpctrm1, #EBFF00 ), array.push ( c_prcntSpctrm1, #F5FF00 ), array.push ( c_prcntSpctrm1, #FFFF00 ),
array.push ( c_prcntSpctrm1, #FFF500 ), array.push ( c_prcntSpctrm1, #FFEB00 ), array.push ( c_prcntSpctrm1, #FFE000 ), array.push ( c_prcntSpctrm1, #FFD600 ), array.push ( c_prcntSpctrm1, #FFCC00 ),
array.push ( c_prcntSpctrm1, #FFC200 ), array.push ( c_prcntSpctrm1, #FFB800 ), array.push ( c_prcntSpctrm1, #FFAD00 ), array.push ( c_prcntSpctrm1, #FFA300 ), array.push ( c_prcntSpctrm1, #FF9900 ),
array.push ( c_prcntSpctrm1, #FF8F00 ), array.push ( c_prcntSpctrm1, #FF8500 ), array.push ( c_prcntSpctrm1, #FF7A00 ), array.push ( c_prcntSpctrm1, #FF7000 ), array.push ( c_prcntSpctrm1, #FF6600 ),
array.push ( c_prcntSpctrm1, #FF5C00 ), array.push ( c_prcntSpctrm1, #FF5200 ), array.push ( c_prcntSpctrm1, #FF4700 ), array.push ( c_prcntSpctrm1, #FF3D00 ), array.push ( c_prcntSpctrm1, #FF3300 ),
array.push ( c_prcntSpctrm1, #FF2900 ), array.push ( c_prcntSpctrm1, #FF1F00 ), array.push ( c_prcntSpctrm1, #FF1400 ), array.push ( c_prcntSpctrm1, #FF0000 ), array.push ( c_prcntSpctrm1, #FF0000 )
var c_prcntSpctrm2 = array.new_color ( na )
if barstate.isfirst
array.push ( c_prcntSpctrm2, #0000FF ),
array.push ( c_prcntSpctrm2, #0200FC ), array.push ( c_prcntSpctrm2, #0500F9 ), array.push ( c_prcntSpctrm2, #0700F7 ), array.push ( c_prcntSpctrm2, #0A00F4 ), array.push ( c_prcntSpctrm2, #0C00F2 ),
array.push ( c_prcntSpctrm2, #0F00EF ), array.push ( c_prcntSpctrm2, #1100ED ), array.push ( c_prcntSpctrm2, #1400EA ), array.push ( c_prcntSpctrm2, #1600E8 ), array.push ( c_prcntSpctrm2, #1900E5 ),
array.push ( c_prcntSpctrm2, #1C00E2 ), array.push ( c_prcntSpctrm2, #1E00E0 ), array.push ( c_prcntSpctrm2, #2100DD ), array.push ( c_prcntSpctrm2, #2300DB ), array.push ( c_prcntSpctrm2, #2600D8 ),
array.push ( c_prcntSpctrm2, #2800D6 ), array.push ( c_prcntSpctrm2, #2B00D3 ), array.push ( c_prcntSpctrm2, #2D00D1 ), array.push ( c_prcntSpctrm2, #3000CE ), array.push ( c_prcntSpctrm2, #3300CC ),
array.push ( c_prcntSpctrm2, #3500C9 ), array.push ( c_prcntSpctrm2, #3800C6 ), array.push ( c_prcntSpctrm2, #3A00C4 ), array.push ( c_prcntSpctrm2, #3D00C1 ), array.push ( c_prcntSpctrm2, #3F00BF ),
array.push ( c_prcntSpctrm2, #4200BC ), array.push ( c_prcntSpctrm2, #4400BA ), array.push ( c_prcntSpctrm2, #4700B7 ), array.push ( c_prcntSpctrm2, #4900B5 ), array.push ( c_prcntSpctrm2, #4C00B2 ),
array.push ( c_prcntSpctrm2, #4F00AF ), array.push ( c_prcntSpctrm2, #5100AD ), array.push ( c_prcntSpctrm2, #5400AA ), array.push ( c_prcntSpctrm2, #5600A8 ), array.push ( c_prcntSpctrm2, #5900A5 ),
array.push ( c_prcntSpctrm2, #5B00A3 ), array.push ( c_prcntSpctrm2, #5E00A0 ), array.push ( c_prcntSpctrm2, #60009E ), array.push ( c_prcntSpctrm2, #63009B ), array.push ( c_prcntSpctrm2, #660099 ),
array.push ( c_prcntSpctrm2, #680096 ), array.push ( c_prcntSpctrm2, #6B0093 ), array.push ( c_prcntSpctrm2, #6D0091 ), array.push ( c_prcntSpctrm2, #70008E ), array.push ( c_prcntSpctrm2, #72008C ),
array.push ( c_prcntSpctrm2, #750089 ), array.push ( c_prcntSpctrm2, #770087 ), array.push ( c_prcntSpctrm2, #7A0084 ), array.push ( c_prcntSpctrm2, #7C0082 ), array.push ( c_prcntSpctrm2, #7F007F ),
array.push ( c_prcntSpctrm2, #82007C ), array.push ( c_prcntSpctrm2, #84007A ), array.push ( c_prcntSpctrm2, #870077 ), array.push ( c_prcntSpctrm2, #890075 ), array.push ( c_prcntSpctrm2, #8C0072 ),
array.push ( c_prcntSpctrm2, #8E0070 ), array.push ( c_prcntSpctrm2, #91006D ), array.push ( c_prcntSpctrm2, #93006B ), array.push ( c_prcntSpctrm2, #960068 ), array.push ( c_prcntSpctrm2, #990066 ),
array.push ( c_prcntSpctrm2, #9B0063 ), array.push ( c_prcntSpctrm2, #9E0060 ), array.push ( c_prcntSpctrm2, #A0005E ), array.push ( c_prcntSpctrm2, #A3005B ), array.push ( c_prcntSpctrm2, #A50059 ),
array.push ( c_prcntSpctrm2, #A80056 ), array.push ( c_prcntSpctrm2, #AA0054 ), array.push ( c_prcntSpctrm2, #AD0051 ), array.push ( c_prcntSpctrm2, #AF004F ), array.push ( c_prcntSpctrm2, #B2004C ),
array.push ( c_prcntSpctrm2, #B50049 ), array.push ( c_prcntSpctrm2, #B70047 ), array.push ( c_prcntSpctrm2, #BA0044 ), array.push ( c_prcntSpctrm2, #BC0042 ), array.push ( c_prcntSpctrm2, #BF003F ),
array.push ( c_prcntSpctrm2, #C1003D ), array.push ( c_prcntSpctrm2, #C4003A ), array.push ( c_prcntSpctrm2, #C60038 ), array.push ( c_prcntSpctrm2, #C90035 ), array.push ( c_prcntSpctrm2, #CC0033 ),
array.push ( c_prcntSpctrm2, #CE0030 ), array.push ( c_prcntSpctrm2, #D1002D ), array.push ( c_prcntSpctrm2, #D3002B ), array.push ( c_prcntSpctrm2, #D60028 ), array.push ( c_prcntSpctrm2, #D80026 ),
array.push ( c_prcntSpctrm2, #DB0023 ), array.push ( c_prcntSpctrm2, #DD0021 ), array.push ( c_prcntSpctrm2, #E0001E ), array.push ( c_prcntSpctrm2, #E2001C ), array.push ( c_prcntSpctrm2, #E50019 ),
array.push ( c_prcntSpctrm2, #E80016 ), array.push ( c_prcntSpctrm2, #EA0014 ), array.push ( c_prcntSpctrm2, #ED0011 ), array.push ( c_prcntSpctrm2, #EF000F ), array.push ( c_prcntSpctrm2, #F2000C ),
array.push ( c_prcntSpctrm2, #F4000A ), array.push ( c_prcntSpctrm2, #F70007 ), array.push ( c_prcntSpctrm2, #F90005 ), array.push ( c_prcntSpctrm2, #FC0002 ), array.push ( c_prcntSpctrm2, #FF0000 )
///////////////////////////////////////////////////////////////////////////////
// Function Declarations
f_prior_sum ( _P, _X ) =>
math.sum ( _P[1], _X - 1 )
f_ma_val ( _P, _typ, _len ) =>
_typ == 'SMA' ? ta.sma ( _P, _len ) : _typ == 'EMA' ? ta.ema ( _P, _len ) : _typ == 'RMA' ? ta.rma ( _P, _len ) : _typ == 'HMA' ? ta.hma ( _P, _len ) : ta.vwma ( _P, _len )
f_pmarp ( _price, _pmarLen, _pmarpLen, _type ) =>
_pmar = math.abs ( _price / f_ma_val ( _price, _type, _pmarLen ))
_pmarpSum = 0
_len = bar_index < _pmarpLen ? bar_index : _pmarpLen
for i = 1 to _len by 1
_pmarpSum += ( _pmar[i] > _pmar ? 0 : 1 )
_pmarpSum
_return = bar_index >= _pmarLen ? _pmarpSum / _len * 100 : na
f_clrSlct ( _percent, _select, _type, _solid, _array1, _array2 ) =>
_select == 'Solid' ? _solid : array.get ( _type == 'Blue Green Red' ? _array1 : _array2, math.round ( _percent ))
///////////////////////////////////////////////////////////////////////////////
// calculations
pmarpOn = i_p_type_line == 'Price Moving Average Ratio Percentile'
ma = f_ma_val ( i_src_price, i_ma_typ, i_ma_len )
pmar = i_src_price / ma
pmarp = f_pmarp ( i_src_price, i_ma_len, i_pmarp_lookback, i_ma_typ )
pmarHigh := pmar > pmarHigh ? pmar : pmarHigh
c_pmar = ( pmar / pmarHigh )*100
plotline = pmarpOn ? pmarp : pmar
c_plotline = f_clrSlct ( pmarpOn ? plotline : c_pmar, i_c_typ_line, i_c_typ_spctrm_line, i_c_solid_line, c_prcntSpctrm1, c_prcntSpctrm2 )
signal_ma = f_ma_val ( plotline, i_signal_ma_typ, i_signal_ma_Len )
c_plotsig = f_clrSlct ( signal_ma, i_c_typ_sig, i_c_typ_spctrm_sig, i_c_solid_sig, c_prcntSpctrm1, c_prcntSpctrm2 )
hi_alert = pmarpOn ? i_hi_alert_pmarp : i_hi_alert_pmar
lo_alert = pmarpOn ? i_lo_alert_pmarp : i_lo_alert_pmar
hi_alertc = pmarpOn ? i_hi_alert_pmarp : i_hi_alert_pmar > pmarHigh ? 100 : ( i_hi_alert_pmar / pmarHigh ) * 100
c_hi_alert = f_clrSlct ( hi_alertc, i_c_typ_line, i_c_typ_spctrm_sig, i_c_solid_sig, c_prcntSpctrm1, c_prcntSpctrm2 )
c_lo_alert = f_clrSlct ( lo_alert, i_c_typ_line, i_c_typ_spctrm_sig, i_c_solid_sig, c_prcntSpctrm1, c_prcntSpctrm2 )
c_bg = color.new ( f_clrSlct ( pmarp, i_c_typ_line, i_c_typ_spctrm_line, i_c_solid_line, c_prcntSpctrm1, c_prcntSpctrm2 ), 50 )
p_hi_alert = plotline > hi_alert
p_lo_alert = plotline < lo_alert
///////////////////////////////////////////////////////////////////////////////
// Line Plot
p_scaleHi = plot ( pmarpOn ? 100 : na, 'Scale high', #ff0000, style = plot.style_line )
p_midLine = plot ( pmarpOn ? 50 : na, 'Midline', #555555, style = plot.style_line )
p_scaleLo = plot ( pmarpOn ? 0 : na, 'Scale low', #0000ff, style = plot.style_line )
plot ( plotline, 'Plot line', c_plotline, i_p_width_line, editable=false )
plot ( pmarpOn ? na : pmarHigh, 'Historical PMAR high', f_clrSlct ( 100, i_c_typ_line, i_c_typ_spctrm_line, i_c_solid_line, c_prcntSpctrm1, c_prcntSpctrm2 ), i_p_width_line, editable=false )
plot ( i_show_signal_ma ? signal_ma : na, 'Signal line', c_plotsig, i_p_width_sig, editable=false )
plot ( i_hi_alert_line_on and pmarpOn ? i_hi_alert_pmarp : i_hi_alert_pmar, 'High alert level', c_hi_alert, style = plot.style_line )
plot ( i_lo_alert_line_on and pmarpOn ? i_lo_alert_pmarp : i_lo_alert_pmar, 'Low alert level', c_lo_alert, style = plot.style_line )
bgcolor ( p_hi_alert and i_hi_alert_line_on and i_bg_hi_signal_on ? c_bg : na )
bgcolor ( p_lo_alert and i_lo_alert_line_on and i_bg_lo_signal_on ? c_bg : na )
///////////////////////////////////////////////////////////////////////////////
// End
Show all 299 lines