コード例 #1
0
        private IResultsProvider Simulate(IEnumerable <IStrategy> strategies)
        {
            SimulationState[] SimulateStrategy(IStrategy strategy, ProgressBar progress)
            {
                var description = strategy.StrategyType.GetDescription();

                Log.Information($"Simulating strategy: {description}");

                var simulator = _simulatorFactory.Create <BacktestingSimulator>();
                var result    = simulator.Evaluate(strategy, _investorProvider.Current, progress: progress);

                return(result.ToArray());
            }

            var(sequential, parallelisable) = strategies.Split(x => x is IAggregateStrategy);
            foreach (var investor in _investorProvider)
            {
                _resultsProvider.Initialise();
                var histories = new ConcurrentDictionary <IStrategy, SimulationState[]>();
                using var progress = ProgressBarProvider.Create(0, "Evaluating...");
                Parallel.ForEach(parallelisable, strategy =>
                {
                    histories[strategy] = SimulateStrategy(strategy, progress);
                });
                foreach (var strategy in sequential)
                {
                    histories[strategy] = SimulateStrategy(strategy, progress);
                }
                _resultsProvider.AddResults(investor, histories);
            }

            return(_resultsProvider);
        }
コード例 #2
0
        public void Setup()
        {
            ConfigurationManager.AppSettings["BacktestingDate"] = "2010-07-01";
            ConfigurationManager.AppSettings["CacheSize"]       = "2000";
            ConfigurationManager.AppSettings["DataPath"]        = "MarketData.csv";
            ConfigurationManager.AppSettings["RelativePath"]    = @"../../../";

            var marketData      = CreateMarketData();
            var marketDataCache = CreateMarketDataCache(marketData);

            var investor = new Investor {
                DailyFunds = 10, OrderDelayDays = 3
            };
            var investorProvider = CreateInvestorProvider();

            var simulationCache = new SimulationCache();

            var simulationFactory = new SimulatorFactory(marketDataCache, simulationCache);
            var ratingService     = new RatingService(
                marketDataCache,
                simulationFactory,
                investorProvider);


            var strategyFactory = CreateStrategyFactory(
                marketDataCache,
                simulationCache,
                investorProvider,
                ratingService);

            var marketDataRepository        = new Mock <IRepository <MarketData> >();
            var simulationResultsRepository = new Mock <IRepository <SimulationResult> >();

            _target = new ResultsProvider(
                marketDataCache,
                marketDataRepository.Object,
                ratingService,
                simulationResultsRepository.Object);

            _target.Initialise();

            var strategy        = strategyFactory.Create(new HolidayEffectParameters());
            var stateJson       = File.ReadAllText(@"HolidayEffectSimulationState.json");
            var simulationState = JsonConvert.DeserializeObject <SimulationState[]>(stateJson);
            var resultsToAdd    = new ConcurrentDictionary <IStrategy, SimulationState[]>();

            resultsToAdd.TryAdd(strategy, simulationState);

            _target.AddResults(investor, resultsToAdd);
        }