Ejemplo n.º 1
0
        //[HttpPost("DailyUpload")]
        //public async Task<IActionResult> DailyUpload(List<IFormFile> files)
        //{
        //    long size = files.Sum(f => f.Length);

        //    // full path to file in temp location
        //    var filePath = Path.GetTempFileName();
        //    //var uploads = Path.Combine(_environment.WebRootPath, "uploads");


        //    foreach (var formFile in files)
        //    {
        //        if (formFile.Length > 0)
        //        {
        //            //using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))


        //            using (var stream = new FileStream(filePath, FileMode.Create))
        //            {
        //                await formFile.CopyToAsync(stream);
        //            }

        //            var stream2 = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        //            using (var streamReader = new StreamReader(stream2, Encoding.UTF8))
        //            {
        //                string line = "";
        //                while ((line = streamReader.ReadLine()) != null)
        //                {
        //                    // process the line
        //                    //Stock Code,	Stock Name,	High,	Low,	Close,	Change,	Volume,	Value,	Frequency
        //                    string[] result = line.Split('|');

        //                    if (result[0].ToUpper() != "STOCK CODE")
        //                    {
        //                        if (!_context.Stock.Any(m => m.StockID == result[0]))
        //                        {
        //                            _context.Stock.Add(new Stock() { StockID = result[0], Name = "Stock " + result[0] });
        //                            await _context.SaveChangesAsync();
        //                        }
        //                        if (_context.StockPrice.Any(m => m.StockID == result[0] && m.Date == DateTime.Today))
        //                        {
        //                            var stock = await _context.StockPrice.SingleOrDefaultAsync(m => m.StockID == result[0] && m.Date == DateTime.Today);
        //                            stock.High = Convert.ToDecimal(result[3]);
        //                            stock.Low = Convert.ToDecimal(result[4]);
        //                            stock.Close = Convert.ToDecimal(result[5]);
        //                            stock.Open = stock.Close + Convert.ToDecimal(result[2]);
        //                            stock.Volume = (long)Convert.ToDecimal(result[6]);
        //                            stock.Frequency = (long)Convert.ToDecimal(result[7]);
        //                            await _context.SaveChangesAsync();
        //                        }
        //                        else
        //                        {
        //                            StockPrice stock = new StockPrice();
        //                            stock.StockID = result[0];
        //                            stock.Date = DateTime.Today;
        //                            stock.High = Convert.ToDecimal(result[3]);
        //                            stock.Low = Convert.ToDecimal(result[4]);
        //                            stock.Close = Convert.ToDecimal(result[5]);
        //                            stock.Open = stock.Close + Convert.ToDecimal(result[2]);
        //                            stock.Volume = (long)Convert.ToDecimal(result[6]);
        //                            stock.Frequency = (long)Convert.ToDecimal(result[7]);
        //                            _context.StockPrice.Add(stock);
        //                            await _context.SaveChangesAsync();

        //                        }
        //                    }
        //                }

        //            }

        //        }
        //    }

        //    // process uploaded files
        //    // Don't rely on or trust the FileName property without validation.

        //    //return Ok(new { count = files.Count, size, filePath });
        //    ViewData["Message"] = "Success";
        //    return View();

        //}


        private async Task <int> CreateStockPriceFile(string filePath)
        {
            var stream = new FileStream(filePath, FileMode.Create);

            using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
            {
                var header = "Date;Stock;Open;High;Low;Close;Resist;Support;GSLine;Trend";
                streamWriter.WriteLine(header);
                foreach (var stock in await _context.Stock.ToListAsync())
                {
                    var lastPrice = await _context.StockPrice.Where(m => m.StockID == stock.StockID).OrderByDescending(x => x.Date).FirstOrDefaultAsync();

                    var line  = "";
                    var trend = Screening.DetermineTrendByWord(lastPrice);

                    if (lastPrice != null)
                    {
                        line = lastPrice.Date.ToString("yyyy/MM/dd") + ";" + stock.StockID + ";" + lastPrice.Open + ";" + lastPrice.High + ";" + lastPrice.Low + ";" + lastPrice.Close + ";" + lastPrice.Resistance + ";" + lastPrice.Support + ";" + lastPrice.GSLineDirection + ";" + trend;
                    }
                    else
                    {
                        line = lastPrice.Date.ToString("yyyy/MM/dd") + ";" + stock.StockID + ";0;0;0;0;0;0;0;0";
                    }

                    streamWriter.WriteLine(line);
                }
            }
            return(1);
        }