#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Ankit_1618
#study("Triangular Stoch RSI Bands", overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 04/2024
input Source = close; #, title="RSI Source")
input atrMulit = 0.89;
input pivotLength1 = 9;
input pivotLength2 = 26;
input dynamicLength = 20; #, "dynamic Length")
input smoothK = 3; #, "K", minval=1)
input smoothD = 3; #, "D", minval=1)
input rsiLength = 14; #, "RSI Length", minval=1)
input StochasticLength = 14; #, "Stochastic Length", minval=1)
input overboughtLevel = 80;
input oversoldLevel = 20;
def na = Double.NaN;
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 14;
def hh = Highest(h, len);
def ll = Lowest(l, len);
def stoch = 100 * (src - ll) / (hh - ll);
plot return = stoch;
}
#-- TMA
script tma {
input src = close;
input length = 20;
def ema1 = ExpAverage(src, length);
def ema2 = ExpAverage(ema1, length);
def ema3 = ExpAverage(ema2, length);
plot tma = ema3;
}
def nATR = ATR(Length = 14);
def nATR1 = Min(Source * 0.3/100, nATR);
def hh1 = Highest(high, pivotLength1);
def hh2 = Highest(high, pivotLength2);
def ll1 = Lowest(low, pivotLength1);
def ll2 = Lowest(low, pivotLength2);
def pivot1 = (hh1 + ll1) / 2;
def pivot2 = (hh2 + ll2) / 2;
def cum = (pivot1 + pivot2) / 2;
def adjust_n = cum - atrMulit * nATR;
def adjust_p = cum + atrMulit * nATR;
def Dynamic_Long = tma(adjust_n, dynamicLength);
def Dynamic_Short = tma(adjust_p, dynamicLength);
def rsi1 = RSI(Price = Source, Length = rsiLength);
def stoch1 = stoch(rsi1, rsi1, rsi1, StochasticLength);
def k = Average(stoch1, smoothK);
def d = Average(k, smoothD);
def uc_long_condition = Source > Dynamic_Long and Source > Dynamic_Short and (k crosses below oversoldLevel);
def uc_short_condition = Source < Dynamic_Long and Source < Dynamic_Short and (k crosses above overboughtLevel);
#/Calculations
#def trend_number = if Source>Dynamic_Short or Source<Dynamic_Long then trend_number[1] + 1 else 0;
def highs = if uc_long_condition then high else if highs[1] then highs[1] else hh2;
def lows = if uc_short_condition then low else if lows[1] then lows[1] else ll2;
def tma_highs = tma(if !isNAN(highs) then highs else hh2,dynamicLength);
def tma_lows = tma(if !isNaN(lows) then lows else ll2,dynamicLength);
def base = (tma_highs+tma_lows)/2;
plot tmaHi = tma_highs; #, color=color.green)
plot tmaLo = tma_lows; #, color=color.red)
plot baseLine = base; #, color=color.gray, transp=50)
tmaHi.SetDefaultColor(Color.GREEN);
tmaLo.SetDefaultColor(Color.RED);
baseLine.SetDefaultColor(Color.GRAY);
#//Plotting LEVELS
input showBand = yes;
input bandLvl1 = 0.236; #, " DEPTH 1")
input bandLvl2 = 0.382; #, " DEPTH 2")
input bandLvl3 = 0.500; #, " DEPTH 3")
input bandLvl4 = 0.618; #, " DEPTH 4")
def diff = if showBand then WildersAverage(tma_highs - tma_lows, dynamicLength) else na;
def upper1 = tma_highs + diff * bandLvl1;
def upper2 = tma_highs + diff * bandLvl2;
def upper3 = tma_highs + diff * bandLvl3;
def upper4 = tma_highs + diff * bandLvl4;
def lower1 = tma_lows - diff * bandLvl1;
def lower2 = tma_lows - diff * bandLvl2;
def lower3 = tma_lows - diff * bandLvl3;
def lower4 = tma_lows - diff * bandLvl4;
def up1 = upper1;
def up2 = upper2;
def up3 = upper3;
def up4 = upper4;
def lo1 = lower1;
def lo2 = lower2;
def lo3 = lower3;
def lo4 = lower4;
DefineGlobalColor("Green1" , CreateColor(4,181,4));
DefineGlobalColor("Green2" , CreateColor(3,145,3));
DefineGlobalColor("Green3" , CreateColor(2,117,2));
DefineGlobalColor("Green4" , CreateColor(1,93,1));
DefineGlobalColor("Red1" , CreateColor(184,4,4));
DefineGlobalColor("Red2" , CreateColor(145,3,3));
DefineGlobalColor("Red3" , CreateColor(117,2,2));
DefineGlobalColor("Red4" , CreateColor(100,1,1));
AddCloud(up4, up3, GlobalColor("Green1"), GlobalColor("Green1"), yes);
AddCloud(up3, up2, GlobalColor("Green2"), GlobalColor("Green2"), yes);
AddCloud(up2, up1, GlobalColor("Green3"), GlobalColor("Green3"), yes);
AddCloud(up1, tma_highs, GlobalColor("Green4"));
AddCloud(lo3, lo4, GlobalColor("Red1"), GlobalColor("Red1"), yes);
AddCloud(lo2, lo3, GlobalColor("Red2"), GlobalColor("Red2"), yes);
AddCloud(lo1, lo2, GlobalColor("Red3"), GlobalColor("Red3"), yes);
AddCloud(tma_lows, lo1, GlobalColor("Red4"));
#AddChartBubble(trend_number==1, close, "TT", Color.WHITE);
#-- END of CODE