Conversion from TV:
Forums › ProRealTime English forum › ProBuilder support › Conversion from TV:
Viewing 3 posts - 1 through 3 (of 3 total)
-
-
04/25/2025 at 9:00 AM #246380
Hello,
I ask to the specialists of conversion between TV and PRT
Could you be so kind to convert the following indicator done by LuxAlgo : SuperTrend AI (Clustering) [LuxAlgo]// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/// © LuxAlgo//@version=5indicator(“SuperTrend AI (Clustering) [LuxAlgo]”, “LuxAlgo – SuperTrend AI”, overlay = true, max_labels_count = 500)//——————————————————————————//Settings//—————————————————————————–{length = input(10, ‘ATR Length’)minMult = input.int(1, ‘Factor Range’, minval = 0, inline = ‘factor’)maxMult = input.int(5, ”, minval = 0, inline = ‘factor’)step = input.float(.5, ‘Step’, minval = 0, step = 0.1)//Trigger errorif minMult > maxMultruntime.error(‘Minimum factor is greater than maximum factor in the range’)perfAlpha = input.float(10, ‘Performance Memory’, minval = 2)fromCluster = input.string(‘Best’, ‘From Cluster’, options = [‘Best’, ‘Average’, ‘Worst’])//OptimizationmaxIter = input.int(1000, ‘Maximum Iteration Steps’, minval = 0, group = ‘Optimization’)maxData = input.int(10000, ‘Historical Bars Calculation’, minval = 0, group = ‘Optimization’)//StylebearCss = input(color.red, ‘Trailing Stop’, inline = ‘ts’, group = ‘Style’)bullCss = input(color.teal, ”, inline = ‘ts’, group = ‘Style’)amaBearCss = input(color.new(color.red, 50), ‘AMA’, inline = ‘ama’, group = ‘Style’)amaBullCss = input(color.new(color.teal, 50), ”, inline = ‘ama’, group = ‘Style’)showGradient = input(true, ‘Candle Coloring’, group = ‘Style’)showSignals = input(true, ‘Show Signals’, group = ‘Style’)//DashboardshowDash = input(true, ‘Show Dashboard’, group = ‘Dashboard’)dashLoc = input.string(‘Top Right’, ‘Location’, options = [‘Top Right’, ‘Bottom Right’, ‘Bottom Left’], group = ‘Dashboard’)textSize = input.string(‘Small’, ‘Size’ , options = [‘Tiny’, ‘Small’, ‘Normal’], group = ‘Dashboard’)//—————————————————————————–}//UDT’s//—————————————————————————–{type supertrendfloat upper = hl2float lower = hl2float outputfloat perf = 0float factorint trend = 0type vectorarray<float> out//—————————————————————————–}//Supertrend//—————————————————————————–{var holder = array.new<supertrend>(0)var factors = array.new<float>(0)//Populate supertrend type arrayif barstate.isfirstfor i = 0 to int((maxMult – minMult) / step)factors.push(minMult + i * step)holder.push(supertrend.new())atr = ta.atr(length)//Compute Supertrend for multiple factorsk = 0for factor in factorsget_spt = holder.get(k)up = hl2 + atr * factordn = hl2 – atr * factorget_spt.trend := close > get_spt.upper ? 1 : close < get_spt.lower ? 0 : get_spt.trendget_spt.upper := close[1] < get_spt.upper ? math.min(up, get_spt.upper) : upget_spt.lower := close[1] > get_spt.lower ? math.max(dn, get_spt.lower) : dndiff = nz(math.sign(close[1] – get_spt.output))get_spt.perf += 2/(perfAlpha+1) * (nz(close – close[1]) * diff – get_spt.perf)get_spt.output := get_spt.trend == 1 ? get_spt.lower : get_spt.upperget_spt.factor := factork += 1//—————————————————————————–}//K-means clustering//—————————————————————————–{factor_array = array.new<float>(0)data = array.new<float>(0)//Populate data arraysif last_bar_index – bar_index <= maxDatafor element in holderdata.push(element.perf)factor_array.push(element.factor)//Intitalize centroids using quartilescentroids = array.new<float>(0)centroids.push(data.percentile_linear_interpolation(25))centroids.push(data.percentile_linear_interpolation(50))centroids.push(data.percentile_linear_interpolation(75))//Intialize clustersvar array<vector> factors_clusters = navar array<vector> perfclusters = naif last_bar_index – bar_index <= maxDatafor _ = 0 to maxIterfactors_clusters := array.from(vector.new(array.new<float>(0)), vector.new(array.new<float>(0)), vector.new(array.new<float>(0)))perfclusters := array.from(vector.new(array.new<float>(0)), vector.new(array.new<float>(0)), vector.new(array.new<float>(0)))//Assign value to clusteri = 0for value in datadist = array.new<float>(0)for centroid in centroidsdist.push(math.abs(value – centroid))idx = dist.indexof(dist.min())perfclusters.get(idx).out.push(value)factors_clusters.get(idx).out.push(factor_array.get(i))i += 1//Update centroidsnew_centroids = array.new<float>(0)for cluster_ in perfclustersnew_centroids.push(cluster_.out.avg())//Test if centroid changedif new_centroids.get(0) == centroids.get(0) and new_centroids.get(1) == centroids.get(1) and new_centroids.get(2) == centroids.get(2)breakcentroids := new_centroids//—————————————————————————–}//Signals and trailing stop//—————————————————————————–{//Get associated supertrendvar float target_factor = navar float perf_idx = navar float perf_ama = navar from = switch fromCluster‘Best’ => 2‘Average’ => 1‘Worst’ => 0//Performance index denominatorden = ta.ema(math.abs(close – close[1]), int(perfAlpha))if not na(perfclusters)//Get average factors within target clustertarget_factor := nz(factors_clusters.get(from).out.avg(), target_factor)//Get performance index of target clusterperf_idx := math.max(nz(perfclusters.get(from).out.avg()), 0) / den//Get new supertrendvar upper = hl2var lower = hl2var os = 0up = hl2 + atr * target_factordn = hl2 – atr * target_factorupper := close[1] < upper ? math.min(up, upper) : uplower := close[1] > lower ? math.max(dn, lower) : dnos := close > upper ? 1 : close < lower ? 0 : osts = os ? lower : upper//Get trailing stop adaptive MAif na(ts[1]) and not na(ts)perf_ama := tselseperf_ama += perf_idx * (ts – perf_ama)//—————————————————————————–}//Dashboard//—————————————————————————–{vartable_position=dashLoc==’Bottom Left’?position.bottom_left:dashLoc==’Top Right’?position.top_right: position.bottom_rightvartable_size=textSize==’Tiny’?size.tiny:textSize==’Small’?size.small: size.normalvar tb = table.new(table_position, 4, 4, bgcolor = #1e222d, border_color = #373a46, border_width = 1, frame_color = #373a46, frame_width = 1)if showDashif barstate.isfirsttb.cell(0, 0, ‘Cluster’, text_color = color.white, text_size = table_size)tb.cell(0, 1, ‘Best’, text_color = color.white, text_size = table_size)tb.cell(0, 2, ‘Average’, text_color = color.white, text_size = table_size)tb.cell(0, 3, ‘Worst’, text_color = color.white, text_size = table_size)tb.cell(1, 0, ‘Size’, text_color = color.white, text_size = table_size)tb.cell(2, 0, ‘Centroid Dispersion’, text_color = color.white, text_size = table_size)tb.cell(3, 0, ‘Factors’, text_color = color.white, text_size = table_size)if barstate.islasttopN = perfclusters.get(2).out.size()midN = perfclusters.get(1).out.size()btmN = perfclusters.get(0).out.size()//Sizetb.cell(1, 1, str.tostring(topN), text_color = color.white, text_size = table_size)tb.cell(1, 2, str.tostring(midN), text_color = color.white, text_size = table_size)tb.cell(1, 3, str.tostring(btmN), text_color = color.white, text_size = table_size)//Contenttb.cell(3, 1, str.tostring(factors_clusters.get(2).out), text_color = color.white, text_size = table_size, text_halign = text.align_left)tb.cell(3, 2, str.tostring(factors_clusters.get(1).out), text_color = color.white, text_size = table_size, text_halign = text.align_left)tb.cell(3, 3, str.tostring(factors_clusters.get(0).out), text_color = color.white, text_size = table_size, text_halign = text.align_left)//Calculate dispersion around centroidi = 0for cluster_ in perfclustersdisp = 0.if cluster_.out.size() > 1for value in cluster_.outdisp += math.abs(value – centroids.get(i))disp /= switch i0 => btmN1 => midN2 => topNi += 1tb.cell(2, 4 – i, str.tostring(disp, ‘#.####’), text_color = color.white, text_size = table_size)//—————————————————————————–}//Plots//—————————————————————————–{css = os ? bullCss : bearCssplot(ts, ‘Trailing Stop’, os != os[1] ? na : css)plot(perf_ama, ‘Trailing Stop AMA’,ta.cross(close, perf_ama) ? na: close > perf_ama ? amaBullCss : amaBearCss)//Candle coloringbarcolor(showGradient ? color.from_gradient(perf_idx, 0, 1, color.new(css, 80), css) : na)//Signalsn = bar_indexif showSignalsif os > os[1]label.new(n, ts, str.tostring(int(perf_idx * 10)), color = bullCss, style = label.style_label_up, textcolor = color.white, size = size.tiny)if os < os[1]label.new(n, ts, str.tostring(int(perf_idx * 10)), color = bearCss, style = label.style_label_down, textcolor = color.white, size = size.tiny)//—————————————————————————–}Thks in advanceReb04/25/2025 at 10:20 AM #246395Hi. this is already done.
https://www.prorealcode.com/topic/conversion-indicador-tradingviewsupertrend-ai/04/25/2025 at 11:09 AM #246399 -
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
Find exclusive trading pro-tools on
Similar topics: