Dominant Direction (DD) for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
jf38ewR.png


Author Message: https://www.tradingview.com/v/OsGPyDnR/

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © peacefulLizard50262
#https://www.tradingview.com/v/OsGPyDnR/
#indicator("Dominant Direction", overlay = true, max_bars_back = 5000)
# converted and mod by Sam4Cok@Samer800    03/2023

input ColorBars = yes;
input ShowLabel = yes;
input showeMAMA = yes;
input source = hl2;     # "Source"
input FastLimit = 0.8;
input SlowLimit = 0.05;
input SigLocation = 4;
input SigSize = 2;

def na = Double.NaN;
def size = Min(SigSize, 5);
#--- Color
DefineGlobalColor("green" , CreateColor(0, 128, 255));
DefineGlobalColor("Red"   , CreateColor(255, 0, 128));
script nz {
    input data  = close;
    input repl = 0;
    def ret_val = if !IsNaN(data) then data else repl;
    plot return = ret_val;
}
#mamaPeriod(src, fastLimit, slowLimit) =>
script MaMaFaMa {
    input sourceEMAMA = hl2;
    input fastlimitEMAMA = 0.8;#, title="Fast Limit"
    input slowlimitEMAMA = 0.05;#, title="Slow Limit"
    def p;# = na
    def i2;# = na
    def q2;# = na
    def re;# = na
    def im;# = na
    def spp;# = na
    def eMAMA;# = na
    def eFAMA;# = na
    def sp = (4 * sourceEMAMA + 3 * sourceEMAMA[1] + 2 * sourceEMAMA[2] + sourceEMAMA[3]) / 10.0;
    def dt = (.0962 * sp + .5769 * nz(sp[2]) - .5769 * nz(sp[4]) - .0962 * nz(sp[6])) * (.075 * nz(p[1]) + .54);
    def q1 = (.0962 * dt + .5769 * nz(dt[2]) - .5769 * nz(dt[4]) - .0962 * nz(dt[6])) * (.075 * nz(p[1]) + .54);
    def i1 = nz(dt[3]);
    def jI = (.0962 * i1 + .5769 * nz(i1[2]) - .5769 * nz(i1[4]) - .0962 * nz(i1[6])) * (.075 * nz(p[1]) + .54);
    def jq = (.0962 * q1 + .5769 * nz(q1[2]) - .5769 * nz(q1[4]) - .0962 * nz(q1[6])) * (.075 * nz(p[1]) + .54);
    def i2_ = i1 - jq;
    def q2_ = q1 + jI;
    i2  = .2 * i2_ + .8 * nz(i2[1]);
    q2  = .2 * q2_ + .8 * nz(q2[1]);
    def re_ = i2 * nz(i2[1]) + q2 * nz(q2[1]);
    def im_ = i2 * nz(q2[1]) - q2 * nz(i2[1]);
    re  = .2 * re_ + .8 * nz(re[1]);
    im  = .2 * im_ + .8 * nz(im[1]);
    def p1  = If(im != 0 and re != 0, 360 / ATan(im / re), nz(p[1]));
    def p2  = If(p1 > 1.5 * nz(p1[1]), 1.5 * nz(p1[1]), If(p1 < 0.67 * nz(p1[1]), 0.67 * nz(p1[1]), p1));
    def p3  = If(p2 < 6, 6, If (p2 > 50, 50, p2));
    p   = .2 * p3 + .8 * nz(p3[1]);
    spp = .33 * p + .67 * nz(spp[1]);
    def phaseEMAMA = ATan(q1 / i1);
    def dphase_ = nz(phaseEMAMA[1]) - phaseEMAMA;
    def dphase = If(dphase_ < 1, 1, dphase_);
    def alpha_ = fastlimitEMAMA / dphase;
    def alpha = If(alpha_ < slowlimitEMAMA, slowlimitEMAMA, If(alpha_ > fastlimitEMAMA, fastlimitEMAMA, alpha_));
    eMAMA = alpha * sourceEMAMA + (1 - alpha) * nz(eMAMA[1]);
    eFAMA = 0.5 * alpha * eMAMA + (1 - .5 * alpha) * nz(eFAMA[1]);
    plot MAMA = eMAMA;
    plot FAMA = eFAMA;
}
#computeAlpha(src, fastLimit, slowLimit) =>
script computeAlpha {
    input src = close;
    input dynLow = 0.5;
    input dynHigh = 0.05;
    def period;# = 0.0
    def Pi = Double.Pi;
    def C1 = 0.0962;
    def C2 = 0.5769;
    def C3 = 0.075 * nz(period[1]) + 0.54;
    def smooth = (4 * src + 3 * nz(src[1]) + 2 * nz(src[2]) + nz(src[3])) / 10;
    def detrend = C3 * (C1 * smooth + C2 * nz(smooth[2]) - C2 * nz(smooth[4]) - C1 * nz(smooth[6]));
#   // Compute InPhase and Quadrature components
    def Q1 = C3 * (C1 * detrend + C2 * nz(detrend[2]) - C2 * nz(detrend[4]) - C1 * nz(detrend[6]));
    def I1 = nz(detrend[3]);
#   // Advance Phase of I1 and Q1 by 90 degrees
    def jI = C3 * (C1 * I1 + C2 * nz(I1[2]) - C2 * nz(I1[4]) - C1 * nz(I1[6]));
    def jQ = C3 * (C1 * Q1 + C2 * nz(Q1[2]) - C2 * nz(Q1[4]) - C1 * nz(Q1[6]));
#    // Phasor addition for 3 bar averaging
    def I2_ = I1 - jQ;
    def Q2_ = Q1 + jI;
#    // Smooth I and Q components before applying discriminator
    def I2 = 0.2 * I2_ + 0.8 * nz(I2[1]);
    def Q2 = 0.2 * Q2_ + 0.8 * nz(Q2[1]);
#    // Extract Homodyne Discriminator
    def Re_ = I2 * nz(I2[1]) + Q2 * nz(Q2[1]);
    def Im_ = I2 * nz(Q2[1]) - Q2 * nz(I2[1]);
    def Re = 0.2 * Re_ + 0.8 * nz(Re[1]);
    def Im = 0.2 * Im_ + 0.8 * nz(Im[1]);
    def period1 = if (Re != 0 and Im != 0) then 2 * Pi / ATan(Im / Re) else 0;
    def period2 = Min(period1, 1.5 * nz(period2[1], period1));
    def period3 = Max(period2, (2 / 3) * nz(period3[1], period2));
    def period4 = Min(Max(period3, dynLow), dynHigh);
    period = period4 * 0.2 + nz(period[1]) * 0.8;
    plot length = period;
}
def last = IsNaN(close[-1]) and !IsNaN(close);
def eMAMA = MaMaFaMa(source, FastLimit, SlowLimit).mama;
def eFAMA = MaMaFaMa(source, FastLimit, SlowLimit).fama;
def length = computeAlpha(source, 2, 4096);
def Period = if !IsNaN(length) then Round(length, 0) else Period[1];
def isrealtime =  !IsNaN(close);
def dd = if isrealtime then source + (source - GetValue(source, Period)) else dd[1];
#def dd = source + (source - GetValue(source, Period));

