public void GetFractal()
        {
            List <FractalResult> results = Indicator.GetFractal(history)
                                           .ToList();

            foreach (FractalResult r in results)
            {
                Console.WriteLine("{0},{1},{2}", r.Date, r.FractalBear, r.FractalBull);
            }

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count);
            Assert.AreEqual(63, results.Where(x => x.FractalBear != null).Count());
            Assert.AreEqual(71, results.Where(x => x.FractalBull != null).Count());

            // sample values
            FractalResult r1 = results[500];

            Assert.AreEqual(null, r1.FractalBear);
            Assert.AreEqual(null, r1.FractalBull);

            FractalResult r2 = results[250];

            Assert.AreEqual(null, r2.FractalBear);
            Assert.AreEqual(256.81m, r2.FractalBull);

            FractalResult r3 = results[180];

            Assert.AreEqual(239.74m, r3.FractalBear);
            Assert.AreEqual(238.52m, r3.FractalBull);

            FractalResult r4 = results[133];

            Assert.AreEqual(234.53m, r4.FractalBear);
            Assert.AreEqual(null, r4.FractalBull);

            FractalResult r5 = results[3];

            Assert.AreEqual(215.17m, r5.FractalBear);
            Assert.AreEqual(null, r5.FractalBull);

            FractalResult r6 = results[1];

            Assert.AreEqual(null, r6.FractalBear);
            Assert.AreEqual(null, r6.FractalBull);
        }
        public void Standard4()
        {
            List <FractalResult> results = Indicator.GetFractal(history, 4)
                                           .ToList();

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count);
            Assert.AreEqual(35, results.Where(x => x.FractalBear != null).Count());
            Assert.AreEqual(34, results.Where(x => x.FractalBull != null).Count());

            // sample values
            FractalResult r1 = results[3];

            Assert.AreEqual(null, r1.FractalBear);
            Assert.AreEqual(null, r1.FractalBull);

            FractalResult r2 = results[7];

            Assert.AreEqual(null, r2.FractalBear);
            Assert.AreEqual(212.53m, r2.FractalBull);

            FractalResult r3 = results[120];

            Assert.AreEqual(233.02m, r3.FractalBear);
            Assert.AreEqual(null, r3.FractalBull);

            FractalResult r4 = results[180];

            Assert.AreEqual(239.74m, r4.FractalBear);
            Assert.AreEqual(null, r4.FractalBull);

            FractalResult r5 = results[250];

            Assert.AreEqual(null, r5.FractalBear);
            Assert.AreEqual(256.81m, r5.FractalBull);

            FractalResult r6 = results[500];

            Assert.AreEqual(null, r6.FractalBear);
            Assert.AreEqual(null, r6.FractalBull);
        }
        public void Standard2()
        {
            List <FractalResult> results = history.GetFractal(2).ToList();

            // assertions

            // proper quantities
            // should always be the same number of results as there is history
            Assert.AreEqual(502, results.Count);
            Assert.AreEqual(63, results.Where(x => x.FractalBear != null).Count());
            Assert.AreEqual(71, results.Where(x => x.FractalBull != null).Count());

            // sample values
            FractalResult r1 = results[1];

            Assert.AreEqual(null, r1.FractalBear);
            Assert.AreEqual(null, r1.FractalBull);

            FractalResult r2 = results[3];

            Assert.AreEqual(215.17m, r2.FractalBear);
            Assert.AreEqual(null, r2.FractalBull);

            FractalResult r3 = results[133];

            Assert.AreEqual(234.53m, r3.FractalBear);
            Assert.AreEqual(null, r3.FractalBull);

            FractalResult r4 = results[180];

            Assert.AreEqual(239.74m, r4.FractalBear);
            Assert.AreEqual(238.52m, r4.FractalBull);

            FractalResult r5 = results[250];

            Assert.AreEqual(null, r5.FractalBear);
            Assert.AreEqual(256.81m, r5.FractalBull);

            FractalResult r6 = results[500];

            Assert.AreEqual(null, r6.FractalBear);
            Assert.AreEqual(null, r6.FractalBull);
        }
