Ejemplo n.º 1
0
        private static List <BSEDownloadCSVType> DownloadFromBSE(DateTime forDate)
        {
            IEnumerable <BSEDownloadCSVType> recordsBSE = new List <BSEDownloadCSVType>();

            //List<BSEDownloadCSVType> records = new List<BSEDownloadCSVType>();
            //Option: Process data from BSE
            try
            {
                var t = BhavCopy.BhavCopyDownloadFromBSE(forDate);
                t.Wait();
                var resultBSE = t.Result;
                //var resultBSE = Task.Run(async () => await BhavCopy.BhavCopyDownloadFromBSE(new DateTime(2020, 03, 12))).Result;
                if (resultBSE.Success)
                {
                    // unzip the file
                    var    zipName            = resultBSE.FileName;
                    var    rawFileStream      = File.OpenRead(zipName);
                    byte[] zippedtoTextBuffer = new byte[rawFileStream.Length];
                    rawFileStream.Read(zippedtoTextBuffer, 0, (int)rawFileStream.Length);

                    var csvFromBSE = Utilities.ZipHelper.Unzip(zippedtoTextBuffer);
                    //Console.WriteLine(csvFromBSE);
                    using (var reader = new StringReader(csvFromBSE))
                        using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                        {
                            recordsBSE = csv.GetRecords <Models.BSEDownloadCSVType>().ToList();
                        }
                    //Console.WriteLine(records.Count());
                    //Console.WriteLine(recordsBSE.Count());
                }
                else
                {
                    Console.WriteLine(resultBSE.exception.Message);
                }
                return(recordsBSE.ToList());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Ejemplo n.º 2
0
        private static List <NSEDownloadCSVType> DownloadFromNSE(DateTime forDate)
        {
            //Option1: Processing data downloaded from NSE
            //var resultNSE = await BhavCopy.BhavCopyDownloadFromNSE(new DateTime(2020, 02, 04));
            IEnumerable <NSEDownloadCSVType> recordsNSE = new List <NSEDownloadCSVType>();

            try
            {
                var t = BhavCopy.BhavCopyDownloadFromNSE(forDate);
                t.Wait();
                var resultNSE = t.Result;
                if (resultNSE.Success)
                {
                    // unzip the file
                    var    zipName            = resultNSE.FileName;
                    var    rawFileStream      = File.OpenRead(zipName);
                    byte[] zippedtoTextBuffer = new byte[rawFileStream.Length];
                    rawFileStream.Read(zippedtoTextBuffer, 0, (int)rawFileStream.Length);

                    var csvFromNSE = Utilities.ZipHelper.Unzip(zippedtoTextBuffer);
                    //Console.WriteLine(csvFromBSE);
                    using (var reader = new StringReader(csvFromNSE))
                        using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                        {
                            recordsNSE = csv.GetRecords <NSEDownloadCSVType>().ToList();
                        }
                }
                else
                {
                    Console.WriteLine(resultNSE.exception.Message);
                }
                return(recordsNSE.ToList());
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 3
0
        public static List <BhavCopy> ParseQuotesFromBhavCopy(string BhavCopyText)
        {
            List <BhavCopy> bhavCopyList = new List <BhavCopy>();

            char[]   delimiterChars = { '\n' };
            char[]   paramsep       = { ',' };
            string[] lines          = BhavCopyText.Split(delimiterChars);

            foreach (string line in lines)
            {
                if (line == "")
                {
                    continue;
                }
                string[] csvReader = line.Trim().Split(paramsep);
                BhavCopy bc        = new BhavCopy();
                var      Ticker    = csvReader[0];
                bc.Ticker = Ticker.ToString();

                var date = csvReader[1];
                bc.Date = DateTime.ParseExact(date.ToString(), "MMddyyyy", CultureInfo.InvariantCulture);

                var open = csvReader[2];
                if (open == "")
                {
                    bc.O = 0;
                }
                else
                {
                    bc.O = Convert.ToDouble(open);
                }

                var High = csvReader[3];
                if (High == "")
                {
                    bc.H = 0;
                }
                else
                {
                    bc.H = Convert.ToDouble(High);
                }

                var Low = csvReader[4];
                if (Low == "")
                {
                    bc.L = 0;
                }
                else
                {
                    bc.L = Convert.ToDouble(Low);
                }

                var Close = csvReader[5];
                if (Close == "")
                {
                    bc.C = 0;
                }
                else
                {
                    bc.C = Convert.ToDouble(Close);
                }

                var Volume = csvReader[6];
                if (Volume == "")
                {
                    bc.Volume = 0;
                }
                else
                {
                    bc.Volume = Convert.ToDouble(Volume);
                }

                bhavCopyList.Add(bc);
            }
            return(bhavCopyList);
        }