コード例 #1
0
        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);
        }