# Arthur Merrill's Deviation From Expected (DFE)

I have long been a fan of Arthur A. Merrill and from time to time like reread his work. Here is a link to an article he wrote in the Market Technicians Journal on DFE.

https://filebin.net/0ur2wuusz1hj57xi

I would like to program DFE and Art's tests of DFE in Amibroker, but am unsure of the best way to go about it; how to split the various tasks up to their logical AB module matches. This is not a please program this for me note. What I am asking for are suggestions as to process; how to best go about coding up Art's indicator and approach to testing it on the platform.

Best regards to all,

John Bollinger -- who obviously has too much time on his hands during the lockdown.

PS I offer a bounty of a nice IPA or two...

Just a quick look and looking at page 25 it is basically just transcribing.

Total is Sum. Period is 20 weeks. So `Sum(x, 20)`.

Rest is just copying pages 24/ 25 almost `1:1`.

``````d = ln(C); // log(C);
e = ln(Foreign("^SPX", "C"));// log(Foreign("^SPX", "C"));

//.... next of pages 24/25
``````

So next create vars `f` and `g`.

Then create new variables (four ones) for Sum.. e.g. `d_total` to `g_total`.
Then create A and B using those four `*_total` vars.
Next `Y` and `DFE`.

Y is `e^(A+B*ln(X))`

e is mathematical constant (Euler). See Wiki.

BTW there are a few typos on page 25 2 Likes

@BBands @fxshrat posted just as I was about to put out my meager effort, but based on the appendix here are some starting calculations

``````// Appendix B

SP500 	= Foreign( "\$SPX", "C" ); //Replace with ticker of your choice
Y 		= Close;  // the close of the currently selected ticker

d = log( C ); // or you could put the variable "Y" in place of the Close
e = log( SP500 );
f = e * e;
g = e * d;

// the letter's in AmiBroker's afl are not case specific
// so we can't just use the uppercase letters like in the article
// Assuming WEEKLY periodicity (use Weekly in AA window) he uses 20 as the parameter
dd = Sum( d, 20 );
ee = Sum( e, 20 );
ff = Sum( f, 20 );
gg = Sum( g, 20 );

// the next line in the paper is confusing (to me at least)
// B = Beta = ( G - DE/20 )/(F=(E*E/20)  ??the denominator is that just F?
// i believe it is a typo as a later paragraph has a modification spelled out more clearly

Beta	= ( gg - dd*ee / 20 ) / ( ff - ( ( ee*ee ) / 20 ) );
Alpha	= dd / 20 - ( Beta*ee ) / 20;
``````
2 Likes

Didn't he say he wanted to do himself? Could be wrong though.

Sorry, it just looked like too much fun to not make an attempt. Also I just had a quick read but the paper begins by discussing what I believe is Comparative Strength (using the term relative strength) and goes on to discuss the difference in price from a regression line.

I'm tied up the rest of the weekend but some of those lines lead me to think perhaps there is some use for

``````Regress20	= LinearReg( log( SP500), 20 );
Diff	 	= log(C) - Regress20;
``````

Good luck John, and let us know what you come up with!

1 Like

Just for the reference, since it was mentioned earlier in this thread: `e^x` is `exp(x)` function in AFL http://www.amibroker.com/f?exp

Thank you for reminder to existing function with regards to Euler.

``````/// @link https://filebin.net/0ur2wuusz1hj57xi/MTA_Journal__14_-_1982_August-pages-23-30.pdf?t=k1eyz2sa
/// possible AFL version
period = 20;

d = ln(C);// equivalent to log(C)

index = Foreign("^GSPC", "C");
average = MA(index, 1);

e = ln(average);// equivalent to log(...)
f = e*e;
g = e*d;

d_sum = Sum(d, period);
e_sum = Sum(e, period);
f_sum = Sum(f, period);
g_sum = Sum(g, period);
printf( "\nD:%g, E:%g, F:%g, G:%g\n", d_sum, e_sum, f_sum, g_sum);

e_ma = MA(e,period);
B = beta = (g_sum-d_sum*e_ma)/(f_sum - e_sum*e_ma);
A = alpha = MA(d,period) - B*e_ma;//(d_sum-B*e_sum)/period;//
printf( "A:%g, B:%g\n", A, B);

//euler = 2.71828;
//Y = euler^(A+B*d);
// equivalent to upper
Y = exp(A+B*d);
DFE = (Ref(Y,-1)/Y-1)*100;
printf("Y:%g, DFE:%g\n", Y, DFE);

Plot( DFE, "DFE", colorRed);
`````` 9 Likes

Why calculate average of 1 period ?

