Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Write stocks symbol/titl data to file
            var stocksTitleFile = GetListOfStockSymbols();

            string stockDetailsFileWithPath = string.Empty;
            IEnumerable <string> existingStockDetailsFiles;

            appFolder = StockAnalysisHelper.GetConfigStringValues(Constants.appFolderKey);
            var stocksDetailsFile          = StockAnalysisHelper.GetConfigStringValues(Constants.StocksDetailsDataFileName);
            var stocksDetailsRefreshPeriod = StockAnalysisHelper.GetConfigIntValues(Constants.StocksDetailsRefreshPeriod);

            if (StockAnalysisHelper.RefreshFile(stocksDetailsFile, out existingStockDetailsFiles, out stockDetailsFileWithPath, stocksDetailsRefreshPeriod))
            {
                //Get stocks title data from json file
                var serializer = new JsonSerializer();
                List <StockTitleModel> stockTitles;
                using (StreamReader sr = new StreamReader(stocksTitleFile))
                    using (JsonTextReader jtr = new JsonTextReader(sr))
                    {
                        stockTitles = serializer.Deserialize <List <StockTitleModel> >(jtr);
                    }

                //var stockSymbols = new List<string> { "viceroy" };
                List <StockDetailsModel> stockDetails = new List <StockDetailsModel>();
                Parallel.ForEach(stockTitles, x =>
                {
                    var stocksDetails = GetStockDetails(x);
                    stockDetails.Add(stocksDetails);
                });

                stockDetailsFileWithPath = string.Concat(appFolder, "\\", stocksDetailsFile, "_", DateTime.Now.ToString("ddMMyy"), ".json");

                existingStockDetailsFiles.ToList().ForEach(x => File.Delete(x));

                using (StreamWriter sw = new StreamWriter(stockDetailsFileWithPath, false))
                {
                    serializer.Serialize(sw, stockDetails);
                }
            }
        }
Exemplo n.º 2
0
        private static string GetListOfStockSymbols()
        {
            string stocksTitleFileWithPath;
            IEnumerable <string> existingStockTitleFiles;


            appFolder = StockAnalysisHelper.GetConfigStringValues(Constants.appFolderKey);
            var stockstitlefile          = StockAnalysisHelper.GetConfigStringValues(Constants.stocksTitleDataFileName);
            int stocksTitleRefreshPeriod = StockAnalysisHelper.GetConfigIntValues(Constants.stocksTitleDataFileName);

            if (StockAnalysisHelper.RefreshFile(stockstitlefile, out existingStockTitleFiles, out stocksTitleFileWithPath, stocksTitleRefreshPeriod))
            {
                List <StockTitleModel> stocksTitleList = new List <StockTitleModel>();
                string NSEFileWithPath = string.Concat(appFolder, "\\", "NSEBhavCopy.csv");
                string BSEFileWithPath = string.Concat(appFolder, "\\", "BSEBhavCopy.zip");
                StockAnalysisHelper.DownloadFile(Constants.NSEBhavData, NSEFileWithPath);

                StockAnalysisHelper.DownloadFile(Constants.BSEBhavData.Replace("<%date%>", GetPastMarketTradingDate()), BSEFileWithPath);

                List <string> stocksList = new List <string>();

                using (var reader = new StreamReader(NSEFileWithPath))
                {
                    // Leave out header
                    reader.ReadLine();

                    while (!reader.EndOfStream)
                    {
                        stocksList.Add(reader.ReadLine().Split(',')[0]);
                    }
                }

                Parallel.ForEach(stocksList, x =>
                {
                    var summaryUrl = FormUrl(string.Concat(x, ".NS"), UrlType.Summary);
                    var obj        = new HtmlWeb();
                    obj.UserAgent  = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";

                    var document          = obj.Load(summaryUrl);
                    var yahooFinTitleNode = document.DocumentNode.SelectSingleNode(Constants.StockTitleHtml);
                    if (yahooFinTitleNode != null)
                    {
                        string stockInfoTitle = yahooFinTitleNode.InnerText;
                        string stockcode      = string.Empty;
                        var title             = GetTitleAndCodeFromText(stockInfoTitle, out stockcode);

                        stocksTitleList.Add(new StockTitleModel {
                            StockTitle = title, NSECode = x, StockType = StockType.NSE
                        });
                    }
                });

                //GetBSEStockSymbols
                using (ZipArchive archive = ZipFile.OpenRead(BSEFileWithPath))
                {
                    List <string> BseStockSymbol = new List <string>();
                    Stream        bseBhavStream  = archive.Entries[0].Open();

                    using (var reader = new StreamReader(bseBhavStream))
                    {
                        // Leave out header
                        reader.ReadLine();

                        while (!reader.EndOfStream)
                        {
                            BseStockSymbol.Add(reader.ReadLine().Split(',')[0]);
                        }
                    }

                    AddBSEStocksTitleData(BseStockSymbol, stocksTitleList);
                }

                existingStockTitleFiles.ToList().ForEach(x => File.Delete(x));
                stocksTitleFileWithPath = string.Concat(appFolder, "\\", stockstitlefile, "_", DateTime.Now.ToString("ddMMyy"), ".json");
                //Write data to json file
                using (StreamWriter sw = new StreamWriter(stocksTitleFileWithPath, false))
                {
                    var serializer = new JsonSerializer();
                    serializer.Serialize(sw, stocksTitleList);
                }
            }
            return(stocksTitleFileWithPath);
        }