#// Indicator for TOS
#// © KivancOzbilgic
#//created by: @Anil_Ozeksi
#//developer: ANIL ÖZEK??
# study(title="Stochastic Optimized Trend Tracker", shorttitle="SOTT",
# Converted by Sam4Cok@Samer800 - 09/2024
declare lower;
input timeframe = AggregationPeriod.MIN;
input MovAvgType = {default VAR, SMA, EMA, WMA, DEMA, TMA, WWMA, ZLEMA, TSF, HULL};
input periodK = 500; #, title="%K Length", minval=1)
input smoothK = 200; #, title="%K Smoothing", minval=1)
input ottPeriod = 2; #, "OTT Period", minval=1)
input ottPercent= 0.5; #, "OTT Percent", type=input.float, step=0.1, minval=0)
input showSupportLine = no; # "Show Support Line?"
input showBubbles = no; # "Show Support Line Crossing Signals?"
def na = Double.NaN;
def last = isNaN(close);
def current = GetAggregationPeriod();
def tf = Max(current, timeframe);
# theme #
DefineGlobalColor("Sky1" , Color.CYAN); # >=90
DefineGlobalColor("Sky2" , CreateColor(0,137,137)); # >=80
DefineGlobalColor("Magenta1" , Color.MAGENTA); # >=40
DefineGlobalColor("Magenta2" , Color.PLUM); # >=30
DefineGlobalColor("movAvg", CreateColor(5,133,225));
# 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 c1 = src - ll;
def c2 = hh - ll;
def stoch = if c2 != 0 then c1 / c2 * 100 else 0;
plot return = stoch;
}
#Var_Func(src, length) =>
script Var_Func {
input src = close;
input length = 0;
def valpha = 2 / (length + 1);
def vud1 = if src > src[1] then src - src[1] else 0;
def vdd1 = if src < src[1] then src[1] - src else 0;
def vUD = Sum(vud1, 9);
def vDD = Sum(vdd1, 9);
def vCMO1 = ((vUD - vDD) / (vUD + vDD));
def vCMO = if isNaN(vCMO1) then vCMO[1] else vCMO1;
def VAR = CompoundValue(1, valpha * AbsValue(vCMO) * src + (1 - valpha * AbsValue(vCMO)) * (VAR[1]), src);
plot result = if isNaN(close) then Double.NaN else VAR;
}
#Wwma_Func(src, length) =>
script Wwma_Func {
input src = close;
input length = 0;
def wwalpha = 1 / length;
def WWMA;
WWMA = wwalpha * src + (1 - wwalpha) * (WWMA[1]);
plot return = WWMA;
}
#Zlema_Func(src, length) =>
script Zlema_Func {
input src = close;
input length = 0;
def zxLag = if length / 2 == Round(length / 2) then length / 2 else (length - 1) / 2;
def zxEMAData = src + src - src[zxLag];
def ZLEMA = ExpAverage(zxEMAData, length);
plot return = ZLEMA;
}
#Tsf_Func(src, length) =>
script Tsf_Func {
input src = close;
input length = 0;
def lrc = Inertia(src, length);
def lrc1 = Inertia(src[1], length);
def lrs = lrc - lrc1;
def TSF = Inertia(src, length) + lrs;
plot retur = TSF;
}
#Wwma_Funcl(srcl, length) =>
script Wwma_Funcl {
input srcl = close;
input length = 0;
def wwalpha = 1 / length;
def WWMA;
WWMA = CompoundValue(1, wwalpha * srcl + (1 - wwalpha) * (WWMA[1]), srcl);
plot return = WWMA;
}
#Zlema_Funcl(srcl, length) =>
script Zlema_Funcl {
input srcl = close;
input length = 0;
def zxLag = if length / 2 == Round(length / 2,0) then length / 2 else (length - 1) / 2;
def zxEMAData = srcl + srcl - srcl[zxLag];
def ZLEMA = ExpAverage(zxEMAData, length);
plot return = ZLEMA;
}
#Tsf_Funcl(srcl, length) =>
script Tsf_Funcl {
input srcl = close;
input length = 0;
def lrc = Inertia(srcl, length);
def lrc1 = Inertia(srcl[1], length);
def lrs = lrc - lrc1;
def TSF = Inertia(srcl, length) + lrs;
plot retur = TSF;
}
#getMA(src, length, type) =>
script getMA {
input src = close;
input length = 100;
input type = "SMA";
def ma = if type == "SMA" then Average(src, length) else
if type == "EMA" then ExpAverage(src, length) else
if type == "WMA" then WMA(src, length) else
if type == "DEMA" then DEMA(src, length) else
if type == "TMA" then Average(Average(src, Ceil(length / 2)), Floor(length / 2) + 1) else
if type == "VAR" then Var_Func(src, length) else
if type == "WWMA" then WWMA_Func(src, length) else
if type == "ZLEMA" then Zlema_Func(src, length) else
if type == "TSF" then Tsf_Func(src, length) else
if type == "HULL" then HullMovingAvg(src, length) else Var_Func(src, length);
plot result = ma;
}
def stoch = stoch(close(Period = tf), high(Period = tf), low(Period = tf), periodK);
def k = getMA(stoch, smoothK, MovAvgType);
def src = k + 1000;
def MAvg = getMA(src, ottPeriod, MovAvgType);
def fark = MAvg * ottPercent * 0.01;
def up = MAvg - fark;
def longStop;
def longStopPrev = CompoundValue(1, if longStop[1] then longStop[1] else up, up);
longStop = if MAvg > longStopPrev then max(up, longStopPrev) else up;
def dn = MAvg + fark;
def shortStop;
def shortStopPrev = CompoundValue(1, if shortStop[1] then shortStop[1] else dn, dn);
shortStop = if MAvg < shortStopPrev then min(dn, shortStopPrev) else dn;
def dir; # = 1
def dir1 = compoundValue(1, if dir[1] then dir[1] else 1, 1);
dir = if dir1 == -1 and MAvg > shortStopPrev then 1 else
if dir1 == 1 and MAvg < longStopPrev then -1 else dir1;
def MT = if dir==1 then longStop else shortStop;
def OTT = if MAvg > MT then MT * (200 + ottPercent) / 200 else MT * (200 - ottPercent) / 200;
def buySignalc = (src Crosses Above OTT[2]);
def sellSignallc = (src Crosses Below OTT[2]);
plot kLine = src;
plot SupportLine = if !showSupportLine then na else MAvg; # "Support Line"
plot ottLine = if !last and OTT[2] then OTT[2] else na;
ottLine.SetLineWeight(2);
SupportLine.SetDefaultColor(GlobalColor("movAvg"));
ottLine.SetDefaultColor(GlobalColor("Magenta1"));
plot h0 = if last then na else 1080;
plot md = if last then na else 1050;
plot h1 = if last then na else 1020;
md.SetDefaultColor(Color.GRAY);
h0.SetDefaultColor(Color.GRAY);
h1.SetDefaultColor(Color.GRAY);
md.SetStyle(Curve.SHORT_DASH);
AddCloud(h0, h1, Color.DARK_GRAY);
#--- sig
AddChartBubble(showBubbles and buySignalc, OTT, "B", Color.GREEN, no);
AddChartBubble(showBubbles and sellSignallc, OTT, "S", Color.RED);
### END of CODE