True Range Adjusted Exponential MovAvg for ThinkOrSwim

samer800

Conversion Expert
VIP
Lifetime
4F2LOUj.png


Author Message:
The True Range Adjusted Exponential Moving Average was created by Vitali Apirine (Stocks and Commodities Jan 2023 pgs 22-27) and this is the latest indicator in his EMA variation series. He has been tweaking the traditional EMA formula using various methods and this indicator of course uses the True Range indicator. The way that this indicator works is that it uses a stochastic of the True Range vs its highest and lowest values over a fixed length to create a multiple which increases as the True Range rises to its highest level and decreases as the True Range falls. This in turn will adjust the Ema to rise or fall depending on the underlying True Range. As with all of my indicators, I have color coded it to turn green when it detects a buy signal or turn red when it detects a sell signal. Darker colors mean it is a very strong signal and let me know if you find any settings that work well overall vs the default settings.

UPPER CODE:
CSS:
#//@cheatcountry
#// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
#// True Range Adjusted Exponential Moving Average [CC] script may be freely distributed under the MIT license.
#indicator('True Range Adjusted Exponential Moving Average [CC]', max_bars_back = 3000, overlay = true)
# Coneverted and mod by Sam4Cok@Samer800 - 01/2023

input barColor = yes;        # 'Allow Bar Color Change?'
input SingleLine   = no;
input SignalBubble = yes;
input TrendLebel = yes;
input Source = close;        # 'Source'
input UseChartTimeframe = no;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;    # 'Resolution'
input AllowRepainting = no;  # 'Allow Repainting?'
input length = 40;           # 'Length'
input mult = 10.0;           # 'Mult'
input Smoothing = yes;
input SmoothingLength = 7;

def na = Double.NaN;
#-----Color
DefineGlobalColor("Green"  , CreateColor(76,175,80));
DefineGlobalColor("Lime"   , Color.Light_GREEN);
DefineGlobalColor("Maroon" , Color.RED);
DefineGlobalColor("Red"    , Color.PINK);
DefineGlobalColor("Black"  , CreateColor(54,58,69));
def src;
if UseChartTimeframe and AllowRepainting {
    src = Source;
} else
if UseChartTimeframe and !AllowRepainting {
    src = Source[1];
} else
if !UseChartTimeframe and AllowRepainting {
    src = close(Period = Aggregation);
} else {
    src = close(Period = Aggregation)[1];
}
def tr = TrueRange(high, close, low);
def alpha = 2.0 / (length + 1);
def trL = Lowest(tr, length);
def trH = Highest(tr, length);
def trAdj = if (trH - trL) != 0 then (tr - trL) / (trH - trL) else 0;

def trEMA = ExpAverage(src, length);
def trAdjEma1;
trAdjEma1 = CompoundValue(1, trAdjEma1[1] + (alpha * (1 + (trAdj * mult)) * (src - trAdjEma1[1])), trEMA);

plot trAdjEma = if smoothing then ExpAverage(trAdjEma1,SmoothingLength) else  trAdjEma1;
trAdjEma.SetHiding(yes);


def slo = src - trAdjEma;
def sig = if slo > 0 then if slo > slo[1] then 2 else 1 else
          if slo < 0 then if slo < slo[1] then -2 else -1 else 0;
def StrongBuy  = if slo > 0 and slo > slo[1] then StrongBuy[1] + 1 else 0;
def StrongSell = if slo < 0 and slo < slo[1] then StrongSell[1] + 1 else 0;
def buy  = if slo > 0 then buy[1] + 1 else 0;
def sell = if slo < 0 then sell[1] + 1 else 0;
def tradjemaColor = if sig > 1 then 2 else
                    if sig > 0 then 1 else
                    if sig < -1 then -2 else
                    if sig < 0 then -1 else 0;
