public AutoComponentsViewModel GetForecastData(Indicator indicator, string assetName, int periods) { try { var viewModel = new AutoComponentsViewModel(); var folder = _directoryManager.GetLastFolder(DirSwitcher.Auto); var dir = _directoryManager.GetDirByIndicator(folder, indicator); var targetFolder = _directoryManager.GetForecastFolderByName(dir, assetName); var images = _directoryManager.ImagePath(DirSwitcher.Auto, indicator, targetFolder, folder); viewModel.ComponentsPath = images.ComponentsImage; viewModel.ForecastPath = images.ForecastImage; viewModel.AssetName = assetName; viewModel.Indicator = indicator; var pathToOut = _directoryManager.FilePathOut(Path.Combine(dir, targetFolder)); var stats = _fileManager.BuildOutTableRows(pathToOut, periods); viewModel.Table = stats.Table; return(viewModel); } catch (Exception e) { throw new Exception(e.Message); } }
public async Task AutoForecast() { string lastFolder; const int dataHours = 200; const int periods = 24; const bool hourlySeasonality = false; const bool dailySeasonality = false; const string readFrom = "assets"; try { IEnumerable <string> assets; if (readFrom.ToLower().Equals("assets")) { assets = _fileManager.ReadAssetsFromExcel(_directoryManager.AsstesLocation); } else { assets = _fileManager.ReadAssetsFromExcel(_directoryManager.ObservablesLocation); } var currentTime = DateTime.Now; Parallel.ForEach(assets, asset => { var pathToFolder = _directoryManager.GenerateForecastFolder(asset, periods, DirSwitcher.Auto, currentTime); var normalized = _processModel.GetDataAuto(asset, dataHours); if (normalized == null || !normalized.Any()) { _directoryManager.RemoveFolder(pathToFolder); var zeroResults = new ExcelLog() { AssetName = asset, Log = Indicator.ZeroRezults.ToString(), Rate = "0", Width = "0", Volume = "0", Change = "0", Rsi = "0" }; Shared.Log(zeroResults); //Shared.Log(asset, Indicator.ZeroRezults, 0, "0", 0, 0, 0); return; } var csv = _fileManager.CreateDataCsv(normalized, pathToFolder); if (string.IsNullOrEmpty(csv)) { var zeroResults = new ExcelLog() { AssetName = asset, Log = Indicator.ZeroRezults.ToString(), Rate = "0", Width = "0", Volume = "0", Change = "0", Rsi = "0" }; Shared.Log(zeroResults); //Shared.Log(asset, Indicator.ZeroRezults, 0, "0", 0, 0, 0); return; } _directoryManager.SaveDataFile(csv, pathToFolder); _pythonExec.RunPython(pathToFolder, periods, hourlySeasonality, dailySeasonality); var pathToOut = _directoryManager.FilePathOut(pathToFolder); var pathToComponents = _directoryManager.FileComponentsOut(pathToFolder); var pathToForecast = _directoryManager.FileForecastOut(pathToFolder); var outCreated = _directoryManager.WaitForFile(pathToOut, 60); var componentsCreated = _directoryManager.WaitForFile(pathToComponents, 10); var forecastCreated = _directoryManager.WaitForFile(pathToForecast, 10); if (!outCreated.Result || !forecastCreated.Result || !componentsCreated.Result) { return; } var stats = _fileManager.BuildOutTableRows(pathToOut, periods); var performance = _utility.DefinePerformance(stats); var marketFeatures = _utility.GetFeatures(normalized, asset); var rsi = _utility.Rsi(normalized); var log = new ExcelLog() { AssetName = asset, Log = performance.Indicator.ToString(), Rate = performance.Rate.ToString(), Width = performance.Width.ToString(), Volume = marketFeatures.Volume.ToString() + "BTC", Change = marketFeatures.Change.ToString("N2"), Rsi = rsi.ToString("N2") + "%" }; Shared.Log(log); //Shared.Log(asset, performance.Indicator, performance.Rate, performance.Width.ToString(),marketFeatures.Volume, marketFeatures.Change, rsi); _directoryManager.SpecifyDirByTrend(performance.Indicator, pathToFolder); } ); lastFolder = _directoryManager.GetLastFolder(DirSwitcher.Auto); var results = _directoryManager.GetListByIndicator(lastFolder); var res = _fileManager.WriteLogExcel(lastFolder, Shared.GetLog); _directoryManager.WriteLogToExcel(lastFolder, res); Shared.ClearLog(); var model = _requestHelper.GetStats(); await _messenger.SendMessage("Done Assets"); } catch (Exception e) { lastFolder = _directoryManager.GetLastFolder(DirSwitcher.Auto); if (Shared.GetLog.Any()) { var res = _fileManager.WriteLogExcel(lastFolder, Shared.GetLog); _directoryManager.WriteLogToExcel(lastFolder, res); Shared.ClearLog(); } await _messenger.SendMessage("Failed Assets"); throw new Exception(e.Message); } }
public async Task <AutoViewModel> MakeAutoForecast(int dataHours, int periods, bool hourlySeasonality, bool dailySeasonality, string readFrom) { var viewModel = new AutoViewModel(); _logger.LogWarning($"Creating Auto Forecast for hours {dataHours}, " + $"for periods {periods}, " + $"from file {readFrom}, " + $"hourly seasonality: {hourlySeasonality}, " + $"daily seasonality: {dailySeasonality}."); string lastFolder; try { IEnumerable <string> assets; if (readFrom.ToLower() == "assets") { assets = _fileManager.ReadAssetsFromExcel(_directoryManager.AsstesLocation); } else { assets = _fileManager.ReadAssetsFromExcel(_directoryManager.ObservablesLocation); } var currentTime = DateTime.Now; Parallel.ForEach(assets, asset => { var pathToFolder = _directoryManager.GenerateForecastFolder(asset, periods, DirSwitcher.Auto, currentTime); var normalized = _processModel.GetDataAuto(asset, dataHours); if (normalized == null || !normalized.Any()) { _directoryManager.RemoveFolder(pathToFolder); var zeroResults = new ExcelLog() { AssetName = asset, Log = Indicator.ZeroRezults.ToString(), Rate = "0", Width = "0", Volume = "0", Change = "0", Rsi = "0" }; Shared.Log(zeroResults); return; } var csv = _fileManager.CreateDataCsv(normalized, pathToFolder); if (string.IsNullOrEmpty(csv)) { var zeroResults = new ExcelLog() { AssetName = asset, Log = Indicator.ZeroRezults.ToString(), Rate = "0", Width = "0", Volume = "0", Change = "0", Rsi = "0" }; Shared.Log(zeroResults); return; } _directoryManager.SaveDataFile(csv, pathToFolder); _pythonExec.RunPython(pathToFolder, periods, hourlySeasonality, dailySeasonality); var pathToOut = _directoryManager.FilePathOut(pathToFolder); var pathToComponents = _directoryManager.FileComponentsOut(pathToFolder); var pathToForecast = _directoryManager.FileForecastOut(pathToFolder); var outCreated = _directoryManager.WaitForFile(pathToOut, 60); var componentsCreated = _directoryManager.WaitForFile(pathToComponents, 10); var forecastCreated = _directoryManager.WaitForFile(pathToForecast, 10); if (!outCreated.Result || !forecastCreated.Result || !componentsCreated.Result) { return; } var stats = _fileManager.BuildOutTableRows(pathToOut, periods); var performance = _utility.DefinePerformance(stats); var marketFeatures = _utility.GetFeatures(normalized, asset); var rsi = _utility.Rsi(normalized); var log = new ExcelLog { AssetName = asset, Log = performance.Indicator.ToString(), Rate = performance.Rate.ToString(), Width = performance.Width.ToString(), Volume = marketFeatures.Volume.ToString() + "BTC", Change = marketFeatures.Change.ToString("N2"), Rsi = rsi.ToString("N2") + "%" }; Shared.Log(log); _directoryManager.SpecifyDirByTrend(performance.Indicator, pathToFolder); } ); lastFolder = _directoryManager.GetLastFolder(DirSwitcher.Auto); var results = _directoryManager.GetListByIndicator(lastFolder); viewModel.NegativeAssets = results.NegativeAssets; viewModel.NeutralAssets = results.NeutralAssets; viewModel.PositiveAssets = results.PositiveAssets; viewModel.StrongPositiveAssets = results.StrongPositiveAssets; var res = _fileManager.WriteLogExcel(lastFolder, Shared.GetLog); _directoryManager.WriteLogToExcel(lastFolder, res); Shared.ClearLog(); viewModel.Report = _directoryManager.GetReport(lastFolder); var model = _requestHelper.GetStats(); viewModel.CallsLeftHisto = model.CallsLeft.Histo; viewModel.CallsMadeHisto = model.CallsMade.Histo; _logger.LogWarning($"Finished auto forecast from file {readFrom}"); } catch (Exception e) { lastFolder = _directoryManager.GetLastFolder(DirSwitcher.Auto); if (Shared.GetLog.Any()) { var res = _fileManager.WriteLogExcel(lastFolder, Shared.GetLog); _directoryManager.WriteLogToExcel(lastFolder, res); Shared.ClearLog(); } _logger.LogError($"Error in Auto Forecast for hours {dataHours}, " + $"for periods {periods}, " + $"from file {readFrom}, " + $"hourly seasonality: {hourlySeasonality}, " + $"daily seasonality: {dailySeasonality}."); throw new Exception(e.Message); } return(viewModel); }