Optimized Trend Tracker OTT for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
cigEudy.png


this is based on OTT code from @KivancOzbilgic.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at http
#// © KivancOzbilgic
#//created by: @Anil_Ozeksi
#//developer: ANIL ÖZEK??
#study("Optimized Trend Tracker","OTT", overlay=true)
# Converted and mod by Sam4Cok@Samer800 11/2022
declare upper;
input BarColor  = no;
input ShowCloud = yes;      #"Highlighter On/Off ?"
input MovAvgType = {default VAR, SMA, EMA, WMA, DEMA, TMA, WWMA, ZLEMA, TSF, HULL};
input Source    = close;#(close, title="Source")
input OttLength = 2;        # OTT Period
input OttCoeff  = 1.4;      # OTT Optimization Coeff
input showSupportLine = no; # "Show Support Line?"
input showBubbles = yes;    # "Show Support Line Crossing Signals?"
input ColorOttLine = yes;    #="Show OTT Color Changes?"

def na = Double.NaN;

def c = Source;
def h = high;
def l = low;
def mPlot = ohlc4;
############## theme 3########################################
DefineGlobalColor("Sky1" , CreateColor(0,221,255));  # >=90
DefineGlobalColor("Sky2" , CreateColor(4,188,217));  # >=80
DefineGlobalColor("Sky3" , CreateColor(4,156,179));  # >=70
DefineGlobalColor("Sky4" , CreateColor(4,127,145));  # >=60
DefineGlobalColor("Sky5" , CreateColor(4,103,117));  # >=50

