Hi boatman,

Here is how I coded it in MQ4 (should be more or less the same in lite-C):

/* -----------------------------------------------------------
agcDecayFactor
calculate decay factor for automatic gain control (AGC)
Concept see John Ehlers: Cycle Analytics For Traders, 54f
----------------------------------------------------------- */
double agcDecayFactor(const int _lowerCutoff, const int _higherCutoff, const double _acceptableSlope = 1.5) {
double hc = (double)_higherCutoff / 2.0; // calculate half the cutoff periods
double lc = (double)_lowerCutoff / 2.0;
double delta = MathAbs(hc - lc); // delta >= 0, so division in expo is always safe
double accSlope = -MathAbs(_acceptableSlope); // make sure gain slope in decibels is negative
double expo = accSlope / (delta * (delta + 1.0)); // calculate exponent to solve logarithm (base: 10)
double out = MathPow(10.0, expo);
return(out);
}


I'll also attach a scan showing how I derived my formula from Ehlers' text on pp. 54-55. Hope it's clear enough. If not, don't hesitate to ask.

Are you aware of the fact that Ehlers sometimes uses the decay factor K directly (0.991), sometimes its sqare root (0.995)? In the autocorrelation chapter, he uses 0.995 in his code listing (8-3), but mentions 0.991 in the text on page 103. Since there is another mistake in the code (370 vs. 360 degrees), I would think that 0.991 is correct. What's your opinion?

Cheers, simplex


Attached Files
AGC_formula_simplex.jpg (278 downloads)
AGC: derivation of decay factor formula

If you can't explain it simply, you don't understand it well enough. (Albert Einstein)