AssignPriceColor(if !barColor then Color.CURRENT else
                 if tradjemaColor == 2 then GlobalColor("Green") else
                 if tradjemaColor == 1 then GlobalColor("Lime") else
                 if tradjemaColor == -2 then GlobalColor("Maroon") else
                 if tradjemaColor == -1 then GlobalColor("Red") else GlobalColor("Black"));
plot TrueAdjEma = trAdjEma;
TrueAdjEma.SetLineWeight(2);
TrueAdjEma.AssignValueColor(if tradjemaColor == 2 then GlobalColor("Green") else
                 if tradjemaColor == 1 then GlobalColor("Lime") else
                 if tradjemaColor == -2 then GlobalColor("Maroon") else
                 if tradjemaColor == -1 then GlobalColor("Red") else GlobalColor("Black"));
plot TrueAdjEma2 = if IsNaN(close) or SingleLine then na else trAdjEma[2];
TrueAdjEma2.AssignValueColor(if tradjemaColor == 2 then GlobalColor("Green") else
                 if tradjemaColor == 1 then GlobalColor("Lime") else
                 if tradjemaColor == -2 then GlobalColor("Maroon") else
                 if tradjemaColor == -1 then GlobalColor("Red") else GlobalColor("Black"));

AddCloud(TrueAdjEma, TrueAdjEma2, Color.DARK_GREEN, Color.DARK_RED, yes);

AddLabel(TrendLebel and StrongBuy > 0, "Strong Buy", GlobalColor("Green"));
AddLabel(TrendLebel and StrongSell > 0, "Strong Sell", GlobalColor("Maroon"));
AddLabel(TrendLebel and buy > 0 and !StrongBuy, "Buy", GlobalColor("Lime"));
AddLabel(TrendLebel and sell > 0 and !StrongSell, "Sell", GlobalColor("Red"));

#//condition
def CrossUp = Crosses(src, trAdjEma[2], CrossingDirection.ABOVE);
def CrossDn = Crosses(src, TrueAdjEma, CrossingDirection.BELOW);
def signal_buy;# = false
def signal_sell;# = false

if  src > TrueAdjEma  {
    signal_buy = yes;
    signal_sell = no;
} else
if  src < TrueAdjEma  {
    signal_buy = no;
    signal_sell = yes;
} else {
    signal_buy = signal_buy[1];
    signal_sell = signal_sell[1];
}
#//only first signal to show
def Buy1;# = false
def Sell1;# = false

Buy1  = if signal_buy then yes else
        if signal_sell then no else if CrossUp then no else if CrossDn then no else Buy1[1];
Sell1 = if signal_sell then yes else
        if signal_buy then no else if CrossUp then no else if CrossDn then no else Sell1[1];

def Final_buy  = signal_buy and !Buy1[1] and SignalBubble;
def Final_sell = signal_sell and !Sell1[1] and SignalBubble;

AddChartBubble(Final_buy, low, "BUY", GlobalColor("Green"), no);
AddChartBubble(Final_sell, high, "SELL", GlobalColor("Maroon"), yes);

#---- END Code

I just added Lower Study.
12geUJU.png

Author Message:

made a sort of conversion of CheatCountries implementation of the True Range Adjusted Exponential Moving Average into a momentum oscillator.
Being True Range based, it the bounds vary based on the chart.

Includes a Bollinger Band for bounds that forms a trend follower based on the 0 point.

Includes CheatCountry color code signals, different color scheme. Bright colors are strong signals, ark are weak, green bull, red bear, the basics.

This oscillator can be used for divergences, trends, signal strength, confirmation, volatility readings, you name it.

Works well on smoothed/filtered signals as well.

CODE:

CSS:
#//@cheatcountry
#// Original Script Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
#// True Range Adjusted Exponential Moving Average [CC] script may be freely distributed under the MIT license.
#indicator('True Range Adjusted Exponential Momentum [CC]-[burgered]' shorttitle = 'TRAEM')
# Coneverted and mod by Sam4Cok@Samer800 - 01/2023
declare lower;

