예제 #1
0
        //private bool StockPriceExists(string id, DateTime date)
        //{
        //    return _context.StockPrice.Any(e => e.StockID == id && e.Date == date);
        //}

        #region "FORMULA"
        //private async Task<bool> InitIndicator()
        //{

        //    foreach (Stock stock in await _context.Stock.Where(s=>s.StockID == "AALI").ToListAsync())
        //    //foreach (Stock stock in _context.Stock.Where(m => m.StockID == "AALI" || m.StockID == "BBCA" || m.StockID == "PTBA"))

        //    {
        //        //Console.Write("TEST " + stock.StockID);
        //        System.Diagnostics.Debug.WriteLine("TEST " + stock.StockID);

        //        //*cari semua histori harga
        //        var data = _context.StockPrice.Where(c => c.StockID == stock.StockID).OrderBy(c => c.Date).ToList();
        //        var count = data.Count();
        //        if (count > 0)
        //        {

        //            for(int i=0; i<count-1; i++)
        //            {
        //                var price = data[0];
        //                var counter = i;
        //                //foreach (StockPrice price in data.ToList().Last())
        //                //{
        //                price.MA20 = Calculation.MA(counter, data.ToArray(), 20);
        //                price.MA60 = Calculation.MA(counter, data.ToArray(), 60);
        //                price.Support = Calculation.Support(counter, data.ToArray());
        //                price.Resistance = Calculation.Resistance(counter, data.ToArray());


        //                dynamic t3 = Calculation.HighestLowest(counter, data.ToArray(), 3);
        //                price.Highest3Months = (t3 == null ? null : t3.Highest);
        //                price.Lowest3Months = (t3 == null ? null : t3.Lowest);

        //                dynamic t6 = Calculation.HighestLowest(counter, data.ToArray(), 6);
        //                price.Highest6Months = (t6 == null ? null : t6.Highest);
        //                price.Lowest6Months = (t6 == null ? null : t6.Lowest);

        //                dynamic t12 = Calculation.HighestLowest(counter, data.ToArray(), 12);
        //                price.Highest12Months = (t12 == null ? null : t12.Highest);
        //                price.Lowest12Months = (t12 == null ? null : t12.Lowest);

        //                price.TrendHigh = Calculation.TrendHigh(counter, data.ToArray(), price.Highest3Months, price.Highest6Months, price.Highest12Months);
        //                price.TrendLow = Calculation.TrendLow(counter, data.ToArray(), price.Lowest3Months, price.Lowest6Months, price.Lowest12Months);

        //                //dynamic highlow = BreakHighLowCalc(counter, data.ToArray());
        //                //price.TrendHigh = highlow.TrendHigh;
        //                //price.TrendLow = highlow.TrendLow;

        //                price.EMA12 = Calculation.EMA(counter, data.ToArray(), 12);
        //                price.EMA26 = Calculation.EMA(counter, data.ToArray(), 26);
        //                if (price.EMA12 == null || price.EMA26 == null)
        //                    price.MACD = null;
        //                else
        //                    price.MACD = price.EMA12 - price.EMA26;
        //                price.SignalLine = Calculation.SignalLine(counter, data.ToArray(), 9);

        //                if (price.MACD == null || price.SignalLine == null)
        //                    price.GSLine = null;
        //                else
        //                    price.GSLine = (Decimal)(price.MACD + price.SignalLine) / 2;

        //                price.SD20 = Calculation.SD(counter, data.ToArray(), 20);

        //                price.BBUpper = price.MA20 + 2 * price.SD20;
        //                price.BBLower = price.MA20 - 2 * price.SD20;
        //            }

        //            //*ambil tanggal terbaru
        //            //var price = await data.LastAsync();


        //            //counter++;
        //            //}
        //        }

        //    }
        //    await _context.SaveChangesAsync();
        //    return true;

        //}
        private async Task <bool> CalculateIndicator2(string stockID, StockPrice price)
        {
            //foreach (Stock stock in await _context.Stock.ToListAsync())
            //foreach (Stock stock in _context.Stock.Where(m => m.StockID == "AALI" || m.StockID == "BBCA" || m.StockID == "PTBA"))

            {
                //Console.Write("TEST " + stock.StockID);
                System.Diagnostics.Debug.WriteLine("TEST " + stockID);


                var      prices = _context.StockPrice.Where(c => c.StockID == stockID).AsNoTracking();
                DateTime last_date;
                if (prices.Count() > 0)
                {
                    last_date = prices.Max(m => m.Date);
                }
                else
                {
                    last_date = price.Date;
                }


                //*cari semua histori harga
                StockPrice[] data = prices.Where(c => c.Date >= last_date.AddMonths(-13)).OrderBy(c => c.Date).AsNoTracking().ToArray();

                //*tambahkan data terbaru dalam memory
                data = data.Append(price).ToArray();



                var countx = data.Count();

                //StockPrice dataPrev = null;
                //var indexPrev = 0;
                if (countx >= 5)
                {
                    //*Update harga 3 hari sebelumnya
                    for (int i = countx - 4; i <= countx - 2; i++)
                    {
                        //dataPrev = data[i];
                        StockPrice dataPrev = await _context.StockPrice.SingleOrDefaultAsync(m => m.Date == data[i].Date && m.StockID == stockID);

                        //indexPrev = countx - 3;


                        dataPrev.Support    = Calculation.Support(i, data);
                        data[i].Support     = dataPrev.Support;
                        dataPrev.Resistance = Calculation.Resistance(i, data);
                        data[i].Resistance  = dataPrev.Resistance;
                        //dataPrev.Support = 60;
                        //dataPrev.Resistance = 28000;
                        await _context.SaveChangesAsync();
                    }
                }
                var count = data.Count();
                if (count > 0)
                {
                    //for(int i=0; i<data.Length-1; i++)

                    //*ambil tanggal terbaru
                    //var price = data.Last();
                    var counter = count - 1;
                    //foreach (StockPrice price in data.ToList().Last())
                    //{
                    price.MA20       = Calculation.MA(counter, data, 20);
                    price.MA60       = Calculation.MA(counter, data, 60);
                    price.Support    = Calculation.Support(counter, data);
                    price.Resistance = Calculation.Resistance(counter, data);


                    dynamic t3 = Calculation.HighestLowest(counter, data, 3);
                    price.Highest3Months = (t3 == null ? null : t3.Highest);
                    price.Lowest3Months  = (t3 == null ? null : t3.Lowest);

                    dynamic t6 = Calculation.HighestLowest(counter, data, 6);
                    price.Highest6Months = (t6 == null ? null : t6.Highest);
                    price.Lowest6Months  = (t6 == null ? null : t6.Lowest);

                    dynamic t12 = Calculation.HighestLowest(counter, data, 12);
                    price.Highest12Months = (t12 == null ? null : t12.Highest);
                    price.Lowest12Months  = (t12 == null ? null : t12.Lowest);

                    price.TrendHigh = Calculation.TrendHigh(counter, data, price.Highest3Months, price.Highest6Months, price.Highest12Months);
                    price.TrendLow  = Calculation.TrendLow(counter, data, price.Lowest3Months, price.Lowest6Months, price.Lowest12Months);

                    //dynamic highlow = BreakHighLowCalc(counter, data);
                    //price.TrendHigh = highlow.TrendHigh;
                    //price.TrendLow = highlow.TrendLow;

                    price.EMA12 = Calculation.EMA(counter, data, 12);
                    price.EMA26 = Calculation.EMA(counter, data, 26);
                    if (price.EMA12 == null || price.EMA26 == null)
                    {
                        price.MACD = null;
                    }
                    else
                    {
                        price.MACD = price.EMA12 - price.EMA26;
                    }
                    price.SignalLine = Calculation.SignalLine(counter, data, 9);

                    if (price.MACD == null || price.SignalLine == null)
                    {
                        price.GSLine = null;
                    }
                    else
                    {
                        price.GSLine = (Decimal)(price.MACD + price.SignalLine) / 2;
                    }


                    if (counter >= 1)
                    {
                        if (price.GSLine != null && data[counter - 1].GSLine != null)
                        {
                            if (price.GSLine > data[counter - 1].GSLine)
                            {
                                price.GSLineDirection = "U";
                            }
                            else if (price.GSLine < data[counter - 1].GSLine)
                            {
                                price.GSLineDirection = "D";
                            }
                            else
                            {
                                price.GSLineDirection = "N";
                            }
                        }
                        else
                        {
                            price.GSLineDirection = "";
                        }
                    }
                    else
                    {
                        price.GSLineDirection = "";
                    }


                    price.SD20 = Calculation.SD(counter, data, 20);

                    price.BBUpper = price.MA20 + 2 * price.SD20;
                    price.BBLower = price.MA20 - 2 * price.SD20;

                    if (price.Frequency > 0)
                    {
                        price.BigWave = (Decimal)price.Volume / (Decimal)(price.Frequency * price.Frequency * price.Frequency);
                    }
                    else
                    {
                        price.BigWave = 0;
                    }

                    price.AverageBigWave = Calculation.AverageBigWave(counter, data, 3);

                    //counter++;
                    //}
                }
            }
            //await _context.SaveChangesAsync();
            return(true);
        }
        public async Task <bool> InitIndicator(string stockID)
        {
            //*cari semua histori harga
            var allData = await _context.StockPrice.Where(c => c.StockID == stockID).OrderBy(c => c.Date).ToListAsync();

            var data = await _context.StockPrice.Where(c => c.StockID == stockID).OrderBy(c => c.Date).AsNoTracking().ToArrayAsync();

            int counter = 0;

            foreach (var price in allData)
            {
                //var data = _context.StockPrice.Where(c => c.StockID == stock.StockID && c.Date >= price.Date.AddMonths(-13)).OrderBy(c => c.Date).AsNoTracking();

                System.Diagnostics.Debug.WriteLine("TEST " + stockID + " " + counter.ToString());

                price.MA20         = Calculation.MA(counter, data, 20);
                data[counter].MA20 = price.MA20;

                price.MA60         = Calculation.MA(counter, data, 60);
                data[counter].MA60 = price.MA60;

                price.Support         = Calculation.Support(counter, data);
                data[counter].Support = price.Support;

                price.Resistance         = Calculation.Resistance(counter, data);
                data[counter].Resistance = price.Resistance;

                dynamic t3 = Calculation.HighestLowest(counter, data, 3);
                price.Highest3Months         = (t3 == null ? null : t3.Highest);
                data[counter].Highest3Months = price.Highest3Months;

                price.Lowest3Months         = (t3 == null ? null : t3.Lowest);
                data[counter].Lowest3Months = price.Lowest3Months;

                dynamic t6 = Calculation.HighestLowest(counter, data, 6);
                price.Highest6Months         = (t6 == null ? null : t6.Highest);
                data[counter].Highest6Months = price.Highest6Months;

                price.Lowest6Months         = (t6 == null ? null : t6.Lowest);
                data[counter].Lowest6Months = price.Lowest6Months;

                dynamic t12 = Calculation.HighestLowest(counter, data, 12);
                price.Highest12Months         = (t12 == null ? null : t12.Highest);
                data[counter].Highest12Months = price.Highest12Months;

                price.Lowest12Months         = (t12 == null ? null : t12.Lowest);
                data[counter].Lowest12Months = price.Lowest12Months;

                price.TrendHigh         = Calculation.TrendHigh(counter, data, price.Highest3Months, price.Highest6Months, price.Highest12Months);
                data[counter].TrendHigh = price.TrendHigh;

                price.TrendLow         = Calculation.TrendLow(counter, data, price.Lowest3Months, price.Lowest6Months, price.Lowest12Months);
                data[counter].TrendLow = price.TrendLow;

                price.EMA12         = Calculation.EMA(counter, data, 12);
                data[counter].EMA12 = price.EMA12;

                price.EMA26         = Calculation.EMA(counter, data, 26);
                data[counter].EMA26 = price.EMA26;


                if (price.EMA12 == null || price.EMA26 == null)
                {
                    price.MACD = null;
                }
                else
                {
                    price.MACD = price.EMA12 - price.EMA26;
                }

                data[counter].MACD = price.MACD;

                price.SignalLine         = Calculation.SignalLine(counter, data, 9);
                data[counter].SignalLine = price.SignalLine;

                if (price.MACD == null || price.SignalLine == null)
                {
                    price.GSLine = null;
                }
                else
                {
                    price.GSLine = (Decimal)(price.MACD + price.SignalLine) / 2;
                }
                data[counter].GSLine = price.GSLine;


                if (counter >= 1)
                {
                    if (price.GSLine != null && data[counter - 1].GSLine != null)
                    {
                        if (price.GSLine > data[counter - 1].GSLine)
                        {
                            price.GSLineDirection = "U";
                        }
                        else if (price.GSLine < data[counter - 1].GSLine)
                        {
                            price.GSLineDirection = "D";
                        }
                        else
                        {
                            price.GSLineDirection = "N";
                        }
                    }
                    else
                    {
                        price.GSLineDirection = "";
                    }
                }
                else
                {
                    price.GSLineDirection = "";
                }

                price.SD20         = Calculation.SD(counter, data, 20);
                data[counter].SD20 = price.SD20;

                if (price.Frequency > 0)
                {
                    price.BigWave = (Decimal)price.Volume / (Decimal)(price.Frequency * price.Frequency * price.Frequency);
                }
                else
                {
                    price.BigWave = 0;
                }

                data[counter].BigWave = price.BigWave;

                price.AverageBigWave         = Calculation.AverageBigWave(counter, data, 3);
                data[counter].AverageBigWave = price.AverageBigWave;


                price.BBUpper         = price.MA20 + 2 * price.SD20;
                data[counter].BBUpper = price.BBUpper;

                price.BBLower         = price.MA20 - 2 * price.SD20;
                data[counter].BBLower = price.BBLower;

                counter++;
            }
            await _context.SaveChangesAsync();

            return(true);
        }