Exemple #4
0
    // FRACTAL CHAOS BANDS
    /// <include file='./info.xml' path='indicator/*' />
    ///
    public static IEnumerable <FcbResult> GetFcb <TQuote>(
        this IEnumerable <TQuote> quotes,
        int windowSpan = 2)
        where TQuote : IQuote
    {
        // check parameter arguments
        ValidateFcb(windowSpan);

        // initialize
        List <FractalResult> fractals = GetFractal(quotes, windowSpan).ToList();
        int length = fractals.Count;
        List <FcbResult> results = new(length);
        decimal?         upperLine = null, lowerLine = null;

        // roll through quotes
        for (int i = 0; i < length; i++)
        {
            int           index = i + 1;
            FractalResult f     = fractals[i];

            FcbResult r = new()
            {
                Date = f.Date
            };

            if (index >= (2 * windowSpan) + 1)
            {
                FractalResult fp = fractals[i - windowSpan];

                upperLine = (fp.FractalBear != null) ? fp.FractalBear : upperLine;
                lowerLine = (fp.FractalBull != null) ? fp.FractalBull : lowerLine;

                r.UpperBand = upperLine;
                r.LowerBand = lowerLine;
            }

            results.Add(r);
        }

        return(results);
    }
        private Task <FractalResult> GenerateAsync(NewtonResult[][] solutions)
        {
            var result = new FractalResult
            {
                Contents = new Hsv[Options.PixelSize.Width, Options.PixelSize.Height],
            };

            var rnd = new Random();

            // Stored as a double to avoid castings later
            double totalPoints = Options.PixelSize.Width * Options.PixelSize.Height;

            result.ColorSpecs = new List <HsvColorSpec>(Options.ColorSpecs?.AsHsvSpecs() ?? Enumerable.Empty <HsvColorSpec>());

            var logT = MathF.Log((float)Options.Precision);

            for (var px = 0; px < Options.PixelSize.Width; ++px)
            {
                for (var py = 0; py < Options.PixelSize.Height; ++py)
                {
                    var solution = solutions[px][py];
                    result.MeanIterations  += solution.Iterations / totalPoints;
                    result.StDevIterations += solution.Iterations * solution.Iterations / totalPoints;

                    if (solution.Status != SolutionStatus.Found)
                    {
                        result.Contents[px, py] = Options.FillColor;
                        continue;
                    }

                    var spec = FindSpec(result.ColorSpecs, solution.Solution, Options.Precision * 10);
                    if (spec == null)
                    {
                        spec = new HsvColorSpec
                        {
                            Root  = solution.Solution,
                            Color = new Hsv(rnd.NextFloat() * 360f, 1f, Options.Depth < 0 ? 0.1f : 1f),
                        };
                        result.ColorSpecs.Add(spec);
                    }

                    if (Options.Depth == 0)
                    {
                        result.Contents[px, py] = spec.Color;
                        continue;
                    }

                    var logD0 = MathF.Log((float)Complex.Abs(solution.PreviousSolution - spec.Root));
                    var logD1 = MathF.Log((float)Complex.Abs(solution.Solution - spec.Root));
                    var value = spec.Color.V;

                    if (solution.Iterations > Options.Threshold)
                    {
                        if (Options.Gradient == 0)
                        {
                            var factor = (solution.Iterations - 1 - Options.Threshold) * Options.Depth * 0.01f;
                            if (Options.Depth > 0)
                            {
                                value /= 1 + factor;
                            }
                            else
                            {
                                value += (1 - value) * (1 - 1 / (1 - factor));
                            }
                        }
                        else
                        {
                            var   factor     = (solution.Iterations - 1 - Options.Threshold) * Options.Depth * 0.01f;
                            var   factorPlus = (solution.Iterations - Options.Threshold) * Options.Depth * 0.01f;
                            float lowValue;
                            float highValue;
                            if (Options.Depth > 0)
                            {
                                lowValue  = value / (1 + factor);
                                highValue = value / (1 + factorPlus);
                            }
                            else
                            {
                                lowValue  = value + (1 - value) * (1 - 1 / (1 - factor));
                                highValue = value + (1 - value) * (1 - 1 / (1 - factorPlus));
                            }

                            if (Math.Abs(logD1 - logD0) < 0.001)
                            {
                                value = highValue;
                            }
                            else
                            {
                                value = highValue + Options.Gradient * (highValue - lowValue) * (logT - logD0) / (logD1 - logD0);
                            }
                        }
                    }

                    value = MathF.Max(0.025F, MathF.Min(0.975F, value));
                    result.Contents[px, py] = new Hsv(spec.Color.H, spec.Color.S, value);
                }
            }

            result.StDevIterations = Math.Sqrt(result.StDevIterations - result.MeanIterations * result.MeanIterations);

            return(Task.FromResult(result));
        }