Add BacktestSpotBot and update BacktestExecutor for spot trading support

- Introduced BacktestSpotBot class to handle backtesting for spot trading scenarios.
- Updated BacktestExecutor to support both BacktestFutures and BacktestSpot trading types.
- Enhanced error handling to provide clearer messages for unsupported trading types.
- Registered new command handlers for OpenSpotPositionRequest and CloseSpotPositionCommand in ApiBootstrap.
- Added unit tests for executing backtests with spot trading configurations, ensuring correct behavior and metrics validation.
This commit is contained in:
2025-12-01 23:41:23 +07:00
parent 3771bb5dde
commit 5bd03259da
9 changed files with 847 additions and 5 deletions

View File

@@ -544,15 +544,22 @@ public class BacktestExecutor
throw new InvalidOperationException("Bot configuration is not initialized");
}
if (config.TradingType != TradingType.BacktestFutures)
if (config.TradingType != TradingType.BacktestFutures && config.TradingType != TradingType.BacktestSpot)
{
throw new InvalidOperationException("BacktestExecutor can only be used for backtesting");
throw new InvalidOperationException($"BacktestExecutor can only be used for backtesting. TradingType must be BacktestFutures or BacktestSpot, but got {config.TradingType}");
}
// Create the trading bot instance
// Create the trading bot instance based on TradingType
using var scope = _scopeFactory.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<TradingBotBase>>();
var tradingBot = new BacktestFuturesBot(logger, _scopeFactory, config);
TradingBotBase tradingBot = config.TradingType switch
{
TradingType.BacktestFutures => new BacktestFuturesBot(logger, _scopeFactory, config),
TradingType.BacktestSpot => new BacktestSpotBot(logger, _scopeFactory, config),
_ => throw new InvalidOperationException($"Unsupported TradingType for backtest: {config.TradingType}")
};
return tradingBot;
}