Relative Trend Index conversion

I am trying to convert code to Amibroker for the RTI (Relative Trend Index) indicator. I can get the easy parts, but have always found the 'for' function hard to get to grips with.
Could someone give me information where to get information how to convert, or help explain how to convert this.
The following has the URL from where the code comes from.
I have converted the Parameter section, now to get the 'Relative Trend Index Calculation' section done.

// // Relative Trend Index (RTI) by Zeiierman — Indicator by Zeiierman — TradingView

// Inputs {
trend_data_count = Param( "Trend Length", 100, 10, 500, 4 );
trend_sensitivity_percentage = Param( "Sensitivity", 95, 50, 98, 1 );
signal_length = Param( "Signal Length", 20, 1, 200, 1 );//MA Period
ob = Param( "", 80, 0, 100, 1 ); //Overbought Line
os = Param( "", 20, 1, 100, 1 ); //Oversold Line

//trend_data_count = input.int(100, step=4, minval=10, title="Trend Length", inline = "RT", group="Relative Trend Index [RTI]", tooltip="")
//trend_sensitivity_percentage = input.int(95, step=1,minval=50, maxval=98,title='Sensitivity ', inline = "RT1", group="Relative Trend Index [RTI]", tooltip="")
//signal_length = input.int(20, step=1,minval=1, maxval=200,title='Signal Length', inline = "", group="Signal Line", tooltip="Set the Ma period.")
//ob = input.float(80, step=1, minval=0, maxval=100, title="", inline = "obos", group="Overbought/Oversold", tooltip="")
//os = input.float(20,step=1, minval=0, maxval=100,title="", inline = "obos", group="Overbought/Oversold", tooltip="Set the OB/OS levels.")
//~~~~~~~~~~~~~~~~~~~~~~~}

// Relative Trend Index Calculation {
upper_trend = close + StDev(close, 2);
lower_trend = close - StDev(close, 2);

upper_array = array.new(0);
lower_array = array.new(0);
for i = 0 to trend_data_count - 1
upper_array.push(upper_trend[i])
lower_array.push(lower_trend[i])
upper_array.sort()
lower_array.sort()

upper_index = math.round( trend_sensitivity_percentage / 100 * trend_data_count) - 1
lower_index = math.round((100 - trend_sensitivity_percentage) / 100 * trend_data_count) - 1
UpperTrend = upper_array.get(upper_index)
LowerTrend = lower_array.get(lower_index)
RelativeTrendIndex = ((close - LowerTrend) / (UpperTrend - LowerTrend))*100
//~~~~~~~~~~~~~~~~~~~~~~~}

Thank you in advance for any help offered.

I have since found within another reply that programming costs $80-$150 per hour. No mention where this service can be found.
So no programming asked for in this post. However if someone has the time to discuss what is happening here in the 'for' function, it may help my search. I tried to search for the 'for' function in the manual, but it makes no mention or comments regarding this. Again thanks in advance.

@pec, I looked at the Pine script source code.

The "strange" part to understand (at least for me) was the usage of the 2 "arrays".
I looked at the version 5 documentation and saw that, as the code is written, each line that involves these "array" operations is executed for each bar.

So the easist way (again, at least for me) to replicate in AB is to use a nested loop in an outer loop that runs on all bars (for chart usage you can limit the calculations only the to the visible bars).

I suggest to use single row matrices (vectors) to provide the arrays' functionality of Pine scripts.

Unfortunately, especially if the (trend_data_count) lookback period is large, the inner loop to refill the matrices (and then sort them and extract the indexes to choose the values that are used to "normalize" the range), it is not very efficient.

Perhaps there are more clever ways to obtain the same result, but certainly starting your first implementation with a loop, checking the result by comparing it with that of TV, will allow you to understand if you are actually replicating the same functionality, and then if necessary, try to optimize the procedure (for instance, you can speed it up a bit applying to the matrices data a technique conceptually similar to a rolling window).

In any case, I hope that some more experienced user than me can show you a better way to do it.

I much appreciate the time you have spent looking at this. Your insights will go toward me working this out and getting a better understanding of the operations needed here. May you be blessed with your journey in trading and AB.

1 Like