McGinley Dynamic Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
This indicator is called McGinley Dynamic for ThinkorSwim. Investopedia dubbed it "The Most Reliable Indicator You've Never Heard Of". I thought it would be interesting to share it here.

The tradition simple and exponential moving averages tend to lag. McGinley Dynamic indicator will try to overcome that by automatically adjusting itself relative to the speed of the market.

Also see an alternate version here: https://usethinkscript.com/threads/mcginley-dynamic-indicator-for-thinkorswim.338/#post-17842

nZaCklD.png



read through the thread for other versions submitted being used by members

thinkScript Code

Code:
# McGinley Dynamic Indicator
# Mobius
# V02.11.2011
#  D = D1 + ((I - D1) / ( N * (I/D1)^4))
# where D1= yesterday's Dynamic, I = today's price, N = smoothing factor.

input value= close;
input length= 20;
input Slength = 5;
def A1= ExpAverage(value, length)[1];
def MDI = A1 + ((value - A1) / Sqr(value / A1));
plot Data = ExpAverage(MDI, Slength);
Data.SetPaintingStrategy(PaintingStrategy.LINE);
Data.SetLineWeight(2);
#Data.SetDefaultColor(GetColor(8));
def A2 = ExpAverage(value, length*3)[1];
def MDI2 = A2 + ((value - A2) / Sqr(value / A2));
plot Data2 = ExpAverage(MDI2, Slength*3);
Data2.DefineColor("Data2_SlopePos", Color.GREEN);
Data2.DefineColor("Data2_SlopeNeg", Color.MAGENTA);
Data2.AssignValueColor(if Data  > Data2 then Data2.Color("Data2_SlopePos") else Data2.Color("Data2_SlopeNeg"));
Data2.setlineweight(2);
data2.hide();
Data.DefineColor("Data_SlopePos", Color.CYAN);
Data.DefineColor("Data_SlopeNeg", Color.RED);
Data.AssignValueColor(if Data  > Data2 then Data.Color("Data_SlopePos") else Data.Color("Data_SlopeNeg"));
data.hide();
plot EMA8 = expaverage(close,8);
EMA8.setdefaultcolor(color.DARK_ORANGE);
EMA8.setlineweight(2);
EMA8.hide();
# End Code ------------
def buySignal = if Data > Data2 and Data[1] <= Data2[1] and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal = if Data < Data2 and Data[1] >= Data2[1] and expaverage(close,85) < average(close,13) then 1 else 0;
plot signal = if buySignal or sellSignal then Data else double.nan;
signal.assignValueColor(if buySignal then color.WHITE else color.YELLOW);
signal.setLineWeight(5);
#signal.SetStyle(curve.points);
signal.setpaintingstrategy(paintingStrategy.SQUARES);

# additional plots from yahoo forum
input price = close;
input length2 = 14;
input displace = 0;
plot WS = WildersAverage(price[-displace], length2);
WS.setDefaultColor(GetColor(8));
input periods = 10;
rec _md = compoundValue( 1, _md[1] + (( close - _md[1] ) / ( periods *
power( close / _md[1], 4 ) ) ), close );
plot MD = _md;
MD.setdefaultcolor(color.CYAN);
MD.setlineweight(2);
MD.hide();

def buySignal2 = if MD > WS and MD[1] <= WS[1] and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal2 = if MD < WS and MD[1] >= WS[1] and expaverage(close,85) < average(close,13) then 1 else 0;
plot signal2 = if buySignal2 or sellSignal2 then MD else double.nan;
signal2.assignValueColor(if buySignal2 then color.WHITE else color.YELLOW);
signal2.setLineWeight(5);
signal2.SetStyle(curve.points);
#signal2.setpaintingstrategy(paintingStrategy.SQUARES);

Here is the simplified version that paints your candles to the trend of the indicator.

Code:
#McGinleyIndicator
declare upper;

input periods = 10;

rec _md = CompoundValue( 1, _md[1] + (( close - _md[1] ) / ( periods * Power( close / _md[1], 4 ) ) ), close );

