示例#1
0
        public async Task Execute(params string[] args)
        {
            _projectSettings.EnsureAllDirectoriesExist();
            var command  = args[0];
            var fromDate = default(DateTime?);
            var toDate   = default(DateTime?);
            var tradingSimulationConfig = TradingSimulationConfig.CreateFrom(_configuration);

            switch (command)
            {
            case "h":
            case "help":
                _logger.LogInfo(HelpMessage);
                break;

            case "getDir":
                _logger.LogInfo($"Working directory: {_projectSettings.WorkingDirectory.FullName}");
                break;

            case "openDir":
                ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe", _projectSettings.WorkingDirectory.FullName);
                startInfo.Verb = "runas";
                Process.Start(startInfo);
                break;

            case "printUnzipped":
                _logger.LogInfo(string.Join(Environment.NewLine, _projectSettings.GetFilesListInDirectory(_projectSettings.UnzippedFilesDirectory)));
                break;


            case "cleanDir":
                _projectSettings.CleanOutputDirectory();
                _logger.LogInfo($"Cleaned directory {_projectSettings.UnzippedFilesDirectory.FullName}");
                break;

            case "cleanLogs":
                _projectSettings.CleanLogs();
                _logger.LogInfo($"Cleaned logs in {_projectSettings.LogDirectory.FullName}");
                break;

            case "dropDb":
                await _databaseManagementService.EnsureDbDoesNotExist(_projectSettings);

                break;

            case "download":
                await _stockQuotesDownloadService.Download(_projectSettings);

                break;

            case "unzip":
                await _stockQuotesMigrationFromCsv.Unzip(_projectSettings);

                break;

            case "migrate":
                await _stockQuotesMigrationFromCsv.Migrate(_projectSettings, TargetLocation.Directory);

                _logger.LogInfo("Successfully migrated data to database.");
                break;

            case "dbVersion":
                var latestDateInDb = await _stockQuoteRepository.GetLatestSessionInDbDateAsync();

                _logger.LogInfo(latestDateInDb.ToShortDateString());
                break;

            case "update":
                await _stockUpdateService.PerformUpdateTillToday();

                break;

            case "print":
                if (args.Length >= 2)
                {
                    var found = _companyRepository.GetById(args[1]);
                    if (found == null)
                    {
                        _logger.LogWarning($"{args[1]} not found.");
                    }
                    else
                    {
                        _logger.LogInfo(string.Join(Environment.NewLine, found.Quotes.Select(x => x.Summary())));
                    }
                }
                else
                {
                    var summary = _companyRepository.Summary();
                    _logger.LogInfo($"{Environment.NewLine}{summary.Count} companies.{string.Join(Environment.NewLine, summary)}");
                }
                break;

            case "simulate":
                if (args.Length >= 3)
                {
                    fromDate = TryParseDateTime(args[1]);
                    toDate   = TryParseDateTime(args[2]);
                    if (fromDate.HasValue && toDate.HasValue)
                    {
                        tradingSimulationConfig.FromDate = fromDate.Value;
                        tradingSimulationConfig.ToDate   = toDate.Value;
                    }
                }

                var allQuotesPrefilterd = _stockQuoteRepository
                                          .GetAll(x => !_projectSettings.BlackListPattern.IsMatch(x.Ticker) &&
                                                  x.DateParsed.InOpenRange(tradingSimulationConfig.FromDate.AddDays(-30), tradingSimulationConfig.ToDate))
                                          .ToList();

                var simulationResult = _tradingSimulator.Simulate(allQuotesPrefilterd, tradingSimulationConfig, _progressReporter);

                _logger.LogInfo(simulationResult.ToString());
                break;

            case "predict":
                if (args.Length >= 2)
                {
                    var date = TryParseDateTime(args[1]);
                    if (date.HasValue)
                    {
                        var prediction = _tradingSimulator.GetSignals(tradingSimulationConfig, date.Value);
                        _logger.LogInfo($"Stonks to buy: {string.Join(", ", prediction.Select(x => x.Ticker).OrderBy(x => x))}. Used prediction made with data from session {prediction.Select(x => x.DateParsed).First()} and config = {tradingSimulationConfig}");
                    }
                    else
                    {
                        _logger.LogError($"{args[1]} is not a valid date. Enter date in format YYYY-MM-DD");
                    }
                }
                else
                {
                    _logger.LogError($"Enter date in format YYYY-MM-DD as second argument after empty space. Example: predict 2020-01-01");
                }
                break;

            default:
                _logger.LogWarning(@$ "{command} not recognized as valid command. {HelpMessage}");
                break;
            }
        }