示例#1
0
        public void GetMacdTest()
        {
            int fastPeriod   = 12;
            int slowPeriod   = 26;
            int signalPeriod = 9;

            IEnumerable <MacdResult> results = Indicator.GetMacd(history, fastPeriod, slowPeriod, signalPeriod);

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count());
            Assert.AreEqual(502 - slowPeriod + 1, results.Where(x => x.Macd != null).Count());
            Assert.AreEqual(502 - slowPeriod - signalPeriod + 1, results.Where(x => x.Signal != null).Count());
            Assert.AreEqual(502 - slowPeriod - signalPeriod + 1, results.Where(x => x.Histogram != null).Count());
            Assert.AreEqual(502 - slowPeriod - signalPeriod, results.Where(x => x.IsBullish != null).Count());
            Assert.AreEqual(502 - slowPeriod - signalPeriod, results.Where(x => x.IsDiverging != null).Count());

            // sample value
            MacdResult result = results.Where(x => x.Date == DateTime.Parse("12/31/2018")).FirstOrDefault();

            Assert.AreEqual((decimal) - 6.2198, Math.Round((decimal)result.Macd, 4));
            Assert.AreEqual((decimal) - 5.8569, Math.Round((decimal)result.Signal, 4));
            Assert.AreEqual((decimal) - 0.3629, Math.Round((decimal)result.Histogram, 4));
            Assert.AreEqual(false, result.IsBullish);
            Assert.AreEqual(false, result.IsDiverging);
        }
示例#2
0
        public void Convergence()
        {
            foreach (int qty in convergeQuantities)
            {
                IEnumerable <Quote>      h = History.GetHistoryLong(130 + qty);
                IEnumerable <MacdResult> r = Indicator.GetMacd(h);

                MacdResult l = r.LastOrDefault();
                Console.WriteLine("MACD on {0:d} with {1,4} periods: {2:N8}",
                                  l.Date, h.Count(), l.Macd);
            }
        }
    public void Macd()
    {
        foreach (int qty in QuotesQuantities)
        {
            IEnumerable <Quote>      quotes = TestData.GetLongish(15 + qty);
            IEnumerable <MacdResult> r      = quotes.GetMacd();

            MacdResult l = r.LastOrDefault();
            Console.WriteLine(
                "MACD on {0:d} with {1,4} periods: {2:N8}",
                l.Date, quotes.Count(), l.Macd);
        }
    }
示例#4
0
    public void Standard()
    {
        int fastPeriods   = 12;
        int slowPeriods   = 26;
        int signalPeriods = 9;

        List <MacdResult> results =
            quotes.GetMacd(fastPeriods, slowPeriods, signalPeriods)
            .ToList();

        // assertions

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

        // sample values
        MacdResult r49 = results[49];

        Assert.AreEqual(1.7203m, Math.Round((decimal)r49.Macd, 4));
        Assert.AreEqual(1.9675m, Math.Round((decimal)r49.Signal, 4));
        Assert.AreEqual(-0.2472m, Math.Round((decimal)r49.Histogram, 4));
        Assert.AreEqual(224.1840m, Math.Round((decimal)r49.FastEma, 4));
        Assert.AreEqual(222.4637m, Math.Round((decimal)r49.SlowEma, 4));

        MacdResult r249 = results[249];

        Assert.AreEqual(2.2353m, Math.Round((decimal)r249.Macd, 4));
        Assert.AreEqual(2.3141m, Math.Round((decimal)r249.Signal, 4));
        Assert.AreEqual(-0.0789m, Math.Round((decimal)r249.Histogram, 4));
        Assert.AreEqual(256.6780m, Math.Round((decimal)r249.FastEma, 4));
        Assert.AreEqual(254.4428m, Math.Round((decimal)r249.SlowEma, 4));

        MacdResult r501 = results[501];

        Assert.AreEqual(-6.2198m, Math.Round((decimal)r501.Macd, 4));
        Assert.AreEqual(-5.8569m, Math.Round((decimal)r501.Signal, 4));
        Assert.AreEqual(-0.3629m, Math.Round((decimal)r501.Histogram, 4));
        Assert.AreEqual(245.4957m, Math.Round((decimal)r501.FastEma, 4));
        Assert.AreEqual(251.7155m, Math.Round((decimal)r501.SlowEma, 4));
    }
