#convert_rsi_momen
#https://usethinkscript.com/threads/convert-tradingview-rsi-momentum-acceleration.19465/
#Conversion Requests
#Convert TradingView RSI Momentum Acceleration
#Veasna 9/2
# I would like to ask for your help in converting the Tradingview script (in the link below) to TOS. It would be #helpful to see the range the price move in weekly time frame.
#However, if there is one that is similar, please let me know.
#Here is the link to the open source in Trading view:
#https://www.tradingview.com/script/lOKosgxd-RSI-Momentum-Acceleration-by-Tartigradia/
#https://www.tradingview.com/script/lOKosgxd-RSI-Momentum-Acceleration-by-Tartigradia/
#// RSI Momentum Acceleration (RSIMomAcc) by Tartigradia
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// Authors: Tartigradia, using a core routine from DGT (dgtrd)
#//
#// Description:
#// Plots the momentum acceleration oscillators from price and RSI, rescaled and with areas above/below highlighted.
#// Usage: in a nutshell, when the background is yellow, it's bearish (RSI decelerates faster than price), whereas when the background is green, it's bullish (RSI accelerates faster than price).
#// Note: it supports using any other indicator's output as the second source input, instead of RSI. PineScript does not allow for more than one source to receive input from other indicators, all the others must only use price as an input.
#// This indicator uses the core routine to calculate Momentum Acceleration Oscillators by DGT:
# https://www.tradingview.com/script/BmXmwmnE-Momentum-Acceleration-by-DGT/
#// This indicator is based on the idea of stinkbug
# https://www.tradingview.com/script/BmXmwmnE-Momentum-Acceleration-by-DGT/#tc8337441
# "RSI is a good momentum indicator showing how excited ppl are on a move, this is why divergences on it work so well. I would like to see the change accelerating or slowing on a move up or down.."
#//@version=5
#indicator('RSI Momentum Acceleration by Tartigradia', 'RSIMomAcc', overlay=false, timeframe="")
declare lower;
def na = double.nan;
def bn = barnumber();
#f_speedy(_d, _t) =>
# // Function by DGTRD, props to them! https://www.tradingview.com/script/BmXmwmnE-Momentum-Acceleration-by-DGT/
# v = ta.sma(ta.change(_d, _t) / _t, 3)
# a = ta.change(v, _t) / _t
# v - a
# ta.change(src , len)
# Compares the current source value to its value length bars ago and returns the difference.
# x = _d - getvalue(_d, _t)
script f_speedy {
input _d = 0;
input _t = 0;
def chg = _d - getvalue(_d, _t);
def len = 3;
# v = ta.sma(chg / _t, len)
def v = MovingAverage( AverageType.Simple, (chg / _t), len );
# a = ta.change(v, _t) / _t
def a = v - getvalue(v, _t)/_t;
plot z = v - a;
}
#f_featurescale(x, a, b) =>
# // Rescale x to be between minimum a and maximum b values
# a + ((x - ta.min(x)) * (b - a) / (ta.max(x) - ta.min(x)))
# ta.min()
# Returns the all-time low value of source from the beginning of the chart up to the current bar.
script f_featurescale {
input x = 0;
input a = 0;
input b = 0;
plot z = a + ((x - lowestall(x)) * (b - a) / (highestall(x) - lowestall(x)));
}
#// Parameters
#var grp1 = 'Momentum parameters'
#t = input.int(13, 'Momentum length', minval=1, group=grp1)
#frescale = input.bool(true, 'Rescale', inline='R1', group=grp1, tooltip='Rescale momentum values so that both plots overlap and #crosses nicely on the same scale.')
#rangemin = input(0.0, 'Min', inline='R1', group=grp1)
#rangemax = input(100.0, 'Max', inline='R1', group=grp1)
input t = 13;
input frescale = yes;
input rangemin = 0.0;
input rangemax = 100.0;
#var grp2 = 'Source 1'
#source1 = input.string('close', title='Source 1', options=['close', 'open', 'high', 'low', 'hl2', 'hlc3', 'hlcc4'], tooltip='Source for the first momentum oscillator', group=grp2) // workaround to only use one input.source(), so that PineScript then allows to use other indicators outputs as input here, otherwise with more than 1 input.source() then we can only select price as input for all sources.
#input source1 = { delault "close", "open", "high", "low", "hl2", "hlc3', 'hlcc4'], tooltip='Source for the first momentum oscillator', group=grp2) // workaround to only use one input.source(), so that PineScript then allows to use other indicators outputs as input here, otherwise with more than 1 input.source() then we can only select price as input for all sources.
input source1 = close;
#var grp3 = 'Source 2'
#source2 = input.string('RSI', 'Source', options=['RSI', 'Custom'], tooltip='Source for the second momentum oscillator. Default to RSI, but can be set to Custom, then can select any source under Source 2 Custom.', group=grp3)
#rsisource = input.string('close', title='RSI Source', options=['close', 'open', 'high', 'low', 'hl2', 'hlc3', 'hlcc4'], tooltip='Source for the RSI if used for Source 2.', group=grp3)
#rsilength = input.int(14, 'RSI Length', minval=1, group=grp3)
#source2_custom = input.source(open, title='Source 2 Custom', tooltip='Custom source for the second momentum oscillator instead of the default RSI. This can be data from any other indicator.', group=grp3)
def rsisource = close;
#// Workaround to use only one source: we manually input each possible price source from an input.string()
#s1 = switch source1
# 'close' => close
# 'open' => open
# 'high' => high
# 'low' => low
# 'hl2' => hl2
# 'hlc3' => hlc3
# 'hlcc4' => hlcc4
#s2 = switch rsisource
# 'close' => close
# 'open' => open
# 'high' => high
# 'low' => low
# 'hl2' => hl2
# 'hlc3' => hlc3
# 'hlcc4' => hlcc4
def s1 = source1;
def s2 = rsisource;
#// Switch between native RSI and custom source for Source 2
#source2_d = source2 == 'RSI' ? ta.rsi(s2, rsilength) : source2_custom
def source2_d = RSI();
#// Calculate momentum oscillator from each data source
#d1 = f_speedy(s1, t)
#d2 = f_speedy(source2_d, t)
def d1 = f_speedy(s1, t);
def d2 = f_speedy(source2_d, t);
#// Rescale data so that they overlap nicely
#fd1 = frescale ? f_featurescale(d1, rangemin, rangemax) : d1
#fd2 = frescale ? f_featurescale(d2, rangemin, rangemax) : d2
def fd1 = if frescale then f_featurescale(d1, rangemin, rangemax) else d1;
def fd2 = if frescale then f_featurescale(d2, rangemin, rangemax) else d2;
#// Plot
#plot(fd1, color=color.new(color.aqua, 0), title='Momentum Acceleration Oscillator 1')
#plot(fd2, color=color.new(color.red, 0), title='Momentum Acceleration Oscillator 2')
plot z1 = fd1;
plot z2 = fd2;
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(1);
z1.hidebubble();
z2.SetDefaultColor(Color.red);
z2.setlineweight(1);
z2.hidebubble();
#bgcolor(fd1 < fd2 ? color.new(color.green, 80) : color.new(color.yellow, 80), title='Highlight background when oscillator 1 above or below oscillator 2')
def pos = double.POSITIVE_INFINITY;
def neg = double.negative_INFINITY;
def grn = if fd2 > fd1 then pos else na;
def red = if fd2 < fd1 then pos else na;
addcloud(grn, neg, color.green);
addcloud(red, neg, color.yellow);
#