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); } }
public void Evaluate(DateTime fromDate, DateTime toDate) { var marketData = _marketDataCache.TakeFrom(fromDate, toDate) .Select(x => x.DeltaPercent / 100) .Split(x => x <= 0); var profit = marketData.TrueSet.ToArray(); var loss = marketData.FalseSet.ToArray(); if (profit.Length == 0) { _fraction = 0; return; } if (loss.Length == 0) { _fraction = 1; return; } var avgProfit = -profit.Average(); var avgLoss = loss.Average(); var probability = (decimal)profit.Length / (profit.Length + loss.Length); var tradeValue = avgProfit / avgLoss; var fraction = probability - ((1 - probability) / tradeValue); _fraction = fraction.Clamp(0, 1); }
public void Evaluate(DateTime fromDate, DateTime toDate) { var marketData = _marketDataCache.TakeFrom(fromDate, toDate).ToArray(); var histogram = ((ProbabilityParameters)_strategy.Parameters).Histogram; var currentPrice = Convert.ToInt32(marketData.Last().DeltaPercent); var tradeValue = (decimal)histogram[currentPrice].Average(); var total = (decimal)histogram.Select(x => x.Value).Count(); var probability = histogram[currentPrice].Count / total; var fraction = probability - (1 - probability) / tradeValue; _fraction = fraction.Clamp(0, 1); }
public void Evaluate(DateTime fromDate, DateTime toDate) { var marketData = _marketDataCache.TakeFrom(fromDate, toDate) .Select(x => x.DeltaPercent / 100) .Where(_ => { var found = _simulationCache.TryGet((_strategy, toDate), out var shouldBuy); if (!found) { return(false); } return(shouldBuy); }) .Split(x => x <= 0); var profit = marketData.TrueSet.ToArray(); var loss = marketData.FalseSet.ToArray(); if (profit.Length == 0) { _fraction = 0; return; } if (loss.Length == 0) { _fraction = 1; return; } var avgProfit = -profit.Average(); var avgLoss = loss.Average(); var probability = (decimal)profit.Length / (profit.Length + loss.Length); var tradeValue = avgProfit / avgLoss; var fraction = probability - (1 - probability) / tradeValue; _fraction = fraction.Clamp(0, 1); }