Postgres (#30)

* Add postgres

* Migrate users

* Migrate geneticRequest

* Try to fix Concurrent call

* Fix asyncawait

* Fix async and concurrent

* Migrate backtests

* Add cache for user by address

* Fix backtest migration

* Fix not open connection

* Fix backtest command error

* Fix concurrent

* Fix all concurrency

* Migrate TradingRepo

* Fix scenarios

* Migrate statistic repo

* Save botbackup

* Add settings et moneymanagement

* Add bot postgres

* fix a bit more backups

* Fix bot model

* Fix loading backup

* Remove cache market for read positions

* Add workers to postgre

* Fix workers api

* Reduce get Accounts for workers

* Migrate synth to postgre

* Fix backtest saved

* Remove mongodb

* botservice decorrelation

* Fix tradingbot scope call

* fix tradingbot

* fix concurrent

* Fix scope for genetics

* Fix account over requesting

* Fix bundle backtest worker

* fix a lot of things

* fix tab backtest

* Remove optimized moneymanagement

* Add light signal to not use User and too much property

* Make money management lighter

* insert indicators to awaitable

* Migrate add strategies to await

* Refactor scenario and indicator retrieval to use asynchronous methods throughout the application

* add more async await

* Add services

* Fix and clean

* Fix bot a bit

* Fix bot and add message for cooldown

* Remove fees

* Add script to deploy db

* Update dfeeploy script

* fix script

* Add idempotent script and backup

* finish script migration

* Fix did user and agent name on start bot
This commit is contained in:
Oda
2025-07-27 15:42:17 +02:00
committed by GitHub
parent 361bfbf6e8
commit 422fecea7b
294 changed files with 23953 additions and 7272 deletions

View File

@@ -2,12 +2,14 @@ using System.Text.Json;
using GeneticSharp;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Core;
using Managing.Domain.Backtests;
using Managing.Domain.Bots;
using Managing.Domain.MoneyManagements;
using Managing.Domain.Risk;
using Managing.Domain.Scenarios;
using Managing.Domain.Users;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
@@ -22,6 +24,7 @@ public class GeneticService : IGeneticService
private readonly IBacktester _backtester;
private readonly ILogger<GeneticService> _logger;
private readonly IMessengerService _messengerService;
private readonly IServiceScopeFactory _serviceScopeFactory;
// Predefined parameter ranges for each indicator (matching backtestGenetic.tsx)
public static readonly Dictionary<string, (double min, double max)> ParameterRanges = new()
@@ -188,12 +191,14 @@ public class GeneticService : IGeneticService
IGeneticRepository geneticRepository,
IBacktester backtester,
ILogger<GeneticService> logger,
IMessengerService messengerService)
IMessengerService messengerService,
IServiceScopeFactory serviceScopeFactory)
{
_geneticRepository = geneticRepository;
_backtester = backtester;
_logger = logger;
_messengerService = messengerService;
_serviceScopeFactory = serviceScopeFactory;
}
public GeneticRequest CreateGeneticRequest(
@@ -247,9 +252,9 @@ public class GeneticService : IGeneticService
return _geneticRepository.GetGeneticRequestByIdForUser(user, id);
}
public void UpdateGeneticRequest(GeneticRequest geneticRequest)
public async Task UpdateGeneticRequestAsync(GeneticRequest geneticRequest)
{
_geneticRepository.UpdateGeneticRequest(geneticRequest);
await _geneticRepository.UpdateGeneticRequestAsync(geneticRequest);
}
public void DeleteGeneticRequestByIdForUser(User user, string id)
@@ -257,9 +262,9 @@ public class GeneticService : IGeneticService
_geneticRepository.DeleteGeneticRequestByIdForUser(user, id);
}
public IEnumerable<GeneticRequest> GetPendingGeneticRequests()
public Task<List<GeneticRequest>> GetGeneticRequestsAsync(GeneticRequestStatus status)
{
return _geneticRepository.GetPendingGeneticRequests();
return _geneticRepository.GetGeneticRequestsAsync(status);
}
/// <summary>
@@ -277,7 +282,7 @@ public class GeneticService : IGeneticService
// Update status to running
request.Status = GeneticRequestStatus.Running;
UpdateGeneticRequest(request);
await UpdateGeneticRequestAsync(request);
// Create or resume chromosome for trading bot configuration
TradingBotChromosome chromosome;
@@ -307,7 +312,7 @@ public class GeneticService : IGeneticService
}
// Create fitness function first
var fitness = new TradingBotFitness(_backtester, request, _logger);
var fitness = new TradingBotFitness(_serviceScopeFactory, request, _logger);
// Create genetic algorithm with better configuration
var ga = new GeneticAlgorithm(
@@ -341,7 +346,7 @@ public class GeneticService : IGeneticService
// Run the genetic algorithm with periodic checks for cancellation
var generationCount = 0;
ga.GenerationRan += (sender, e) =>
ga.GenerationRan += async (sender, e) =>
{
generationCount = ga.GenerationsNumber;
@@ -362,7 +367,7 @@ public class GeneticService : IGeneticService
request.BestChromosome = JsonSerializer.Serialize(geneValues);
}
UpdateGeneticRequest(request);
await UpdateGeneticRequestAsync(request);
// Check for cancellation
if (cancellationToken.IsCancellationRequested)
@@ -381,7 +386,7 @@ public class GeneticService : IGeneticService
// Update request status to pending so it can be resumed
request.Status = GeneticRequestStatus.Pending;
UpdateGeneticRequest(request);
await UpdateGeneticRequestAsync(request);
return new GeneticAlgorithmResult
{
@@ -413,7 +418,7 @@ public class GeneticService : IGeneticService
completed_at = DateTime.UtcNow
});
UpdateGeneticRequest(request);
await UpdateGeneticRequestAsync(request);
// Send notification about the completed genetic algorithm
try
@@ -442,7 +447,7 @@ public class GeneticService : IGeneticService
request.Status = GeneticRequestStatus.Failed;
request.ErrorMessage = ex.Message;
request.CompletedAt = DateTime.UtcNow;
UpdateGeneticRequest(request);
await UpdateGeneticRequestAsync(request);
throw;
}
@@ -505,7 +510,7 @@ public class TradingBotChromosome : ChromosomeBase
private readonly List<IndicatorType> _eligibleIndicators;
private readonly double _maxTakeProfit;
private readonly Random _random = new Random();
private int[]? _indicatorSelectionPattern;
private int[] _indicatorSelectionPattern;
// Gene structure:
// 0-3: Trading parameters (takeProfit, stopLoss, cooldownPeriod, maxLossStreak)
@@ -552,7 +557,7 @@ public class TradingBotChromosome : ChromosomeBase
GenerateIndicatorSelectionPattern();
}
return new Gene(_indicatorSelectionPattern![geneIndex - 5]);
return new Gene(_indicatorSelectionPattern[geneIndex - 5]);
}
else
{
@@ -790,7 +795,7 @@ public class TradingBotChromosome : ChromosomeBase
return _random.Next((int)range.min, (int)range.max + 1);
}
private string? GetParameterName(int index)
private string GetParameterName(int index)
{
return index switch
{
@@ -879,14 +884,14 @@ public class GeneticIndicator
/// </summary>
public class TradingBotFitness : IFitness
{
private readonly IBacktester _backtester;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly GeneticRequest _request;
private GeneticAlgorithm _geneticAlgorithm;
private readonly ILogger<GeneticService> _logger;
public TradingBotFitness(IBacktester backtester, GeneticRequest request, ILogger<GeneticService> logger)
public TradingBotFitness(IServiceScopeFactory serviceScopeFactory, GeneticRequest request, ILogger<GeneticService> logger)
{
_backtester = backtester;
_serviceScopeFactory = serviceScopeFactory;
_request = request;
_logger = logger;
}
@@ -909,19 +914,22 @@ public class TradingBotFitness : IFitness
// Get current generation number (default to 0 if not available)
var currentGeneration = _geneticAlgorithm?.GenerationsNumber ?? 0;
// Run backtest
var backtest = _backtester.RunTradingBotBacktest(
config,
_request.StartDate,
_request.EndDate,
_request.User,
true,
false, // Don't include candles
_request.RequestId,
new
{
generation = currentGeneration
}
// Run backtest using scoped service to avoid DbContext concurrency issues
var backtest = ServiceScopeHelpers.WithScopedService<IBacktester, Backtest>(
_serviceScopeFactory,
backtester => backtester.RunTradingBotBacktest(
config,
_request.StartDate,
_request.EndDate,
_request.User,
true,
false, // Don't include candles
_request.RequestId,
new
{
generation = currentGeneration
}
)
).Result;
// Calculate multi-objective fitness based on backtest results
@@ -929,8 +937,9 @@ public class TradingBotFitness : IFitness
return fitness;
}
catch (Exception)
catch (Exception ex)
{
_logger.LogWarning("Fitness evaluation failed for chromosome: {Message}", ex.Message);
// Return low fitness for failed backtests
return 0.1;
}