If you look at the PDF then you will see that at last page there it is listed the average of SP500. But since (AFAICS) there is not a MA period mentioned in that paper I set to default `1` since MA of period 1 is just index. So you may set MA period yourself or may remove MA and replace by just index.

3 Likes

fxshrat, portfoliobuilder, Tomasz:

A big thanks to you all! If we are ever in the same place at the same time a nice dinner on me.

I'll dig in to your cornucopia now and update next week.

Stay safe,

John

3 Likes

fxhrat,

After a close read of a couple of related papers by Art I think that he used the word average interchangeably with index, so today we would write:
(C) CLOSING INDEX, S&P 500
Which means that your single period average is in fact correct.

`` John``

Hey fxshrat,

Could you please expand on this a bit? I wrote it as
100 * d / Y -100;

Thanks,

`` John``
1 Like

Good catch! It appears that when this paper was retyped for the MTA Journal back in the day a few typos crept in. I have a copy Art's original paper. The correct lines are:

``````B = Beta = (G - DE/20)/(F - E^2/20))
A = Alpha = D/20 - BE/20
``````

Best,

`` John``

But AFAICS SP500 is not closing average but it is market capitalization divided by divisor (with divisor being a \$ value at around 9 billion set by Standard & Poor's).
So it is \$ divided by \$, so unit being canceled and we get some index value.

Well, who knows what he meant...

`(x/y-1)*100` is same as `x/y*100-100`. First one is just factorised.

As for using Ref() as numerator of DFE calculation... somewhere in that paper there was mentioned to compare current value to a previous value....

Now after second look and looking at table on last page I see that 24.25 of DFE equation refers to log(C) so yes you may change to `DFE = (d/Y-1)*100;`  Here is update:

``````/// @link https://filebin.net/0ur2wuusz1hj57xi/MTA_Journal__14_-_1982_August-pages-23-30.pdf?t=k1eyz2sa
/// possible AFL version 1.1
period = 20;

d = ln(C);// equivalent to log(C)

index = Foreign("^GSPC", "C");
average = index;// or MA(index, some_period); depending on what the paper meant

e = ln(average);// equivalent to log(...)
f = e*e;
g = e*d;

d_sum = Sum(d, period);
e_sum = Sum(e, period);
f_sum = Sum(f, period);
g_sum = Sum(g, period);
printf( "\nD:%g, E:%g, F:%g, G:%g\n", d_sum, e_sum, f_sum, g_sum);

e_ma = MA(e,period);
B = beta = (g_sum-d_sum*e_ma)/(f_sum - e_sum*e_ma);
A = alpha = MA(d,period) - B*e_ma;//(d_sum-B*e_sum)/period;//
printf( "A:%g, B:%g\n", A, B);

Y = exp(A+B*d);// -> Euler^(A+B*d);
DFE = (d/Y-1)*100;
printf("Y:%g, DFE:%g\n", Y, DFE);

Plot( DFE, "DFE", colorRed);
``````
3 Likes

Note: If one just wants to calculate the DFE, one can drop the whole sums.
AFAICS this also leads to more precise results. (see e.g. Beta).
The only thing that makes me wonder is the totally different Y... in comparison with the "Sum-Y" it often seems so random.

``````/// @link https://filebin.net/0ur2wuusz1hj57xi/MTA_Journal__14_-_1982_August-pages-23-30.pdf?t=k1eyz2sa
period = 20;

d = ln(C); // equivalent to log(C)

index = Foreign("^GSPC", "C");
average = index; // or MA(index, some_period); depending on what the paper meant
e = ln(average); // equivalent to log(...)

d_ma = MA(d, period);
e_ma = MA(e, period);
B = beta = Correlation(d, e, period) * StDev(d, period) / (StDev(e, period) + 1e-9);
A = alpha = d_ma - B*e_ma;
printf( "A:%g, B:%g\n", A, B);

Y = exp(A+B*d); // -> Euler^(A+B*d);
DFE = (d/Y-1)*100;
printf("Y:%g, DFE:%g\n", Y, DFE);

Plot( DFE, "DFE", colorBlue);
``````
2 Likes

For complicity, documented Beta calculation can be found here (for example):

Beta could be calculated by first dividing the security's standard deviation of returns by the benchmark's standard deviation of returns. The resulting value is multiplied by the correlation of the security's returns and the benchmark's returns.

Love: "for complicity". Great usage!

`` John Bollinger``
1 Like

lol

Actually what I really meant was "for sake of completeness".
Sometimes I mix up words sounding similar (English not being mother tongue).

Whatever got it to the screen, I really liked it!

1 Like