Re: Already Converted TradingView Indicators to MT4 Indicators
Posted: Sat Jun 29, 2024 11:54 am
Can someone help to port the Trending RSI from TradingView to MT4.?
https://tw.tradingview.com/script/qe0D9 ... hartPrime/
I think this indicator is really good. It takes a new approach to RSI intended to provide all of the missing information that traditional RSI lacks. Questions such as "why does the price continue to decline even during an oversold period?" can be aided using the Trending RSI.
To make the porting easier, I have already removed those not that useful feature from the code, e.g. the glowing effect of the line.
https://tw.tradingview.com/script/qe0D9 ... hartPrime/
I think this indicator is really good. It takes a new approach to RSI intended to provide all of the missing information that traditional RSI lacks. Questions such as "why does the price continue to decline even during an oversold period?" can be aided using the Trending RSI.

Code: Select all
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartPrime
//@version=5
indicator("Trending RSI [ChartPrime]", max_bars_back = 5000, timeframe = "", timeframe_gaps = true)
// Declarations {
convolve(float[] weights, float[] kernel, int iterations)=>
var float[] convolution = weights.copy()
if iterations > 0
if barstate.isfirst
for iteration = 0 to iterations - 1
float[] temp = array.new<float>()
for i = 0 to convolution.size() + kernel.size() - 2
float sum = 0.0
for j = 0 to kernel.size() - 1
int index = i - j
if index >= 0 and index < convolution.size()
sum += convolution.get(index) * kernel.get(j)
else
continue
temp.push(sum)
convolution := temp.copy()
convolution
method truncate_weights(float[] weights, bool enable = true)=>
if enable
int max_idx = weights.indexof(weights.max())
if max_idx > 0
for i = 0 to weights.indexof(weights.max()) - 1
weights.shift()
else
weights
weights
binomia_ma(float source, int length, bool enable)=>
float pre_filter = ta.sma(source, 2)
float[] h = array.from(0.5, 0.5)
float[] weights = convolve(h, h, length * 10 - 1).truncate_weights()
if enable and not na(source)
float sum = 0
float weight = 0
for i = 0 to math.min(weights.size() - 1, bar_index)
float w = weights.get(i)
weight += w
sum += nz(pre_filter[i], nz(source[i])) * w
sum/weight
else
float(na)
precalculate_phi_coefficients(simple int length, simple float phase)=>
var float[] coefficients = array.new<float>(length)
var float E = 0.0
const float SQRT_PIx2 = math.sqrt(2.0 * math.pi)
const float MULTIPLIER = -0.5 / 0.93
var int length_1 = length - 1
var float length_2 = length * 0.52353
if barstate.isfirst
for int i=0 to length_1
float alpha = (i + phase - length_2) * MULTIPLIER
float beta = 1.0 / (0.2316419 * math.abs(alpha) + 1.0)
float phi = (math.exp(math.pow(alpha, 2) * -0.5)
*-0.398942280) * beta *
( 0.319381530 + beta *
(-0.356563782 + beta *
( 1.781477937 + beta *
(-1.821255978 + beta
* 1.330274429)))) + 1.011
if alpha < 0.0
phi := 1.0 - phi
float weight = phi / SQRT_PIx2
E += weight
coefficients.set(i, weight)
[coefficients, E]
phi_smoother(series float source, series float[] coefficients, series float E, simple float hf_ratio = 0.5)=>
float sma2 = source * math.min(math.max(0.5, hf_ratio), 1) + nz(source[1], source) * math.max(math.min(0.5, 1 - hf_ratio), 0)
int length = coefficients.size()
if length > 1
float W = 0.0
for int i=0 to length - 1
float weight = coefficients.get(i)
W += weight * sma2[i]
W / E
else
source
rma(float source = close, float length = 9)=>
float alpha = 1 / length
var float smoothed = na
smoothed := alpha * source + (1 - alpha) * nz(smoothed[1])
rsi(float source, int length)=>
float up = math.max(source - source[1], 0)
float down = math.max(source[1] - source, 0)
float rs = rma(up, length) / rma(down, length)
float rsi = 100 - 100 / (1 + rs)
rsi
reverse_rsi(series float source, simple int length, float value)=>
float src = (source - ta.rma(source, length)) / (length / 2)
float alpha = 1 / length
float average_up_count = 0.0
float average_down_count = 0.0
average_up_count := src > src[1]
? alpha * (src - src[1]) + (1 - alpha) * nz(average_up_count[1], 1)
: (1 - alpha) * nz(average_up_count[1], 1)
average_down_count := src > src[1]
? (1 - alpha) * nz(average_down_count[1], 1)
: alpha * (src[1] - src) + (1 - alpha) * nz(average_down_count[1], 1)
float reversed_value = (length - 1) * (average_down_count * value / (100 - value) - average_up_count)
float reverse_rsi = reversed_value >= 0 ? src + reversed_value : src + reversed_value * (100 - value) / value
// }
// Inputs {
const string group_1 = "Settings"
const string top_value_tip = "Pick what RSI value you want the top range to represent."
const string bottom_value_tip = "Pick what RSI value you want the bottom range to represent"
float source = input.source(close, "Source", group = group_1)
int length = input.int(14, "Length", minval = 1, group = group_1)
float top_value = input.float(70, "Top Range", minval = 50, maxval = 99, tooltip = top_value_tip, group = group_1)
float bottom_value = input.float(30, "Bottom Range", minval = 1, maxval = 50, tooltip = bottom_value_tip, group = group_1)
bool ma_enable = input.bool(false, "MA Length ", inline = "MA", group = group_1)
int ma_length = input.int(30, "", minval = 0, maxval = 100, inline = "MA", group = group_1)
const string group_2 = "Smoothing"
const string smoothing_tip = "Adjusts the period of the filter."
const string speed_tip = "Adjusts the responsiveness of the filter."
const string hf_tip = "Adjusts the pre smoothing of the filter." + "\n" + "1 is no pre smoothing while 0.5 is maximum pre smoothing."
int left = input.int(15, "Look Left", minval = 1, tooltip = left_tip, group = group_3)
int right = input.int(15, "Look Right", minval = 1, tooltip = right_tip, group = group_3)
int lower_range = input.int(5, "Lower Range", minval = 1, tooltip = lower_range_tip, group = group_3)
int upper_range = input.int(60, "Upper Range", minval = 1, tooltip = upper_range_tip, group = group_3)
string divergence_label = input.string("Line", "Divergence Drawings", ["Line", "Label", "Line and Label"], group = group_3)
bool bullish_divergence = false // input.bool(false, "Enable Bullish Divergence", group = group_3)
bool bearish_divergence = false // input.bool(false, "Enable Bearish Divergence", group = group_3)
const string group_4 = "Color"
bool glow = false // input.bool(true, "Glow", group = group_4)
color rsi_color = input.color(#ffffff, "RSI", group = group_4)
color ma_color = input.color(#FFEE54, "MA Color", group = group_4)
color bg_color = input.color(#7E57C21A, "BG", group = group_4)
color top_color = input.color(#a222e283, "Upper Range", group = group_4)
color bottom_color = input.color(#e5b60c83, "Lower Range", group = group_4)
color center_bullish_up_color = input.color(#21a110, "Center", inline = "Center", group = group_4)
color center_bullish_down_color = input.color(#005e05, "", inline = "Center", group = group_4)
color center_bearish_up_color = input.color(#FCA0A9, "", inline = "Center", group = group_4)
color center_bearish_down_color = input.color(#FB3E38, "", inline = "Center", group = group_4)
color bullish_divergence_color = input.color(#00B374, "Bullish Divergence", group = group_4)
color bearish_divergence_color = input.color(#F03042, "Bearish Divergence", group = group_4)
color divergence_text_color = input.color(#FEFEFE, "Divergence Text", group = group_4)
color zero_line_color = input.color(#9E9E9E, "0 Line Color", group = group_4)
// }
// Calculations {
[smoothing_coefficients, smoothing_E] = precalculate_phi_coefficients(smoothing, smoothing_phase)
[boundary_coefficients, boundary_E] = precalculate_phi_coefficients(boundary_smoothing, boundary_smoothing_phase)
float top = phi_smoother(reverse_rsi(source, length, top_value), boundary_coefficients, boundary_E, boundary_hf)
float bottom = phi_smoother(reverse_rsi(source, length, bottom_value), boundary_coefficients, boundary_E, boundary_hf)
float center = phi_smoother(reverse_rsi(source, length, 50), smoothing_coefficients, smoothing_E, smoothing_hf)
float rsi = phi_smoother(reverse_rsi(source, length, rsi(source, length)), smoothing_coefficients, smoothing_E, smoothing_hf)
float ma = binomia_ma(rsi, ma_length, ma_enable)
float center_delta = center - center[1]
// }
// Color Calculations {
color center_color = center >= 0
? (center_delta > 0 ? center_bullish_up_color : center_bullish_down_color)
: (center_delta > 0 ? center_bearish_up_color : center_bearish_down_color)
color top_glow_color_max = color.new(top_color, 80)
color bottom_glow_color_max = color.new(bottom_color, 80)
color top_glow_color_min = color.new(top_color, 100)
color bottom_glow_color_min = color.new(bottom_color, 100)
color zero_line_solid = color.new(zero_line_color, 80)
color zero_line_dashed = color.new(zero_line_color, 50)
// }
// Plotting {
plot(0, "0 Line", zero_line_solid, display = display.pane)
top_band = plot(top, "Upper Range", top_color)
bottom_band = plot(bottom, "Lower Range", bottom_color)
fill(top_band, bottom_band, bg_color, "RSI Background Fill")
plot(center, "Center", center_color,2)
plot(ma, "MA", ma_color, display = ma_enable ? display.all : display.pane)
plot(rsi, "RSI", rsi_color)
// }