Add bundle backtest
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using Managing.Api.Models.Requests;
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Domain.Backtests;
|
||||
@@ -12,6 +11,7 @@ using Managing.Domain.Strategies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using MoneyManagementRequest = Managing.Domain.Backtests.MoneyManagementRequest;
|
||||
|
||||
namespace Managing.Api.Controllers;
|
||||
|
||||
@@ -33,7 +33,6 @@ public class BacktestController : BaseController
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly IMoneyManagementService _moneyManagementService;
|
||||
private readonly IGeneticService _geneticService;
|
||||
private readonly IBacktestRepository _backtestRepository;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BacktestController"/> class.
|
||||
@@ -52,7 +51,6 @@ public class BacktestController : BaseController
|
||||
IAccountService accountService,
|
||||
IMoneyManagementService moneyManagementService,
|
||||
IGeneticService geneticService,
|
||||
IBacktestRepository backtestRepository,
|
||||
IUserService userService) : base(userService)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
@@ -61,7 +59,6 @@ public class BacktestController : BaseController
|
||||
_accountService = accountService;
|
||||
_moneyManagementService = moneyManagementService;
|
||||
_geneticService = geneticService;
|
||||
_backtestRepository = backtestRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -153,8 +150,8 @@ public class BacktestController : BaseController
|
||||
[HttpGet]
|
||||
[Route("ByRequestId/{requestId}/Paginated")]
|
||||
public async Task<ActionResult<PaginatedBacktestsResponse>> GetBacktestsByRequestIdPaginated(
|
||||
string requestId,
|
||||
int page = 1,
|
||||
string requestId,
|
||||
int page = 1,
|
||||
int pageSize = 50,
|
||||
string sortBy = "score",
|
||||
string sortOrder = "desc")
|
||||
@@ -179,10 +176,11 @@ public class BacktestController : BaseController
|
||||
return BadRequest("Sort order must be 'asc' or 'desc'");
|
||||
}
|
||||
|
||||
var (backtests, totalCount) = _backtester.GetBacktestsByRequestIdPaginated(requestId, page, pageSize, sortBy, sortOrder);
|
||||
|
||||
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
|
||||
@@ -410,10 +408,12 @@ public class BacktestController : BaseController
|
||||
/// This endpoint creates a request that will be processed by a background worker.
|
||||
/// </summary>
|
||||
/// <param name="requests">The list of backtest requests to execute.</param>
|
||||
/// <param name="name">Display name for the bundle (required).</param>
|
||||
/// <returns>The bundle backtest request with ID for tracking progress.</returns>
|
||||
[HttpPost]
|
||||
[Route("Bundle")]
|
||||
public async Task<ActionResult<BundleBacktestRequest>> RunBundle([FromBody] List<RunBacktestRequest> requests)
|
||||
public async Task<ActionResult<BundleBacktestRequest>> RunBundle([FromBody] List<RunBacktestRequest> requests,
|
||||
[FromQuery] string name)
|
||||
{
|
||||
if (requests == null || !requests.Any())
|
||||
{
|
||||
@@ -425,10 +425,15 @@ public class BacktestController : BaseController
|
||||
return BadRequest("Maximum of 10 backtests allowed per bundle request");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return BadRequest("Bundle name is required");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var user = await GetUser();
|
||||
|
||||
|
||||
// Validate all requests before creating the bundle
|
||||
foreach (var request in requests)
|
||||
{
|
||||
@@ -449,7 +454,8 @@ public class BacktestController : BaseController
|
||||
|
||||
if (string.IsNullOrEmpty(request.Config.MoneyManagementName) && request.Config.MoneyManagement == null)
|
||||
{
|
||||
return BadRequest("Invalid request: Either money management name or money management object is required");
|
||||
return BadRequest(
|
||||
"Invalid request: Either money management name or money management object is required");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,10 +467,11 @@ public class BacktestController : BaseController
|
||||
TotalBacktests = requests.Count,
|
||||
CompletedBacktests = 0,
|
||||
FailedBacktests = 0,
|
||||
Status = BundleBacktestRequestStatus.Pending
|
||||
Status = BundleBacktestRequestStatus.Pending,
|
||||
Name = name
|
||||
};
|
||||
|
||||
_backtestRepository.InsertBundleBacktestRequestForUser(user, bundleRequest);
|
||||
_backtester.InsertBundleBacktestRequestForUser(user, bundleRequest);
|
||||
|
||||
return Ok(bundleRequest);
|
||||
}
|
||||
@@ -483,7 +490,7 @@ public class BacktestController : BaseController
|
||||
public async Task<ActionResult<IEnumerable<BundleBacktestRequest>>> GetBundleBacktestRequests()
|
||||
{
|
||||
var user = await GetUser();
|
||||
var bundleRequests = _backtestRepository.GetBundleBacktestRequestsByUser(user);
|
||||
var bundleRequests = _backtester.GetBundleBacktestRequestsByUser(user);
|
||||
return Ok(bundleRequests);
|
||||
}
|
||||
|
||||
@@ -497,7 +504,7 @@ public class BacktestController : BaseController
|
||||
public async Task<ActionResult<BundleBacktestRequest>> GetBundleBacktestRequest(string id)
|
||||
{
|
||||
var user = await GetUser();
|
||||
var bundleRequest = _backtestRepository.GetBundleBacktestRequestByIdForUser(user, id);
|
||||
var bundleRequest = _backtester.GetBundleBacktestRequestByIdForUser(user, id);
|
||||
|
||||
if (bundleRequest == null)
|
||||
{
|
||||
@@ -518,16 +525,17 @@ public class BacktestController : BaseController
|
||||
public async Task<ActionResult> DeleteBundleBacktestRequest(string id)
|
||||
{
|
||||
var user = await GetUser();
|
||||
|
||||
|
||||
// First, delete the bundle request
|
||||
_backtestRepository.DeleteBundleBacktestRequestByIdForUser(user, id);
|
||||
|
||||
_backtester.DeleteBundleBacktestRequestByIdForUser(user, id);
|
||||
|
||||
// Then, delete all related backtests
|
||||
var backtestsDeleted = _backtester.DeleteBacktestsByRequestId(id);
|
||||
|
||||
return Ok(new {
|
||||
BundleRequestDeleted = true,
|
||||
RelatedBacktestsDeleted = backtestsDeleted
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
BundleRequestDeleted = true,
|
||||
RelatedBacktestsDeleted = backtestsDeleted
|
||||
});
|
||||
}
|
||||
|
||||
@@ -641,21 +649,21 @@ public class BacktestController : BaseController
|
||||
public async Task<ActionResult> DeleteGeneticRequest(string id)
|
||||
{
|
||||
var user = await GetUser();
|
||||
|
||||
|
||||
// First, delete the genetic request
|
||||
_geneticService.DeleteGeneticRequestByIdForUser(user, id);
|
||||
|
||||
|
||||
// Then, delete all related backtests
|
||||
var backtestsDeleted = _backtester.DeleteBacktestsByRequestId(id);
|
||||
|
||||
return Ok(new {
|
||||
GeneticRequestDeleted = true,
|
||||
RelatedBacktestsDeleted = backtestsDeleted
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
GeneticRequestDeleted = true,
|
||||
RelatedBacktestsDeleted = backtestsDeleted
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Notifies subscribers about the backtesting results via SignalR.
|
||||
/// </summary>
|
||||
|
||||
@@ -5,6 +5,7 @@ using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.ManageBot.Commands;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Scenarios;
|
||||
|
||||
@@ -4,6 +4,7 @@ using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.ManageBot.Commands;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Scenarios;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Managing.Domain.Backtests;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
@@ -7,8 +8,6 @@ namespace Managing.Api.Models.Requests;
|
||||
/// </summary>
|
||||
public class GetCandlesWithIndicatorsRequest
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The ticker symbol.
|
||||
/// </summary>
|
||||
@@ -33,4 +32,4 @@ public class GetCandlesWithIndicatorsRequest
|
||||
/// Optional scenario for calculating indicators.
|
||||
/// </summary>
|
||||
public ScenarioRequest Scenario { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Managing.Domain.Bots;
|
||||
|
||||
namespace Managing.Api.Models.Requests
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
|
||||
@@ -62,6 +62,14 @@ namespace Managing.Application.Abstractions.Services
|
||||
bool DeleteBacktestsByRequestId(string requestId);
|
||||
(IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByUserPaginated(User user, int page, int pageSize, string sortBy = "score", string sortOrder = "desc");
|
||||
|
||||
// Bundle backtest methods
|
||||
void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest);
|
||||
IEnumerable<BundleBacktestRequest> GetBundleBacktestRequestsByUser(User user);
|
||||
BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, string id);
|
||||
void UpdateBundleBacktestRequest(BundleBacktestRequest bundleRequest);
|
||||
void DeleteBundleBacktestRequestByIdForUser(User user, string id);
|
||||
IEnumerable<BundleBacktestRequest> GetPendingBundleBacktestRequests();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.Text.Json;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Strategies;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
@@ -13,12 +16,11 @@ namespace Managing.Application.Workers;
|
||||
/// </summary>
|
||||
public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
{
|
||||
private readonly IBacktestRepository _backtestRepository;
|
||||
// Removed direct repository usage for bundle requests
|
||||
private readonly IBacktester _backtester;
|
||||
private static readonly WorkerType _workerType = WorkerType.BundleBacktest;
|
||||
|
||||
public BundleBacktestWorker(
|
||||
IBacktestRepository backtestRepository,
|
||||
IBacktester backtester,
|
||||
ILogger<BundleBacktestWorker> logger,
|
||||
IWorkerService workerService) : base(
|
||||
@@ -27,7 +29,6 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
TimeSpan.FromMinutes(1),
|
||||
workerService)
|
||||
{
|
||||
_backtestRepository = backtestRepository;
|
||||
_backtester = backtester;
|
||||
}
|
||||
|
||||
@@ -36,7 +37,7 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
try
|
||||
{
|
||||
// Get pending bundle backtest requests
|
||||
var pendingRequests = _backtestRepository.GetPendingBundleBacktestRequests();
|
||||
var pendingRequests = _backtester.GetPendingBundleBacktestRequests();
|
||||
|
||||
foreach (var bundleRequest in pendingRequests)
|
||||
{
|
||||
@@ -61,10 +62,12 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
|
||||
// Update status to running
|
||||
bundleRequest.Status = BundleBacktestRequestStatus.Running;
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
|
||||
// Deserialize the backtest requests as dynamic objects
|
||||
var backtestRequests = JsonSerializer.Deserialize<List<JsonElement>>(bundleRequest.BacktestRequestsJson);
|
||||
// Deserialize the backtest requests as strongly-typed objects
|
||||
var backtestRequests =
|
||||
JsonSerializer.Deserialize<List<RunBacktestRequest>>(
|
||||
bundleRequest.BacktestRequestsJson);
|
||||
if (backtestRequests == null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to deserialize backtest requests");
|
||||
@@ -78,28 +81,27 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
|
||||
try
|
||||
{
|
||||
var requestElement = backtestRequests[i];
|
||||
|
||||
var runBacktestRequest = backtestRequests[i];
|
||||
// Update current backtest being processed
|
||||
bundleRequest.CurrentBacktest = $"Backtest {i + 1} of {backtestRequests.Count}";
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
|
||||
// Convert JSON element to domain model and run backtest
|
||||
await RunSingleBacktest(requestElement, bundleRequest.RequestId, cancellationToken);
|
||||
// Run the backtest directly with the strongly-typed request
|
||||
await RunSingleBacktest(runBacktestRequest, bundleRequest, i, cancellationToken);
|
||||
|
||||
// Update progress
|
||||
bundleRequest.CompletedBacktests++;
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
|
||||
_logger.LogInformation("Completed backtest {Index} for bundle request {RequestId}",
|
||||
_logger.LogInformation("Completed backtest {Index} for bundle request {RequestId}",
|
||||
i + 1, bundleRequest.RequestId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error processing backtest {Index} for bundle request {RequestId}",
|
||||
_logger.LogError(ex, "Error processing backtest {Index} for bundle request {RequestId}",
|
||||
i + 1, bundleRequest.RequestId);
|
||||
bundleRequest.FailedBacktests++;
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,36 +123,120 @@ public class BundleBacktestWorker : BaseWorker<BundleBacktestWorker>
|
||||
|
||||
bundleRequest.CompletedAt = DateTime.UtcNow;
|
||||
bundleRequest.CurrentBacktest = null;
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
|
||||
_logger.LogInformation("Completed processing bundle backtest request {RequestId} with status {Status}",
|
||||
_logger.LogInformation("Completed processing bundle backtest request {RequestId} with status {Status}",
|
||||
bundleRequest.RequestId, bundleRequest.Status);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error processing bundle backtest request {RequestId}", bundleRequest.RequestId);
|
||||
|
||||
|
||||
bundleRequest.Status = BundleBacktestRequestStatus.Failed;
|
||||
bundleRequest.ErrorMessage = ex.Message;
|
||||
bundleRequest.CompletedAt = DateTime.UtcNow;
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
_backtester.UpdateBundleBacktestRequest(bundleRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunSingleBacktest(JsonElement requestElement, string bundleRequestId, CancellationToken cancellationToken)
|
||||
// Change RunSingleBacktest to accept RunBacktestRequest directly
|
||||
private async Task RunSingleBacktest(RunBacktestRequest runBacktestRequest, BundleBacktestRequest bundleRequest,
|
||||
int index, CancellationToken cancellationToken)
|
||||
{
|
||||
// For now, we'll use a simplified approach that simulates backtest execution
|
||||
// In a real implementation, you would parse the JSON and convert to domain models
|
||||
|
||||
// Simulate backtest processing time
|
||||
await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
|
||||
|
||||
_logger.LogInformation("Processed backtest for bundle request {RequestId}", bundleRequestId);
|
||||
|
||||
// TODO: Implement actual backtest execution by:
|
||||
// 1. Parsing the JSON request element
|
||||
// 2. Converting to TradingBotConfig domain model
|
||||
// 3. Calling _backtester.RunTradingBotBacktest with proper parameters
|
||||
// 4. Handling the results and saving to database if needed
|
||||
if (runBacktestRequest == null || runBacktestRequest.Config == null)
|
||||
{
|
||||
_logger.LogError("Invalid RunBacktestRequest in bundle (null config)");
|
||||
return;
|
||||
}
|
||||
|
||||
// Map MoneyManagement
|
||||
MoneyManagement moneyManagement = null;
|
||||
if (!string.IsNullOrEmpty(runBacktestRequest.Config.MoneyManagementName))
|
||||
{
|
||||
// In worker context, we cannot resolve by name (no user/db), so skip or set null
|
||||
// Optionally, log a warning
|
||||
_logger.LogWarning("MoneyManagementName provided but cannot resolve in worker context: {Name}",
|
||||
(string)runBacktestRequest.Config.MoneyManagementName);
|
||||
}
|
||||
else if (runBacktestRequest.Config.MoneyManagement != null)
|
||||
{
|
||||
var mmReq = runBacktestRequest.Config.MoneyManagement;
|
||||
moneyManagement = new MoneyManagement
|
||||
{
|
||||
Name = mmReq.Name,
|
||||
Timeframe = mmReq.Timeframe,
|
||||
StopLoss = mmReq.StopLoss,
|
||||
TakeProfit = mmReq.TakeProfit,
|
||||
Leverage = mmReq.Leverage
|
||||
};
|
||||
moneyManagement.FormatPercentage();
|
||||
}
|
||||
|
||||
// Map Scenario
|
||||
Scenario scenario = null;
|
||||
if (runBacktestRequest.Config.Scenario != null)
|
||||
{
|
||||
var sReq = runBacktestRequest.Config.Scenario;
|
||||
scenario = new Scenario(sReq.Name, sReq.LoopbackPeriod)
|
||||
{
|
||||
User = null // No user context in worker
|
||||
};
|
||||
foreach (var indicatorRequest in sReq.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 = null // No user context in worker
|
||||
};
|
||||
scenario.AddIndicator(indicator);
|
||||
}
|
||||
}
|
||||
|
||||
// Map TradingBotConfig
|
||||
var backtestConfig = new TradingBotConfig
|
||||
{
|
||||
AccountName = runBacktestRequest.Config.AccountName,
|
||||
MoneyManagement = moneyManagement,
|
||||
Ticker = runBacktestRequest.Config.Ticker,
|
||||
ScenarioName = runBacktestRequest.Config.ScenarioName,
|
||||
Scenario = scenario,
|
||||
Timeframe = runBacktestRequest.Config.Timeframe,
|
||||
IsForWatchingOnly = runBacktestRequest.Config.IsForWatchingOnly,
|
||||
BotTradingBalance = runBacktestRequest.Config.BotTradingBalance,
|
||||
IsForBacktest = true,
|
||||
CooldownPeriod = runBacktestRequest.Config.CooldownPeriod,
|
||||
MaxLossStreak = runBacktestRequest.Config.MaxLossStreak,
|
||||
MaxPositionTimeHours = runBacktestRequest.Config.MaxPositionTimeHours,
|
||||
FlipOnlyWhenInProfit = runBacktestRequest.Config.FlipOnlyWhenInProfit,
|
||||
FlipPosition = runBacktestRequest.Config.FlipPosition,
|
||||
Name = $"{bundleRequest.Name} #{index + 1}",
|
||||
CloseEarlyWhenProfitable = runBacktestRequest.Config.CloseEarlyWhenProfitable,
|
||||
UseSynthApi = runBacktestRequest.Config.UseSynthApi,
|
||||
UseForPositionSizing = runBacktestRequest.Config.UseForPositionSizing,
|
||||
UseForSignalFiltering = runBacktestRequest.Config.UseForSignalFiltering,
|
||||
UseForDynamicStopLoss = runBacktestRequest.Config.UseForDynamicStopLoss
|
||||
};
|
||||
|
||||
// Run the backtest (no user context)
|
||||
var result = await _backtester.RunTradingBotBacktest(
|
||||
backtestConfig,
|
||||
runBacktestRequest.StartDate,
|
||||
runBacktestRequest.EndDate,
|
||||
null, // No user context in worker
|
||||
runBacktestRequest.Save,
|
||||
runBacktestRequest.WithCandles,
|
||||
bundleRequest.RequestId // Use bundleRequestId as requestId for traceability
|
||||
);
|
||||
|
||||
_logger.LogInformation("Processed backtest for bundle request {RequestId}", bundleRequest.RequestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -573,5 +573,36 @@ namespace Managing.Application.Backtesting
|
||||
_backtestRepository.GetBacktestsByUserPaginated(user, page, pageSize, sortBy, sortOrder);
|
||||
return (backtests, totalCount);
|
||||
}
|
||||
|
||||
// Bundle backtest methods
|
||||
public void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
_backtestRepository.InsertBundleBacktestRequestForUser(user, bundleRequest);
|
||||
}
|
||||
|
||||
public IEnumerable<BundleBacktestRequest> GetBundleBacktestRequestsByUser(User user)
|
||||
{
|
||||
return _backtestRepository.GetBundleBacktestRequestsByUser(user);
|
||||
}
|
||||
|
||||
public BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, string id)
|
||||
{
|
||||
return _backtestRepository.GetBundleBacktestRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public void UpdateBundleBacktestRequest(BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
_backtestRepository.UpdateBundleBacktestRequest(bundleRequest);
|
||||
}
|
||||
|
||||
public void DeleteBundleBacktestRequestByIdForUser(User user, string id)
|
||||
{
|
||||
_backtestRepository.DeleteBundleBacktestRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public IEnumerable<BundleBacktestRequest> GetPendingBundleBacktestRequests()
|
||||
{
|
||||
return _backtestRepository.GetPendingBundleBacktestRequests();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,12 @@ public class BundleBacktestRequest
|
||||
[Required]
|
||||
public BundleBacktestRequestStatus Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Display name for the bundle backtest request
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The list of backtest requests to execute (serialized as JSON)
|
||||
/// </summary>
|
||||
@@ -143,4 +149,4 @@ public enum BundleBacktestRequestStatus
|
||||
/// Request was cancelled
|
||||
/// </summary>
|
||||
Cancelled
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
namespace Managing.Domain.Backtests;
|
||||
|
||||
/// <summary>
|
||||
/// Request model for indicator configuration without user information
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Managing.Common;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
namespace Managing.Domain.Backtests;
|
||||
|
||||
public class MoneyManagementRequest
|
||||
{
|
||||
@@ -1,6 +1,7 @@
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
namespace Managing.Domain.Backtests;
|
||||
|
||||
/// <summary>
|
||||
/// Request model for running a backtest
|
||||
@@ -37,6 +38,12 @@ public class RunBacktestRequest
|
||||
/// </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;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the money management to use (optional if MoneyManagement is provided)
|
||||
/// </summary>
|
||||
@@ -46,4 +53,4 @@ public class RunBacktestRequest
|
||||
/// The money management details (optional if MoneyManagementName is provided)
|
||||
/// </summary>
|
||||
public MoneyManagement? MoneyManagement { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
namespace Managing.Domain.Backtests;
|
||||
|
||||
/// <summary>
|
||||
/// Request model for scenario configuration without user information
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Managing.Domain.Backtests;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Api.Models.Requests;
|
||||
namespace Managing.Domain.Bots;
|
||||
|
||||
/// <summary>
|
||||
/// Simplified trading bot configuration request with only primary properties
|
||||
@@ -314,7 +314,7 @@ public class BacktestRepository : IBacktestRepository
|
||||
public void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
bundleRequest.User = user;
|
||||
var dto = MapToDto(bundleRequest);
|
||||
var dto = MongoMappers.Map(bundleRequest);
|
||||
_bundleBacktestRepository.InsertOne(dto);
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ public class BacktestRepository : IBacktestRepository
|
||||
.Where(b => b.User.Name == user.Name)
|
||||
.ToList();
|
||||
|
||||
return bundleRequests.Select(MapToDomain);
|
||||
return bundleRequests.Select(MongoMappers.Map);
|
||||
}
|
||||
|
||||
public BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, string id)
|
||||
@@ -333,7 +333,7 @@ public class BacktestRepository : IBacktestRepository
|
||||
|
||||
if (bundleRequest != null && bundleRequest.User.Name == user.Name)
|
||||
{
|
||||
return MapToDomain(bundleRequest);
|
||||
return MongoMappers.Map(bundleRequest);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -341,7 +341,7 @@ public class BacktestRepository : IBacktestRepository
|
||||
|
||||
public void UpdateBundleBacktestRequest(BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
var dto = MapToDto(bundleRequest);
|
||||
var dto = MongoMappers.Map(bundleRequest);
|
||||
_bundleBacktestRepository.ReplaceOne(dto);
|
||||
}
|
||||
|
||||
@@ -361,55 +361,6 @@ public class BacktestRepository : IBacktestRepository
|
||||
.Where(b => b.Status == BundleBacktestRequestStatus.Pending)
|
||||
.ToList();
|
||||
|
||||
return pendingRequests.Select(MapToDomain);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a domain model to DTO
|
||||
/// </summary>
|
||||
/// <param name="domain">The domain model</param>
|
||||
/// <returns>The DTO</returns>
|
||||
private static BundleBacktestRequestDto MapToDto(BundleBacktestRequest domain)
|
||||
{
|
||||
return new BundleBacktestRequestDto
|
||||
{
|
||||
RequestId = domain.RequestId,
|
||||
User = MongoMappers.Map(domain.User),
|
||||
CompletedAt = domain.CompletedAt,
|
||||
Status = domain.Status,
|
||||
BacktestRequestsJson = domain.BacktestRequestsJson,
|
||||
TotalBacktests = domain.TotalBacktests,
|
||||
CompletedBacktests = domain.CompletedBacktests,
|
||||
FailedBacktests = domain.FailedBacktests,
|
||||
ErrorMessage = domain.ErrorMessage,
|
||||
ProgressInfo = domain.ProgressInfo,
|
||||
CurrentBacktest = domain.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = domain.EstimatedTimeRemainingSeconds
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a DTO to domain model
|
||||
/// </summary>
|
||||
/// <param name="dto">The DTO</param>
|
||||
/// <returns>The domain model</returns>
|
||||
private static BundleBacktestRequest MapToDomain(BundleBacktestRequestDto dto)
|
||||
{
|
||||
return new BundleBacktestRequest
|
||||
{
|
||||
RequestId = dto.RequestId,
|
||||
User = MongoMappers.Map(dto.User),
|
||||
CreatedAt = dto.CreatedAt,
|
||||
CompletedAt = dto.CompletedAt,
|
||||
Status = dto.Status,
|
||||
BacktestRequestsJson = dto.BacktestRequestsJson,
|
||||
TotalBacktests = dto.TotalBacktests,
|
||||
CompletedBacktests = dto.CompletedBacktests,
|
||||
FailedBacktests = dto.FailedBacktests,
|
||||
ErrorMessage = dto.ErrorMessage,
|
||||
ProgressInfo = dto.ProgressInfo,
|
||||
CurrentBacktest = dto.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = dto.EstimatedTimeRemainingSeconds
|
||||
};
|
||||
return pendingRequests.Select(MongoMappers.Map);
|
||||
}
|
||||
}
|
||||
@@ -19,4 +19,5 @@ public class BundleBacktestRequestDto : Document
|
||||
public string? ProgressInfo { get; set; }
|
||||
public string? CurrentBacktest { get; set; }
|
||||
public int? EstimatedTimeRemainingSeconds { get; set; }
|
||||
}
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -1108,4 +1108,51 @@ public static class MongoMappers
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region BundleBacktestRequests
|
||||
|
||||
public static BundleBacktestRequestDto Map(BundleBacktestRequest domain)
|
||||
{
|
||||
if (domain == null) return null;
|
||||
return new BundleBacktestRequestDto
|
||||
{
|
||||
RequestId = domain.RequestId,
|
||||
User = Map(domain.User),
|
||||
CompletedAt = domain.CompletedAt,
|
||||
Status = domain.Status,
|
||||
BacktestRequestsJson = domain.BacktestRequestsJson,
|
||||
TotalBacktests = domain.TotalBacktests,
|
||||
CompletedBacktests = domain.CompletedBacktests,
|
||||
FailedBacktests = domain.FailedBacktests,
|
||||
ErrorMessage = domain.ErrorMessage,
|
||||
ProgressInfo = domain.ProgressInfo,
|
||||
CurrentBacktest = domain.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = domain.EstimatedTimeRemainingSeconds,
|
||||
Name = domain.Name
|
||||
};
|
||||
}
|
||||
|
||||
public static BundleBacktestRequest Map(BundleBacktestRequestDto dto)
|
||||
{
|
||||
if (dto == null) return null;
|
||||
return new BundleBacktestRequest
|
||||
{
|
||||
RequestId = dto.RequestId,
|
||||
User = Map(dto.User),
|
||||
CreatedAt = dto.CreatedAt,
|
||||
CompletedAt = dto.CompletedAt,
|
||||
Status = dto.Status,
|
||||
BacktestRequestsJson = dto.BacktestRequestsJson,
|
||||
TotalBacktests = dto.TotalBacktests,
|
||||
CompletedBacktests = dto.CompletedBacktests,
|
||||
FailedBacktests = dto.FailedBacktests,
|
||||
ErrorMessage = dto.ErrorMessage,
|
||||
ProgressInfo = dto.ProgressInfo,
|
||||
CurrentBacktest = dto.CurrentBacktest,
|
||||
EstimatedTimeRemainingSeconds = dto.EstimatedTimeRemainingSeconds,
|
||||
Name = dto.Name
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -716,6 +716,163 @@ export class BacktestClient extends AuthorizedApiBase {
|
||||
return Promise.resolve<Backtest>(null as any);
|
||||
}
|
||||
|
||||
backtest_RunBundle(name: string | null | undefined, requests: RunBacktestRequest[]): Promise<BundleBacktestRequest> {
|
||||
let url_ = this.baseUrl + "/Backtest/Bundle?";
|
||||
if (name !== undefined && name !== null)
|
||||
url_ += "name=" + encodeURIComponent("" + name) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
const content_ = JSON.stringify(requests);
|
||||
|
||||
let options_: RequestInit = {
|
||||
body: content_,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||
return this.http.fetch(url_, transformedOptions_);
|
||||
}).then((_response: Response) => {
|
||||
return this.processBacktest_RunBundle(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processBacktest_RunBundle(response: Response): Promise<BundleBacktestRequest> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as BundleBacktestRequest;
|
||||
return result200;
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<BundleBacktestRequest>(null as any);
|
||||
}
|
||||
|
||||
backtest_GetBundleBacktestRequests(): Promise<BundleBacktestRequest[]> {
|
||||
let url_ = this.baseUrl + "/Backtest/Bundle";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||
return this.http.fetch(url_, transformedOptions_);
|
||||
}).then((_response: Response) => {
|
||||
return this.processBacktest_GetBundleBacktestRequests(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processBacktest_GetBundleBacktestRequests(response: Response): Promise<BundleBacktestRequest[]> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as BundleBacktestRequest[];
|
||||
return result200;
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<BundleBacktestRequest[]>(null as any);
|
||||
}
|
||||
|
||||
backtest_GetBundleBacktestRequest(id: string): Promise<BundleBacktestRequest> {
|
||||
let url_ = this.baseUrl + "/Backtest/Bundle/{id}";
|
||||
if (id === undefined || id === null)
|
||||
throw new Error("The parameter 'id' must be defined.");
|
||||
url_ = url_.replace("{id}", encodeURIComponent("" + id));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||
return this.http.fetch(url_, transformedOptions_);
|
||||
}).then((_response: Response) => {
|
||||
return this.processBacktest_GetBundleBacktestRequest(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processBacktest_GetBundleBacktestRequest(response: Response): Promise<BundleBacktestRequest> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as BundleBacktestRequest;
|
||||
return result200;
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<BundleBacktestRequest>(null as any);
|
||||
}
|
||||
|
||||
backtest_DeleteBundleBacktestRequest(id: string): Promise<FileResponse> {
|
||||
let url_ = this.baseUrl + "/Backtest/Bundle/{id}";
|
||||
if (id === undefined || id === null)
|
||||
throw new Error("The parameter 'id' must be defined.");
|
||||
url_ = url_.replace("{id}", encodeURIComponent("" + id));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Accept": "application/octet-stream"
|
||||
}
|
||||
};
|
||||
|
||||
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||
return this.http.fetch(url_, transformedOptions_);
|
||||
}).then((_response: Response) => {
|
||||
return this.processBacktest_DeleteBundleBacktestRequest(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processBacktest_DeleteBundleBacktestRequest(response: Response): Promise<FileResponse> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200 || status === 206) {
|
||||
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
|
||||
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
|
||||
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
|
||||
if (fileName) {
|
||||
fileName = decodeURIComponent(fileName);
|
||||
} else {
|
||||
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
|
||||
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
|
||||
}
|
||||
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<FileResponse>(null as any);
|
||||
}
|
||||
|
||||
backtest_RunGenetic(request: RunGeneticRequest): Promise<GeneticRequest> {
|
||||
let url_ = this.baseUrl + "/Backtest/Genetic";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
@@ -3880,6 +4037,33 @@ export interface MoneyManagementRequest {
|
||||
leverage: number;
|
||||
}
|
||||
|
||||
export interface BundleBacktestRequest {
|
||||
requestId: string;
|
||||
user: User;
|
||||
createdAt: Date;
|
||||
completedAt?: Date | null;
|
||||
status: BundleBacktestRequestStatus;
|
||||
name: string;
|
||||
backtestRequestsJson: string;
|
||||
results?: Backtest[] | null;
|
||||
totalBacktests: number;
|
||||
completedBacktests: number;
|
||||
failedBacktests: number;
|
||||
progressPercentage?: number;
|
||||
errorMessage?: string | null;
|
||||
progressInfo?: string | null;
|
||||
currentBacktest?: string | null;
|
||||
estimatedTimeRemainingSeconds?: number | null;
|
||||
}
|
||||
|
||||
export enum BundleBacktestRequestStatus {
|
||||
Pending = "Pending",
|
||||
Running = "Running",
|
||||
Completed = "Completed",
|
||||
Failed = "Failed",
|
||||
Cancelled = "Cancelled",
|
||||
}
|
||||
|
||||
export interface GeneticRequest {
|
||||
requestId: string;
|
||||
user: User;
|
||||
|
||||
@@ -686,6 +686,33 @@ export interface MoneyManagementRequest {
|
||||
leverage: number;
|
||||
}
|
||||
|
||||
export interface BundleBacktestRequest {
|
||||
requestId: string;
|
||||
user: User;
|
||||
createdAt: Date;
|
||||
completedAt?: Date | null;
|
||||
status: BundleBacktestRequestStatus;
|
||||
name: string;
|
||||
backtestRequestsJson: string;
|
||||
results?: Backtest[] | null;
|
||||
totalBacktests: number;
|
||||
completedBacktests: number;
|
||||
failedBacktests: number;
|
||||
progressPercentage?: number;
|
||||
errorMessage?: string | null;
|
||||
progressInfo?: string | null;
|
||||
currentBacktest?: string | null;
|
||||
estimatedTimeRemainingSeconds?: number | null;
|
||||
}
|
||||
|
||||
export enum BundleBacktestRequestStatus {
|
||||
Pending = "Pending",
|
||||
Running = "Running",
|
||||
Completed = "Completed",
|
||||
Failed = "Failed",
|
||||
Cancelled = "Cancelled",
|
||||
}
|
||||
|
||||
export interface GeneticRequest {
|
||||
requestId: string;
|
||||
user: User;
|
||||
|
||||
Reference in New Issue
Block a user