SuperTrend Polyfactor Oscillator [LuxAlgo] For ThinkOrSwim

@samer800 Cau you please help me convert this SuperTrend Polyfactor Oscillator [LuxAlgo]
https://app.luxalgo.com/library/indicator/SuperTrend-Polyfactor-Oscillator

thnanks so much

/ 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=5
indicator("SuperTrend Polyfactor Oscillator [LuxAlgo]", "LuxAlgo - SuperTrend Polyfactor Oscillator")
//-----------------------------------------------------------------------------}
//Settings
//-----------------------------------------------------------------------------{
length = input.int(10, minval = 2)
starting = input.float(1, 'Starting Factor', minval = 0)
increment = input.float(.5, minval = 0, step = .1)

normalize = input.string('None', options = ['None', 'Max-Min', 'Absolute Sum'])

//Style
mesh = input(true, inline = 'mesh', group = 'Style')
upCss = input.color(color.new(#089981, 90), '', inline = 'mesh', group = 'Style')
dnCss = input.color(color.new(#f23645, 90), '', inline = 'mesh', group = 'Style')

//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type supertrend
float upper = hl2
float lower = hl2
float output
float perf = 0
float factor
int trend = 0

//-----------------------------------------------------------------------------}
//Function
//-----------------------------------------------------------------------------{
norm(value, diffs, den)=>
normalized = switch normalize
'Max-Min' => (value - diffs.min()) / diffs.range()
'Absolute Sum' => value / den
=> value

//-----------------------------------------------------------------------------}
//Supertrend
//-----------------------------------------------------------------------------{
var holder = array.new<supertrend>(0)
diffs = array.new<float>(0)

//Populate supertrend type array
if barstate.isfirst
for i = 0 to 19
holder.push(supertrend.new())

atr = ta.atr(length)

//Compute Supertrend for multiple factors
k = 0
den = 0.
factor = starting

for i = 0 to 19
get_spt = holder.get(k)

up = hl2 + atr * factor
dn = hl2 - atr * factor

get_spt.trend := close > get_spt.upper ? 1 : close < get_spt.lower ? 0 : get_spt.trend
get_spt.upper := close[1] < get_spt.upper ? math.min(up, get_spt.upper) : up
get_spt.lower := close[1] > get_spt.lower ? math.max(dn, get_spt.lower) : dn
get_spt.output := get_spt.trend == 1 ? get_spt.lower : get_spt.upper

diffs.push(close - get_spt.output)

k += 1
den += math.abs(close - get_spt.output)
factor += increment

//Outputs
median = norm(diffs.median(), diffs, den)
stdev = normalize != 'Max-Min' ? norm(diffs.stdev(), diffs, den) : na

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
//Mesh
plot(mesh ? norm(diffs.get(0), diffs, den) : na , color = diffs.get(0) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(1), diffs, den) : na , color = diffs.get(1) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(2), diffs, den) : na , color = diffs.get(2) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(3), diffs, den) : na , color = diffs.get(3) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(4), diffs, den) : na , color = diffs.get(4) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(5), diffs, den) : na , color = diffs.get(5) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(6), diffs, den) : na , color = diffs.get(6) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(7), diffs, den) : na , color = diffs.get(7) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(8), diffs, den) : na , color = diffs.get(8) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(9), diffs, den) : na , color = diffs.get(9) > 0 ? upCss : dnCss , style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(10), diffs, den) : na, color = diffs.get(10) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(11), diffs, den) : na, color = diffs.get(11) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(12), diffs, den) : na, color = diffs.get(12) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(13), diffs, den) : na, color = diffs.get(13) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(14), diffs, den) : na, color = diffs.get(14) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(15), diffs, den) : na, color = diffs.get(15) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(16), diffs, den) : na, color = diffs.get(16) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(17), diffs, den) : na, color = diffs.get(17) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(18), diffs, den) : na, color = diffs.get(18) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)
plot(mesh ? norm(diffs.get(19), diffs, den) : na, color = diffs.get(19) > 0 ? upCss : dnCss, style = plot.style_histogram, display = display.all - display.status_line, editable = false)

