Esempio n. 1
0
        /// <summary>
        /// Mains the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main(string[] args)
        {
            var options = new Options();
            var parsed  = CommandLine.Parser.Default.ParseArguments(args, options);

            if (parsed && !string.IsNullOrEmpty(options.InputFile))
            {
                var companyName       = "";
                var status            = "";
                var episodeLength     = 5;
                var redis             = ConnectionMultiplexer.Connect("localhost:6379");
                var manager           = new RedisManager(redis);
                var initialCapital    = 100000;
                var dayNumber         = 0;
                int totalTrainingDays = 0;

                var agentParameters = new DeepRLAgentParameters();
                agentParameters.DiscountFactor     = options.DiscountFactor > 0 ? options.DiscountFactor : agentParameters.DiscountFactor;
                agentParameters.eGreedyProbability = options.eGreedyProbability > 0 ? options.eGreedyProbability : agentParameters.eGreedyProbability;
                agentParameters.MiniBatchSize      = options.MiniBatchSize > 0 ? options.MiniBatchSize : agentParameters.MiniBatchSize;
                agentParameters.MemoryReplaySize   = options.MemoryReplaySize > 0 ? options.MemoryReplaySize : agentParameters.MemoryReplaySize;
                agentParameters.QNetwork.MaxIterationPerTrainging = options.MaxIterationPerTrainging > 0 ? options.MaxIterationPerTrainging : agentParameters.QNetwork.MaxIterationPerTrainging;
                agentParameters.QNetwork.TrainingError            = options.TrainingError > 0 ? options.TrainingError : agentParameters.QNetwork.TrainingError;

                var agent = new DeepRLAgent(agentParameters);;

                var stockParameters = new StockExchangeParameters();
                stockParameters.EpisodeLength      = episodeLength;
                stockParameters.InitialCapital     = initialCapital;
                stockParameters.SimulationVelocity = options.Velocity > 0 ? (int)(options.Velocity * 1000.0) : stockParameters.SimulationVelocity;
                stockParameters.TransactionCost    = options.TransactionCost > 0 ? options.TransactionCost : stockParameters.TransactionCost;
                stockParameters.RewardCalculator   = RewardCalculator.Use(RewardCalculatorType.WinningsOverLoosings);

                var stock = new StockExchange(stockParameters, manager, agent, null);

                manager.Subscribe(RedisPubSubChannels.OnDayComplete, (channel, msg) =>
                {
                    var a        = JsonConvert.DeserializeObject <OnDayComplete>(msg);
                    dayNumber    = a.DayNumber - totalTrainingDays;
                    initialValue = initialValue == 0.0 ? a.Period.Open : initialValue;
                    DrawSummarySection(companyName, status, dayNumber, initialCapital, a);
                });


                agent.OnTrainingEpochComplete += DrawStatusBar;

                var continueSimulated = false;
                companyName = options.InputFile;

                do
                {
                    stock.DataProvider = new CsvDataProvider(companyName, episodeLength);
                    DateTime endTraining = DateTime.Today;

                    if (options.TrainingPhase > 0)
                    {
                        var firstDay = stock.DataProvider.GetAll().ToList().Select(s => s.Date).Min();
                        endTraining = firstDay.AddYears(options.TrainingPhase);

                        stock.DataProvider.Seek(endDate: endTraining);
                        status = "Etapa: Entrenamiento";
                    }


                    System.Console.Clear();
                    System.Console.SetCursorPosition(0, 0);
                    DrawHeaderSection();

                    var stockTask = Task.Run(() => stock.Run(new JobCancellationToken(false)));
                    stockTask.Wait();

                    if (options.TrainingPhase > 0)
                    {
                        status = "Etapa: Entrenamiento Completado";
                        WriteLine(StatusBarLine + 5, "Etapa de entrenamiento Completada, Presione una tecla para continuar con la etapa de evaluacion... ");
                        System.Console.ReadLine();
                        ClearCurrentLine();
                        totalTrainingDays  = dayNumber;
                        stock.TotalOfYears = 0;

                        status = "Etapa: Evaluacion";

                        stock.CurrentState = null;
                        stock.DataProvider.Seek(startDate: endTraining);
                        //agent.Save(context);


                        agentParameters.DiscountFactor     = options.DiscountFactor > 0 ? options.DiscountFactor : agentParameters.DiscountFactor;
                        agentParameters.eGreedyProbability = options.eGreedyProbability > 0 ? options.eGreedyProbability : agentParameters.eGreedyProbability;
                        agentParameters.MiniBatchSize      = options.MiniBatchSize > 0 ? options.MiniBatchSize : agentParameters.MiniBatchSize;
                        agentParameters.MemoryReplaySize   = options.MemoryReplaySize > 0 ? options.MemoryReplaySize : agentParameters.MemoryReplaySize;

                        agent       = new DeepRLAgent(agentParameters);
                        stock.Agent = agent;

                        stockTask = Task.Run(() => stock.Run(new JobCancellationToken(false)));
                        stockTask.Wait();
                    }

                    WriteLine(StatusBarLine + 5, "Simulacion Finalizada, Presione una tecla para finalizar o ingrese otro papel : ");
                    var previousCompany = companyName;
                    companyName       = System.Console.ReadLine();
                    continueSimulated = !string.IsNullOrEmpty(companyName) && companyName != previousCompany;

                    stock.CurrentState = null;
                } while (continueSimulated);
            }
        }