# McGinley Dynamic Indicator for ThinkorSwim

#### BenTen

Staff member
Staff
VIP
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. ### 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);
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);
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 <= Data2 and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal = if Data < Data2 and Data >= Data2 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 + (( close - _md ) / ( periods *
power( close / _md, 4 ) ) ), close );
plot MD = _md;
MD.setdefaultcolor(color.CYAN);
MD.setlineweight(2);
MD.hide();

def buySignal2 = if MD > WS and MD <= WS and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal2 = if MD < WS and MD >= WS 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 + (( close - _md ) / ( periods * Power( close / _md, 4 ) ) ), close );

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

https://tos.mx/XqTZTf
Credits:

#### mc01439

##### Well-known member
2019 Donor
VIP
This indicator seems to give much more information for a better trade with the following plots. It seems the other plots were added to the McGinley Dynamic Indicator but I think they enhance it.

Data - ON
Date2 - ON
EMA8 - OFF
Signal - ON
WS - OFF
MD - OFF
Signal2 - OFF because seems to be a late or confirming signal

Tried to paste a chart image but not sure how to use the new insert image.

Thank You for posting.

Last edited by a moderator:
• ehkawdoh and BenTen

#### ganq

##### Member
2019 Donor
Hi @mc01439 and @BenTen - Thanks for sharing this. Do you guys have any descriptions of the multiple inputs and the plots on this indicator and what they mean?

#### mc01439

##### Well-known member
2019 Donor
VIP
@ganq - 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.

https://tos.mx/FTtV0o1

Last edited by a moderator:

#### ganq

##### Member
2019 Donor

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!

#### mc01439

##### Well-known member
2019 Donor
VIP
@gang - No the one attached is the high and low bands of the MDI by Mobius that I referenced in post #9.

• ganq

#### BenTen

Staff member
Staff
VIP
@David45 Try this Detrended MACD

Code:
``````# Detrended MACD

# Mobius

# V01.04.2015

declare lower;

input fastLength = 12;

input slowLength = 26;

input MACDLength = 9;

input averageType = AverageType.EXPONENTIAL;

input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close + (close - close[(fastlength-1)/2]), fastLength) - MovingAverage(averageType, close + (close - close[(slowLength-1)/2]), slowLength);

plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;

plot ZeroLine = 0;

plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;

plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);

DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));

Avg.SetDefaultColor(GetColor(8));

Diff.SetDefaultColor(GetColor(5));

Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

Diff.SetLineWeight(3);

Diff.DefineColor("Positive and Up", Color.GREEN);

Diff.DefineColor("Positive and Down", Color.DARK_GREEN);

Diff.DefineColor("Negative and Down", Color.RED);

Diff.DefineColor("Negative and Up", Color.DARK_RED);

Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff then Diff.color("Negative and Down") else Diff.color("Negative and Up"));

ZeroLine.SetDefaultColor(GetColor(0));

UpSignal.SetDefaultColor(Color.UPTICK);

UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownSignal.SetDefaultColor(Color.DOWNTICK);

DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

# End Code Detrended MACD``````

#### Mr Bee

##### New member
VIP
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.

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);
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)) )) ;``````

#### Hustle

##### New member
Hey @BenTen . Is there a MTF version of the Detrended MACD? If not, I can ask in a new thread. Thank you in advance!

#### BenTen

Staff member
Staff
VIP
@Hustle Detrended MACD is basically the same as the regular MACD but all the lengths are shorten by half. You can use the the MACD MTF here and adjust the length from there.

#### samoya

##### Active member
VIP
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. Maybe one more detail to use it in connection after the breakout of BB Squeeze #### germanburrito

##### Active member
@samoya I like this one too

Code:
``````# Mobius
# V04.05.2014

#  D = D + (I - D) / ( N * (I/D)^4)
# where D = 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) / 2;

h = Max(high, close);

l = Min(low, close);

c = (o + h + l + close) / 4;

TR = Max(h, c) - Min(l, c);

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 + (I - D) /  Ascaled * (Power((I / D), 4)), I);

plot MDIa = D;

MDIa.SetPaintingStrategy(PaintingStrategy.LINE);

MDIa.SetLineWeight(2);

mdia.AssignValueColor(if mdia < mdia then Color.red else if mdia > mdia then Color.green else Color.white);

MDIa.SetDefaultColor(GetColor(8));

addlabel(1, ( "McGinley Frac=" + MDIa ), if mdia < mdia then color.magenta else color.cyan);

plot LowerBand = D + num_Dev_Dn * sDev;
plot UpperBand = D + num_Dev_Up * sDev;``````

• mailbagman2000

#### ibenjamin305

##### New member
Can anyone add any alerts(bell or chime) when the sslup and ssldown lines cross each other?
Can you repost the study because the link doesnt work.

Last edited by a moderator:

#### MerryDay

Staff member
Staff
VIP
@ibenjamin305
The Shared Link: https://tos.mx/gYEjjz0 works just fine or you can just copy the study below:
Ruby:
``````# 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);
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);
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 <= Data2 and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal = if Data < Data2 and Data >= Data2 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 + (( close - _md ) / ( periods *
power( close / _md, 4 ) ) ), close );
plot MD = _md;
MD.setdefaultcolor(color.CYAN);
MD.setlineweight(2);
MD.hide();

def buySignal2 = if MD > WS and MD <= WS and expaverage(close,8) > average(close,13) then 1 else 0;
def sellSignal2 = if MD < WS and MD >= WS 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);``````

Last edited:
• ibenjamin305

#### QUIKTDR1

##### Active member
VIP
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.

#### MerryDay

Staff member
Staff
VIP
McGinley Dynamic Indicator Scan  Ruby:
``````#McGinleyIndicator  Scan Only
input periods = 10;
rec _md = CompoundValue( 1, _md + (( close - _md ) / ( periods * Power( close / _md, 4 ) ) ), close );
def MD = _md;
plot signal = close crosses above MD ;``````
@QUIKTDR1

#### ehkawdoh

##### New member
WHAT TIME FRAME DO YOU USE PLS?

Last edited by a moderator:

#### Cali-G

##### New member
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

# 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

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

input I = close;
input N = 13;

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

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

# End Code McGinley Dynamic Indicator

Last edited by a moderator:

### Not the exact question you're looking for?

87k+ Posts
190 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?