PDF-MA Supertrend [BackQuant] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
mod note:
This indicator is too complex for use in scans, watchlists, and conditional orders

8LizUf3.png


(Modified Script)
Author Message:
The PDF-MA Supertrend combines the innovative Probability Density Function (PDF) smoothing with the widely popular Supertrend methodology, creating a robust tool for identifying trends and generating actionable trading signals. This indicator is designed to provide precise entries and exits by dynamically adapting to market volatility while visualizing long and short opportunities directly on the chart.

CODE:

CSS:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input timeframe = AggregationPeriod.MIN;
input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 1.7; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = yes;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source, Period = tf);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else high(Period = tf);
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else low(Period = tf);


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high(Period = tf)[1]) then (high(Period = tf) - low(Period = tf)) else
            TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrPeriod);

def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GRAY);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE
 
Last edited by a moderator:
Everything You Wanted To Know About This Indicator
This indicator is too complex for use in scans, watchlists, and conditional orders
Schwab purposefully throttles complex script use in scans, watchlists, and conditional orders to prevent high load runs on its servers.

Does Not Repaint: while the current candle will update until is closes; the previous candles do not repaint unless you are using the MTF function

This is a SuperTrend Indicator --
This study was ported over from Tradingview. Here are the notes from the author:
https://www.tradingview.com/script/YuIn2PIz-PDF-MA-Supertrend-BackQuant/

What is SuperTrend? How do you use it? What timeframes work best?
Find the answers to all supertrend questions here:
https://usethinkscript.com/threads/supertrend-indicator-by-mobius-for-thinkorswim.7/#post-77876
 
Last edited:
8LizUf3.png


(Modified Script)
Author Message:
The PDF-MA Supertrend combines the innovative Probability Density Function (PDF) smoothing with the widely popular Supertrend methodology, creating a robust tool for identifying trends and generating actionable trading signals. This indicator is designed to provide precise entries and exits by dynamically adapting to market volatility while visualizing long and short opportunities directly on the chart.

CODE:

CSS:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input timeframe = AggregationPeriod.MIN;
input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 1.7; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = yes;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source, Period = tf);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else high(Period = tf);
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else low(Period = tf);


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high(Period = tf)[1]) then (high(Period = tf) - low(Period = tf)) else
            TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrPeriod);

def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GRAY);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE
Is this scannable? If so, what key variables, do I set the filter for? Thanks again Samer for your help on all the coding you do for Think Script. I have used many of your codes for various timeframes.
 
How does one scan for entry signals? Thanks,

Yes, I understand the problem with complex scripts for scanning. Perhaps a workaround would be isolating the enter signal and attaching a bar count script that could be viewed in a watchlist column.
 
mod note:
This indicator is too complex for use in scans, watchlists, and conditional orders

Schwab purposefully throttles complex script use in scans, watchlists, and conditional orders to prevent high load runs on its servers.

Yes, I understand the problem with complex scripts for scanning. Perhaps a workaround would be isolating the enter signal and attaching a bar count script that could be viewed in a watchlist column.

The code required to create the enter signal is too complex.
Sadly no, there is no way to make it less complex

@briand @TechGuy
 
I like the 2 and 2.3, so I put them together.

1738810717556.png


Code:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input timeframe = AggregationPeriod.MIN;
input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 2.0; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input supertrendFactor2 = 2.3; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = no;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source, Period = tf);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else high(Period = tf);
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else low(Period = tf);


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high(Period = tf)[1]) then (high(Period = tf) - low(Period = tf)) else
            TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrPeriod);

#Line 1
def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

