Example #1
0
    public void Removed()
    {
        List <WmaResult> results = quotes.GetWma(20)
                                   .RemoveWarmupPeriods()
                                   .ToList();

        // assertions
        Assert.AreEqual(502 - 19, results.Count);

        WmaResult last = results.LastOrDefault();

        Assert.AreEqual(246.5110m, Math.Round((decimal)last.Wma, 4));
    }
Example #2
0
    public void Standard()
    {
        List <WmaResult> results = quotes.GetWma(20).ToList();

        // assertions

        // proper quantities
        // should always be the same number of results as there is quotes
        Assert.AreEqual(502, results.Count);
        Assert.AreEqual(483, results.Where(x => x.Wma != null).Count());

        // sample values
        WmaResult r1 = results[149];

        Assert.AreEqual(235.5253m, Math.Round((decimal)r1.Wma, 4));

        WmaResult r2 = results[501];

        Assert.AreEqual(246.5110m, Math.Round((decimal)r2.Wma, 4));
    }
Example #3
0
        public void GetWmaTest()
        {
            int lookbackPeriod = 20;
            IEnumerable <WmaResult> results = Indicator.GetWma(history, lookbackPeriod);

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count());
            Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Wma != null).Count());

            // sample values
            WmaResult r1 = results.Where(x => x.Index == 502).FirstOrDefault();

            Assert.AreEqual(246.5110m, Math.Round((decimal)r1.Wma, 4));

            WmaResult r2 = results.Where(x => x.Index == 150).FirstOrDefault();

            Assert.AreEqual(235.5253m, Math.Round((decimal)r2.Wma, 4));
        }
Example #4
0
        public void GetWma()
        {
            int lookbackPeriod       = 20;
            List <WmaResult> results = Indicator.GetWma(history, lookbackPeriod).ToList();

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count);
            Assert.AreEqual(502 - lookbackPeriod + 1, results.Where(x => x.Wma != null).Count());

            // sample values
            WmaResult r1 = results[501];

            Assert.AreEqual(246.5110m, Math.Round((decimal)r1.Wma, 4));

            WmaResult r2 = results[149];

            Assert.AreEqual(235.5253m, Math.Round((decimal)r2.Wma, 4));
        }
Example #5
0
    // HULL MOVING AVERAGE
    /// <include file='./info.xml' path='indicator/*' />
    ///
    public static IEnumerable <HmaResult> GetHma <TQuote>(
        this IEnumerable <TQuote> quotes,
        int lookbackPeriods)
        where TQuote : IQuote
    {
        // sort quotes
        List <TQuote> quotesList = quotes.SortToList();

        // check parameter arguments
        ValidateHma(lookbackPeriods);

        // initialize
        List <Quote> synthHistory = new();

        List <WmaResult> wmaN1 = GetWma(quotes, lookbackPeriods).ToList();
        List <WmaResult> wmaN2 = GetWma(quotes, lookbackPeriods / 2).ToList();

        // roll through quotes, to get interim synthetic quotes
        for (int i = 0; i < quotesList.Count; i++)
        {
            TQuote q = quotesList[i];

            Quote sh = new()
            {
                Date = q.Date
            };

            WmaResult w1 = wmaN1[i];
            WmaResult w2 = wmaN2[i];

            if (w1.Wma != null && w2.Wma != null)
            {
                sh.Close = (decimal)((w2.Wma * 2m) - w1.Wma);
                synthHistory.Add(sh);
            }
        }

        // add back truncated null results
        int sqN      = (int)Math.Sqrt(lookbackPeriods);
        int shiftQty = lookbackPeriods - 1;

        List <HmaResult> results = quotesList
                                   .Take(shiftQty)
                                   .Select(x => new HmaResult
        {
            Date = x.Date
        })
                                   .ToList();

        // calculate final HMA = WMA with period SQRT(n)
        List <HmaResult> hmaResults = synthHistory.GetWma(sqN)
                                      .Select(x => new HmaResult
        {
            Date = x.Date,
            Hma  = x.Wma
        })
                                      .ToList();

        // add WMA to results
        results.AddRange(hmaResults);
        results = results.OrderBy(x => x.Date).ToList();

        return(results);
    }