Check the video to know how to use the indicator.
CODE:
CSS:
# https://www.tradingview.com/v/JAtirpgd/
#// This source code is subject to the terms of the Mozilla Public License
#// © loxx
#indicator("FDI-Adaptive Supertrend w/ Floating Levels [Loxx]"
# converted and mod by Sam4Cok@Samer800 - 10 / 2023
input useChartTimeframe = {Default "Yes", "No"};
input customTimeframe = AggregationPeriod.FIFTEEN_MIN;
input Source = FundamentalType.HL2;# "Source"
input FractalPeriodIngest = 30; # "Fractal Period Ingest"
input speed = 20; # "Speed"
input Multiplier = 3.0; # "Multiplier"
input MakeItAdaptive = yes; # "Make it adaptive?"
input FloatingLevelLookbackPeriod = 25; # "Floating Level Lookback Period"
input FloatingUpLevelPercentage = 80; # "Floating Levels Up Level %"
input FloatingDownLevelPercentage = 20; # "Floating Levels Down Level %"
input colorBars = yes; # "Color bars?"
input ShowFloatingLevels = yes; # "Show Floating Levels?"
input FillFloatingLevels = yes; # "Fill Floating Levels?"
input showSignals = yes; # "Show signals?"
def na = Double.NaN;
def per = FractalPeriodIngest;
def flLookBack = FloatingLevelLookbackPeriod;
def flLevelUp = FloatingUpLevelPercentage;
def flLevelDown = FloatingDownLevelPercentage;
def showfloat = ShowFloatingLevels;
def showfill = FillFloatingLevels;
def src = Fundamental(Source);
def sTF = Fundamental(Source, Period = customTimeframe);
def hTF = Fundamental(FundamentalType.HIGH, Period = customTimeframe);
def lTF = Fundamental(FundamentalType.LOW, Period = customTimeframe);
def cTF = Fundamental(FundamentalType.CLOSE, Period = customTimeframe);
#def lTF = Fundamental(FundamentalType.LOW, Period = customTimeframe);
def sMTF; def hMTF; def lMTF; def cMTF;
Switch (useChartTimeframe) {
Case "No" :
sMTF = sTF;
hMTF = hTF;
lMTF = lTF;
cMTF = cTF;
Default :
sMTF = src;
hMTF = high;
lMTF = low;
cMTF = close;
}
#-- Color
DefineGlobalColor("10", CreateColor(0,165,255));
DefineGlobalColor("20", CreateColor(0,232,152));
DefineGlobalColor("80", CreateColor(255,0,56));
DefineGlobalColor("90", CreateColor(255,0,148));
script RMA {
input x = close;
input t = 14;
def ema = CompoundValue(1, (x - EMA[1]) * (1 / t) + EMA[1], x);
plot out = ema;
}
#fdip(float src, int per, int speedin)=>
script fdip {
input src = close;
input per = 14;
input speedin = 20;
def fmax = Highest(src, per);
def fmin = Lowest(src, per);
def diff = fold j = 1 to per with q do
(src[j] - fmin) / (fmax - fmin);
def length = fold i = 1 to per with p do
if i > 0 then p +
Sqrt(Power(diff[i] - GetValue(diff, i + 1), 2) + (1 / Power(per, 2))) else p;
def fdi = 1 + (Log(length) + Log(2)) / Log(2 * per);
def traildim = 1 / (2 - fdi);
def alpha = traildim / 2;
def speed = Round(speedin * alpha, 0);
plot out = speed;
}
#pine_supertrend(src, factor, atrPeriod) =>
script SuperTrend {
input src = hl2;
input factor = 3;
input atrPeriod = 14;
input h = high;
input l = low;
input c = close;
def tr = TrueRange(h, c, l);
def nATR = RMA(tr, atrPeriod);
def up = src + factor * nATR;
def dn = src - factor * nATR;
def lowerBand;
def upperBand;
def up1 = if upperBand[1] then upperBand[1] else up;
def dn1 = if lowerBand[1] then lowerBand[1] else dn;
upperBand = if (up < up1) or (c[1] > up1) then up else up1;
lowerBand = if (dn > dn1) or (c[1] < dn1) then dn else dn1;
def trend;# = na
def superTrend;# = na
def prevSuperTrend = if superTrend[1] then superTrend[1] else up1;
if (IsNaN(up) or IsNaN(dn)) {
trend = 1;
} else
if prevSuperTrend == up1 {
trend = if c > upperBand then -1 else 1;
} else {
trend = if c < lowerBand then 1 else -1;
}
superTrend = if trend == -1 then lowerBand else upperBand;
plot ST = superTrend;
plot dir = trend;
}
def masterdom = fdip(sMTF, per, speed);
def len1 = if Floor(masterdom) < 1 then 1 else Floor(masterdom);
def len = if len1 then len1 else 1;
def lenType = if MakeItAdaptive then len else per;
def supertrend = SuperTrend(sMTF, Multiplier, lenType, hMTF,lMTF,cMTF).ST;
def direction = SuperTrend(sMTF, Multiplier, lenType, hMTF,lMTF,cMTF).DIR;
def mini = Lowest(supertrend, flLookBack);
def maxi = Highest(supertrend, flLookBack);
def rrange = maxi - mini;
def flu = mini + flLevelUp * rrange / 100.0;
def fld = mini + flLevelDown * rrange / 100.0;
def flm = mini + 0.5 * rrange;
plot top = if showfloat then flu else na; # "Top float"
plot bot = if showfloat then fld else na; # "bottom float"
plot mid = if showfloat then flm else na; # "Mid Float"
top.SetDefaultColor(Color.DARK_GREEN);
bot.SetDefaultColor(Color.DARK_RED);
mid.SetDefaultColor(Color.GRAY);
AddCloud(if !showfill then na else top, mid, Color.DARK_GREEN);
AddCloud(if !showfill then na else mid, bot, Color.DARK_RED);
def stNA = direction < 0;
plot STDn = if stNA then na else supertrend; # "Supertrend"
plot STUp = if stNA then supertrend else na;
STUp.SetLineWeight(2);
STDn.SetLineWeight(2);
STUp.SetDefaultColor(GlobalColor("10"));
STDn.SetDefaultColor(GlobalColor("90"));
# -- Signals
def goLong = direction == -1 and direction[1] == 1;
def goShort = direction == 1 and direction[1] == -1;
plot SigUp = if showsignals and goLong then low else na;
plot SigDn = if showsignals and goShort then high else na;
SigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else
if direction == -1 then GlobalColor("20") else GlobalColor("80"));
#--- END of CODE