#Line 2
def st02    = f_supertrend(pdf_ma_out, nATR, supertrendFactor2, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend02 = f_supertrend(pdf_ma_out, nATR, supertrendFactor2, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st12    = supertrend(supertrendFactor2, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend12 = supertrend(supertrendFactor2, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st2 = if useFakeoutOption then st02 else st12;
def trend2 = if useFakeoutOption then trend02 else trend12;

plot stBull2 = if showST and trend2 == 1 then st2 else na;  # "Suptrend Bullish"
plot stBear2 = if !showST or trend2 == 1 then na else st2;  # "Suptrend Bearish"

stBull2.SetLineWeight(2);
stBear2.SetLineWeight(2);
stBull2.SetDefaultColor(Color.CYAN);
stBear2.SetDefaultColor(Color.MAGENTA);

AddCloud(stBull, stBull2, Color.cyan, Color.cyan);
AddCloud(stBear, stBear2, Color.MAGENTA, Color.MAGENTA);

#AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
#AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GREEN);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(CREATECOLOR(255,204,0));
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
#AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
#AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE
 
8LizUf3.png


(Modified Script)
Author Message:
The PDF-MA Supertrend combines the innovative Probability Density Function (PDF) smoothing with the widely popular Supertrend methodology, creating a robust tool for identifying trends and generating actionable trading signals. This indicator is designed to provide precise entries and exits by dynamically adapting to market volatility while visualizing long and short opportunities directly on the chart.

CODE:

CSS:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input timeframe = AggregationPeriod.MIN;
input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 1.7; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = yes;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source, Period = tf);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else high(Period = tf);
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else low(Period = tf);


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high(Period = tf)[1]) then (high(Period = tf) - low(Period = tf)) else
            TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrPeriod);

def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GRAY);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE
For some reason, TOS is rejecting "input timeframe = AggregationPeriod.MIN;" ?
 
8LizUf3.png


(Modified Script)
Author Message:
The PDF-MA Supertrend combines the innovative Probability Density Function (PDF) smoothing with the widely popular Supertrend methodology, creating a robust tool for identifying trends and generating actionable trading signals. This indicator is designed to provide precise entries and exits by dynamically adapting to market volatility while visualizing long and short opportunities directly on the chart.

CODE:

CSS:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input timeframe = AggregationPeriod.MIN;
input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 1.7; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = yes;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();
def tf = Max(cap, timeframe);
def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source, Period = tf);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else high(Period = tf);
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close(Period = tf) else low(Period = tf);


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high(Period = tf)[1]) then (high(Period = tf) - low(Period = tf)) else
            TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def nATR = WildersAverage(tr, atrPeriod);

def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close(Period = tf)[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GRAY);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE

Can you do the timer down to Ticks? I use 133tks on futures as well as minutes
 
Can you do the timer down to Ticks? I use 133tks on futures as well as minutes

There is nothing better for analyzing price action than tick charts; esp futures & indices

But heads up tick/range/renko charts don’t do time! So any study that relies on time? Yeah, won’t work.

@samer800’s conversions are 🔥 but they use time to plot on higher timeframes which means no-go for tick/range/renko.

Wanna run @samer800 studies on those charts?
You gotta strip out all time references.
This script had 17 time-based lines took ‘em out; now it runs smooth on @mnunez021 's 133 tick chart 🚀

xkmqFgC.png


Here is @samer800 PDF-MA Supertrend [BackQuant] for ThinkOrSwim with all time references removed:
Ruby:
#// Indicator for TOS
#// © BackQuant
# indicator( "PDF-MA Supertrend [BackQuant]", "PDFMA ST [BackQuant]",
#-- Converted by Sam4Cok@Samer800     - 01/2025