input barColor = yes;        # 'Allow Bar Color Change?'
input SingleLine   = no;
input SignalBubble = yes;
input TrendLebel = yes;
input Source = close;        # 'Source'
input UseChartTimeframe = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;    # 'Resolution'
input AllowRepainting = no;  # 'Allow Repainting?'
input length = 60;           # 'Length'
input mult = 20.0;           # 'Mult'
input Smoothing = yes;
input SmoothingLength = 7;
input colorSwitch = 3;
input stdevBandLength = 200;
input x = 1.7;
input y = 1.05;
input z = 1.7;

def na = Double.NaN;
def ColSwitch = if colorSwitch>4 then 4 else if colorSwitch<1 then 1 else colorSwitch;
#-----Color
DefineGlobalColor("Green"  , CreateColor(76, 175, 80));
DefineGlobalColor("Lime"   , Color.LIGHT_GREEN);
DefineGlobalColor("Maroon" , Color.RED);
DefineGlobalColor("Red"    , Color.PINK);
DefineGlobalColor("Black"  , CreateColor(54, 58, 69));
DefineGlobalColor("Blue"   , CreateColor(41,98,255));
def src;
if UseChartTimeframe and AllowRepainting {
    src = Source;
} else
if UseChartTimeframe and !AllowRepainting {
    src = Source[1];
} else
if !UseChartTimeframe and AllowRepainting {
    src = close(Period = Aggregation);
} else {
    src = close(Period = Aggregation)[1];
}
#trAdjEma(lengthy) =>
script trAdjEma {
    input src = close;
    input length = 60;
    input mult = 20;
    input Smoothing = yes;
    input SmoothingLength = 7;
    def tr = TrueRange(high, close, low);
    def alpha = 2.0 / (length + 1);
    def trL = Lowest(tr, length);
    def trH = Highest(tr, length);
    def trAdj = if (trH - trL) != 0 then (tr - trL) / (trH - trL) else 0;
    def trEMA = ExpAverage(src, length);
    def trAdjEma1;
    trAdjEma1 = CompoundValue(1, trAdjEma1[1] + (alpha * (1 + (trAdj * mult)) * (src - trAdjEma1[1])), trEMA);
    def trAdjEma = if Smoothing then ExpAverage(trAdjEma1, SmoothingLength) else trAdjEma1;
    def slo = src - trAdjEma;
    def sig = if slo > 0 then if slo > slo[1] then 2 else 1 else
              if slo < 0 then if slo < slo[1] then -2 else -1 else 0;
    def tradjemaColor = if sig > 1 then 2 else
                        if sig > 0 then 1 else
                        if sig < -1 then -2 else
                        if sig < 0 then -1 else 0;
    plot trAdEma = trAdjEma;
    plot Color  = tradjemaColor;
}
def trAdjEma1 = trAdjEma(src, length, mult, Smoothing, SmoothingLength).trAdEma;
def Color1    = trAdjEma(src, length, mult, Smoothing, SmoothingLength).Color;
def trAdjEma2 = trAdjEma(src, length * x, mult, Smoothing, SmoothingLength).trAdEma;
def Color2    = trAdjEma(src, length * x, mult, Smoothing, SmoothingLength).Color;
def Color3    = trAdjEma(src, power(length, y), mult, Smoothing, SmoothingLength).Color;
def trAdjEma4 = trAdjEma(src, power(length, z), mult, Smoothing, SmoothingLength).trAdEma;
def Color4    = trAdjEma(src, power(length, z), mult, Smoothing, SmoothingLength).Color;

def colorSW = if colSwitch == 1 then color1 else
              if colSwitch == 2 then color2 else
              if colSwitch == 3 then color3 else
              if colSwitch == 4 then color4 else na;
#barcolor(bar ? colorSW : na)

