Remove candle from backtest
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
using Managing.Api.Models.Responses;
|
||||
using Managing.Api.Models.Requests;
|
||||
using Managing.Api.Models.Responses;
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.ManageBot.Commands;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Statistics;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Managing.Domain.Trades;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -30,6 +34,7 @@ public class DataController : ControllerBase
|
||||
private readonly IStatisticService _statisticService;
|
||||
private readonly IHubContext<CandleHub> _hubContext;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ITradingService _tradingService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DataController"/> class.
|
||||
@@ -40,13 +45,15 @@ public class DataController : ControllerBase
|
||||
/// <param name="statisticService">Service for statistical analysis.</param>
|
||||
/// <param name="hubContext">SignalR hub context for real-time communication.</param>
|
||||
/// <param name="mediator">Mediator for handling commands and queries.</param>
|
||||
/// <param name="tradingService">Service for trading operations.</param>
|
||||
public DataController(
|
||||
IExchangeService exchangeService,
|
||||
IAccountService accountService,
|
||||
ICacheService cacheService,
|
||||
IStatisticService statisticService,
|
||||
IHubContext<CandleHub> hubContext,
|
||||
IMediator mediator)
|
||||
IMediator mediator,
|
||||
ITradingService tradingService)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_accountService = accountService;
|
||||
@@ -54,6 +61,7 @@ public class DataController : ControllerBase
|
||||
_statisticService = statisticService;
|
||||
_hubContext = hubContext;
|
||||
_mediator = mediator;
|
||||
_tradingService = tradingService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -211,19 +219,55 @@ public class DataController : ControllerBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves candle data for a given exchange, ticker, start date, and timeframe.
|
||||
/// Retrieves candles with indicators values for backtest details display.
|
||||
/// </summary>
|
||||
/// <param name="exchange">The exchange to retrieve candles from.</param>
|
||||
/// <param name="ticker">The ticker symbol to retrieve candles for.</param>
|
||||
/// <param name="startDate">The start date for the candle data.</param>
|
||||
/// <param name="timeframe">The timeframe for the candle data.</param>
|
||||
/// <returns>A list of <see cref="Candle"/> objects.</returns>
|
||||
/// <param name="exchange">The trading exchange.</param>
|
||||
/// <param name="ticker">The ticker symbol.</param>
|
||||
/// <param name="startDate">The start date for the candles.</param>
|
||||
/// <param name="endDate">The end date for the candles.</param>
|
||||
/// <param name="timeframe">The timeframe for the candles.</param>
|
||||
/// <param name="scenario">The scenario object to calculate indicators values (optional).</param>
|
||||
/// <returns>A response containing candles and indicators values.</returns>
|
||||
[Authorize]
|
||||
[HttpGet("GetCandles")]
|
||||
public async Task<ActionResult<List<Candle>>> GetCandles(TradingExchanges exchange, Ticker ticker,
|
||||
DateTime startDate, Timeframe timeframe)
|
||||
[HttpPost("GetCandlesWithIndicators")]
|
||||
public async Task<ActionResult<CandlesWithIndicatorsResponse>> GetCandlesWithIndicators(
|
||||
[FromBody] GetCandlesWithIndicatorsRequest request)
|
||||
{
|
||||
return Ok(await _exchangeService.GetCandlesInflux(exchange, ticker, startDate, timeframe));
|
||||
try
|
||||
{
|
||||
// Get candles for the specified period
|
||||
var candles = await _exchangeService.GetCandlesInflux(TradingExchanges.Evm, request.Ticker,
|
||||
request.StartDate, request.Timeframe, request.EndDate);
|
||||
|
||||
if (candles == null || candles.Count == 0)
|
||||
{
|
||||
return Ok(new CandlesWithIndicatorsResponse
|
||||
{
|
||||
Candles = new List<Candle>(),
|
||||
IndicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>()
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate indicators values if scenario is provided
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues = null;
|
||||
if (request.Scenario != null && request.Scenario.Indicators != null &&
|
||||
request.Scenario.Indicators.Count > 0)
|
||||
{
|
||||
// Map ScenarioRequest to domain Scenario object
|
||||
var domainScenario = MapScenarioRequestToScenario(request.Scenario);
|
||||
indicatorsValues = await _tradingService.CalculateIndicatorsValuesAsync(domainScenario, candles);
|
||||
}
|
||||
|
||||
return Ok(new CandlesWithIndicatorsResponse
|
||||
{
|
||||
Candles = candles,
|
||||
IndicatorsValues = indicatorsValues ?? new Dictionary<IndicatorType, IndicatorsResultBase>()
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"Error retrieving candles with indicators: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -605,4 +649,35 @@ public class DataController : ControllerBase
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a ScenarioRequest to a domain Scenario object.
|
||||
/// </summary>
|
||||
/// <param name="scenarioRequest">The scenario request to map.</param>
|
||||
/// <returns>A domain Scenario object.</returns>
|
||||
private Scenario MapScenarioRequestToScenario(ScenarioRequest scenarioRequest)
|
||||
{
|
||||
var scenario = new Scenario(scenarioRequest.Name, scenarioRequest.LoopbackPeriod);
|
||||
|
||||
foreach (var indicatorRequest in scenarioRequest.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
|
||||
};
|
||||
|
||||
scenario.AddIndicator(indicator);
|
||||
}
|
||||
|
||||
return scenario;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user