input displayOptions = {default "Supertrend", "Bar Color", "Supertrend & Bar Color"};
input source = FundamentalType.CLOSE; #, "Price Source", group=inputs)
input smoothingMethod = AverageType.EXPONENTIAL; # "Smoothing Method", options=["EMA", "SMA"], group=inputs)
input smoothingPeriod = 20; # "Smoothing Period", minval=1, group=inputs)
input pdfVariance = 1.5; # "PDF Variance", step=0.1, minval=0.1, group=inputs)
input pdfMean = 0.0; # "PDF Mean", step=0.1, minval=-1.0, maxval=1.0, group=inputs)
input atrPeriod = 12; # "ATR Period", group = "Supertrend", inline = "ST")
input supertrendFactor = 1.7; # "Factor", group = "Supertrend", inline = "ST", step = 0.01)
input useFakeoutOption = yes; # "Show Supertrend on chart?", group = "UI Settings")
input FakeoutBreakType = {"High/Low",default "Candle Close"};     # "Fakeout Type"
input FakoutAtrMult = 1.5;       # "Fakout ATR Mult"
input FakeoutCountLimit = 5;     # "Fakeout Index Limit"
input showFibonacciLines = yes;
input extendFiboLinesBy = 5; #, "Extend", group = "Fibonacci",
input fib1 = 0.236; #, "1")*100
input fib2 = 0.382; #, "2")*100
input fib3 = 0.618; #, "3")*100
input fib4 = 0.786; #, "4")*100

def na = Double.NaN;
def last = IsNaN(close);
def cap = GetAggregationPeriod();

def showST = displayOptions == displayOptions."Supertrend" or displayOptions == displayOptions."Supertrend & Bar Color";
def colorBars = displayOptions == displayOptions."Bar Color" or displayOptions == displayOptions."Supertrend & Bar Color";
def src = Fundamental(source);
def highSrc = if FakeoutBreakType==FakeoutBreakType."Candle Close" then close else high;
def lowSrc =  if FakeoutBreakType==FakeoutBreakType."Candle Close" then close else low;


script pdf_ma {
    input src = close;
    input period = 20;
    input diff = 1.5;
    input offset = 0;
    input method = AverageType.EXPONENTIAL;
    def pi = Double.Pi;
    def variance = Max(diff, 0.1);
    def mean = Min(Max(offset, -1), 1);
    def step = pi / (period - 1);
    def sum_weights = fold k = 0 to period with p do
                    p + (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((k * step) - mean) / (2 * Sqr(variance))));
    def weighted_sum = fold i = 0 to period with q do
                  q + src[i] * (1 / (variance * Sqrt(2 * pi)) * Exp(- Sqr((i * step) - mean) / (2 * Sqr(variance))));
    def weighted_avg = weighted_sum / sum_weights;
    def ma = MovingAverage(method, src, period);
    def subject = (weighted_avg + ma) / 2;
    plot out = subject;
}
Script supertrend {
input factor = 3;
input atr = close;
input src = hl2;
input highSrc = high;
input lowSrc = low;
input closeSrc = close;
    def upBand = src + atr * factor;
    def loBand = src - atr * factor;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or closeSrc < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or closeSrc > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if highSrc > upperBand then -1 else 1;
    } else {
        _direction = if lowSrc < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if isNaN(superTrend) then Double.NaN else superTrend;
    plot dir = if isNaN(_direction) then Double.NaN else _direction;
    }
script f_supertrend {
    input src = close;
    input nATR = 1;
    input factor = 1.7;
    input highSrc = high;
    input lowSrc = low;
    input FAKEOUT_INDEX_LIMIT = 5;
    input FAKEOUT_ATR_MULT = 1.5;
    def na = Double.NaN;
    def lower = src - nATR * factor;
    def upper = src + nATR * factor;
    def state = {default ini, Bull, Bear};
    def dnLine;
    def upLine;
    def fakeoutIndex;
    def fakeoutSt;
    def dn = if IsNaN(dnLine[1]) then lower else if !dnLine[1] then lower else dnLine[1];
    def up = if IsNaN(upLine[1]) then upper else if !upLine[1] then upper else upLine[1];
    def dnLine1 = if state[1] == state.Bull and dn > lower then dn else lower;
    def upLine1 = if state[1] == state.Bear and up < upper then up else upper;
    switch (state[1]) {
    case Bull :
        if lowSrc < dnLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = upLine1;
                fakeoutIndex = 0;
            } else  if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((dnLine[1] - lowSrc) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bear;
                upLine = fakeoutSt[1];
                dnLine = dnLine1;
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    case Bear:
        if highSrc > upLine[1] {
            if (IsNaN(fakeoutIndex[1]) or IsNaN(fakeoutSt[1])) {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = dnLine1;
                fakeoutIndex = 0;
            } else if (fakeoutIndex[1] >= FAKEOUT_INDEX_LIMIT) or ((highSrc - upLine[1]) > nATR * FAKEOUT_ATR_MULT) {
                state = state.Bull;
                upLine = upLine1;
                dnLine = fakeoutSt[1];
                fakeoutSt = na;
                fakeoutIndex = na;
            } else {
                state = state[1];
                upLine = upLine[1];
                dnLine = dnLine[1];
                fakeoutSt = fakeoutSt[1];
                fakeoutIndex = fakeoutIndex[1] + 1;
            }
        } else {
            state = state[1];
            upLine = upLine1;
            dnLine = dnLine1;
            fakeoutSt = na;
            fakeoutIndex = na;
        }
    default :
        dnLine = dnLine1;
        upLine = upLine1;
        state = state.Bull;
        fakeoutSt = na;
        fakeoutIndex = na;
}
    plot st  = if state == state.Bull then dnLine else upLine;
    plot dir = if state == state.Bear then -1 else 1;
}

