570 lines
22 KiB
C#
570 lines
22 KiB
C#
using Managing.Api.Models.Requests;
|
|
using Managing.Application.Abstractions;
|
|
using Managing.Application.Abstractions.Services;
|
|
using Managing.Application.Hubs;
|
|
using Managing.Domain.Backtests;
|
|
using Managing.Domain.Bots;
|
|
using Managing.Domain.MoneyManagements;
|
|
using Managing.Domain.Scenarios;
|
|
using Managing.Domain.Strategies;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace Managing.Api.Controllers;
|
|
|
|
/// <summary>
|
|
/// Controller for managing backtest operations.
|
|
/// Provides endpoints for creating, retrieving, and deleting backtests.
|
|
/// Returns complete backtest configurations for easy bot deployment.
|
|
/// Requires authorization for access.
|
|
/// </summary>
|
|
[ApiController]
|
|
[Authorize]
|
|
[Route("[controller]")]
|
|
[Produces("application/json")]
|
|
public class BacktestController : BaseController
|
|
{
|
|
private readonly IHubContext<BotHub> _hubContext;
|
|
private readonly IBacktester _backtester;
|
|
private readonly IScenarioService _scenarioService;
|
|
private readonly IAccountService _accountService;
|
|
private readonly IMoneyManagementService _moneyManagementService;
|
|
private readonly IGeneticService _geneticService;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="BacktestController"/> class.
|
|
/// </summary>
|
|
/// <param name="hubContext">The SignalR hub context for real-time communication.</param>
|
|
/// <param name="backtester">The service for backtesting strategies.</param>
|
|
/// <param name="scenarioService">The service for managing scenarios.</param>
|
|
/// <param name="accountService">The service for account management.</param>
|
|
/// <param name="moneyManagementService">The service for money management strategies.</param>
|
|
/// <param name="geneticService">The service for genetic algorithm operations.</param>
|
|
public BacktestController(
|
|
IHubContext<BotHub> hubContext,
|
|
IBacktester backtester,
|
|
IScenarioService scenarioService,
|
|
IAccountService accountService,
|
|
IMoneyManagementService moneyManagementService,
|
|
IGeneticService geneticService,
|
|
IUserService userService) : base(userService)
|
|
{
|
|
_hubContext = hubContext;
|
|
_backtester = backtester;
|
|
_scenarioService = scenarioService;
|
|
_accountService = accountService;
|
|
_moneyManagementService = moneyManagementService;
|
|
_geneticService = geneticService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves all backtests for the authenticated user.
|
|
/// Each backtest includes the complete TradingBotConfig for easy bot deployment.
|
|
/// </summary>
|
|
/// <returns>A list of backtests with complete configurations.</returns>
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Backtest>>> Backtests()
|
|
{
|
|
var user = await GetUser();
|
|
return Ok(_backtester.GetBacktestsByUser(user));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a specific backtest by ID for the authenticated user.
|
|
/// This endpoint will also populate the candles for visualization and includes
|
|
/// the complete TradingBotConfig that can be used to start a new bot.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the backtest to retrieve.</param>
|
|
/// <returns>The requested backtest with populated candle data and complete configuration.</returns>
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Backtest>> Backtest(string id)
|
|
{
|
|
var user = await GetUser();
|
|
var backtest = _backtester.GetBacktestByIdForUser(user, id);
|
|
|
|
if (backtest == null)
|
|
{
|
|
return NotFound($"Backtest with ID {id} not found or doesn't belong to the current user.");
|
|
}
|
|
|
|
return Ok(backtest);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a specific backtest by ID for the authenticated user.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the backtest to delete.</param>
|
|
/// <returns>An ActionResult indicating the outcome of the operation.</returns>
|
|
[HttpDelete]
|
|
public async Task<ActionResult> DeleteBacktest(string id)
|
|
{
|
|
var user = await GetUser();
|
|
return Ok(_backtester.DeleteBacktestByUser(user, id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes multiple backtests by their IDs for the authenticated user.
|
|
/// </summary>
|
|
/// <param name="request">The request containing the array of backtest IDs to delete.</param>
|
|
/// <returns>An ActionResult indicating the outcome of the operation.</returns>
|
|
[HttpDelete("multiple")]
|
|
public async Task<ActionResult> DeleteBacktests([FromBody] DeleteBacktestsRequest request)
|
|
{
|
|
var user = await GetUser();
|
|
return Ok(_backtester.DeleteBacktestsByIdsForUser(user, request.BacktestIds));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves all backtests for a specific genetic request ID.
|
|
/// This endpoint is used to view the results of a genetic algorithm optimization.
|
|
/// </summary>
|
|
/// <param name="requestId">The request ID to filter backtests by.</param>
|
|
/// <returns>A list of backtests associated with the specified request ID.</returns>
|
|
[HttpGet]
|
|
[Route("ByRequestId/{requestId}")]
|
|
public async Task<ActionResult<IEnumerable<Backtest>>> GetBacktestsByRequestId(string requestId)
|
|
{
|
|
if (string.IsNullOrEmpty(requestId))
|
|
{
|
|
return BadRequest("Request ID is required");
|
|
}
|
|
|
|
var backtests = _backtester.GetBacktestsByRequestId(requestId);
|
|
return Ok(backtests);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves paginated backtests for a specific genetic request ID.
|
|
/// This endpoint is used to view the results of a genetic algorithm optimization with pagination support.
|
|
/// </summary>
|
|
/// <param name="requestId">The request ID to filter backtests by.</param>
|
|
/// <param name="page">Page number (defaults to 1)</param>
|
|
/// <param name="pageSize">Number of items per page (defaults to 50, max 100)</param>
|
|
/// <param name="sortBy">Field to sort by (defaults to "score")</param>
|
|
/// <param name="sortOrder">Sort order - "asc" or "desc" (defaults to "desc")</param>
|
|
/// <returns>A paginated list of backtests associated with the specified request ID.</returns>
|
|
[HttpGet]
|
|
[Route("ByRequestId/{requestId}/Paginated")]
|
|
public async Task<ActionResult<PaginatedBacktestsResponse>> GetBacktestsByRequestIdPaginated(
|
|
string requestId,
|
|
int page = 1,
|
|
int pageSize = 50,
|
|
string sortBy = "score",
|
|
string sortOrder = "desc")
|
|
{
|
|
if (string.IsNullOrEmpty(requestId))
|
|
{
|
|
return BadRequest("Request ID is required");
|
|
}
|
|
|
|
if (page < 1)
|
|
{
|
|
return BadRequest("Page must be greater than 0");
|
|
}
|
|
|
|
if (pageSize < 1 || pageSize > 100)
|
|
{
|
|
return BadRequest("Page size must be between 1 and 100");
|
|
}
|
|
|
|
if (sortOrder != "asc" && sortOrder != "desc")
|
|
{
|
|
return BadRequest("Sort order must be 'asc' or 'desc'");
|
|
}
|
|
|
|
var (backtests, totalCount) = _backtester.GetBacktestsByRequestIdPaginated(requestId, page, pageSize, sortBy, sortOrder);
|
|
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var response = new PaginatedBacktestsResponse
|
|
{
|
|
Backtests = backtests.Select(b => new LightBacktestResponse
|
|
{
|
|
Id = b.Id,
|
|
Config = b.Config,
|
|
FinalPnl = b.FinalPnl,
|
|
WinRate = b.WinRate,
|
|
GrowthPercentage = b.GrowthPercentage,
|
|
HodlPercentage = b.HodlPercentage,
|
|
StartDate = b.StartDate,
|
|
EndDate = b.EndDate,
|
|
MaxDrawdown = b.MaxDrawdown,
|
|
Fees = b.Fees,
|
|
SharpeRatio = b.SharpeRatio,
|
|
Score = b.Score,
|
|
ScoreMessage = b.ScoreMessage
|
|
}),
|
|
TotalCount = totalCount,
|
|
CurrentPage = page,
|
|
PageSize = pageSize,
|
|
TotalPages = totalPages,
|
|
HasNextPage = page < totalPages,
|
|
HasPreviousPage = page > 1
|
|
};
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves paginated backtests for the authenticated user.
|
|
/// </summary>
|
|
/// <param name="page">Page number (defaults to 1)</param>
|
|
/// <param name="pageSize">Number of items per page (defaults to 50, max 100)</param>
|
|
/// <param name="sortBy">Field to sort by (defaults to "score")</param>
|
|
/// <param name="sortOrder">Sort order - "asc" or "desc" (defaults to "desc")</param>
|
|
/// <returns>A paginated list of backtests for the user.</returns>
|
|
[HttpGet]
|
|
[Route("Paginated")]
|
|
public async Task<ActionResult<PaginatedBacktestsResponse>> GetBacktestsPaginated(
|
|
int page = 1,
|
|
int pageSize = 50,
|
|
string sortBy = "score",
|
|
string sortOrder = "desc")
|
|
{
|
|
var user = await GetUser();
|
|
|
|
if (page < 1)
|
|
{
|
|
return BadRequest("Page must be greater than 0");
|
|
}
|
|
|
|
if (pageSize < 1 || pageSize > 100)
|
|
{
|
|
return BadRequest("Page size must be between 1 and 100");
|
|
}
|
|
|
|
if (sortOrder != "asc" && sortOrder != "desc")
|
|
{
|
|
return BadRequest("Sort order must be 'asc' or 'desc'");
|
|
}
|
|
|
|
var (backtests, totalCount) = _backtester.GetBacktestsByUserPaginated(user, page, pageSize, sortBy, sortOrder);
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var response = new PaginatedBacktestsResponse
|
|
{
|
|
Backtests = backtests.Select(b => new LightBacktestResponse
|
|
{
|
|
Id = b.Id,
|
|
Config = b.Config,
|
|
FinalPnl = b.FinalPnl,
|
|
WinRate = b.WinRate,
|
|
GrowthPercentage = b.GrowthPercentage,
|
|
HodlPercentage = b.HodlPercentage,
|
|
StartDate = b.StartDate,
|
|
EndDate = b.EndDate,
|
|
MaxDrawdown = b.MaxDrawdown,
|
|
Fees = b.Fees,
|
|
SharpeRatio = b.SharpeRatio,
|
|
Score = b.Score,
|
|
ScoreMessage = b.ScoreMessage
|
|
}),
|
|
TotalCount = totalCount,
|
|
CurrentPage = page,
|
|
PageSize = pageSize,
|
|
TotalPages = totalPages,
|
|
HasNextPage = page < totalPages,
|
|
HasPreviousPage = page > 1
|
|
};
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a backtest with the specified configuration.
|
|
/// The returned backtest includes a complete TradingBotConfig that preserves all
|
|
/// settings including nullable MaxPositionTimeHours for easy bot deployment.
|
|
/// </summary>
|
|
/// <param name="request">The backtest request containing configuration and parameters.</param>
|
|
/// <returns>The result of the backtest with complete configuration.</returns>
|
|
[HttpPost]
|
|
[Route("Run")]
|
|
public async Task<ActionResult<Backtest>> Run([FromBody] RunBacktestRequest request)
|
|
{
|
|
if (request?.Config == null)
|
|
{
|
|
return BadRequest("Backtest configuration is required");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(request.Config.AccountName))
|
|
{
|
|
return BadRequest("Account name is required");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(request.Config.ScenarioName) && request.Config.Scenario == null)
|
|
{
|
|
return BadRequest("Either scenario name or scenario object is required");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(request.Config.MoneyManagementName) && request.Config.MoneyManagement == null)
|
|
{
|
|
return BadRequest("Either money management name or money management object is required");
|
|
}
|
|
|
|
try
|
|
{
|
|
Backtest backtestResult = null;
|
|
var account = await _accountService.GetAccount(request.Config.AccountName, true, false);
|
|
var user = await GetUser();
|
|
|
|
// Get money management
|
|
MoneyManagement moneyManagement;
|
|
if (!string.IsNullOrEmpty(request.Config.MoneyManagementName))
|
|
{
|
|
moneyManagement =
|
|
await _moneyManagementService.GetMoneyMangement(user, request.Config.MoneyManagementName);
|
|
if (moneyManagement == null)
|
|
return BadRequest("Money management not found");
|
|
}
|
|
else
|
|
{
|
|
moneyManagement = Map(request.Config.MoneyManagement);
|
|
moneyManagement?.FormatPercentage();
|
|
}
|
|
|
|
// Handle scenario - either from ScenarioRequest or ScenarioName
|
|
Scenario scenario = null;
|
|
if (request.Config.Scenario != null)
|
|
{
|
|
// Convert ScenarioRequest to Scenario domain object
|
|
scenario = new Scenario(request.Config.Scenario.Name, request.Config.Scenario.LoopbackPeriod)
|
|
{
|
|
User = user
|
|
};
|
|
|
|
// Convert IndicatorRequest objects to Indicator domain objects
|
|
foreach (var indicatorRequest in request.Config.Scenario.Indicators)
|
|
{
|
|
var indicator = new Indicator(indicatorRequest.Name, indicatorRequest.Type)
|
|
{
|
|
SignalType = indicatorRequest.SignalType,
|
|
MinimumHistory = indicatorRequest.MinimumHistory,
|
|
Period = indicatorRequest.Period,
|
|
FastPeriods = indicatorRequest.FastPeriods,
|
|
SlowPeriods = indicatorRequest.SlowPeriods,
|
|
SignalPeriods = indicatorRequest.SignalPeriods,
|
|
Multiplier = indicatorRequest.Multiplier,
|
|
SmoothPeriods = indicatorRequest.SmoothPeriods,
|
|
StochPeriods = indicatorRequest.StochPeriods,
|
|
CyclePeriods = indicatorRequest.CyclePeriods,
|
|
User = user
|
|
};
|
|
scenario.AddIndicator(indicator);
|
|
}
|
|
}
|
|
|
|
// Convert TradingBotConfigRequest to TradingBotConfig for backtest
|
|
var backtestConfig = new TradingBotConfig
|
|
{
|
|
AccountName = request.Config.AccountName,
|
|
MoneyManagement = moneyManagement,
|
|
Ticker = request.Config.Ticker,
|
|
ScenarioName = request.Config.ScenarioName,
|
|
Scenario = scenario, // Use the converted scenario object
|
|
Timeframe = request.Config.Timeframe,
|
|
IsForWatchingOnly = request.Config.IsForWatchingOnly,
|
|
BotTradingBalance = request.Config.BotTradingBalance,
|
|
IsForBacktest = true,
|
|
CooldownPeriod = request.Config.CooldownPeriod,
|
|
MaxLossStreak = request.Config.MaxLossStreak,
|
|
MaxPositionTimeHours = request.Config.MaxPositionTimeHours,
|
|
FlipOnlyWhenInProfit = request.Config.FlipOnlyWhenInProfit,
|
|
FlipPosition = request.Config.FlipPosition, // Computed based on BotType
|
|
Name = request.Config.Name ??
|
|
$"Backtest-{request.Config.ScenarioName ?? request.Config.Scenario?.Name ?? "Custom"}-{DateTime.UtcNow:yyyyMMdd-HHmmss}",
|
|
CloseEarlyWhenProfitable = request.Config.CloseEarlyWhenProfitable,
|
|
UseSynthApi = request.Config.UseSynthApi,
|
|
UseForPositionSizing = request.Config.UseForPositionSizing,
|
|
UseForSignalFiltering = request.Config.UseForSignalFiltering,
|
|
UseForDynamicStopLoss = request.Config.UseForDynamicStopLoss
|
|
};
|
|
|
|
backtestResult = await _backtester.RunTradingBotBacktest(
|
|
backtestConfig,
|
|
request.StartDate,
|
|
request.EndDate,
|
|
user,
|
|
request.Save,
|
|
request.WithCandles,
|
|
null); // No requestId for regular backtests
|
|
|
|
await NotifyBacktesingSubscriberAsync(backtestResult);
|
|
|
|
return Ok(backtestResult);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, $"Error running backtest: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a genetic algorithm optimization with the specified configuration.
|
|
/// This endpoint saves the genetic request to the database and returns the request ID.
|
|
/// The actual genetic algorithm execution will be handled by a background service.
|
|
/// </summary>
|
|
/// <param name="request">The genetic algorithm request containing configuration and parameters.</param>
|
|
/// <returns>The genetic request with ID for tracking progress.</returns>
|
|
[HttpPost]
|
|
[Route("Genetic")]
|
|
public async Task<ActionResult<GeneticRequest>> RunGenetic([FromBody] RunGeneticRequest request)
|
|
{
|
|
if (request == null)
|
|
{
|
|
return BadRequest("Genetic request is required");
|
|
}
|
|
|
|
if (request.EligibleIndicators == null || !request.EligibleIndicators.Any())
|
|
{
|
|
return BadRequest("At least one eligible indicator is required");
|
|
}
|
|
|
|
if (request.StartDate >= request.EndDate)
|
|
{
|
|
return BadRequest("Start date must be before end date");
|
|
}
|
|
|
|
if (request.PopulationSize <= 0 || request.Generations <= 0)
|
|
{
|
|
return BadRequest("Population size and generations must be greater than 0");
|
|
}
|
|
|
|
if (request.MutationRate < 0 || request.MutationRate > 1)
|
|
{
|
|
return BadRequest("Mutation rate must be between 0 and 1");
|
|
}
|
|
|
|
try
|
|
{
|
|
var user = await GetUser();
|
|
|
|
// Create genetic request using the GeneticService directly
|
|
var geneticRequest = _geneticService.CreateGeneticRequest(
|
|
user,
|
|
request.Ticker,
|
|
request.Timeframe,
|
|
request.StartDate,
|
|
request.EndDate,
|
|
request.Balance,
|
|
request.PopulationSize,
|
|
request.Generations,
|
|
request.MutationRate,
|
|
request.SelectionMethod,
|
|
request.CrossoverMethod,
|
|
request.MutationMethod,
|
|
request.ElitismPercentage,
|
|
request.MaxTakeProfit,
|
|
request.EligibleIndicators);
|
|
|
|
return Ok(geneticRequest);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, $"Error creating genetic request: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves all genetic requests for the authenticated user.
|
|
/// </summary>
|
|
/// <returns>A list of genetic requests with their current status.</returns>
|
|
[HttpGet]
|
|
[Route("Genetic")]
|
|
public async Task<ActionResult<IEnumerable<GeneticRequest>>> GetGeneticRequests()
|
|
{
|
|
var user = await GetUser();
|
|
var geneticRequests = _geneticService.GetGeneticRequestsByUser(user);
|
|
return Ok(geneticRequests);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a specific genetic request by ID for the authenticated user.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the genetic request to retrieve.</param>
|
|
/// <returns>The requested genetic request with current status and results.</returns>
|
|
[HttpGet]
|
|
[Route("Genetic/{id}")]
|
|
public async Task<ActionResult<GeneticRequest>> GetGeneticRequest(string id)
|
|
{
|
|
var user = await GetUser();
|
|
var geneticRequest = _geneticService.GetGeneticRequestByIdForUser(user, id);
|
|
|
|
if (geneticRequest == null)
|
|
{
|
|
return NotFound($"Genetic request with ID {id} not found or doesn't belong to the current user.");
|
|
}
|
|
|
|
return Ok(geneticRequest);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a specific genetic request by ID for the authenticated user.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the genetic request to delete.</param>
|
|
/// <returns>An ActionResult indicating the outcome of the operation.</returns>
|
|
[HttpDelete]
|
|
[Route("Genetic/{id}")]
|
|
public async Task<ActionResult> DeleteGeneticRequest(string id)
|
|
{
|
|
var user = await GetUser();
|
|
_geneticService.DeleteGeneticRequestByIdForUser(user, id);
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Notifies subscribers about the backtesting results via SignalR.
|
|
/// </summary>
|
|
/// <param name="backtesting">The backtest result to notify subscribers about.</param>
|
|
private async Task NotifyBacktesingSubscriberAsync(Backtest backtesting)
|
|
{
|
|
if (backtesting != null)
|
|
{
|
|
await _hubContext.Clients.All.SendAsync("BacktestsSubscription", backtesting);
|
|
}
|
|
}
|
|
|
|
public MoneyManagement Map(MoneyManagementRequest moneyManagementRequest)
|
|
{
|
|
return new MoneyManagement
|
|
{
|
|
Name = moneyManagementRequest.Name,
|
|
StopLoss = moneyManagementRequest.StopLoss,
|
|
TakeProfit = moneyManagementRequest.TakeProfit,
|
|
Leverage = moneyManagementRequest.Leverage,
|
|
Timeframe = moneyManagementRequest.Timeframe
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request model for running a backtest
|
|
/// </summary>
|
|
public class RunBacktestRequest
|
|
{
|
|
/// <summary>
|
|
/// The trading bot configuration request to use for the backtest
|
|
/// </summary>
|
|
public TradingBotConfigRequest Config { get; set; }
|
|
|
|
/// <summary>
|
|
/// The start date for the backtest
|
|
/// </summary>
|
|
public DateTime StartDate { get; set; }
|
|
|
|
/// <summary>
|
|
/// The end date for the backtest
|
|
/// </summary>
|
|
public DateTime EndDate { get; set; }
|
|
|
|
/// <summary>
|
|
/// Whether to save the backtest results
|
|
/// </summary>
|
|
public bool Save { get; set; } = false;
|
|
|
|
/// <summary>
|
|
/// Whether to include candles and indicators values in the response.
|
|
/// Set to false to reduce response size dramatically.
|
|
/// </summary>
|
|
public bool WithCandles { get; set; } = false;
|
|
} |