Add Genetic workers
This commit is contained in:
@@ -27,6 +27,7 @@ namespace Managing.Application.Backtesting
|
||||
private readonly IScenarioService _scenarioService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly IMessengerService _messengerService;
|
||||
private readonly IGeneticService _geneticService;
|
||||
|
||||
public Backtester(
|
||||
IExchangeService exchangeService,
|
||||
@@ -35,7 +36,8 @@ namespace Managing.Application.Backtesting
|
||||
ILogger<Backtester> logger,
|
||||
IScenarioService scenarioService,
|
||||
IAccountService accountService,
|
||||
IMessengerService messengerService)
|
||||
IMessengerService messengerService,
|
||||
IGeneticService geneticService)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_botFactory = botFactory;
|
||||
@@ -44,6 +46,7 @@ namespace Managing.Application.Backtesting
|
||||
_scenarioService = scenarioService;
|
||||
_accountService = accountService;
|
||||
_messengerService = messengerService;
|
||||
_geneticService = geneticService;
|
||||
}
|
||||
|
||||
public Backtest RunSimpleBotBacktest(Workflow workflow, bool save = false)
|
||||
@@ -456,5 +459,83 @@ namespace Managing.Application.Backtesting
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new genetic algorithm request
|
||||
/// </summary>
|
||||
/// <param name="user">The user creating the request</param>
|
||||
/// <param name="ticker">The ticker to optimize for</param>
|
||||
/// <param name="timeframe">The timeframe to use</param>
|
||||
/// <param name="startDate">The start date for the backtest period</param>
|
||||
/// <param name="endDate">The end date for the backtest period</param>
|
||||
/// <param name="balance">The starting balance</param>
|
||||
/// <param name="populationSize">The population size for the genetic algorithm</param>
|
||||
/// <param name="generations">The number of generations to evolve</param>
|
||||
/// <param name="mutationRate">The mutation rate (0.0 - 1.0)</param>
|
||||
/// <param name="selectionMethod">The selection method to use</param>
|
||||
/// <param name="elitismPercentage">The percentage of elite individuals to preserve</param>
|
||||
/// <param name="maxTakeProfit">The maximum take profit percentage</param>
|
||||
/// <param name="eligibleIndicators">The list of eligible indicators</param>
|
||||
/// <returns>The created genetic request</returns>
|
||||
public GeneticRequest CreateGeneticRequest(
|
||||
User user,
|
||||
Ticker ticker,
|
||||
Timeframe timeframe,
|
||||
DateTime startDate,
|
||||
DateTime endDate,
|
||||
decimal balance,
|
||||
int populationSize,
|
||||
int generations,
|
||||
double mutationRate,
|
||||
string selectionMethod,
|
||||
int elitismPercentage,
|
||||
double maxTakeProfit,
|
||||
List<IndicatorType> eligibleIndicators)
|
||||
{
|
||||
return _geneticService.CreateGeneticRequest(
|
||||
user, ticker, timeframe, startDate, endDate, balance,
|
||||
populationSize, generations, mutationRate, selectionMethod,
|
||||
elitismPercentage, maxTakeProfit, eligibleIndicators);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all genetic requests for a user
|
||||
/// </summary>
|
||||
/// <param name="user">The user to get requests for</param>
|
||||
/// <returns>Collection of genetic requests</returns>
|
||||
public IEnumerable<GeneticRequest> GetGeneticRequestsByUser(User user)
|
||||
{
|
||||
return _geneticService.GetGeneticRequestsByUser(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific genetic request by ID for a user
|
||||
/// </summary>
|
||||
/// <param name="user">The user</param>
|
||||
/// <param name="id">The request ID</param>
|
||||
/// <returns>The genetic request or null if not found</returns>
|
||||
public GeneticRequest GetGeneticRequestByIdForUser(User user, string id)
|
||||
{
|
||||
return _geneticService.GetGeneticRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a genetic request
|
||||
/// </summary>
|
||||
/// <param name="geneticRequest">The genetic request to update</param>
|
||||
public void UpdateGeneticRequest(GeneticRequest geneticRequest)
|
||||
{
|
||||
_geneticService.UpdateGeneticRequest(geneticRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a genetic request by ID for a user
|
||||
/// </summary>
|
||||
/// <param name="user">The user</param>
|
||||
/// <param name="id">The request ID</param>
|
||||
public void DeleteGeneticRequestByIdForUser(User user, string id)
|
||||
{
|
||||
_geneticService.DeleteGeneticRequestByIdForUser(user, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
82
src/Managing.Application/GeneticService.cs
Normal file
82
src/Managing.Application/GeneticService.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Users;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application;
|
||||
|
||||
/// <summary>
|
||||
/// Service implementation for managing genetic algorithm requests
|
||||
/// </summary>
|
||||
public class GeneticService : IGeneticService
|
||||
{
|
||||
private readonly IGeneticRepository _geneticRepository;
|
||||
|
||||
public GeneticService(IGeneticRepository geneticRepository)
|
||||
{
|
||||
_geneticRepository = geneticRepository;
|
||||
}
|
||||
|
||||
public GeneticRequest CreateGeneticRequest(
|
||||
User user,
|
||||
Ticker ticker,
|
||||
Timeframe timeframe,
|
||||
DateTime startDate,
|
||||
DateTime endDate,
|
||||
decimal balance,
|
||||
int populationSize,
|
||||
int generations,
|
||||
double mutationRate,
|
||||
string selectionMethod,
|
||||
int elitismPercentage,
|
||||
double maxTakeProfit,
|
||||
List<IndicatorType> eligibleIndicators)
|
||||
{
|
||||
var id = Guid.NewGuid().ToString(); // Generate unique GUID
|
||||
var geneticRequest = new GeneticRequest(id)
|
||||
{
|
||||
Ticker = ticker,
|
||||
Timeframe = timeframe,
|
||||
StartDate = startDate,
|
||||
EndDate = endDate,
|
||||
Balance = balance,
|
||||
PopulationSize = populationSize,
|
||||
Generations = generations,
|
||||
MutationRate = mutationRate,
|
||||
SelectionMethod = selectionMethod,
|
||||
ElitismPercentage = elitismPercentage,
|
||||
MaxTakeProfit = maxTakeProfit,
|
||||
EligibleIndicators = eligibleIndicators,
|
||||
Status = GeneticRequestStatus.Pending
|
||||
};
|
||||
|
||||
_geneticRepository.InsertGeneticRequestForUser(user, geneticRequest);
|
||||
return geneticRequest;
|
||||
}
|
||||
|
||||
public IEnumerable<GeneticRequest> GetGeneticRequestsByUser(User user)
|
||||
{
|
||||
return _geneticRepository.GetGeneticRequestsByUser(user);
|
||||
}
|
||||
|
||||
public GeneticRequest GetGeneticRequestByIdForUser(User user, string id)
|
||||
{
|
||||
return _geneticRepository.GetGeneticRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public void UpdateGeneticRequest(GeneticRequest geneticRequest)
|
||||
{
|
||||
_geneticRepository.UpdateGeneticRequest(geneticRequest);
|
||||
}
|
||||
|
||||
public void DeleteGeneticRequestByIdForUser(User user, string id)
|
||||
{
|
||||
_geneticRepository.DeleteGeneticRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public IEnumerable<GeneticRequest> GetPendingGeneticRequests()
|
||||
{
|
||||
return _geneticRepository.GetPendingGeneticRequests();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user