def pdf_ma_out = pdf_ma(src, smoothingPeriod, pdfVariance, pdfMean, smoothingMethod);

def tr = if IsNaN(high[1]) then (high - low) else
            TrueRange(high, close, low);
def nATR = WildersAverage(tr, atrPeriod);

def st0    = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).st;
def trend0 = f_supertrend(pdf_ma_out, nATR, supertrendFactor, highSrc, lowSrc, FakeoutCountLimit, FakoutAtrMult).dir;
def st1    = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close[1]).st;
def trend1 = supertrend(supertrendFactor, nATR, pdf_ma_out, highSrc, lowSrc, close[1]).dir;

def st = if useFakeoutOption then st0 else st1;
def trend = if useFakeoutOption then trend0 else trend1;

plot stBull = if showST and trend == 1 then st else na;  # "Suptrend Bullish"
plot stBear = if !showST or trend == 1 then na else st;  # "Suptrend Bearish"

stBull.SetLineWeight(2);
stBear.SetLineWeight(2);
stBull.SetDefaultColor(Color.CYAN);
stBear.SetDefaultColor(Color.MAGENTA);

AddCloud(if showST and trend == 1 then ohlc4 else na, st, CreateColor(0, 47, 47), Color.DARK_GRAY);
AddCloud(if !showST or trend == 1 then na else st , ohlc4, CreateColor(47, 0, 47), Color.DARK_GRAY);

#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend == 1 then if close > st then Color.GREEN else Color.DARK_GREEN else
                 if close < st then Color.RED else Color.DARK_RED);

#// Creat Fib Lines at the bottom and the top



def v_236 = fib1 * 100;
def v_382 = fib2 * 100;
def v_618 = fib3 * 100;
def v_786 = fib4 * 100;

def atr = ATR(Length = 200);
def bar = if !last then bar[1] + 1 else bar[1];
def change = trend != trend[1];
def lastTrend = if change then bar - 1 else lastTrend[1];
def lastBar = if !last then HighestAll(lastTrend) else lastBar[1];

def fibLo;
def fibHi;
def barLo;
def barHi;
if showFibonacciLines and bar >= lastBar {
    if trend == -1 and change {
        fibLo = low;
        fibHi = high + atr * 3;
        barLo = 0;
        barHi = 0;
    } else if trend == 1 and change {
        fibLo = high;
        fibHi = low - atr * 3;
        barLo = 0;
        barHi = 0;
    } else {
        if !last {
            fibLo = if !fibLo[1] then If(trend == -1, low, high) else Min(low, fibLo[1]);
            fibHi = if !fibHi[1] then If(trend == -1, high + atr * 3, low - atr * 3) else Max(high, fibHi[1]);
        } else {
            fibLo = if fibLo[1] then fibLo[1] else If(trend == -1, low, high) ;
            fibHi = if fibHi[1] then fibHi[1] else If(trend == -1, high + atr * 3, low - atr * 3);
        }
        barLo = if low == fibLo then bar else 0;
        barHi = if high == fibHi then bar else 0;
    }
} else {
    fibLo = na;
    fibHi = na;
    barLo = 0;
    barHi = 0;
}

