4
$\begingroup$

I'm trying to build a class to create Aroon series. But it seems I don't understand the steps well. I'm not sure about what purpose I have to use the period parameter.

Here is my first attempt:

/// <summary>
/// Aroon
/// </summary>
public class Aroon : IndicatorCalculatorBase
{
    public override List<Ohlc> OhlcList { get; set; }
    public int Period { get; set; }

    public Aroon(int period) 
    {
        this.Period = period;
    }

    /// <summary>
    /// Aroon up: {((number of periods) - (number of periods since highest high)) / (number of periods)} x 100
    /// Aroon down: {((number of periods) - (number of periods since lowest low)) / (number of periods)} x 100
    /// </summary>
    /// <see cref="http://www.investopedia.com/ask/answers/112814/what-aroon-indicator-formula-and-how-indicator-calculated.asp"/>
    /// <returns></returns>
    public override IIndicatorSerie Calculate()
    {
        AroonSerie aroonSerie = new AroonSerie();

        int indexToProcess = 0;

        while (indexToProcess < this.OhlcList.Count)
        {
            List<Ohlc> tempOhlc = this.OhlcList.Skip(indexToProcess).Take(Period).ToList();
            indexToProcess += tempOhlc.Count;

            for (int i = 0; i < tempOhlc.Count; i++)
            {   
                int highestHighIndex = 0, lowestLowIndex = 0;
                double highestHigh = tempOhlc.Min(x => x.High), lowestLow = tempOhlc.Max(x => x.Low);
                for (int j = 0; j < i; j++)
                {
                    if (tempOhlc[j].High > highestHigh)
                    {
                        highestHighIndex = j;
                        highestHigh = tempOhlc[j].High;
                    }

                    if (tempOhlc[j].Low < lowestLow)
                    {
                        lowestLowIndex = j;
                        lowestLow = tempOhlc[j].Low;
                    }
                }

                int up = ((this.Period - (i - highestHighIndex)) / this.Period) * 100;
                aroonSerie.Up.Add(up);

                int down = ((this.Period - (i - lowestLowIndex)) / this.Period) * 100;
                aroonSerie.Down.Add(down);
            }
        }

        return aroonSerie;
    }
}

Is there anyone else tried to do that before? Here is a few reference points about the indicator:

$\endgroup$
  • 1
    $\begingroup$ Hi anilca, welcome to Quant.SE! For those that don't know (like me), can you provide a reference where this indicator is described? $\endgroup$ – Bob Jansen Apr 22 '15 at 8:38
  • 1
    $\begingroup$ Hi @BobJansen. I added a few references describing the aroon indicator. Thanks. $\endgroup$ – anilca Apr 22 '15 at 12:28
2
+50
$\begingroup$

I answered @Anilca's question in SO (and the answer was accepted)

I summarize my answer with the working solution:

public class Aroon : IndicatorCalculatorBase
{

    public override List<OhlcSample> OhlcList { get; set; }
    private readonly int _period;

    public int Period
    {
        get { return _period; }
    }

    public Aroon(int period)
    {
        _period = period;
    }

    public override IIndicatorSerie Calculate()
    {
        var aroonSerie = new AroonSerie();
        for (var i = _period; i < OhlcList.Count; i++)
        {

            var aroonUp = CalculateAroonUp(i);
            var aroonDown = CalculateAroonDown(i);

            aroonSerie.Down.Add(aroonDown);
            aroonSerie.Up.Add(aroonUp);
        }

        return aroonSerie;
    }

    private double CalculateAroonUp(int i)
    {
        var maxIndex = FindMax(i - _period, i);

        var up = CalcAroon(i - maxIndex);

        return up;
    }

    private double CalculateAroonDown(int i)
    {
        var minIndex = FindMin(i - _period, i);

        var down = CalcAroon(i - minIndex);

        return down;
    }

    private double CalcAroon(int numOfDays)
    {
        var result = ((_period - numOfDays)) * ((double)100 / _period);
        return result;
    }

    private int FindMin(int startIndex, int endIndex)
    {
        var min = double.MaxValue;
        var index = startIndex;
        for (var i = startIndex; i <= endIndex; i++)
        {
            if (min < OhlcList[i].Low)
                continue;

            min = OhlcList[i].Low;
            index = i;
        }
        return index;
    }

    private int FindMax(int startIndex, int endIndex)
    {
        var max = double.MinValue;
        var index = startIndex;
        for (var i = startIndex; i <= endIndex; i++)
        {
            if (max > OhlcList[i].High)
                continue;

            max = OhlcList[i].High;
            index = i;
        }
        return index;
    }
}

P.S

Guys, i usually active at SO... if anyone need clarifications, please ask in the source question (and feel free to do this)

$\endgroup$
0
$\begingroup$
 public class Aroon
    {
        public bool AroonDown
        {
            get;
            set;
        }
        public double Period
        {
            get;
            set;
        }
        public Aroon()
        {
        }
        public Aroon(double period)
        {
            Period = period;
        }
        public IList<double> Execute(IList<double> src)
        {
            if (!this.AroonDown)
            {
                return this.ExecuteUp(src);
            }
            return this.ExecuteDown(src);
        }
        public IList<double> ExecuteDown(IList<double> src)
        {
            double[] period = new double[src.Count];
            for (int i = 0; i < src.Count; i++)
            {
                double num = IndUtil.LowestBarNum(src, i, Period);
                period[i] = 100 * (Period - num) / Period;
            }
            return period;
        }
        public IList<double> ExecuteUp(IList<double> src)
    {
        double[] period = new double[src.Count];
        for (int i = 0; i < src.Count; i++)
        {
            double num = IndUtil.HighestBarNum(src, i, Period);
            period[i] = 100 * ((Period - num) / Period);
        }
        return period;
    }
    }

    public class IndUtil
    {
      public static double  HighestBarNum(IList<double> src, int i, double Period)
        {
             int maxIndex = -1;
         double maxValue = Double.MinValue; // Immediately overwritten anyway

            int index = 0;

            int srccount = src.Count();
            var mincount = Math.Min(i + Period, srccount);

            for (int k = i; k < mincount; k++)
            {
                double value = src[k];
                if (value.CompareTo(maxValue) > 0 || maxIndex == -1)
                {
                    maxIndex = index;
                    maxValue = value;
                }
                index++;
            }
            return maxIndex;
        }
      public static double LowestBarNum(IList<double> src, int i, double Period)
      {
          int minIndex = -1;
          double minValue = Double.MaxValue; // Immediately overwritten anyway

          int index = 0;


           int srccount=src.Count();
           var mincount = Math.Min(i + Period, srccount);
          for (int k = i; k < mincount; k++)
          {

              double value = src[k];
              if (value.CompareTo(minValue) < 0 || minIndex == -1)
              {
                  minIndex = index;
                  minValue = value;
              }
              index++;
          }
          return minIndex;
      }
    }
$\endgroup$
  • $\begingroup$ I don't know how to implement LowestBarNum and HighestBarNum methods. Can you help me please? $\endgroup$ – anilca Apr 24 '15 at 7:28
  • $\begingroup$ added 2 methods and replaced i=1 to i=0 $\endgroup$ – John Apr 24 '15 at 8:35
0
$\begingroup$

AROON Indicator is a built-in function in TA-lib, you should try it first rather than implement it yourself.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.