示例#5
0
    public void Removed()
    {
        int fastPeriods   = 12;
        int slowPeriods   = 26;
        int signalPeriods = 9;

        List <MacdResult> results =
            quotes.GetMacd(fastPeriods, slowPeriods, signalPeriods)
            .RemoveWarmupPeriods()
            .ToList();

        // assertions
        Assert.AreEqual(502 - (slowPeriods + signalPeriods + 250), results.Count);

        MacdResult last = results.LastOrDefault();

        Assert.AreEqual(-6.2198m, Math.Round((decimal)last.Macd, 4));
        Assert.AreEqual(-5.8569m, Math.Round((decimal)last.Signal, 4));
        Assert.AreEqual(-0.3629m, Math.Round((decimal)last.Histogram, 4));
    }
示例#6
0
        public void Standard()
        {
            int fastPeriod   = 12;
            int slowPeriod   = 26;
            int signalPeriod = 9;

            List <MacdResult> results =
                Indicator.GetMacd(history, fastPeriod, slowPeriod, signalPeriod)
                .ToList();

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count);
            Assert.AreEqual(477, results.Where(x => x.Macd != null).Count());
            Assert.AreEqual(469, results.Where(x => x.Signal != null).Count());
            Assert.AreEqual(469, results.Where(x => x.Histogram != null).Count());

            // sample values
            MacdResult r1 = results[49];

            Assert.AreEqual(1.7203m, Math.Round((decimal)r1.Macd, 4));
            Assert.AreEqual(1.9675m, Math.Round((decimal)r1.Signal, 4));
            Assert.AreEqual(-0.2472m, Math.Round((decimal)r1.Histogram, 4));

            MacdResult r2 = results[249];

            Assert.AreEqual(2.2353m, Math.Round((decimal)r2.Macd, 4));
            Assert.AreEqual(2.3141m, Math.Round((decimal)r2.Signal, 4));
            Assert.AreEqual(-0.0789m, Math.Round((decimal)r2.Histogram, 4));

            MacdResult r3 = results[501];

            Assert.AreEqual(-6.2198m, Math.Round((decimal)r3.Macd, 4));
            Assert.AreEqual(-5.8569m, Math.Round((decimal)r3.Signal, 4));
            Assert.AreEqual(-0.3629m, Math.Round((decimal)r3.Histogram, 4));
        }
示例#7
0
                }                                                   // Stochastic Indicator

                public Metric()
                {
                    BB   = new BollingerBandsResult();
                    MACD = new MacdResult();
                }
示例#8
0
    // MOVING AVERAGE CONVERGENCE/DIVERGENCE (MACD) OSCILLATOR
    /// <include file='./info.xml' path='indicator/*' />
    ///
    public static IEnumerable <MacdResult> GetMacd <TQuote>(
        this IEnumerable <TQuote> quotes,
        int fastPeriods   = 12,
        int slowPeriods   = 26,
        int signalPeriods = 9)
        where TQuote : IQuote
    {
        // convert quotes
        List <BasicD> bdList = quotes.ConvertToBasic(CandlePart.Close);

        // check parameter arguments
        ValidateMacd(fastPeriods, slowPeriods, signalPeriods);

        // initialize
        List <EmaResult> emaFast = CalcEma(bdList, fastPeriods);
        List <EmaResult> emaSlow = CalcEma(bdList, slowPeriods);

        int               length  = bdList.Count;
        List <BasicD>     emaDiff = new();
        List <MacdResult> results = new(length);

        // roll through quotes
        for (int i = 0; i < length; i++)
        {
            BasicD    h  = bdList[i];
            EmaResult df = emaFast[i];
            EmaResult ds = emaSlow[i];

            MacdResult result = new()
            {
                Date    = h.Date,
                FastEma = df.Ema,
                SlowEma = ds.Ema
            };

            if (df?.Ema != null && ds?.Ema != null)
            {
                double macd = (double)(df.Ema - ds.Ema);
                result.Macd = (decimal)macd;

                // temp data for interim EMA of macd
                BasicD diff = new()
                {
                    Date  = h.Date,
                    Value = macd
                };

                emaDiff.Add(diff);
            }

            results.Add(result);
        }

        // add signal and histogram to result
        List <EmaResult> emaSignal = CalcEma(emaDiff, signalPeriods);

        for (int d = slowPeriods - 1; d < length; d++)
        {
            MacdResult r  = results[d];
            EmaResult  ds = emaSignal[d + 1 - slowPeriods];

            r.Signal    = ds.Ema;
            r.Histogram = r.Macd - r.Signal;
        }

        return(results);
    }