#plot tt = fibLo;
def fibLL = if fibLo then LowestAll(fibLo) else fibLL[1];
def fibHH = if fibHi then HighestAll(fibHi) else fibHH[1];

def val0;
def val1;
if !last[extendFiboLinesBy] {
    if trend == -1 {
        val0 = fibHH;
        val1 = fibLL;
    } else if trend == 1 {
        val0 = fibLL;
        val1 = fibHH;
    } else {
        val0 = val0[1];
        val1 = val1[1];
    }
} else {
    val0 = na;
    val1 = na;
}
def lastBarLo = if barLo then HighestAll(barLo) else lastBarLo[1];
def lastBarHi = if barHi then HighestAll(barHi) else lastBarHi[1];
def size_step = (fibHH - fibLL) / 100;

plot line = if bar == lastBarLo then low else
            if bar == lastBarHi then high else na;
line.EnableApproximation();
line.SetStyle(Curve.MEDIUM_DASH);
line.SetDefaultColor(Color.CYAN);

plot fiboLo = if val0 then val0 else na; #fibLL;
plot fiboHi = if val1 then val1 else na; #fibHH;
plot fiboLvL1 = val0 - size_step * v_236 * trend * -1;
plot fiboLvL2 = val0 - size_step * v_382 * trend * -1;
plot fiboMid  = val0 - size_step * 50    * trend * -1;
plot fiboLvL3 = val0 - size_step * v_618 * trend * -1;
plot fiboLvL4 = val0 - size_step * v_786 * trend * -1;

fiboLo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboHi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboLvL4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fiboMid.SetPaintingStrategy(PaintingStrategy.DASHES);
fiboLo.SetDefaultColor(Color.LIGHT_GRAY);
fiboHi.SetDefaultColor(Color.LIGHT_GRAY);
fiboMid.SetDefaultColor(Color.GRAY);
fiboLvL1.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL2.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL3.SetDefaultColor(Color.LIGHT_GRAY);
fiboLvL4.SetDefaultColor(Color.LIGHT_GRAY);

#-- Clouds

AddCloud(fiboLvL3, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
AddCloud(fiboMid, fiboLvL4, Color.DARK_GRAY, Color.DARK_GRAY);
#AddCloud(hl2,  stUp,  CreateColor(0, 47, 0));
#AddCloud(stDn,  hl2,  CreateColor(57, 0, 0));

#-- Bubbles
AddChartBubble(bar == lastBarLo, low, AsDollars(low), Color.CYAN, no);
AddChartBubble(bar == lastBarHi, high, AsDollars(high), Color.CYAN);

#-- END of CODE
 
Last edited:
there is timeframe options to chose, so does it mean this one is MTF support?

Yes, @samer800 creates his indicators so that you can add a secondary aggregation
You can choose your preferred timeframe in settings.
 
Last edited by a moderator:
Perhaps I missed it, but is there a description on the features and use of this study? Thanks.
 
Last edited by a moderator:
Everything You Wanted To Know About This Indicator
This indicator is too complex for use in scans, watchlists, and conditional orders
Schwab purposefully throttles complex script use in scans, watchlists, and conditional orders to prevent high load runs on its servers.

Does Not Repaint: while the current candle will update until is closes; the previous candles do not repaint unless you are using the MTF function

This is a SuperTrend Indicator --
This study was ported over from Tradingview. Here are the notes from the author:
https://www.tradingview.com/script/YuIn2PIz-PDF-MA-Supertrend-BackQuant/

What is SuperTrend? How do you use it? What timeframes work best?
Find the answers to all supertrend questions here:
https://usethinkscript.com/threads/supertrend-indicator-by-mobius-for-thinkorswim.7/#post-77876

@Tek @CDG @jdlpnw
 

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
404 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