public static DailyStockList OutputToCsv(DailyStockList list, string filePath) { try { using StreamWriter file = new StreamWriter(filePath); file.WriteLine(DailyStockData.Header); foreach (DailyStockData value in list) { file.WriteLine(value.ToCsv()); } } catch (Exception ex) { Utils.WriteToConsole(Utils.ExToString(ex), true, "OutputToCsv"); } return(list); }
private static void ProcessTicker(string ticker, string floatStr) { currentTicker = ticker; try { var flt = Convert.ToInt64(floatStr); if (dirPath.IsValidDirectory()) { string outputFile = $"{dirPath}{ticker}.csv"; string floatFile = $"{dirPath}{ticker}-float.csv"; string pointFile = $"{dirPath}{ticker}-point.csv"; string simpleFile = $"{dirPath}{ticker}-sma.csv"; string volFile = $"{dirPath}{ticker}-volma.csv"; var result = DailyStockList.GetData(ticker); if (result.IsSuccess) { var list = OutputDailyStockData(result.Success, outputFile); /* * var floats = new DailyFloatList (list, flt); * floats.OutputAsCsv (floatFile); * * var points = new DailyPointList (list); * points.OutputAsCsv (pointFile); */ DailyVolumeList vols = new (list, ticker); AverageVolumeList avgs = new (vols, ticker, 20); avgs.OutputAsCsv(volFile); SimpleAverageList simple = new (list, ticker, 20, 120); simple.OutputAsCsv(simpleFile); } else { Utils.WriteToConsole(result.Failure.Item1, true, result.Failure.Item2); } } // No reason for an 'else' as the exception has already been recorded. } catch (Exception ex) { Utils.WriteToConsole($"{Utils.ExToString (ex)}\nTicker: {currentTicker}", true, "ProcessTicker"); } }
public static Result <DailyStockList, (string, string)> GetData(string ticker) { try { DailyStockList list = new DailyStockList(ticker); var downloadResult = DownloadData(ticker); if (downloadResult.IsSuccess) { HtmlDocument doc = downloadResult.Success; var table = doc.DocumentNode.Descendants("table").First(); var tbody = table.Descendants("tbody").First(); var rows = tbody.Descendants("tr"); foreach (var row in rows) { var cells = row.Descendants("td"); if (cells.Count() >= 7) { DailyStockData values = new DailyStockData( cells.ElementAt(0).InnerText, cells.ElementAt(1).InnerText, cells.ElementAt(2).InnerText, cells.ElementAt(3).InnerText, cells.ElementAt(4).InnerText, cells.ElementAt(5).InnerText, cells.ElementAt(6).InnerText.Replace(",", "")); list.Add(values); } } list.Reverse(); return(Result <DailyStockList, (string, string)> .Succeeded(list)); } else { return(Result <DailyStockList, (string, string)> .Failed(downloadResult.Failure)); } } catch (Exception ex) { return(Result <DailyStockList, (string, string)> .Failed(($"{Utils.ExToString (ex)}", "GetData"))); } }