 # 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.

Is the (close,85) in the code a mistake, shouldn't it be 8? The same with signal 2

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;

• BenTen

#### BenTen

Staff member
Staff
VIP
@mc01439 It could’ve been. I didn’t notice. Do you see any difference when correcting it?

#### mc01439

##### Well-known member
2019 Donor
VIP
Yes it does. Ran a strategy test on day charts for 5 years. This is a raw run - signal to signal.

For Signal
set at 85 (\$66170) 28 trades
set at 8 (\$310) 28 trades

For Signal 2
set at 85 +\$4530 18 trades
set at 8 (\$3130) 17 trades

Ran a strategy test on Weekly for 10 years. This is a raw run - signal to signal.

For Signal
set at 85 (\$39680) 18 trades
set at 8 (\$55240) 16 trades

• s1111

#### BenTen

Staff member
Staff
VIP
Hmm. Interesting. Why is it that Signal 2 (set to 85) is profitable?

#### mc01439

##### Well-known member
2019 Donor
VIP
I will need to look deeper. Did the test above real quick. Another option written by Mobius.

This one tested out signal to signal on the 5 year Daily @ \$73,640.00. Please keep in mind this is a raw test.

Code used for the test;
def HT = MDI > MDI;
def LT = MDI < MDI;

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

# 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``````

• • #### cherif

##### Member
VIP
The close,85 is only a typo. Close,8 is the correct code entry.

• mc01439 and BenTen

#### ganq

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

#### ganq

##### New member
2019 Donor
VIP

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

VIP

#### Rojo Grande

##### Member
VIP
Hello All, I have been trying to write a watch list column that shows the closing price, and if it was above the McGinley line the price displays green, and if below the line price displays red. Since coding is new to me (I am getting better and learning lots) this one is over my head. Please help, thanks all.

Update: Perseverance pays off, GOT IT!!!!

#### 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
it looks like there is an error in the formula. I only see a square not a power of 4. And the N variable is missing Which looks like it is a constant of 0.6. Snip below from the original post. Odd that the code does not follow the formula written just above it in the script

# D = D1 + ((I - D1) / ( N * (I/D1)^4))

def MDI = A1 + ((value - A1) / Sqr(value / A1));

from one of the links on the previous page has a section of code that also shows the ^4 in the equation
mg1 = na(mg1) ? src : mg1 + (src - mg1) / (0.6 * MGLength1 * pow(src/mg1, 4))

The more I look at the code the more I think that there are some errors in the logic. The MGI is not initialized correctly as the definition of the average is to use the previous average with the present value, the previous average is set up as an EMA from one candle back, not MGI. I am not sure the best way to initialize this average, I have been playing around with the <fold> function from the TMO indicator to try and set the MGI = to the value at candle [length] back but not quite sure how to have the <do> function iterate starting at a large number and working to 0.

Last edited:

#### 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!