Add indicators to backtest and bot (#14)
* Add indicators to backtest and bot * Remove
This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Core;
|
||||
using Managing.Core.FixedSizedQueue;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Managing.Domain.Workflows;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
@@ -20,17 +23,19 @@ namespace Managing.Application.Backtesting
|
||||
private readonly ILogger<Backtester> _logger;
|
||||
private readonly IExchangeService _exchangeService;
|
||||
private readonly IBotFactory _botFactory;
|
||||
private readonly IScenarioService _scenarioService;
|
||||
|
||||
public Backtester(
|
||||
IExchangeService exchangeService,
|
||||
IBotFactory botFactory,
|
||||
IBacktestRepository backtestRepository,
|
||||
ILogger<Backtester> logger)
|
||||
ILogger<Backtester> logger, IScenarioService scenarioService)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_botFactory = botFactory;
|
||||
_backtestRepository = backtestRepository;
|
||||
_logger = logger;
|
||||
_scenarioService = scenarioService;
|
||||
}
|
||||
|
||||
public Backtest RunSimpleBotBacktest(Workflow workflow, bool save = false)
|
||||
@@ -124,7 +129,7 @@ namespace Managing.Application.Backtesting
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Backtest GetBacktestingResult(
|
||||
private Backtest GetBacktestingResult(
|
||||
Ticker ticker,
|
||||
Scenario scenario,
|
||||
Timeframe timeframe,
|
||||
@@ -143,10 +148,15 @@ namespace Managing.Application.Backtesting
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
bot.OptimizedCandles.Enqueue(candle);
|
||||
bot.Candles.Add(candle);
|
||||
bot.Run();
|
||||
}
|
||||
|
||||
bot.Candles = new HashSet<Candle>(candles);
|
||||
// bot.UpdateStrategiesValues();
|
||||
|
||||
var strategies = _scenarioService.GetStrategies();
|
||||
var strategiesValues = GetStrategiesValues(strategies, candles);
|
||||
|
||||
var finalPnl = bot.GetProfitAndLoss();
|
||||
var winRate = bot.GetWinRate();
|
||||
@@ -165,12 +175,39 @@ namespace Managing.Application.Backtesting
|
||||
WalletBalances = bot.WalletBalances.ToList(),
|
||||
Statistics = stats,
|
||||
OptimizedMoneyManagement = optimizedMoneyManagement,
|
||||
MoneyManagement = moneyManagement
|
||||
MoneyManagement = moneyManagement,
|
||||
StrategiesValues = strategiesValues
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Dictionary<StrategyType, StrategiesResultBase> GetStrategiesValues(IEnumerable<Strategy> strategies,
|
||||
List<Candle> candles)
|
||||
{
|
||||
var strategiesValues = new Dictionary<StrategyType, StrategiesResultBase>();
|
||||
var fixedCandles = new FixedSizeQueue<Candle>(10000);
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
fixedCandles.Enqueue(candle);
|
||||
}
|
||||
|
||||
foreach (var strategy in strategies)
|
||||
{
|
||||
try
|
||||
{
|
||||
var s = ScenarioHelpers.BuildStrategy(strategy, 10000);
|
||||
s.Candles = fixedCandles;
|
||||
strategiesValues[strategy.Type] = s.GetStrategyValues();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
return strategiesValues;
|
||||
}
|
||||
|
||||
public IEnumerable<Backtest> GetBacktests()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user