Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public IEnumerable <SimulationState> Evaluate(
            IStrategy strategy,
            Investor investor,
            DateTime?endDate     = null,
            ProgressBar progress = null)
        {
            var state     = new SimulationState();
            var remaining = _dataCache.Count - _dataCache.BacktestingIndex;

            using var childProgress = ProgressBarProvider.Create(progress, remaining, $"Evaluating: {strategy.StrategyType.GetDescription()}");
            foreach (var data in _dataCache.TakeFrom(Configuration.BacktestingDate, endDate))
            {
                Optimise(strategy);
                var shouldBuy = _simulationCache.GetOrCreate((strategy, data.Date), () => ShouldBuy(strategy, data));

                state = state.UpdateState(data, shouldBuy);
                state.AddFunds(investor.DailyFunds);
                state.ExecuteOrders();

                if (state.ShouldBuy)
                {
                    var funds = strategy.GetStake(data.Date, state.TotalFunds);
                    state.AddBuyOrder(investor.OrderBrokerage, investor.OrderDelayDays, funds);
                }

                childProgress?.Tick();
                yield return(state);
            }
        }
Ejemplo n.º 3
0
        public void RateMarketData(DateTime?toDate = null)
        {
            var buyDates = _marketDataCache
                           .TakeUntil(toDate)
                           .Skip(_marketDataCache.BacktestingIndex)
                           .Select(x => x.Date)
                           .ToDictionary(k => k, _ => true);

            using var progressBar = ProgressBarProvider.Create(_marketDataCache.BacktestingIndex, "Initialising...");
            _marketAverage        = SimulateBuyDates(buyDates);
            _marketMaximum        = GetMarketMaximum(buyDates, progressBar);
        }