def satan = (trAdjEma1-trAdjEma2)/trAdjEma4;
def highway = highest(satan, length*4);
def lowway = lowest(satan, length*4);
def rangeway = (highway-lowway);
def SlowTrAdjEma = satan/rangeway;
plot TrAdjEmaLine = SlowTrAdjEma; #'TrAdjEma', color = colorSW, linewidth = 2)
TrAdjEmaLine.SetLineWeight(2);
TrAdjEmaLine.AssignValueColor(if colorSW == 2 then GlobalColor("Green") else
                 if colorSW == 1 then GlobalColor("Lime") else
                 if colorSW == -2 then GlobalColor("Maroon") else
                 if colorSW == -1 then GlobalColor("Red") else GlobalColor("Black"));

def tr = TrueRange(high, close, low);
def alpha = 2.0 / (stdevBandLength + 1);
def trL = lowest(tr, stdevBandLength);
def trH = highest(tr, stdevBandLength);
def trAdj = if (trH - trL) != 0 then (tr - trL) / (trH - trL) else 0;
def trEMA = ExpAverage(src, stdevBandLength);
def trAdjEmaTemp;
    trAdjEmaTemp = CompoundValue(1,trAdjEmaTemp[1] + (alpha*(1 + (trAdj*1)) * (SlowTrAdjEma - trAdjEmaTemp[1])), trEMA);
def trAdjEma = if Smoothing then ExpAverage(trAdjEmaTemp, SmoothingLength) else trAdjEmaTemp;

def dev = 1 * stdev(SlowTrAdjEma, stdevBandLength);
def dev2 = 2 * stdev(SlowTrAdjEma, stdevBandLength);
def upper = trAdjEma + dev;
def lower = trAdjEma - dev;
def upper2 = trAdjEma + dev2;
def lower2 = trAdjEma - dev2;

plot linexx = if isNaN(close) then na else 0;
linexx.AssignValueColor(if trAdjEma>0 then color.green else color.red);

def trAdjEmapl = trAdjEma;
plot UpBand1 = upper;
plot LoBand1 = lower;
plot UpBand2 = upper2;
plot LoBand2 = lower2;
UpBand1.SetDefaultColor(GlobalColor("Blue"));
UpBand2.SetDefaultColor(GlobalColor("Blue"));
LoBand1.SetDefaultColor(GlobalColor("Blue"));
LoBand2.SetDefaultColor(GlobalColor("Blue"));

AddCloud(trAdjEmapl, linexx, color.DARK_GREEN, Color.DARK_RED, yes);

AssignPriceColor(if !barColor then Color.CURRENT else
                 if colorSW == 2 then GlobalColor("Green") else
                 if colorSW == 1 then GlobalColor("Lime") else
                 if colorSW == -2 then GlobalColor("Maroon") else
                 if colorSW == -1 then GlobalColor("Red") else GlobalColor("Black"));

#---- END Code
 
Last edited by a moderator:
Thank you so much for all your work. Your indicators are adding so much value. Really appreciate your efforts.

Would you mind providing some explanation regarding the repainting code in order to better understand conditions of repaint such as what and when since it can be turned on and off in the property settings?

if UseChartTimeframe and AllowRepainting {
src = Source;
} else
if UseChartTimeframe and !AllowRepainting {
src = Source[1];
} else
if !UseChartTimeframe and AllowRepainting {
src = close(Period = Aggregation);
} else {
src = close(Period = Aggregation)[1];
}
 
repaint will consider current bar in the calculation where can be changed. no repaint will calculate only once the candle confirmed.
 
repaint will consider current bar in the calculation where can be changed. no repaint will calculate only once the candle confirmed.
Thank you. Also, thank you for sacrificing your time to help others with your great work here coding. Lastly, your commentary describing the calculations, usage, and indicator objective is very helpful!
 
I just converted it with some addition. :)

I think you're a bit too modest. When the traders.com code was first published, I charted it and didn't see a lot of use for it. Now that I've plotted it with your mod, it's a LOT more useful. Can't wait to do some back testing on it.

As stated above, your generosity is remarkable. Thank you for sharing your time and your talents!
 

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