Example #1
0
        public static void Main(string[] args)
        {
            form.Show();

            initPyImports();
            const int sampleLength = 56;
            var       candles      = LoadDataFromFile();
            //DrawChart(candles);
            //ReadLine();

            var prices = candles.Select(x => new[] { x.Close }.Average()).ToArray();

            Console.SetError(new StringWriter());
            double prevPred = 0;
            double prevY    = 0;
            double balance  = 0;
            double currPred;
            double balanceDiff;

            double[] currPredIntervals;

            var seq = Enumerable.Range(0, 200).Select(i =>
            {
                //for (int i = 0; i < 200; i++)
                //{
                Write(i);

                var sampleX                   = new ArraySegment <double>(prices, i, sampleLength).ToArray();
                var sampleY                   = prices[i + sampleLength];
                var stats                     = WithStopwatch(() => GetBestCorrelations(prices, sampleX, 20), out var time);
                var indexes                   = stats.best.Select(x => x.index);
                var weights                   = stats.best.Select(x => x.correlation).ToList();
                var weightsSum                = weights.Sum();
                var weightsScaled             = weights.Select(x => x / weightsSum).ToList();
                var X                         = indexes.Select(x => prices.Skip(x).Take(sampleLength).ToList()).ToList();
                var y                         = indexes.Select(x => prices.Skip(x).Skip(sampleLength).Take(1).ToList()).ToList();
                (currPred, currPredIntervals) = WithStopwatch(() => calcGam(X, y, weightsScaled, sampleX.ToList()), out time);

                return(new
                {
                    currPred,
                    candle = candles[i + sampleLength],
                    currY = y[0][0],
                    sampleY,
                    currPredIntervals
                });

                //}
            }).AsParallel();

            var j = 0;

            foreach (var it in seq)
            {
                form.AddCandle(it.candle);
                form.AddIndi(1, it.currPred);
                form.AddIndi(3, it.currPredIntervals);

                if (j > 0)
                {
                    Console.SetCursorPosition(0, 0);
                    balanceDiff             = (it.sampleY - prevY) * Math.Sign(it.currPred - prevY) * 100_000;
                    Console.ForegroundColor = ConsoleColor.Green;
                    WriteLine(balance      += balanceDiff);
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Application.DoEvents();
                }
                prevPred = it.currPred;
                prevY    = it.sampleY;
                j++;
            }


            ReadLine();
        }