DefineGlobalColor("Magenta1" , CreateColor(216,0,255));  # >=40
DefineGlobalColor("Magenta2" , CreateColor(187,4,219));  # >=30
DefineGlobalColor("Magenta3" , CreateColor(155,5,181));  # >=20
DefineGlobalColor("Magenta4" , CreateColor(123,3,143));  # >=10
DefineGlobalColor("Magenta5" , CreateColor(100,2,117));  # >=0
#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 vCMO = ((vUD - vDD) / (vUD + vDD));
    def VAR;
    VAR = valpha * AbsValue(vCMO) * src + (1 - valpha * AbsValue(vCMO)) * (VAR[1]);
    plot result = 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;
}
#Var_Funcl(srcl, length) =>
script Var_Funcl {
    input srcl = close;
    input length = 0;
    def valphal = 2 / (length + 1);
    def vud1l = if srcl > srcl[1] then srcl - srcl[1] else 0;
    def vdd1l = if srcl < srcl[1] then srcl[1] - srcl else 0;
    def vUDl = Sum(vud1l, 9);
    def vDDl = Sum(vdd1l, 9);
    def vCMOl = ((vUDl - vDDl) / (vUDl + vDDl));
    def VARl;
    VARl = (valphal * AbsValue(vCMOl) * srcl) + (1 - valphal * AbsValue(vCMOl)) * (VARl[1]);
    plot return = VARl;
}
#Wwma_Funcl(srcl, length) =>
script Wwma_Funcl {
    input srcl = close;
    input length = 0;
    def wwalpha = 1 / length;
    def WWMA;
    WWMA = wwalpha * srcl + (1 - wwalpha) * (WWMA[1]);
    plot return = WWMA;
}
#Zlema_Funcl(srcl, length) =>
script Zlema_Funcl {
    input srcl = close;
    input length = 0;
    def zxLag = if length / 2 == Round(length / 2) 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;
    ma = if type == "SMA" then SimpleMovingAvg(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(SimpleMovingAvg(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 WMA(2 * WMA(src, length / 2) - WMA(src, length), Round(Sqrt(length))) else Double.NaN;
    plot result = ma;
}
#OTT(Source, percent) =>
script OTT {
    input Source    = close;
    input percent   = 0;
    def fark = Source * percent * 0.01;
    def UP = Source + fark;
    def LW = Source - fark;
    def UP_Band = if ((UP < UP_Band[1]) or (Source[1] > UP_Band[1])) then UP else UP_Band[1];
    def LW_Band = if ((LW > LW_Band[1]) or (Source[1] < LW_Band[1])) then LW else LW_Band[1];
    def MT = if ((MT[1] == UP_Band[1]) and (Source < UP_Band)) then UP_Band
        else if ((MT[1] == UP_Band[1]) and (Source > UP_Band)) then LW_Band
        else if ((MT[1] == LW_Band[1]) and (Source > LW_Band)) then LW_Band
        else if ((MT[1] == LW_Band)    and (Source < LW_Band)) then UP_Band
        else LW_Band;
    plot return = MT;
}
def MAvg = getMA(c, OttLength, MovAvgType);
def MT   = OTT(MAvg, OttCoeff);
def OTT = if MAvg > MT then MT * (200 + OttCoeff) / 200 else MT * (200 - OttCoeff) / 200;

plot SupportLine = if(!showSupportLine,na, MAvg);    # "Support Line"
SupportLine.SetDefaultColor(CreateColor(5,133,225));

def OTTC = if OTT[2] > OTT[3] then 2 else
           if OTT[2] < OTT[3] then -2 else
           if OTT[2] == OTT[3] and  MAvg>OTT[2] then 1 else
           if OTT[2] == OTT[3] and  MAvg<OTT[2] then -1 else 0;
plot OttLine  = if(isNaN(c),na,OTT);             # "OTT LINE"
OttLine.SetHiding(yes);
plot OttLine1 = if(isNaN(c),na,OTT[1]);          # "OTT LINE"
OttLine1.SetHiding(yes);
plot OttLine2 = if(isNaN(c),na,OTT[2]);          # "OTT LINE"
#OttLine2.SetHiding(yes);

OttLine2.SetLineWeight(2);
OttLine2.AssignValueColor(if !ColorOttLine then Color.CYAN else
                          if OTTC==2 then CreateColor(41,98,255) else
                          if OTTC==1 then CreateColor(21,49,127) else
                          if OTTC==-2 then CreateColor(156,39,176) else
                          if OTTC==-1 then CreateColor(81,20,92) else Color.GRAY);

def buySignalk  = crosses(MAvg, OTT[2],CrossingDirection.ABOVE);
def sellSignalk = crosses(MAvg, OTT[2],CrossingDirection.BELOW);
def SigCount = if (buySignalk or sellSignalk) then 1 else SigCount[1] + 1;
def buySignal = buySignalk and SigCount[1]>5;
def sellSignal = sellSignalk and SigCount[1]>5;

def longFillColor = if ShowCloud then if MAvg>OTT then yes else na else na;
def shortFillColor = if ShowCloud then if MAvg<OTT then yes else na else na;

#--- Clouds
AddCloud(if(longFillColor,mPlot,na), OttLine2, CreateColor(21,49,127),CreateColor(21,49,127));
AddCloud(if(shortFillColor,mPlot,na), OttLine2,CreateColor(81,20,92), CreateColor(81,20,92));

AddCloud(OttLine , OttLine2, CreateColor(21,49,127), CreateColor(81,20,92));
AddCloud(OttLine1, OttLine2, CreateColor(21,49,127), CreateColor(81,20,92));

#--- Bubbles
AddChartBubble(buySignal and showBubbles,l,"B", CreateColor(41,98,255), no);
AddChartBubble(sellSignal and showBubbles,h,"S", Color.MAGENTA,yes);

#--- Price Color
def ExtUp = OTT[2] > OTT[3] and Source>OttLine2 and mPlot>MAvg;
def Up    = mPlot>MAvg and Source>OttLine2;
def ExtDn = OTT[2] < OTT[3] and Source<OttLine2 and mPlot<MAvg;
def Dn    = mPlot<MAvg and Source<OttLine2;

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if ExtUp then Color.GREEN else
                 if Up then Color.DARK_GREEN else
                 if ExtDn then Color.RED else
                 if Dn then Color.DARK_RED else Color.GRAY);

### END of CODE
 
Does this work in Scan? does it repaint?
I can't imagine a script this complex working in a scan, you can try.

If it doesn't, you need to decide what part of this code are you attempting to scan for?
If you edit out all the choices, and create a static script, AND if then scanned for the different subsets.
Maybe?


There are many moving parts to this code, which part are you seeing repaint?
 
cigEudy.png


this is based on OTT code from @KivancOzbilgic.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at http
#// © KivancOzbilgic
#//created by: @Anil_Ozeksi
#//developer: ANIL ÖZEK??
#study("Optimized Trend Tracker","OTT", overlay=true)
# Converted and mod by Sam4Cok@Samer800 11/2022
declare upper;
input BarColor  = no;
input ShowCloud = yes;      #"Highlighter On/Off ?"
input MovAvgType = {default VAR, SMA, EMA, WMA, DEMA, TMA, WWMA, ZLEMA, TSF, HULL};
input Source    = close;#(close, title="Source")
input OttLength = 2;        # OTT Period
input OttCoeff  = 1.4;      # OTT Optimization Coeff
input showSupportLine = no; # "Show Support Line?"
input showBubbles = yes;    # "Show Support Line Crossing Signals?"
input ColorOttLine = yes;    #="Show OTT Color Changes?"

def na = Double.NaN;

def c = Source;
def h = high;
def l = low;
def mPlot = ohlc4;
############## theme 3########################################
DefineGlobalColor("Sky1" , CreateColor(0,221,255));  # >=90
DefineGlobalColor("Sky2" , CreateColor(4,188,217));  # >=80
DefineGlobalColor("Sky3" , CreateColor(4,156,179));  # >=70
DefineGlobalColor("Sky4" , CreateColor(4,127,145));  # >=60
DefineGlobalColor("Sky5" , CreateColor(4,103,117));  # >=50

DefineGlobalColor("Magenta1" , CreateColor(216,0,255));  # >=40
DefineGlobalColor("Magenta2" , CreateColor(187,4,219));  # >=30
DefineGlobalColor("Magenta3" , CreateColor(155,5,181));  # >=20
DefineGlobalColor("Magenta4" , CreateColor(123,3,143));  # >=10
DefineGlobalColor("Magenta5" , CreateColor(100,2,117));  # >=0
#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 vCMO = ((vUD - vDD) / (vUD + vDD));
    def VAR;
    VAR = valpha * AbsValue(vCMO) * src + (1 - valpha * AbsValue(vCMO)) * (VAR[1]);
    plot result = 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;
}
#Var_Funcl(srcl, length) =>
script Var_Funcl {
    input srcl = close;
    input length = 0;
    def valphal = 2 / (length + 1);
    def vud1l = if srcl > srcl[1] then srcl - srcl[1] else 0;
    def vdd1l = if srcl < srcl[1] then srcl[1] - srcl else 0;
    def vUDl = Sum(vud1l, 9);
    def vDDl = Sum(vdd1l, 9);
    def vCMOl = ((vUDl - vDDl) / (vUDl + vDDl));
    def VARl;
    VARl = (valphal * AbsValue(vCMOl) * srcl) + (1 - valphal * AbsValue(vCMOl)) * (VARl[1]);
    plot return = VARl;
}
#Wwma_Funcl(srcl, length) =>
script Wwma_Funcl {
    input srcl = close;
    input length = 0;
    def wwalpha = 1 / length;
    def WWMA;
    WWMA = wwalpha * srcl + (1 - wwalpha) * (WWMA[1]);
    plot return = WWMA;
}
#Zlema_Funcl(srcl, length) =>
script Zlema_Funcl {
    input srcl = close;
    input length = 0;
    def zxLag = if length / 2 == Round(length / 2) 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;
    ma = if type == "SMA" then SimpleMovingAvg(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(SimpleMovingAvg(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 WMA(2 * WMA(src, length / 2) - WMA(src, length), Round(Sqrt(length))) else Double.NaN;
    plot result = ma;
}
#OTT(Source, percent) =>
script OTT {
    input Source    = close;
    input percent   = 0;
    def fark = Source * percent * 0.01;
    def UP = Source + fark;
    def LW = Source - fark;
    def UP_Band = if ((UP < UP_Band[1]) or (Source[1] > UP_Band[1])) then UP else UP_Band[1];
    def LW_Band = if ((LW > LW_Band[1]) or (Source[1] < LW_Band[1])) then LW else LW_Band[1];
    def MT = if ((MT[1] == UP_Band[1]) and (Source < UP_Band)) then UP_Band
        else if ((MT[1] == UP_Band[1]) and (Source > UP_Band)) then LW_Band
        else if ((MT[1] == LW_Band[1]) and (Source > LW_Band)) then LW_Band
        else if ((MT[1] == LW_Band)    and (Source < LW_Band)) then UP_Band
        else LW_Band;
    plot return = MT;
}
def MAvg = getMA(c, OttLength, MovAvgType);
def MT   = OTT(MAvg, OttCoeff);
def OTT = if MAvg > MT then MT * (200 + OttCoeff) / 200 else MT * (200 - OttCoeff) / 200;

plot SupportLine = if(!showSupportLine,na, MAvg);    # "Support Line"
SupportLine.SetDefaultColor(CreateColor(5,133,225));

def OTTC = if OTT[2] > OTT[3] then 2 else
           if OTT[2] < OTT[3] then -2 else
           if OTT[2] == OTT[3] and  MAvg>OTT[2] then 1 else
           if OTT[2] == OTT[3] and  MAvg<OTT[2] then -1 else 0;
plot OttLine  = if(isNaN(c),na,OTT);             # "OTT LINE"
OttLine.SetHiding(yes);
plot OttLine1 = if(isNaN(c),na,OTT[1]);          # "OTT LINE"
OttLine1.SetHiding(yes);
plot OttLine2 = if(isNaN(c),na,OTT[2]);          # "OTT LINE"
#OttLine2.SetHiding(yes);

OttLine2.SetLineWeight(2);
OttLine2.AssignValueColor(if !ColorOttLine then Color.CYAN else
                          if OTTC==2 then CreateColor(41,98,255) else
                          if OTTC==1 then CreateColor(21,49,127) else
                          if OTTC==-2 then CreateColor(156,39,176) else
                          if OTTC==-1 then CreateColor(81,20,92) else Color.GRAY);

def buySignalk  = crosses(MAvg, OTT[2],CrossingDirection.ABOVE);
def sellSignalk = crosses(MAvg, OTT[2],CrossingDirection.BELOW);
def SigCount = if (buySignalk or sellSignalk) then 1 else SigCount[1] + 1;
def buySignal = buySignalk and SigCount[1]>5;
def sellSignal = sellSignalk and SigCount[1]>5;

def longFillColor = if ShowCloud then if MAvg>OTT then yes else na else na;
def shortFillColor = if ShowCloud then if MAvg<OTT then yes else na else na;

#--- Clouds
AddCloud(if(longFillColor,mPlot,na), OttLine2, CreateColor(21,49,127),CreateColor(21,49,127));
AddCloud(if(shortFillColor,mPlot,na), OttLine2,CreateColor(81,20,92), CreateColor(81,20,92));

AddCloud(OttLine , OttLine2, CreateColor(21,49,127), CreateColor(81,20,92));
AddCloud(OttLine1, OttLine2, CreateColor(21,49,127), CreateColor(81,20,92));

#--- Bubbles
AddChartBubble(buySignal and showBubbles,l,"B", CreateColor(41,98,255), no);
AddChartBubble(sellSignal and showBubbles,h,"S", Color.MAGENTA,yes);

#--- Price Color
def ExtUp = OTT[2] > OTT[3] and Source>OttLine2 and mPlot>MAvg;
def Up    = mPlot>MAvg and Source>OttLine2;
def ExtDn = OTT[2] < OTT[3] and Source<OttLine2 and mPlot<MAvg;
def Dn    = mPlot<MAvg and Source<OttLine2;

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if ExtUp then Color.GREEN else
                 if Up then Color.DARK_GREEN else
                 if ExtDn then Color.RED else
                 if Dn then Color.DARK_RED else Color.GRAY);

### END of CODE
WHY ARE SOME CANDLES DARKER GREEN AND OTHERS VERY BRIGHT LIGHT GREEN?
 
Does this repaint?

@novadolla
There is not any obvious repainting calculations in this thread.
Please note that indicators on this forum that repaint have a "repaints" prefix before their name.
It is possible for a script to be overlooked and not be properly classified.
Can you provide more information as to why you believe this indicator might be repainting?
 
I wouldn't use this indicator. Depending on how many days of historical data you load, it changes plots. Not consistent.

Comparison of 5d vs 1d on 3m.

MYWGbCZ.png

OWU3rBr.png
 
Last edited:
I wouldn't use this indicator. Depending on how many days of historical data you load, it changes plots. Not consistent.

Comparison of 5d vs 1d on 3m.
just to clarify.
changing the historical data may change some indicators based on the way how calculated the formulas. This is not repainting as per my understanding.

Repainting as per my understanding occurs when the signal keeps changing with the same provided historical data.
Pls note that using any indicator (whether specified repainting or not( will be at your sole responsibility since I never give any financial advise. I just facilitate a tool where you can use in the way you want if you decided to use it.
 
just to clarify.
changing the historical data may change some indicators based on the way how calculated the formulas. This is not repainting as per my understanding.

Repainting as per my understanding occurs when the signal keeps changing with the same provided historical data.
Pls note that using any indicator (whether specified repainting or not( will be at your sole responsibility since I never give any financial advise. I just facilitate a tool where you can use in the way you want if you decided to use it.
Should be tested. If the indicator depends on the whole window of data, when candles drop off from the back, we should observe if it recalculates and show different signals. It is a suspicion but should be tested.
 
I wouldn't use this indicator. Depending on how many days of historical data you load, it changes plots. Not consistent.

Comparison of 5d vs 1d on 3m.

View attachment 17830
View attachment 17831
Link for the original OTT in Trading View or can check YouTube for testing of the indicator and strategies under the "optimized trend tracker kivancozbilgic" title

https://www.tradingview.com/script/zVhoDQME/

As per the author indicated,
"OTT will be much sensitive to trend movements if it is smaller. And vice versa, will be less sensitive when it is longer.

As the period increases it will become less sensitive to little trends and price actions."

Hope that answer the repaint doubts ...
 

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