plot MD = _md;
AssignPriceColor(if MD > MD[1] then Color.GREEN else Color.RED);

Shareable Link

https://tos.mx/XqTZTf
Credits:
 

Attachments

  • nZaCklD.png
    nZaCklD.png
    93.1 KB · Views: 554
Last edited by a moderator:
Below is the code for the MDI I use. Buy above the top line, Sell below the bottom line.

For me, setting of 13 works.
7s27buy.png

Ruby:
# Archive Name: McGinley Dynamic Indicator_Mobius
# Archive Section: Trend   
# Suggested Tos Name: McGinleyDynamicIndicator_Mobius
# Archive Date: 5.05.2018
# Archive Notes:  Lounge 5.02.2018
# 16:59 Mobius: An oldie but goodie
# 17:01 Mobius: The lag on that is only a few bars and that lag can be mostly removed by detrending it by half the input length
 
# A More Advanced Adaptive Moving Average
# McGinley Dynamic Indicator
# Mobius
# V03.11.2011
#  D = D[1] + (I - D[1]) / ( N * (I/D[1])^4)
# where D[1] = yesterday's Dynamic, I = today's price, N = smoothing factor.
 
input Ih = high;
input Ii = low;
input N = 13;

#High Low Bands added 7/18/2019 by mc01439
#High
def Dh;
Dh = CompoundValue(1, Dh[1] + (Ih - Dh[1]) /  (N * Power((Ih / Dh[1]), 4)), Ih);
 
plot MDIh = Dh;
MDIh.SetPaintingStrategy(PaintingStrategy.LINE);
#MDIh.SetLineWeight(3);
#MDIh.SetDefaultColor(Color.White);

MDIh.DefineColor("Up", CreateColor( 0,  102, 255));
MDIh.DefineColor("Down", CreateColor( 255, 255, 0));
MDIh.SetLineWeight(2);
MDIh.AssignValueColor(if MDIh > MDIh[1] and MDIh[1] > MDIh[2] and MDIh[2] > MDIh[3] and MDIh[3] > MDIh[4] then MDIh.Color("Up") else if MDIh < MDIh[1] and MDIh[1] < MDIh[2] and MDIh[2] < MDIh[3] and MDIh[3] < MDIh[4] then MDIh.Color("Down") else Color.MAGENTA);

#Low
def Di;
Di = CompoundValue(1, Di[1] + (Ii - Di[1]) /  (N * Power((Ii / Di[1]), 4)), Ii);
 
plot MDIi = Di;
MDIi.SetPaintingStrategy(PaintingStrategy.LINE);
#MDIi.SetLineWeight(3);
#MDIi.SetDefaultColor(Color.White);

MDIi.DefineColor("Up", CreateColor( 0,  102, 255));
MDIi.DefineColor("Down", CreateColor( 255, 255, 0));
MDIi.SetLineWeight(2);
MDIi.AssignValueColor(if MDIi > MDIi[1] and MDIi[1] > MDIi[2] and MDIi[2] > MDIi[3] and MDIi[3] > MDIi[4] then MDIi.Color("Up") else if MDIi < MDIi[1] and MDIi[1] < MDIi[2] and MDIi[2] < MDIi[3] and MDIi[3] < MDIi[4] then MDIi.Color("Down") else Color.MAGENTA);

addCloud (MDIh, MDIi, Color.GRAY, Color.GRAY);
 
# End Code McGinley Dynamic Indicator
https://tos.mx/FTtV0o1
 
Last edited by a moderator:
@mc01439 Thanks for the reply!

Are the 2 lines in your screenshot: Data and Data2 from the study that @BenTen posted?

On mobile now, will go home and install your shared study - Thanks again!
 
@gang - They are the high and low bands of the MDI created by Mobius
 
Last edited by a moderator:
not clean but i think this is closer to the intended implementation data2 is my take. length is how long before to start creating the MGI and N is smoothing.
FtKgw1t.png

Code:
# McGinley Dynamic Indicator
# Mobius
# V02.11.2011
#  D = D1 + ((I - D1) / ( N * (I/D1)^4))
# where D1= yesterday's Dynamic, I = today's price, N = smoothing factor.

