private async Task ExecuteStrategy(StrategyContext strategyContext, CancellationToken token) { while (!token.IsCancellationRequested) { _logger.LogInformation("Getting candles..."); var candles = (await _binanceApi.GetCandles(strategyContext.Market, strategyContext.Interval, 25)).ToList(); await _strategy.Execute(candles); await Task.Delay(TimeSpan.FromMinutes(30), token); } }
private async Task RunTestAsync(string market, string candleSize, string strategyName) { var candles = (await _api.GetCandles(market, candleSize, 1000)).ToList(); var strategy = _strategyFactory.GetStrategy(strategyName); await strategy.Execute(candles); var sb = new StringBuilder(); if (strategy.Trades.Any()) { decimal totalProfit = strategy.Trades.Last(t => t.Direction == TradeDirection.Sell).VolumeEur; sb.AppendLine($"Start-End Date:{candles.First().OpenDate} - {candles.Last().OpenDate} {(candles.First().OpenDate - candles.Last().OpenDate).Days} days"); sb.AppendLine($"InitialInvestment: {strategy.InitialInvestment}"); sb.AppendLine($"Pnl: {totalProfit.Round(2)} ({MathExtensions.PercentageDifference(strategy.InitialInvestment, totalProfit)}%)"); sb.AppendLine($"Trades: "); for (int i = 0; i < strategy.Trades.Count; i++) { var p = strategy.Trades[i]; if (p.Direction == TradeDirection.Buy) { var percentage = i > 0 ? MathExtensions.PercentageDifference(strategy.Trades[i - 1].Volume, p.Volume) : 0; sb.AppendLine($"{p.TradeDate}: {p.Direction} >> {Math.Round(p.VolumeEur, 2)}EUR * {p.Price.Round(2)} = {p.Volume.Round(2)}??? ({percentage.Round(2)}%)"); } if (p.Direction == TradeDirection.Sell) { var percentage = i > 0 ? MathExtensions.PercentageDifference(strategy.Trades[i - 1].VolumeEur, p.VolumeEur) : 0; sb.AppendLine($"{p.TradeDate}: {p.Direction} >> {strategy.Trades[i - 1].Volume.Round(2)}??? @ {p.Price.Round(2)} = {Math.Round(p.VolumeEur, 2)}EUR ({percentage.Round(2)}%)"); } } } else { sb.AppendLine("No trades executed"); } await _telegram.SendMessageAsync(sb.ToString()); }