//Median
plot(median, 'Median', median > (normalize == 'Max-Min' ? .5 : 0) ? #90bff9 : #ffcc80)

//Stdev Area
up = plot(stdev, color = na, editable = false)
dn = plot(-stdev, color = na, editable = false)
fill(up, dn, color.new(#90bff9, 80), title = 'Stdev Area')

//-----------------------------------------------------------------------------}
try the below. May not give exact results since the code uses arrays but pretty close.

CSS:
#/ This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-S
#// © LuxAlgo
#indicator("SuperTrend Polyfactor Oscillator [LuxAlgo]", "LuxAlgo - SuperTrend Polyfactor Oscillator")
# Converted by Sam4Cok@Samer800    - 12/2023 - Not Exact Results
Declare lower;

input src = hl2;
input length = 10;#, minval = 2)
input StartingFactor = 1;#, 'Starting Factor', minval = 0)
input increment = 0.5;#, minval = 0, step = .1)
input normalize = {default "None", "Max-Min", "Absolute Sum"};
input mesh = yes;# inline = 'mesh', group = 'Style')


def na = Double.NaN;
def last = isNaN(close);
def maxmin = normalize == normalize."Max-Min";

DefineGlobalColor("up", CreateColor(6, 116, 98));
DefineGlobalColor("dn", CreateColor(206, 13, 29));
DefineGlobalColor("mup", CreateColor(144,191,249));
DefineGlobalColor("mdn", CreateColor(255,204,128));
#/Function
script norm {
    input value = close;
    input min = 0;
    input max = 0;
    input den = 0;
    input normalize = "Max-Min";
    def norm = if normalize == "Max-Min" then (value - min) / (max - min) else
               if normalize == "Absolute Sum" then value / den else value;
    plot out = norm;
}
#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input length = 10;
    def nATR = ATR(Length = length);
    def up = src + nATR * factor;
    def dn = src - nATR * factor;
    def upper;
    def lower;
    def trend = if close > upper[1] then 1 else
                if close < lower[1] then 0 else trend[1];
    upper = if close[1] < upper[1] then Min(up, upper[1]) else up;
    lower = if close[1] > lower[1] then Max(dn, lower[1]) else dn;
    def output = if trend == 1 then lower else upper;
    plot ST = output;
}

def st0  = supertrend(src, StartingFactor + increment * 0, length);
def st1  = supertrend(src, StartingFactor + increment * 1, length);
def st2  = supertrend(src, StartingFactor + increment * 2, length);
def st3  = supertrend(src, StartingFactor + increment * 3, length);
def st4  = supertrend(src, StartingFactor + increment * 4, length);
def st5  = supertrend(src, StartingFactor + increment * 5, length);
def st6  = supertrend(src, StartingFactor + increment * 6, length);
def st7  = supertrend(src, StartingFactor + increment * 7, length);
def st8  = supertrend(src, StartingFactor + increment * 8, length);
def st9  = supertrend(src, StartingFactor + increment * 9, length);
def st10 = supertrend(src, StartingFactor + increment * 10, length);
def st11 = supertrend(src, StartingFactor + increment * 11, length);
def st12 = supertrend(src, StartingFactor + increment * 12, length);
def st13 = supertrend(src, StartingFactor + increment * 13, length);
def st14 = supertrend(src, StartingFactor + increment * 14, length);
def st15 = supertrend(src, StartingFactor + increment * 15, length);
def st16 = supertrend(src, StartingFactor + increment * 16, length);
def st17 = supertrend(src, StartingFactor + increment * 17, length);
def st18 = supertrend(src, StartingFactor + increment * 18, length);
def st19 = supertrend(src, StartingFactor + increment * 19, length);

def diffs0 = close - st0;
def diffs1 = close - st1;
def diffs2 = close - st2;
def diffs3 = close - st3;
def diffs4 = close - st4;
def diffs5 = close - st5;
def diffs6 = close - st6;
def diffs7 = close - st7;
def diffs8 = close - st8;
def diffs9 = close - st9;
def diffs10 = close - st10;
def diffs11 = close - st11;
def diffs12 = close - st12;
def diffs13 = close - st13;
def diffs14 = close - st14;
def diffs15 = close - st15;
def diffs16 = close - st16;
def diffs17 = close - st17;
def diffs18 = close - st18;
def diffs19 = close - st19;

def max1 = Max(diffs0, Max(diffs1, Max(diffs2, diffs3)));
def max2 = Max(max1, Max(diffs4, Max(diffs5, Max(diffs6, diffs7))));
def max3 = Max(max2, Max(diffs8, Max(diffs9, Max(diffs10, diffs11))));
def max4 = Max(max3, Max(diffs12, Max(diffs13, Max(diffs14, diffs15))));
def max5 = Max(max4, Max(diffs16, Max(diffs17, Max(diffs18, diffs19))));

def min1 = Min(diffs0, Min(diffs1,  Min(diffs2, diffs3)));
def min2 = Min(min1,   Min(diffs4,  Min(diffs5,  Min(diffs6, diffs7))));
def min3 = Min(min2,   Min(diffs8,  Min(diffs9,  Min(diffs10, diffs11))));
def min4 = Min(min3,   Min(diffs12, Min(diffs13, Min(diffs14, diffs15))));
def min5 = Min(min4,   Min(diffs16, Min(diffs17, Min(diffs18, diffs19))));

def max = max5;
def min = min5;

def totDif = diffs0 + diffs1 + diffs2 + diffs3 + diffs4 + diffs5 + diffs6 + diffs7 + diffs8 + diffs9 +
             diffs10 + diffs11 + diffs12 + diffs13 + diffs14 + diffs15 + diffs16 + diffs17 + diffs18 + diffs19;
def totDiff = totDif;
def meanDiff = totDiff / 20;
def difMid = meanDiff;#Median(totDiff, 1) / 20;#meanDiff;

def sqDif0 = Sqr(meanDiff - diffs0);
def sqDif1 = Sqr(meanDiff - diffs1);
def sqDif2 = Sqr(meanDiff - diffs2);
def sqDif3 = Sqr(meanDiff - diffs3);
def sqDif4 = Sqr(meanDiff - diffs4);
def sqDif5 = Sqr(meanDiff - diffs5);
def sqDif6 = Sqr(meanDiff - diffs6);
def sqDif7 = Sqr(meanDiff - diffs7);
def sqDif8 = Sqr(meanDiff - diffs8);
def sqDif9 = Sqr(meanDiff - diffs9);
def sqDif10 = Sqr(meanDiff - diffs10);
def sqDif11 = Sqr(meanDiff - diffs11);
def sqDif12 = Sqr(meanDiff - diffs12);
def sqDif13 = Sqr(meanDiff - diffs13);
def sqDif14 = Sqr(meanDiff - diffs14);
def sqDif15 = Sqr(meanDiff - diffs15);
def sqDif16 = Sqr(meanDiff - diffs16);
def sqDif17 = Sqr(meanDiff - diffs17);
def sqDif18 = Sqr(meanDiff - diffs18);
def sqDif19 = Sqr(meanDiff - diffs19);

def sumSqr = sqDif0 + sqDif1 + sqDif2 + sqDif3 + sqDif4 + sqDif5 + sqDif6 + sqDif7 + sqDif8 + sqDif9 +
             sqDif10 + sqDif11 + sqDif12 + sqDif13 + sqDif14 + sqDif15 + sqDif16 + sqDif17 + sqDif18 + sqDif19;
def divSumSqr = sumSqr / 19;
def difStdev = Sqrt(divSumSqr);

def den0 = AbsValue(close - st0);
def den1 = AbsValue(close - st1);
def den2 = AbsValue(close - st2);
def den3 = AbsValue(close - st3);
def den4 = AbsValue(close - st4);
def den5 = AbsValue(close - st5);
def den6 = AbsValue(close - st6);
def den7 = AbsValue(close - st7);
def den8 = AbsValue(close - st8);
def den9 = AbsValue(close - st9);
def den10 = AbsValue(close - st10);
def den11 = AbsValue(close - st11);
def den12 = AbsValue(close - st12);
def den13 = AbsValue(close - st13);
def den14 = AbsValue(close - st14);
def den15 = AbsValue(close - st15);
def den16 = AbsValue(close - st16);
def den17 = AbsValue(close - st17);
def den18 = AbsValue(close - st18);
def den19 = AbsValue(close - st19);

def den_ = den0 + den1 + den2 + den3 + den4 + den5 + den6 + den7 + den8 + den9 + den10 +
          den11 + den12 + den13 + den14 + den15 + den16 + den17 + den18 + den19;
def den = den_;
#/Outputs
def median = norm(difMid, min, max, den, normalize);
def stdev = if !maxmin then  norm(difStdev, min, max, den, normalize) else na;

#//Plots
#//Median
def medCol = if maxmin then 0.5 else 0;
plot medLine = median;#, 'Median'
medLine.AssignValueColor(if median > medCol then GlobalColor("mup") else GlobalColor("mdn"));
#//Stdev Area
def up = stdev;
def dn = -stdev;

AddCloud(up, dn, Color.GRAY);    # 'Stdev Area'
AddCloud(up, dn, Color.DARK_GRAY);    # 'Stdev Area'

#//Mesh
plot stOsc0 = if mesh then norm(diffs0, min, max, den, normalize) else na;
plot stOsc1 = if mesh then norm(diffs1, min, max, den, normalize) else na;
plot stOsc2 = if mesh then norm(diffs2, min, max, den, normalize) else na;
plot stOsc3 = if mesh then norm(diffs3, min, max, den, normalize) else na;
plot stOsc4 = if mesh then norm(diffs4, min, max, den, normalize) else na;
plot stOsc5 = if mesh then norm(diffs5, min, max, den, normalize) else na;
plot stOsc6 = if mesh then norm(diffs6, min, max, den, normalize) else na;
plot stOsc7 = if mesh then norm(diffs7, min, max, den, normalize) else na;
plot stOsc8 = if mesh then norm(diffs8, min, max, den, normalize) else na;
plot stOsc9 = if mesh then norm(diffs9, min, max, den, normalize) else na;
plot stOsc10 = if mesh then norm(diffs10, min, max, den, normalize) else na;
plot stOsc11 = if mesh then norm(diffs11, min, max, den, normalize) else na;
plot stOsc12 = if mesh then norm(diffs12, min, max, den, normalize) else na;
plot stOsc13 = if mesh then norm(diffs13, min, max, den, normalize) else na;
plot stOsc14 = if mesh then norm(diffs14, min, max, den, normalize) else na;
plot stOsc15 = if mesh then norm(diffs15, min, max, den, normalize) else na;
plot stOsc16 = if mesh then norm(diffs16, min, max, den, normalize) else na;
plot stOsc17 = if mesh then norm(diffs17, min, max, den, normalize) else na;
plot stOsc18 = if mesh then norm(diffs18, min, max, den, normalize) else na;
plot stOsc19 = if mesh then norm(diffs19, min, max, den, normalize) else na;

plot zeroLine = if !last and !mesh then 0 else na;
zeroLine.SetStyle(Curve.SHORT_DASH);
zeroLine.SetDefaultColor(Color.GRAY);
stOsc0.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc1.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc3.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc4.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc5.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc6.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc7.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc8.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc9.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc10.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc11.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc12.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc13.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc14.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc15.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc16.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc17.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc18.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
stOsc19.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

stOsc0.AssignValueColor(if diffs0 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc1.AssignValueColor(if diffs1 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc2.AssignValueColor(if diffs2 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc3.AssignValueColor(if diffs3 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc4.AssignValueColor(if diffs4 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc5.AssignValueColor(if diffs5 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc6.AssignValueColor(if diffs6 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc7.AssignValueColor(if diffs7 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc8.AssignValueColor(if diffs8 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc9.AssignValueColor(if diffs9 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc10.AssignValueColor(if diffs10 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc11.AssignValueColor(if diffs11 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc12.AssignValueColor(if diffs12 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc13.AssignValueColor(if diffs13 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc14.AssignValueColor(if diffs14 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc15.AssignValueColor(if diffs15 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc16.AssignValueColor(if diffs16 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc17.AssignValueColor(if diffs17 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc18.AssignValueColor(if diffs18 > 0 then GlobalColor("up") else  GlobalColor("dn"));
stOsc19.AssignValueColor(if diffs19 > 0 then GlobalColor("up") else  GlobalColor("dn"));

#-- END of CODE
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
363 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top