def upPred = if dd > source then 1 else 0;
def DnPred = if dd < source then 1 else 0;

plot PredUp = if last[SigLocation] then if upPred[SigLocation] == 1 then dd[SigLocation] else na else na;
PredUp.AssignValueColor(if upPred[SigLocation] == 1 then GlobalColor("green") else Color.DARK_GRAY);
PredUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PredUp.SetLineWeight(size);

plot PredDn = if last[SigLocation] then if DnPred[SigLocation] == 1 then dd[SigLocation] else na else na;
PredDn.AssignValueColor(if DnPred[SigLocation] == 1 then GlobalColor("red") else Color.DARK_GRAY);
PredDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
PredDn.SetLineWeight(size);

AddLabel(ShowLabel , if upPred == 1 then "Domnt Dir: UP" else
                    if DnPred == 1 then "Domnt Dir: DOWN" else "Domnt Dir: NO DIRECTION",
                    if upPred == 1 then GlobalColor("green") else
                    if DnPred == 1 then GlobalColor("red") else Color.DARK_GRAY);
AddLabel(ShowLabel , if upPred == 1 then "Domnt Pred($" + Round(dd, 2) + ")" else
                    if DnPred == 1 then "Domnt Pred($" + Round(dd, 2) + ")" else "Dominant Direction: NO DIRECTION",
                    if upPred == 1 then GlobalColor("green") else
                    if DnPred == 1 then GlobalColor("red") else Color.DARK_GRAY);

def fillEMAMA = eMAMA > eFAMA;

plot p_eMAMA = if showeMAMA then eMAMA else na; # "Ehler's MAMA"
plot p_eFAMA = if showeMAMA then eFAMA else na; # "Ehler's FAMA"
p_eMAMA.AssignValueColor(if fillEMAMA then GlobalColor("green") else GlobalColor("red"));
p_eFAMA.AssignValueColor(if fillEMAMA then GlobalColor("green") else GlobalColor("red"));
AddCloud(p_eMAMA, p_eFAMA, GlobalColor("green"), Color.DARK_RED);

plot zigzag = if last[1] then source[1] else
              if last[SigLocation] then dd[SigLocation] else na;
zigzag.AssignValueColor(if !IsNaN(PredUp) then GlobalColor("green") else
                        if !IsNaN(PredDn) then GlobalColor("red") else Color.DARK_GRAY);
zigzag.EnableApproximation();
zigzag.SetLineWeight(size);

#--- PRice color

AssignPriceColor(if !ColorBars then Color.CURRENT else
                 if fillEMAMA then Color.GREEN else Color.RED);


#--- END CODE
 
Last edited by a moderator:

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