Category Trend  Published on 30/06/2015

Kaufman Adaptative Moving Average

Description

The Adaptive Moving Average (AMA) aka Kaufman Adaptive Moving Average (KAMA) was created by Perry Kaufman and first presented in his book Smarter Trading (1995).  This moving average offered a significant advantage over previous attempts at ‘intelligent’ averages because it allowed the user greater control. 

Author : Abdallah HACID

Solution Visual studio


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Lib;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    [Levels(50, 0, -50)]
    public class KfAdaptMAIndicator : Indicator
    {
        [Parameter("Period", DefaultValue = 9)]
        public int Period { get; set; }

        [Parameter("FastPeriod", DefaultValue = 2)]
        public int FastPeriod { get; set; }

        [Parameter("SlowPeriod", DefaultValue = 30)]
        public int SlowPeriod { get; set; }

        [Output("Kaufman Adaptative Moving Average", Color = Colors.SteelBlue)]
        public IndicatorDataSeries Result { get; set; }


        protected override void Initialize()
        {
            // Initialize and create nested indicators
        }

        public override void Calculate(int index)
        {
            if (index != 0)
            {
                int period = Math.Min(index, Period);
                double lastClose = MarketSeries.Close.LastValue;
                DataSeries Close = MarketSeries.Close;

                double Fastest = 2.0 / (FastPeriod + 1);
                double Slowest = 2.0 / (SlowPeriod + 1);
                double denominator = Close.fold((acc, previewClose, close) => acc + Math.Abs(close - previewClose), (double)0, +1, index - period, index);
                ;
                double numerator = Math.Abs(lastClose - MarketSeries.Close[index - period]);
                ;
                double alpha;

                if (denominator != 0)
                    alpha = Math.Pow(((numerator / denominator) * (Fastest - Slowest) + Slowest), 2);
                else
                    alpha = 0;

                Result[index] = (alpha * lastClose) + ((1 - alpha) * Result.Last(1));
            }
            else
                Result[index] = 1;
        }
    }
}


AY
aysos75

Joined on 28.09.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: KfAdaptMAIndicator.algo
  • Rating: 5
  • Installs: 4125
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
jani's avatar
jani · 2 years ago

Thanks, an excellent indicator. I made a version without the "Close.fold..." feature, as I do not understand how that works and I needed to write the calculation as a function to incorporate this  to my cBot indicator

AY
aysos75 · 9 years ago

@MZen

Close.fold((acc, previewClose, close) => acc + Math.Abs(close - previewClose), (double)0, +1, index - period, index);

is an anonyme method, with abstract type, the definition is in the cAlgo.Lib (file DataSeriesExtensions).

 The fold method traverses the list by applying an operator to "compress" into a single value. here he make the sum of (close(i)-previewClose(i)).

MZ
MZen · 9 years ago

Could you tell what Close.fold is and where to find it?