input value= close;
input length= 20;
input Slength = 5;
input N = 20;
def A1= ExpAverage(value, length)[1];
def MDI = A1 + ((value - A1) / 0.6*power(value / A1, 4));
plot Data = MDI; #ExpAverage(MDI, Slength);
Data.SetPaintingStrategy(PaintingStrategy.LINE);
Data.SetLineWeight(2);

plot data2 =fold i = 0 to length
           with s = getvalue (close, length+1)
           do   (s + ((getvalue(close,length-i)-s)/(0.6*N*power(getvalue(close,length-i)/s ,4)) )) ;
 
Last edited by a moderator:
I tried to use the Mc Ginley with the Higs and Lows.
Maybe it could be used
  • to go Long if Price closes over the MCG Higs, Exit when Price touches the MCG Lows
  • to go Short if Price closes below the MCG Lows, Exit when Price touches the MCG Higs
Maybe this could be an approach.
0Tjtn7x.png


Maybe one more detail to use it in connection after the breakout of BB Squeeze
fch4XLS.png
 
Last edited by a moderator:
@samoya I like this one too
vNjx38F.png

Code:
# Mobius
# V04.05.2014

#  D = D[1] + (I - D[1]) / ( N * (I/D[1])^4)
# where D[1] = yesterday's Dynamic, I = today's price, N = smoothing factor.
# Self Adjusting with Fractal Dimensions Algorithm
#re-labelled MDI with 'a' to avoid conflict with above script

input I = close;
input N = 10;
input AscaleLow = 3;
input AscaleHigh = 10;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

def o;
def h;
def l;
def c;
def TR;

def B;
def sDev = stdev(data = I, length = N);
def Ascaled;


# Internal Script
script Scale {
    input c = close;
    input Min = .01;
    input Max =   1;
    def hh = HighestAll(c);
    def ll   = LowestAll(c);
    plot Range = (((Max - Min) * (c - ll)) /  (hh - ll)) + Min;
}

# Calculations
o = (open + close[1]) / 2;
h = Max(high, close[1]);
l = Min(low, close[1]);
c = (o + h + l + close) / 4;
TR = Max(h, c[1]) - Min(l, c[1]);
B = (Log(Sum(TR, N) / (Highest(h, N) - Lowest(l, N)))

             / Log(10)) / (Log(N) / Log(10));

Ascaled = Round(Scale(c = B, min = AscaleLow, max = AscaleHigh), 2);

def D;
D = CompoundValue(1, D[1] + (I - D[1]) /  Ascaled * (Power((I / D[1]), 4)), I);

plot MDIa = D;
MDIa.SetPaintingStrategy(PaintingStrategy.LINE);
MDIa.SetLineWeight(2);
mdia.AssignValueColor(if mdia < mdia[1] then Color.red else if mdia > mdia[1] then Color.green else Color.white);

  MDIa.SetDefaultColor(GetColor(8));

addlabel(1, ( "McGinley Frac=" + MDIa ), if mdia < mdia[1] then color.magenta else color.cyan);
plot LowerBand = D + num_Dev_Dn * sDev;
plot UpperBand = D + num_Dev_Up * sDev;
 
Last edited by a moderator:
Is there any way to develop a scan to pinpoint when McGinley Dynamic crosses price? I tried to do it with a custom scan but the save doesn't light so I couldn't save. This would develop an alert that could be used in a scan.

TY in advance!
 
Help to overlay an Indicator from higher time frame. Plz!
Hi peeps. I would like to have this indicator based on a 30min time frame, overlayed on a 5 min chart. Could some please help out, I'd appreciate it greatly. Thanks
 
Last edited by a moderator:
Hi peeps. I would like to have this indicator based on a 30min time frame, overlayed on a 5 min chart. Could some please help out, I'd appreciate it greatly. Thanks

