Example #1
0
        private void Predict()
        {
            double firstDay     = context.DayStats.OrderByDescending(x => x.Date).Select(x => (double)x.Date.Day + 1).First();
            int    amountOfDays = 182;

            var result = new LinReg[5];
            var days   = context.DayStats.Select(x => (double)x.Date.Day).ToArray();

            Maths.LinearRegression(days,
                                   context.DayStats.Select(x => (double)x.DAU).ToArray(),
                                   out result[0].rSquared,
                                   out result[0].yIntercept,
                                   out result[0].Slope);
            Maths.LinearRegression(days,
                                   context.DayStats.Select(x => (double)x.NewUsers).ToArray(),
                                   out result[1].rSquared,
                                   out result[1].yIntercept,
                                   out result[1].Slope);
            Maths.LinearRegression(days,
                                   context.DayStats.Select(x => (double)x.Revenue).ToArray(),
                                   out result[2].rSquared,
                                   out result[2].yIntercept,
                                   out result[2].Slope);
            Maths.LinearRegression(days,
                                   context.ItemStatDays.GroupBy(x => x.Date).OrderBy(x => x.Key).Select(g => (double)g.Sum(p => p.Amount)).ToArray(),
                                   out result[3].rSquared,
                                   out result[3].yIntercept,
                                   out result[3].Slope);
            Maths.LinearRegression(days,
                                   context.ItemStatDays.GroupBy(x => x.Date).OrderBy(x => x.Key).Select(g => (double)g.Sum(p => p.USD)).ToArray(),
                                   out result[4].rSquared,
                                   out result[4].yIntercept,
                                   out result[4].Slope);

            Prediction[] predictions = new Prediction[amountOfDays];

            for (int i = 0; i < amountOfDays; i++)
            {
                predictions[i] = new Prediction
                {
                    Date       = new DateTime(2018, 1, 1).AddDays(firstDay + i - 1),
                    DAU        = Convert.ToInt32((result[0].Slope * (firstDay + i)) + result[0].yIntercept),
                    NewUsers   = Convert.ToInt32((result[1].Slope * (firstDay + i)) + result[1].yIntercept),
                    Revenue    = Math.Round((result[2].Slope * (firstDay + i)) + result[2].yIntercept, 2),
                    SoldAmount = Convert.ToInt32((result[3].Slope * (firstDay + i)) + result[3].yIntercept),
                    SoldUSD    = (result[4].Slope * (firstDay + i)) + result[4].yIntercept
                };

                Console.WriteLine("Day: {0}   DAU: {1}   NewUsers: {2}   Revenue:{3}   SoldAmount: {4}   SoldUSD: {5}",
                                  predictions[i].Date.ToShortDateString(), predictions[i].DAU, predictions[i].NewUsers, predictions[i].Revenue, predictions[i].SoldAmount,
                                  predictions[i].SoldUSD);
            }
            context.Prediction.AddRange(predictions);
            context.SaveChanges();
        }
Example #2
0
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = @"TTMS momentum histogram with squeeze dots.";
                Name        = "TTMS";

                Calculate                = Calculate.OnPriceChange;
                IsOverlay                = false;
                DisplayInDataBox         = true;
                DrawOnPricePanel         = false;
                DrawHorizontalGridLines  = false;
                DrawVerticalGridLines    = false;
                PaintPriceMarkers        = true;
                ScaleJustification       = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                IsSuspendedWhileInactive = true;

                MomentumLength = 20;
                SqueezeLength  = 20;

                SqueezeDotBrush      = Brushes.Red;
                NormalDotBrush       = Brushes.Blue;
                HistAboveZeroRising  = Brushes.Lime;
                HistAboveZeroFalling = Brushes.DarkSlateGray;
                HistBelowZeroFalling = Brushes.Red;
                HistBelowZeroRising  = Brushes.DarkSlateGray;

                SoundAlertsOn  = false;
                BuySoundAlert  = "Alert3.wav";
                SellSoundAlert = "Alert4.wav";

                AddPlot(new Stroke(Brushes.DarkSlateGray, 5), PlotStyle.Bar, "MomentumHistogram");
                AddPlot(new Stroke(NormalDotBrush, 2), PlotStyle.Dot, "SqueezeDots");
            }
            else if (State == State.Configure)
            {
                lrm_            = new Series <double>(this);
                medianPriceOsc_ = new Series <double>(this);

                BB   = Bollinger(2.0, SqueezeLength);
                KC   = KeltnerChannel(1.5, SqueezeLength);
                DC   = DonchianChannel(MomentumLength);
                LR   = LinReg(medianPriceOsc_, MomentumLength);
                ema_ = EMA(MomentumLength);
            }
        }