mod note:
(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:
This indicator is too complex for use in scans, watchlists, and conditional orders

(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: