Ejemplo n.º 1
0
        /// <summary>
        /// Executes simulation. Simulation can be canceled using given cancelation token.
        /// </summary>
        /// <param name="cancelationToken">Cancelation token with which the simulation can be canceled.</param>
        public void ExecuteSimulation(CancellationToken?cancelationToken)
        {
            Trace.WriteLine("Simulation started!");
            try
            {
                var simulators = _scenarios.Select(scenario => new ProfitSimulator(scenario, _historyLoaders.Find(loader => loader.Crypto == scenario.Crypto))).ToList();

                var threader = new ThreadedExecutor(simulators.Select(simulator => new Func <bool>(simulator.ExecuteSimulationStep)).ToList());


                try
                {
                    while (threader.ExecuteSteps())
                    {
                        cancelationToken?.ThrowIfCancellationRequested();
                    }
                }
                catch (OperationCanceledException)
                {
                    Trace.WriteLine("Simulation canceled!");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                }
                finally
                {
                    simulators.ForEach(simulator => simulator.FinalizeSimulation());
                }

                Trace.WriteLine("Simulation done.");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }