Send notif on good backtest
This commit is contained in:
@@ -26,6 +26,7 @@ namespace Managing.Application.Backtesting
|
||||
private readonly IBotFactory _botFactory;
|
||||
private readonly IScenarioService _scenarioService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly IMessengerService _messengerService;
|
||||
|
||||
public Backtester(
|
||||
IExchangeService exchangeService,
|
||||
@@ -33,7 +34,8 @@ namespace Managing.Application.Backtesting
|
||||
IBacktestRepository backtestRepository,
|
||||
ILogger<Backtester> logger,
|
||||
IScenarioService scenarioService,
|
||||
IAccountService accountService)
|
||||
IAccountService accountService,
|
||||
IMessengerService messengerService)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_botFactory = botFactory;
|
||||
@@ -41,6 +43,7 @@ namespace Managing.Application.Backtesting
|
||||
_logger = logger;
|
||||
_scenarioService = scenarioService;
|
||||
_accountService = accountService;
|
||||
_messengerService = messengerService;
|
||||
}
|
||||
|
||||
public Backtest RunSimpleBotBacktest(Workflow workflow, bool save = false)
|
||||
@@ -133,7 +136,7 @@ namespace Managing.Application.Backtesting
|
||||
tradingBot.User = user;
|
||||
await tradingBot.LoadAccount();
|
||||
|
||||
var result = GetBacktestingResult(config, tradingBot, candles, withCandles);
|
||||
var result = await GetBacktestingResult(config, tradingBot, candles, user, withCandles);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
@@ -170,10 +173,11 @@ namespace Managing.Application.Backtesting
|
||||
return candles;
|
||||
}
|
||||
|
||||
private Backtest GetBacktestingResult(
|
||||
private async Task<Backtest> GetBacktestingResult(
|
||||
TradingBotConfig config,
|
||||
ITradingBot bot,
|
||||
List<Candle> candles,
|
||||
User user = null,
|
||||
bool withCandles = false)
|
||||
{
|
||||
if (candles == null || candles.Count == 0)
|
||||
@@ -248,7 +252,8 @@ namespace Managing.Application.Backtesting
|
||||
var score = BacktestScorer.CalculateTotalScore(scoringParams);
|
||||
|
||||
// Create backtest result with conditional candles and indicators values
|
||||
var result = new Backtest(config, bot.Positions, bot.Signals.ToList(), withCandles ? candles : new List<Candle>())
|
||||
var result = new Backtest(config, bot.Positions, bot.Signals.ToList(),
|
||||
withCandles ? candles : new List<Candle>())
|
||||
{
|
||||
FinalPnl = finalPnl,
|
||||
WinRate = winRate,
|
||||
@@ -258,14 +263,52 @@ namespace Managing.Application.Backtesting
|
||||
WalletBalances = bot.WalletBalances.ToList(),
|
||||
Statistics = stats,
|
||||
OptimizedMoneyManagement = optimizedMoneyManagement,
|
||||
IndicatorsValues = withCandles ? AggregateValues(indicatorsValues, bot.IndicatorsValues) : new Dictionary<IndicatorType, IndicatorsResultBase>(),
|
||||
IndicatorsValues = withCandles
|
||||
? AggregateValues(indicatorsValues, bot.IndicatorsValues)
|
||||
: new Dictionary<IndicatorType, IndicatorsResultBase>(),
|
||||
Score = score,
|
||||
Id = Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
// Send notification if backtest meets criteria
|
||||
await SendBacktestNotificationIfCriteriaMet(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task SendBacktestNotificationIfCriteriaMet(Backtest backtest)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check if backtest meets criteria: score > 85, at least 5 positions, winrate > 65%, risk-reward >= 1.5:1
|
||||
var score = backtest.Score;
|
||||
var tradeCount = backtest.Positions?.Count ?? 0;
|
||||
var winRate = backtest.WinRate;
|
||||
|
||||
// Calculate risk-reward ratio from money management settings
|
||||
var riskRewardRatio = 0.0;
|
||||
if (backtest.Config.MoneyManagement != null)
|
||||
{
|
||||
var stopLoss = (double)backtest.Config.MoneyManagement.StopLoss;
|
||||
var takeProfit = (double)backtest.Config.MoneyManagement.TakeProfit;
|
||||
|
||||
if (stopLoss > 0 && takeProfit > 0)
|
||||
{
|
||||
riskRewardRatio = takeProfit / stopLoss;
|
||||
}
|
||||
}
|
||||
|
||||
if (score > 85 && tradeCount >= 5 && winRate > 65 && riskRewardRatio >= 1.5)
|
||||
{
|
||||
await _messengerService.SendBacktestNotification(backtest);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to send backtest notification for backtest {Id}", backtest.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<IndicatorType, IndicatorsResultBase> AggregateValues(
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues,
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> botStrategiesValues)
|
||||
|
||||
Reference in New Issue
Block a user