# Archive Name: McGinley Dynamic Indicator_Mobius
# Archive Section: Trend
# Suggested Tos Name: McGinleyDynamicIndicator_Mobius
# Archive Date: 5.05.2018
# Archive Notes: Lounge 5.02.2018
# 16:59 Mobius: An oldie but goodie
# 17:01 Mobius: The lag on that is only a few bars and that lag can be mostly removed by detrending it by half the input length

# A More Advanced Adaptive Moving Average
# McGinley Dynamic Indicator
# Mobius
# V03.11.2011
# D = D[1] + (I - D[1]) / ( N * (I/D[1])^4)
# where D[1] = yesterday's Dynamic, I = today's price, N = smoothing factor.

input I = close;
input N = 13;

def D;
D = CompoundValue(1, D[1] + (I - D[1]) / (N * Power((I / D[1]), 4)), I);

plot MDI = D;
MDI.SetPaintingStrategy(PaintingStrategy.LINE);
MDI.SetLineWeight(1);
MDI.SetDefaultColor(Color.White);

# End Code McGinley Dynamic Indicator

This is the part that does that

input agg = aggregationPeriod.THIRTY_MIN;
def I = close(period=agg);

Code:
# Archive Name: McGinley Dynamic Indicator_Mobius
# Archive Section: Trend
# Suggested Tos Name: McGinleyDynamicIndicator_Mobius
# Archive Date: 5.05.2018
# Archive Notes: Lounge 5.02.2018
# 16:59 Mobius: An oldie but goodie
# 17:01 Mobius: The lag on that is only a few bars and that lag can be mostly removed by detrending it by half the input length

# A More Advanced Adaptive Moving Average
# McGinley Dynamic Indicator
# Mobius
# V03.11.2011
# D = D[1] + (I - D[1]) / ( N * (I/D[1])^4)
# where D[1] = yesterday's Dynamic, I = today's price, N = smoothing factor.

input agg = aggregationPeriod.THIRTY_MIN;
def I = close(period=agg);
input N = 13;

def D;
D = CompoundValue(1, D[1] + (I - D[1]) / (N * Power((I / D[1]), 4)), I);

plot MDI = D;
MDI.SetPaintingStrategy(PaintingStrategy.LINE);
MDI.SetLineWeight(1);
MDI.SetDefaultColor(Color.White);

# End Code McGinley Dynamic Indicator
 
Hi Sleepyz, When I use this script with symbol $NYHGH-$NYLOW it plots a vertical line on my weekly chart causing thinkorswim to freeze for about two minutes. I unchecked the show plot of the MDI line, went to a different symbol and back to $NYHGH-$NYLOW and the issue doesn't occur. I have the same vertical line with $TICK on the 4HR chart and with stock symbol $EFTR on a 4Hr chart. $EFTR dropped from $16 to $2 a month ago, which may be causing the dynamic indicator to print a vertical line. I spent over an hour trying to figure it out with Schwab tech support, and he told me that the error reports indicated that my 8gb Radion graphics card maxes out to 100% when I select these symbols. After we hung up, I noticed they all had a vertical line from the MDI script and found the issue went away when I unchecked the show plot of the MDI line.
 
Last edited:
Hi Sleepyz, When I use this script with symbol $NYHGH-$NYLOW it plots a vertical line on my weekly chart causing thinkorswim to freeze for about two minutes. I unchecked the show plot of the MDI line, went to a different symbol and back to $NYHGH-$NYLOW and the issue doesn't occur. I have the same vertical line with $TICK on the 4HR chart and with stock symbol $EFTR on a 4Hr chart. $EFTR dropped from $16 to $2 a month ago, which may be causing the dynamic indicator to print a vertical line. I spent over an hour trying to figure it out with Schwab tech support, and he told me that the error reports indicated that my 8gb Radion graphics card maxes out to 100% when I select these symbols. After we hung up, I noticed they all had a vertical line from the MDI script and found the issue went away when I unchecked the show plot of the MDI line.

That is an indicator by Mobius. All I did was explain how to possibly make it Multi-timeframe. Even the original code in my post does not plot a usable line with the symbols you provided.

I would recommend that you try to use a different version from this thread.
 

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