Attachments forums

List of attachments posted on this forum.


All files on forums: 136849

Re: No Nonsense Forex - Indicators

Centaur, Fri Dec 02, 2022 7:35 pm

Kaufman's Adaptive Moving Average (KAMA)

Just some final words on the KAMA, I've found a lot of different calculations for the indicator, but I believe this below to be as true to the original works as possible, written as a function. I've attached the original works of Kaufman.

Also the Fast EMA period and the Slow EMA period really is not to be adjusted or optimized, this only gives a range of plotting seeing your scaled smoothing constant is squared this would be equivalent to an ema of 900 periods in a range bound market (flat, no reaction to price action), and an ema of 4 periods in a very trending market (very reactive to price action), so I've removed it as input and made them fix smoothing constants. The only input is the lookback period.

Kaufman also recommends to trade on the change of the KAMA value. Buy when the KAMA turns up, and sell when the KAMA turns down, to prevent whipsaws in range bound markets Kaufman introduced a filter based on the standard deviation of the change of the KAMA value times a percentage, this percentage can be an input.

You'll notice I've bounded the efficiency ration between 0.000001 and 0.9999999. In other words the efficiency ratio is close to 0.0 but never 0.0 and close to 1.0 but never 1.0. Also checking for a divide by 0.0 error, in reality this can never be due to the calculation, but just in case.

Code: Select all

//+------------------------------------------------------------------+
//| Indicator: Kaufman's Adaptive Moving Average (KAMA)              |
//+------------------------------------------------------------------+
int inKAMA(const int rates_total, const int prev_calculated, const int len, const double &in_buf[], double &out_buf[])
  {
//--- check period
   if(rates_total < len)
      return(0);
//--- calculate start position
   int bar;
   double fast_sc = StringToDouble(DoubleToString(2.0 / 3.0, 4)), slow_sc = StringToDouble(DoubleToString(2.0 / 31.0, 4));
   if(prev_calculated == 0)
      bar = 0;
   else
      bar = prev_calculated - 1;
//--- main loop
   for(int i = bar; i < rates_total && !_StopFlag; i++)
     {
      if(i < len)
         out_buf[i] = in_buf[i];
      else
        {
         double direction = in_buf[i] - in_buf[i - len];
         double volatility = 0.0;
         for(int k = 0; k < len; k++)
            volatility += fabs(in_buf[i - k] - in_buf[i - k - 1]);
         double er = volatility == 0.0 ? 0.0 : direction == volatility ? 0.9999999999999 : direction == 0.0 ? 0.0000000000001 : fabs(direction / volatility);
         double ssc = pow(er * (fast_sc - slow_sc) + slow_sc, 2);
         out_buf[i] = out_buf[i - 1] + ssc * (in_buf[i] - out_buf[i - 1]);
        }
     }
   return(rates_total);
  }
All files in topic