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);
            }
        }
Beispiel #2
0
        public async Task <ManualViewModel> MakeManualForecast(string asset, int dataHours, int periods,
                                                               bool hourlySeasonality, bool dailySeasonality)
        {
            var viewModel = new ManualViewModel();

            var directory = _directoryManager;
            var file      = _fileManager;

            try
            {
                var normalized = _processModel.GetDataManual(asset, dataHours);
                var location   = directory.GenerateForecastFolder(asset, periods, DirSwitcher.Manual);

                var csv = _fileManager.CreateDataCsv(normalized, location);
                if (string.IsNullOrEmpty(csv))
                {
                    throw new Exception("Not enough data: " + asset);
                }

                _directoryManager.SaveDataFile(csv, location);

                _pythonExec.RunPython(location, periods, hourlySeasonality, dailySeasonality);

                var pathToOut        = directory.FilePathOut(location);
                var pathToComponents = directory.FileComponentsOut(location);
                var pathToForecast   = directory.FileForecastOut(location);
                var outCreated       = await directory.WaitForFile(pathToOut, 60);

                var componentsCreated = await directory.WaitForFile(pathToComponents, 10);

                var forecastCreated = await directory.WaitForFile(pathToForecast, 10);

                var images = directory.ImagePath(DirSwitcher.Manual);

                if (forecastCreated)
                {
                    viewModel.ForecastPath = images.ForecastImage;
                }
                else
                {
                    throw new Exception("forecast.png not found");
                }

                if (componentsCreated)
                {
                    viewModel.ComponentsPath = images.ComponentsImage;
                }
                else
                {
                    throw new Exception("components.png not found");
                }

                if (outCreated)
                {
                    var stats       = file.BuildOutTableRows(pathToOut, periods);
                    var performance = _utility.DefinePerformance(stats);
                    viewModel.Table     = stats.Table;
                    viewModel.Indicator = performance.Indicator;
                    viewModel.Rate      = performance.Rate.ToString("N2");
                    viewModel.Width     = performance.Width.ToString();
                    var marketFeatures = _utility.GetFeatures(normalized, asset);
                    viewModel.Volume = marketFeatures.Volume.ToString();
                    viewModel.Change = marketFeatures.Change.ToString("N2");
                    var rsi = _utility.Rsi(normalized);
                    viewModel.Rsi = rsi.ToString("N2");
                }
                else
                {
                    throw new Exception("out.csv not found");
                }

                viewModel.AssetName = asset;


                var model = _requestHelper.GetStats();
                viewModel.CallsLeftHisto = model.CallsLeft.Histo;
                viewModel.CallsMadeHisto = model.CallsMade.Histo;
                return(viewModel);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }