Orlean (#32)
* Start building with orlean * Add missing file * Serialize grain state * Remove grain and proxies * update and add plan * Update a bit * Fix backtest grain * Fix backtest grain * Clean a bit
This commit is contained in:
@@ -29,12 +29,6 @@ public interface IBotService
|
||||
/// <returns>ITradingBot instance configured for backtesting</returns>
|
||||
Task<ITradingBot> CreateBacktestTradingBot(TradingBotConfig config);
|
||||
|
||||
// Legacy methods - these will use TradingBot internally but maintain backward compatibility
|
||||
Task<ITradingBot> CreateScalpingBot(TradingBotConfig config);
|
||||
Task<ITradingBot> CreateBacktestScalpingBot(TradingBotConfig config);
|
||||
Task<ITradingBot> CreateFlippingBot(TradingBotConfig config);
|
||||
Task<ITradingBot> CreateBacktestFlippingBot(TradingBotConfig config);
|
||||
|
||||
IBot CreateSimpleBot(string botName, Workflow workflow);
|
||||
Task<string> StopBot(string botName);
|
||||
Task<bool> DeleteBot(string botName);
|
||||
|
||||
@@ -52,5 +52,6 @@ namespace Managing.Application.Abstractions
|
||||
Task<bool> UpdateIndicatorByUser(User user, IndicatorType indicatorType, string name, int? period, int? fastPeriods,
|
||||
int? slowPeriods, int? signalPeriods, double? multiplier, int? stochPeriods, int? smoothPeriods,
|
||||
int? cyclePeriods);
|
||||
Task<Scenario> GetScenarioByNameAndUserAsync(string scenarioName, User user);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,13 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Bots;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Core.FixedSizedQueue;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Domain.Workflows;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
@@ -32,6 +28,7 @@ namespace Managing.Application.Backtesting
|
||||
private readonly IMessengerService _messengerService;
|
||||
private readonly IKaigenService _kaigenService;
|
||||
private readonly IHubContext<BacktestHub> _hubContext;
|
||||
private readonly IGrainFactory _grainFactory;
|
||||
|
||||
public Backtester(
|
||||
IExchangeService exchangeService,
|
||||
@@ -42,7 +39,8 @@ namespace Managing.Application.Backtesting
|
||||
IAccountService accountService,
|
||||
IMessengerService messengerService,
|
||||
IKaigenService kaigenService,
|
||||
IHubContext<BacktestHub> hubContext)
|
||||
IHubContext<BacktestHub> hubContext,
|
||||
IGrainFactory grainFactory)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_botFactory = botFactory;
|
||||
@@ -53,6 +51,7 @@ namespace Managing.Application.Backtesting
|
||||
_messengerService = messengerService;
|
||||
_kaigenService = kaigenService;
|
||||
_hubContext = hubContext;
|
||||
_grainFactory = grainFactory;
|
||||
}
|
||||
|
||||
public Backtest RunSimpleBotBacktest(Workflow workflow, bool save = false)
|
||||
@@ -80,8 +79,8 @@ namespace Managing.Application.Backtesting
|
||||
/// <param name="withCandles">Whether to include candles and indicators values in the response</param>
|
||||
/// <param name="requestId">The request ID to associate with this backtest (optional)</param>
|
||||
/// <param name="metadata">Additional metadata to associate with this backtest (optional)</param>
|
||||
/// <returns>The backtest results</returns>
|
||||
public async Task<Backtest> RunTradingBotBacktest(
|
||||
/// <returns>The lightweight backtest results</returns>
|
||||
public async Task<LightBacktest> RunTradingBotBacktest(
|
||||
TradingBotConfig config,
|
||||
DateTime startDate,
|
||||
DateTime endDate,
|
||||
@@ -114,25 +113,7 @@ namespace Managing.Application.Backtesting
|
||||
try
|
||||
{
|
||||
var candles = GetCandles(config.Ticker, config.Timeframe, startDate, endDate);
|
||||
|
||||
var result = await RunBacktestWithCandles(config, candles, user, withCandles, requestId, metadata);
|
||||
|
||||
// Set start and end dates
|
||||
result.StartDate = startDate;
|
||||
result.EndDate = endDate;
|
||||
|
||||
// Ensure RequestId is set - required for PostgreSQL NOT NULL constraint
|
||||
if (string.IsNullOrEmpty(result.RequestId))
|
||||
{
|
||||
result.RequestId = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
if (save && user != null)
|
||||
{
|
||||
_backtestRepository.InsertBacktestForUser(user, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
return await RunBacktestWithCandles(config, candles, user, save, withCandles, requestId, metadata);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -172,8 +153,10 @@ namespace Managing.Application.Backtesting
|
||||
/// <param name="candles">The candles to use for backtesting</param>
|
||||
/// <param name="user">The user running the backtest (optional)</param>
|
||||
/// <param name="withCandles">Whether to include candles and indicators values in the response</param>
|
||||
/// <returns>The backtest results</returns>
|
||||
public async Task<Backtest> RunTradingBotBacktest(
|
||||
/// <param name="requestId">The request ID to associate with this backtest (optional)</param>
|
||||
/// <param name="metadata">Additional metadata to associate with this backtest (optional)</param>
|
||||
/// <returns>The lightweight backtest results</returns>
|
||||
public async Task<LightBacktest> RunTradingBotBacktest(
|
||||
TradingBotConfig config,
|
||||
List<Candle> candles,
|
||||
User user = null,
|
||||
@@ -181,43 +164,49 @@ namespace Managing.Application.Backtesting
|
||||
string requestId = null,
|
||||
object metadata = null)
|
||||
{
|
||||
return await RunBacktestWithCandles(config, candles, user, withCandles, requestId, metadata);
|
||||
return await RunBacktestWithCandles(config, candles, user, false, withCandles, requestId, metadata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Core backtesting logic - handles the actual backtest execution with pre-loaded candles
|
||||
/// </summary>
|
||||
private async Task<Backtest> RunBacktestWithCandles(
|
||||
private async Task<LightBacktest> RunBacktestWithCandles(
|
||||
TradingBotConfig config,
|
||||
List<Candle> candles,
|
||||
User user = null,
|
||||
bool save = false,
|
||||
bool withCandles = false,
|
||||
string requestId = null,
|
||||
object metadata = null)
|
||||
{
|
||||
var tradingBot = await _botFactory.CreateBacktestTradingBot(config);
|
||||
// Ensure this is a backtest configuration
|
||||
if (!config.IsForBacktest)
|
||||
{
|
||||
throw new InvalidOperationException("Backtest configuration must have IsForBacktest set to true");
|
||||
}
|
||||
|
||||
// Scenario and indicators should already be loaded in constructor by BotService
|
||||
// This is just a validation check to ensure everything loaded properly
|
||||
if (tradingBot is TradingBot bot && !bot.Indicators.Any())
|
||||
// Validate that scenario and indicators are properly loaded
|
||||
if (config.Scenario == null && string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"No indicators were loaded for scenario '{config.ScenarioName ?? config.Scenario?.Name}'. " +
|
||||
"This indicates a problem with scenario loading.");
|
||||
"Backtest configuration must include either Scenario object or ScenarioName");
|
||||
}
|
||||
|
||||
tradingBot.User = user;
|
||||
tradingBot.Account = await GetAccountFromConfig(config);
|
||||
|
||||
var result =
|
||||
await GetBacktestingResult(config, tradingBot, candles, user, withCandles, requestId, metadata);
|
||||
|
||||
if (user != null)
|
||||
if (config.Scenario == null && !string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
result.User = user;
|
||||
var fullScenario = await _scenarioService.GetScenarioByNameAndUserAsync(config.ScenarioName, user);
|
||||
config.Scenario = LightScenario.FromScenario(fullScenario);
|
||||
}
|
||||
|
||||
return result;
|
||||
// Create a clean copy of the config to avoid Orleans serialization issues
|
||||
var cleanConfig = CreateCleanConfigForOrleans(config);
|
||||
|
||||
// Create Orleans grain for backtesting
|
||||
var backtestGrain = _grainFactory.GetGrain<IBacktestTradingBotGrain>(Guid.NewGuid());
|
||||
|
||||
// Run the backtest using the Orleans grain and return LightBacktest directly
|
||||
return await backtestGrain.RunBacktestAsync(cleanConfig, candles, user, save, withCandles, requestId,
|
||||
metadata);
|
||||
}
|
||||
|
||||
private async Task<Account> GetAccountFromConfig(TradingBotConfig config)
|
||||
@@ -237,128 +226,16 @@ namespace Managing.Application.Backtesting
|
||||
return candles;
|
||||
}
|
||||
|
||||
private async Task<Backtest> GetBacktestingResult(
|
||||
TradingBotConfig config,
|
||||
ITradingBot bot,
|
||||
List<Candle> candles,
|
||||
User user = null,
|
||||
bool withCandles = false,
|
||||
string requestId = null,
|
||||
object metadata = null)
|
||||
|
||||
/// <summary>
|
||||
/// Creates a clean copy of the trading bot config for Orleans serialization
|
||||
/// Uses LightScenario and LightIndicator to avoid FixedSizeQueue serialization issues
|
||||
/// </summary>
|
||||
private TradingBotConfig CreateCleanConfigForOrleans(TradingBotConfig originalConfig)
|
||||
{
|
||||
if (candles == null || candles.Count == 0)
|
||||
{
|
||||
throw new Exception("No candle to backtest");
|
||||
}
|
||||
|
||||
var totalCandles = candles.Count;
|
||||
var currentCandle = 0;
|
||||
var lastLoggedPercentage = 0;
|
||||
|
||||
_logger.LogInformation("Starting backtest with {TotalCandles} candles for {Ticker} on {Timeframe}",
|
||||
totalCandles, config.Ticker, config.Timeframe);
|
||||
|
||||
bot.WalletBalances.Add(candles.FirstOrDefault()!.Date, config.BotTradingBalance);
|
||||
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
bot.OptimizedCandles.Enqueue(candle);
|
||||
bot.Candles.Add(candle);
|
||||
await bot.Run();
|
||||
|
||||
currentCandle++;
|
||||
|
||||
// Check if wallet balance fell below 10 USDC and break if so
|
||||
var currentWalletBalance = bot.WalletBalances.Values.LastOrDefault();
|
||||
if (currentWalletBalance < 10m)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Backtest stopped early: Wallet balance fell below 10 USDC (Current: {CurrentBalance:F2} USDC) at candle {CurrentCandle}/{TotalCandles} from {CandleDate}",
|
||||
currentWalletBalance, currentCandle, totalCandles, candle.Date.ToString("yyyy-MM-dd HH:mm"));
|
||||
break;
|
||||
}
|
||||
|
||||
// Log progress every 10% or every 1000 candles, whichever comes first
|
||||
var currentPercentage = (int)((double)currentCandle / totalCandles * 100);
|
||||
var shouldLog = currentPercentage >= lastLoggedPercentage + 10 ||
|
||||
currentCandle % 1000 == 0 ||
|
||||
currentCandle == totalCandles;
|
||||
|
||||
if (shouldLog && currentPercentage > lastLoggedPercentage)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Backtest progress: {CurrentCandle}/{TotalCandles} ({Percentage}%) - Processing candle from {CandleDate}",
|
||||
currentCandle, totalCandles, currentPercentage, candle.Date.ToString("yyyy-MM-dd HH:mm"));
|
||||
lastLoggedPercentage = currentPercentage;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Backtest processing completed. Calculating final results...");
|
||||
|
||||
bot.Candles = new HashSet<Candle>(candles);
|
||||
|
||||
// Only calculate indicators values if withCandles is true
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues = null;
|
||||
if (withCandles)
|
||||
{
|
||||
indicatorsValues = GetIndicatorsValues(bot.Config.Scenario.Indicators, candles);
|
||||
}
|
||||
|
||||
var finalPnl = bot.GetProfitAndLoss();
|
||||
var winRate = bot.GetWinRate();
|
||||
var stats = TradingHelpers.GetStatistics(bot.WalletBalances);
|
||||
var growthPercentage =
|
||||
TradingHelpers.GetGrowthFromInitalBalance(bot.WalletBalances.FirstOrDefault().Value, finalPnl);
|
||||
var hodlPercentage = TradingHelpers.GetHodlPercentage(candles[0], candles.Last());
|
||||
|
||||
var fees = bot.GetTotalFees();
|
||||
var scoringParams = new BacktestScoringParams(
|
||||
sharpeRatio: (double)stats.SharpeRatio,
|
||||
growthPercentage: (double)growthPercentage,
|
||||
hodlPercentage: (double)hodlPercentage,
|
||||
winRate: winRate,
|
||||
totalPnL: (double)finalPnl,
|
||||
fees: (double)fees,
|
||||
tradeCount: bot.Positions.Count,
|
||||
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime,
|
||||
maxDrawdown: stats.MaxDrawdown,
|
||||
initialBalance: bot.WalletBalances.FirstOrDefault().Value,
|
||||
tradingBalance: config.BotTradingBalance,
|
||||
startDate: candles[0].Date,
|
||||
endDate: candles.Last().Date,
|
||||
timeframe: config.Timeframe,
|
||||
moneyManagement: config.MoneyManagement
|
||||
);
|
||||
|
||||
var scoringResult = BacktestScorer.CalculateDetailedScore(scoringParams);
|
||||
|
||||
// Create backtest result with conditional candles and indicators values
|
||||
var result = new Backtest(config, bot.Positions, bot.Signals.ToList(),
|
||||
withCandles ? candles : new List<Candle>())
|
||||
{
|
||||
FinalPnl = finalPnl,
|
||||
WinRate = winRate,
|
||||
GrowthPercentage = growthPercentage,
|
||||
HodlPercentage = hodlPercentage,
|
||||
Fees = fees,
|
||||
WalletBalances = bot.WalletBalances.ToList(),
|
||||
Statistics = stats,
|
||||
IndicatorsValues = withCandles
|
||||
? AggregateValues(indicatorsValues, bot.IndicatorsValues)
|
||||
: new Dictionary<IndicatorType, IndicatorsResultBase>(),
|
||||
Score = scoringResult.Score,
|
||||
ScoreMessage = scoringResult.GenerateSummaryMessage(),
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
RequestId = requestId,
|
||||
Metadata = metadata,
|
||||
StartDate = candles.FirstOrDefault()!.OpenTime,
|
||||
EndDate = candles.LastOrDefault()!.OpenTime,
|
||||
};
|
||||
|
||||
// Send notification if backtest meets criteria
|
||||
await SendBacktestNotificationIfCriteriaMet(result);
|
||||
|
||||
return result;
|
||||
// Since we're now using LightScenario in TradingBotConfig, we can just return the original config
|
||||
// The conversion to LightScenario is already done when loading the scenario
|
||||
return originalConfig;
|
||||
}
|
||||
|
||||
private async Task SendBacktestNotificationIfCriteriaMet(Backtest backtest)
|
||||
@@ -376,56 +253,6 @@ namespace Managing.Application.Backtesting
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<IndicatorType, IndicatorsResultBase> AggregateValues(
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues,
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> botStrategiesValues)
|
||||
{
|
||||
// Foreach strategy type, only retrieve the values where the strategy is not present already in the bot
|
||||
// Then, add the values to the bot values
|
||||
|
||||
var result = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
foreach (var indicator in indicatorsValues)
|
||||
{
|
||||
// if (!botStrategiesValues.ContainsKey(strategy.Key))
|
||||
// {
|
||||
// result[strategy.Key] = strategy.Value;
|
||||
// }else
|
||||
// {
|
||||
// result[strategy.Key] = botStrategiesValues[strategy.Key];
|
||||
// }
|
||||
|
||||
result[indicator.Key] = indicator.Value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private Dictionary<IndicatorType, IndicatorsResultBase> GetIndicatorsValues(List<Indicator> indicators,
|
||||
List<Candle> candles)
|
||||
{
|
||||
var indicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
var fixedCandles = new FixedSizeQueue<Candle>(10000);
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
fixedCandles.Enqueue(candle);
|
||||
}
|
||||
|
||||
foreach (var indicator in indicators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var s = ScenarioHelpers.BuildIndicator(indicator, 10000);
|
||||
s.Candles = fixedCandles;
|
||||
indicatorsValues[indicator.Type] = s.GetIndicatorValues();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
return indicatorsValues;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteBacktestAsync(string id)
|
||||
{
|
||||
|
||||
@@ -12,14 +12,14 @@ namespace Managing.Application.Bots.Base
|
||||
private readonly IExchangeService _exchangeService;
|
||||
private readonly IMessengerService _messengerService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||
private readonly ILogger<TradingBotBase> _tradingBotLogger;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IBotService _botService;
|
||||
private readonly IBackupBotService _backupBotService;
|
||||
|
||||
public BotFactory(
|
||||
IExchangeService exchangeService,
|
||||
ILogger<TradingBot> tradingBotLogger,
|
||||
ILogger<TradingBotBase> tradingBotLogger,
|
||||
IMessengerService messengerService,
|
||||
IAccountService accountService,
|
||||
ITradingService tradingService,
|
||||
|
||||
403
src/Managing.Application/Bots/Grains/BacktestTradingBotGrain.cs
Normal file
403
src/Managing.Application/Bots/Grains/BacktestTradingBotGrain.cs
Normal file
@@ -0,0 +1,403 @@
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Managing.Application.Abstractions.Models;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Core.FixedSizedQueue;
|
||||
using Managing.Domain.Backtests;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Candles;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Shared.Helpers;
|
||||
using Managing.Domain.Strategies;
|
||||
using Managing.Domain.Strategies.Base;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Domain.Users;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Orleans.Concurrency;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Bots.Grains;
|
||||
|
||||
/// <summary>
|
||||
/// Orleans grain for backtest trading bot operations.
|
||||
/// Uses composition with TradingBotBase to maintain separation of concerns.
|
||||
/// This grain is stateless and follows the exact pattern of GetBacktestingResult from Backtester.cs.
|
||||
/// </summary>
|
||||
[StatelessWorker]
|
||||
public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
|
||||
{
|
||||
private readonly ILogger<BacktestTradingBotGrain> _logger;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly IBacktestRepository _backtestRepository;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
public BacktestTradingBotGrain(
|
||||
ILogger<BacktestTradingBotGrain> logger,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
IBacktestRepository backtestRepository)
|
||||
{
|
||||
_logger = logger;
|
||||
_scopeFactory = scopeFactory;
|
||||
_backtestRepository = backtestRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a complete backtest following the exact pattern of GetBacktestingResult from Backtester.cs
|
||||
/// </summary>
|
||||
/// <param name="config">The trading bot configuration for this backtest</param>
|
||||
/// <param name="candles">The candles to use for backtesting</param>
|
||||
/// <param name="withCandles">Whether to include candles and indicators values in the response</param>
|
||||
/// <param name="requestId">The request ID to associate with this backtest</param>
|
||||
/// <param name="metadata">Additional metadata to associate with this backtest</param>
|
||||
/// <returns>The complete backtest result</returns>
|
||||
public async Task<LightBacktest> RunBacktestAsync(
|
||||
TradingBotConfig config,
|
||||
List<Candle> candles,
|
||||
User user = null,
|
||||
bool save = false,
|
||||
bool withCandles = false,
|
||||
string requestId = null,
|
||||
object metadata = null)
|
||||
{
|
||||
if (candles == null || candles.Count == 0)
|
||||
{
|
||||
throw new Exception("No candle to backtest");
|
||||
}
|
||||
|
||||
// Create a fresh TradingBotBase instance for this backtest
|
||||
var tradingBot = await CreateTradingBotInstance(config);
|
||||
tradingBot.Start();
|
||||
|
||||
var totalCandles = candles.Count;
|
||||
var currentCandle = 0;
|
||||
var lastLoggedPercentage = 0;
|
||||
|
||||
_logger.LogInformation("Starting backtest with {TotalCandles} candles for {Ticker} on {Timeframe}",
|
||||
totalCandles, config.Ticker, config.Timeframe);
|
||||
|
||||
// Initialize wallet balance with first candle
|
||||
tradingBot.WalletBalances.Clear();
|
||||
tradingBot.WalletBalances.Add(candles.FirstOrDefault()!.Date, config.BotTradingBalance);
|
||||
|
||||
// Process all candles following the exact pattern from GetBacktestingResult
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
tradingBot.OptimizedCandles.Enqueue(candle);
|
||||
tradingBot.Candles.Add(candle);
|
||||
await tradingBot.Run();
|
||||
|
||||
currentCandle++;
|
||||
|
||||
// Check if wallet balance fell below 10 USDC and break if so
|
||||
var currentWalletBalance = tradingBot.WalletBalances.Values.LastOrDefault();
|
||||
if (currentWalletBalance < 10m)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Backtest stopped early: Wallet balance fell below 10 USDC (Current: {CurrentBalance:F2} USDC) at candle {CurrentCandle}/{TotalCandles} from {CandleDate}",
|
||||
currentWalletBalance, currentCandle, totalCandles, candle.Date.ToString("yyyy-MM-dd HH:mm"));
|
||||
break;
|
||||
}
|
||||
|
||||
// Log progress every 10% or every 1000 candles, whichever comes first
|
||||
var currentPercentage = (int)((double)currentCandle / totalCandles * 100);
|
||||
var shouldLog = currentPercentage >= lastLoggedPercentage + 10 ||
|
||||
currentCandle % 1000 == 0 ||
|
||||
currentCandle == totalCandles;
|
||||
|
||||
if (shouldLog && currentPercentage > lastLoggedPercentage)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Backtest progress: {CurrentCandle}/{TotalCandles} ({Percentage}%) - Processing candle from {CandleDate}",
|
||||
currentCandle, totalCandles, currentPercentage, candle.Date.ToString("yyyy-MM-dd HH:mm"));
|
||||
lastLoggedPercentage = currentPercentage;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Backtest processing completed. Calculating final results...");
|
||||
|
||||
// Set all candles for final calculations
|
||||
tradingBot.Candles = new HashSet<Candle>(candles);
|
||||
|
||||
// Only calculate indicators values if withCandles is true
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues = null;
|
||||
if (withCandles)
|
||||
{
|
||||
// Convert LightScenario back to full Scenario for indicator calculations
|
||||
var fullScenario = config.Scenario.ToScenario();
|
||||
indicatorsValues = GetIndicatorsValues(fullScenario.Indicators, candles);
|
||||
}
|
||||
|
||||
// Calculate final results following the exact pattern from GetBacktestingResult
|
||||
var finalPnl = tradingBot.GetProfitAndLoss();
|
||||
var winRate = tradingBot.GetWinRate();
|
||||
var stats = TradingHelpers.GetStatistics(tradingBot.WalletBalances);
|
||||
var growthPercentage =
|
||||
TradingHelpers.GetGrowthFromInitalBalance(tradingBot.WalletBalances.FirstOrDefault().Value, finalPnl);
|
||||
var hodlPercentage = TradingHelpers.GetHodlPercentage(candles[0], candles.Last());
|
||||
|
||||
var fees = tradingBot.GetTotalFees();
|
||||
var scoringParams = new BacktestScoringParams(
|
||||
sharpeRatio: (double)stats.SharpeRatio,
|
||||
growthPercentage: (double)growthPercentage,
|
||||
hodlPercentage: (double)hodlPercentage,
|
||||
winRate: winRate,
|
||||
totalPnL: (double)finalPnl,
|
||||
fees: (double)fees,
|
||||
tradeCount: tradingBot.Positions.Count,
|
||||
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime,
|
||||
maxDrawdown: stats.MaxDrawdown,
|
||||
initialBalance: tradingBot.WalletBalances.FirstOrDefault().Value,
|
||||
tradingBalance: config.BotTradingBalance,
|
||||
startDate: candles[0].Date,
|
||||
endDate: candles.Last().Date,
|
||||
timeframe: config.Timeframe,
|
||||
moneyManagement: config.MoneyManagement
|
||||
);
|
||||
|
||||
var scoringResult = BacktestScorer.CalculateDetailedScore(scoringParams);
|
||||
|
||||
// Generate requestId if not provided
|
||||
var finalRequestId = requestId ?? Guid.NewGuid().ToString();
|
||||
|
||||
// Create backtest result with conditional candles and indicators values
|
||||
var result = new Backtest(config, tradingBot.Positions, tradingBot.Signals.ToList(),
|
||||
withCandles ? candles : new List<Candle>())
|
||||
{
|
||||
FinalPnl = finalPnl,
|
||||
WinRate = winRate,
|
||||
GrowthPercentage = growthPercentage,
|
||||
HodlPercentage = hodlPercentage,
|
||||
Fees = fees,
|
||||
WalletBalances = tradingBot.WalletBalances.ToList(),
|
||||
Statistics = stats,
|
||||
IndicatorsValues = withCandles
|
||||
? AggregateValues(indicatorsValues, tradingBot.IndicatorsValues)
|
||||
: new Dictionary<IndicatorType, IndicatorsResultBase>(),
|
||||
Score = scoringResult.Score,
|
||||
ScoreMessage = scoringResult.GenerateSummaryMessage(),
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
RequestId = finalRequestId,
|
||||
Metadata = metadata,
|
||||
StartDate = candles.FirstOrDefault()!.OpenTime,
|
||||
EndDate = candles.LastOrDefault()!.OpenTime,
|
||||
};
|
||||
|
||||
if (save && user != null)
|
||||
{
|
||||
_backtestRepository.InsertBacktestForUser(user, result);
|
||||
}
|
||||
|
||||
// Send notification if backtest meets criteria
|
||||
await SendBacktestNotificationIfCriteriaMet(result);
|
||||
|
||||
// Clean up the trading bot instance
|
||||
tradingBot.Stop();
|
||||
|
||||
// Convert Backtest to LightBacktest for safe Orleans serialization
|
||||
return ConvertToLightBacktest(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Backtest to LightBacktest for safe Orleans serialization
|
||||
/// </summary>
|
||||
/// <param name="backtest">The full backtest to convert</param>
|
||||
/// <returns>A lightweight backtest suitable for Orleans serialization</returns>
|
||||
private LightBacktest ConvertToLightBacktest(Backtest backtest)
|
||||
{
|
||||
return new LightBacktest
|
||||
{
|
||||
Id = backtest.Id,
|
||||
Config = backtest.Config,
|
||||
FinalPnl = backtest.FinalPnl,
|
||||
WinRate = backtest.WinRate,
|
||||
GrowthPercentage = backtest.GrowthPercentage,
|
||||
HodlPercentage = backtest.HodlPercentage,
|
||||
StartDate = backtest.StartDate,
|
||||
EndDate = backtest.EndDate,
|
||||
MaxDrawdown = backtest.Statistics?.MaxDrawdown,
|
||||
Fees = backtest.Fees,
|
||||
SharpeRatio = (double?)backtest.Statistics?.SharpeRatio,
|
||||
Score = backtest.Score,
|
||||
ScoreMessage = backtest.ScoreMessage
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a TradingBotBase instance using composition for backtesting
|
||||
/// </summary>
|
||||
private async Task<TradingBotBase> CreateTradingBotInstance(TradingBotConfig config, User user = null)
|
||||
{
|
||||
// Validate configuration for backtesting
|
||||
if (config == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot configuration is not initialized");
|
||||
}
|
||||
|
||||
if (!config.IsForBacktest)
|
||||
{
|
||||
throw new InvalidOperationException("BacktestTradingBotGrain can only be used for backtesting");
|
||||
}
|
||||
|
||||
// Create the trading bot instance
|
||||
var logger = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ILogger<TradingBotBase>>();
|
||||
var tradingBot = new TradingBotBase(logger, _scopeFactory, config);
|
||||
|
||||
// Set the user if available
|
||||
if (user != null)
|
||||
{
|
||||
tradingBot.User = user;
|
||||
}
|
||||
|
||||
return tradingBot;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends notification if backtest meets criteria (following Backtester.cs pattern)
|
||||
/// </summary>
|
||||
private async Task SendBacktestNotificationIfCriteriaMet(Backtest backtest)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (backtest.Score > 60)
|
||||
{
|
||||
// Note: In a real implementation, you would inject IMessengerService
|
||||
// For now, we'll just log the notification
|
||||
_logger.LogInformation("Backtest {BacktestId} scored {Score} - notification criteria met",
|
||||
backtest.Id, backtest.Score);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to send backtest notification for backtest {Id}", backtest.Id);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aggregates indicator values (following Backtester.cs pattern)
|
||||
/// </summary>
|
||||
private Dictionary<IndicatorType, IndicatorsResultBase> AggregateValues(
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> indicatorsValues,
|
||||
Dictionary<IndicatorType, IndicatorsResultBase> botStrategiesValues)
|
||||
{
|
||||
var result = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
foreach (var indicator in indicatorsValues)
|
||||
{
|
||||
result[indicator.Key] = indicator.Value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets indicators values (following Backtester.cs pattern)
|
||||
/// </summary>
|
||||
private Dictionary<IndicatorType, IndicatorsResultBase> GetIndicatorsValues(List<Indicator> indicators,
|
||||
List<Candle> candles)
|
||||
{
|
||||
var indicatorsValues = new Dictionary<IndicatorType, IndicatorsResultBase>();
|
||||
var fixedCandles = new FixedSizeQueue<Candle>(10000);
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
fixedCandles.Enqueue(candle);
|
||||
}
|
||||
|
||||
foreach (var indicator in indicators)
|
||||
{
|
||||
try
|
||||
{
|
||||
var s = ScenarioHelpers.BuildIndicator(indicator, 10000);
|
||||
s.Candles = fixedCandles;
|
||||
indicatorsValues[indicator.Type] = s.GetIndicatorValues();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error building indicator {IndicatorType}", indicator.Type);
|
||||
}
|
||||
}
|
||||
|
||||
return indicatorsValues;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<BacktestProgress> GetBacktestProgressAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task StartAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<BotStatus> GetStatusAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<TradingBotConfig> GetConfigurationAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<Position> OpenPositionManuallyAsync(TradeDirection direction)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task ToggleIsForWatchOnlyAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<TradingBotResponse> GetBotDataAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task LoadBackupAsync(BotBackup backup)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task SaveBackupAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<decimal> GetProfitAndLossAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<int> GetWinRateAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<long> GetExecutionCountAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<DateTime> GetStartupTimeAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<DateTime> GetCreateDateAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
490
src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs
Normal file
490
src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs
Normal file
@@ -0,0 +1,490 @@
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Managing.Application.Abstractions.Models;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Trades;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Bots.Grains;
|
||||
|
||||
/// <summary>
|
||||
/// Orleans grain for live trading bot operations.
|
||||
/// Uses composition with TradingBotBase to maintain separation of concerns.
|
||||
/// This grain handles live trading scenarios with real-time market data and execution.
|
||||
/// </summary>
|
||||
public class LiveTradingBotGrain : Grain<TradingBotGrainState>, ITradingBotGrain
|
||||
{
|
||||
private readonly ILogger<LiveTradingBotGrain> _logger;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private TradingBotBase? _tradingBot;
|
||||
private IDisposable? _timer;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
public LiveTradingBotGrain(
|
||||
ILogger<LiveTradingBotGrain> logger,
|
||||
IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
public override async Task OnActivateAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await base.OnActivateAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation("LiveTradingBotGrain {GrainId} activated", this.GetPrimaryKey());
|
||||
|
||||
// Initialize the grain state if not already done
|
||||
if (!State.IsInitialized)
|
||||
{
|
||||
State.Identifier = this.GetPrimaryKey().ToString();
|
||||
State.CreateDate = DateTime.UtcNow;
|
||||
State.Status = BotStatus.Down;
|
||||
State.IsInitialized = true;
|
||||
await WriteStateAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("LiveTradingBotGrain {GrainId} deactivating. Reason: {Reason}",
|
||||
this.GetPrimaryKey(), reason.Description);
|
||||
|
||||
// Stop the timer and trading bot
|
||||
await StopAsync();
|
||||
|
||||
await base.OnDeactivateAsync(reason, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (State.Status == BotStatus.Up)
|
||||
{
|
||||
_logger.LogWarning("Bot {GrainId} is already running", this.GetPrimaryKey());
|
||||
return;
|
||||
}
|
||||
|
||||
if (State.Config == null || string.IsNullOrEmpty(State.Config.Name))
|
||||
{
|
||||
throw new InvalidOperationException("Bot configuration is not properly initialized");
|
||||
}
|
||||
|
||||
// Ensure this is not a backtest configuration
|
||||
if (State.Config.IsForBacktest)
|
||||
{
|
||||
throw new InvalidOperationException("LiveTradingBotGrain cannot be used for backtesting");
|
||||
}
|
||||
|
||||
// Create the TradingBotBase instance using composition
|
||||
_tradingBot = await CreateTradingBotInstance();
|
||||
|
||||
// Load backup if available
|
||||
if (State.User != null)
|
||||
{
|
||||
await LoadBackupFromState();
|
||||
}
|
||||
|
||||
// Start the trading bot
|
||||
_tradingBot.Start();
|
||||
|
||||
// Update state
|
||||
State.Status = BotStatus.Up;
|
||||
State.StartupTime = DateTime.UtcNow;
|
||||
await WriteStateAsync();
|
||||
|
||||
// Start Orleans timer for periodic execution
|
||||
StartTimer();
|
||||
|
||||
_logger.LogInformation("LiveTradingBotGrain {GrainId} started successfully", this.GetPrimaryKey());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to start LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
State.Status = BotStatus.Down;
|
||||
await WriteStateAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Stop the timer
|
||||
_timer?.Dispose();
|
||||
_timer = null;
|
||||
|
||||
// Stop the trading bot
|
||||
if (_tradingBot != null)
|
||||
{
|
||||
_tradingBot.Stop();
|
||||
|
||||
// Save backup before stopping
|
||||
await SaveBackupToState();
|
||||
|
||||
_tradingBot = null;
|
||||
}
|
||||
|
||||
// Update state
|
||||
State.Status = BotStatus.Down;
|
||||
await WriteStateAsync();
|
||||
|
||||
_logger.LogInformation("LiveTradingBotGrain {GrainId} stopped successfully", this.GetPrimaryKey());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to stop LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<BotStatus> GetStatusAsync()
|
||||
{
|
||||
return Task.FromResult(State.Status);
|
||||
}
|
||||
|
||||
public Task<TradingBotConfig> GetConfigurationAsync()
|
||||
{
|
||||
return Task.FromResult(State.Config);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateConfigurationAsync(TradingBotConfig newConfig)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
// Ensure this is not a backtest configuration
|
||||
if (newConfig.IsForBacktest)
|
||||
{
|
||||
throw new InvalidOperationException("LiveTradingBotGrain cannot be used for backtesting");
|
||||
}
|
||||
|
||||
// Update the configuration in the trading bot
|
||||
var success = await _tradingBot.UpdateConfiguration(newConfig);
|
||||
|
||||
if (success)
|
||||
{
|
||||
// Update the state
|
||||
State.Config = newConfig;
|
||||
await WriteStateAsync();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to update configuration for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Position> OpenPositionManuallyAsync(TradeDirection direction)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
return await _tradingBot.OpenPositionManually(direction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to open manual position for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ToggleIsForWatchOnlyAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
await _tradingBot.ToggleIsForWatchOnly();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to toggle watch-only mode for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TradingBotResponse> GetBotDataAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
return new TradingBotResponse
|
||||
{
|
||||
Identifier = State.Identifier,
|
||||
Name = State.Name,
|
||||
Status = State.Status,
|
||||
Config = State.Config,
|
||||
Positions = _tradingBot.Positions,
|
||||
Signals = _tradingBot.Signals.ToList(),
|
||||
WalletBalances = _tradingBot.WalletBalances,
|
||||
ProfitAndLoss = _tradingBot.GetProfitAndLoss(),
|
||||
WinRate = _tradingBot.GetWinRate(),
|
||||
ExecutionCount = _tradingBot.ExecutionCount,
|
||||
StartupTime = State.StartupTime,
|
||||
CreateDate = State.CreateDate
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get bot data for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task LoadBackupAsync(BotBackup backup)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
_tradingBot.LoadBackup(backup);
|
||||
|
||||
// Update state from backup
|
||||
State.User = backup.User;
|
||||
State.Identifier = backup.Identifier;
|
||||
State.Status = backup.LastStatus;
|
||||
State.CreateDate = backup.Data.CreateDate;
|
||||
State.StartupTime = backup.Data.StartupTime;
|
||||
await WriteStateAsync();
|
||||
|
||||
_logger.LogInformation("Backup loaded successfully for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to load backup for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveBackupAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
await _tradingBot.SaveBackup();
|
||||
await SaveBackupToState();
|
||||
|
||||
_logger.LogInformation("Backup saved successfully for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to save backup for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<decimal> GetProfitAndLossAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
return _tradingBot.GetProfitAndLoss();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get P&L for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetWinRateAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot is not running");
|
||||
}
|
||||
|
||||
return _tradingBot.GetWinRate();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get win rate for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task<long> GetExecutionCountAsync()
|
||||
{
|
||||
return Task.FromResult(State.ExecutionCount);
|
||||
}
|
||||
|
||||
public Task<DateTime> GetStartupTimeAsync()
|
||||
{
|
||||
return Task.FromResult(State.StartupTime);
|
||||
}
|
||||
|
||||
public Task<DateTime> GetCreateDateAsync()
|
||||
{
|
||||
return Task.FromResult(State.CreateDate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a TradingBotBase instance using composition
|
||||
/// </summary>
|
||||
private async Task<TradingBotBase> CreateTradingBotInstance()
|
||||
{
|
||||
// Validate configuration for live trading
|
||||
if (State.Config == null)
|
||||
{
|
||||
throw new InvalidOperationException("Bot configuration is not initialized");
|
||||
}
|
||||
|
||||
if (State.Config.IsForBacktest)
|
||||
{
|
||||
throw new InvalidOperationException("LiveTradingBotGrain cannot be used for backtesting");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(State.Config.AccountName))
|
||||
{
|
||||
throw new InvalidOperationException("Account name is required for live trading");
|
||||
}
|
||||
|
||||
// Create the trading bot instance
|
||||
var logger = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ILogger<TradingBotBase>>();
|
||||
var tradingBot = new TradingBotBase(logger, _scopeFactory, State.Config);
|
||||
|
||||
// Set the user if available
|
||||
if (State.User != null)
|
||||
{
|
||||
tradingBot.User = State.User;
|
||||
}
|
||||
|
||||
return tradingBot;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the Orleans timer for periodic bot execution
|
||||
/// </summary>
|
||||
private void StartTimer()
|
||||
{
|
||||
if (_tradingBot == null) return;
|
||||
|
||||
var interval = _tradingBot.Interval;
|
||||
_timer = RegisterTimer(
|
||||
async _ => await ExecuteBotCycle(),
|
||||
null,
|
||||
TimeSpan.FromMilliseconds(interval),
|
||||
TimeSpan.FromMilliseconds(interval));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes one cycle of the trading bot
|
||||
/// </summary>
|
||||
private async Task ExecuteBotCycle()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_tradingBot == null || State.Status != BotStatus.Up)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute the bot's Run method
|
||||
await _tradingBot.Run();
|
||||
|
||||
// Update execution count
|
||||
State.ExecutionCount++;
|
||||
|
||||
await SaveBackupToState();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during bot execution cycle for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current bot state to Orleans state storage
|
||||
/// </summary>
|
||||
private async Task SaveBackupToState()
|
||||
{
|
||||
if (_tradingBot == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
// Sync state from TradingBotBase
|
||||
State.Config = _tradingBot.Config;
|
||||
State.Signals = _tradingBot.Signals;
|
||||
State.Positions = _tradingBot.Positions;
|
||||
State.WalletBalances = _tradingBot.WalletBalances;
|
||||
State.PreloadSince = _tradingBot.PreloadSince;
|
||||
State.PreloadedCandlesCount = _tradingBot.PreloadedCandlesCount;
|
||||
State.Interval = _tradingBot.Interval;
|
||||
State.MaxSignals = _tradingBot._maxSignals;
|
||||
State.LastBackupTime = DateTime.UtcNow;
|
||||
|
||||
await WriteStateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to save state for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads bot state from Orleans state storage
|
||||
/// </summary>
|
||||
private async Task LoadBackupFromState()
|
||||
{
|
||||
if (_tradingBot == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
// Sync state to TradingBotBase
|
||||
_tradingBot.Signals = State.Signals;
|
||||
_tradingBot.Positions = State.Positions;
|
||||
_tradingBot.WalletBalances = State.WalletBalances;
|
||||
_tradingBot.PreloadSince = State.PreloadSince;
|
||||
_tradingBot.PreloadedCandlesCount = State.PreloadedCandlesCount;
|
||||
_tradingBot.Config = State.Config;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to load state for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey());
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_timer?.Dispose();
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,12 @@ namespace Managing.Application.Bots
|
||||
{
|
||||
public class SimpleBot : Bot
|
||||
{
|
||||
public readonly ILogger<TradingBot> Logger;
|
||||
public readonly ILogger<TradingBotBase> Logger;
|
||||
private readonly IBotService _botService;
|
||||
private readonly IBackupBotService _backupBotService;
|
||||
private Workflow _workflow;
|
||||
|
||||
public SimpleBot(string name, ILogger<TradingBot> logger, Workflow workflow, IBotService botService,
|
||||
public SimpleBot(string name, ILogger<TradingBotBase> logger, Workflow workflow, IBotService botService,
|
||||
IBackupBotService backupBotService) :
|
||||
base(name)
|
||||
{
|
||||
|
||||
@@ -22,9 +22,9 @@ using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Bots;
|
||||
|
||||
public class TradingBot : Bot, ITradingBot
|
||||
public class TradingBotBase : Bot, ITradingBot
|
||||
{
|
||||
public readonly ILogger<TradingBot> Logger;
|
||||
public readonly ILogger<TradingBotBase> Logger;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
public TradingBotConfig Config { get; set; }
|
||||
@@ -41,8 +41,8 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
public int _maxSignals = 10; // Maximum number of signals to keep in memory
|
||||
|
||||
public TradingBot(
|
||||
ILogger<TradingBot> logger,
|
||||
public TradingBotBase(
|
||||
ILogger<TradingBotBase> logger,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
TradingBotConfig config
|
||||
)
|
||||
@@ -71,7 +71,9 @@ public class TradingBot : Bot, ITradingBot
|
||||
// Load indicators if scenario is provided in config
|
||||
if (Config.Scenario != null)
|
||||
{
|
||||
LoadIndicators(Config.Scenario);
|
||||
// Convert LightScenario to full Scenario for indicator loading
|
||||
var fullScenario = Config.Scenario.ToScenario();
|
||||
LoadIndicators(fullScenario);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -151,8 +153,6 @@ public class TradingBot : Bot, ITradingBot
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async Task LoadAccount()
|
||||
@@ -185,8 +185,8 @@ public class TradingBot : Bot, ITradingBot
|
||||
}
|
||||
else
|
||||
{
|
||||
// Store the scenario in config and load indicators
|
||||
Config.Scenario = scenario;
|
||||
// Convert full Scenario to LightScenario for storage and load indicators
|
||||
Config.Scenario = LightScenario.FromScenario(scenario);
|
||||
LoadIndicators(ScenarioHelpers.GetIndicatorsFromScenario(scenario));
|
||||
|
||||
Logger.LogInformation($"Loaded scenario '{scenario.Name}' with {Indicators.Count} indicators");
|
||||
@@ -1594,6 +1594,9 @@ public class TradingBot : Bot, ITradingBot
|
||||
|
||||
public override async Task SaveBackup()
|
||||
{
|
||||
if (Config.IsForBacktest)
|
||||
return;
|
||||
|
||||
var data = new TradingBotBackup
|
||||
{
|
||||
Config = Config,
|
||||
@@ -1908,7 +1911,9 @@ public class TradingBot : Bot, ITradingBot
|
||||
{
|
||||
if (newConfig.Scenario != null)
|
||||
{
|
||||
LoadScenario(newConfig.Scenario);
|
||||
// Convert LightScenario to full Scenario for loading
|
||||
var fullScenario = newConfig.Scenario.ToScenario();
|
||||
LoadScenario(fullScenario);
|
||||
|
||||
// Compare indicators after scenario change
|
||||
var newIndicators = Indicators?.ToList() ?? new List<IIndicator>();
|
||||
@@ -2068,14 +2073,14 @@ public class TradingBot : Bot, ITradingBot
|
||||
}
|
||||
|
||||
var isInCooldown = positionClosingDate >= cooldownCandle.Date;
|
||||
|
||||
|
||||
if (isInCooldown)
|
||||
{
|
||||
var intervalMilliseconds = CandleExtensions.GetIntervalFromTimeframe(Config.Timeframe);
|
||||
var intervalMinutes = intervalMilliseconds / (1000.0 * 60.0); // Convert milliseconds to minutes
|
||||
var cooldownEndTime = cooldownCandle.Date.AddMinutes(intervalMinutes * Config.CooldownPeriod);
|
||||
var remainingTime = cooldownEndTime - DateTime.UtcNow;
|
||||
|
||||
|
||||
Logger.LogWarning(
|
||||
$"⏳ **Cooldown Period Active**\n" +
|
||||
$"Cannot open new positions\n" +
|
||||
117
src/Managing.Application/Bots/TradingBotGrainState.cs
Normal file
117
src/Managing.Application/Bots/TradingBotGrainState.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Domain.Users;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Bots;
|
||||
|
||||
/// <summary>
|
||||
/// Orleans grain state for TradingBot.
|
||||
/// This class represents the persistent state of a trading bot grain.
|
||||
/// All properties must be serializable for Orleans state management.
|
||||
/// </summary>
|
||||
[GenerateSerializer]
|
||||
public class TradingBotGrainState
|
||||
{
|
||||
/// <summary>
|
||||
/// The trading bot configuration
|
||||
/// </summary>
|
||||
[Id(0)]
|
||||
public TradingBotConfig Config { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Collection of trading signals generated by the bot
|
||||
/// </summary>
|
||||
[Id(1)]
|
||||
public HashSet<LightSignal> Signals { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// List of trading positions opened by the bot
|
||||
/// </summary>
|
||||
[Id(2)]
|
||||
public List<Position> Positions { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Historical wallet balances tracked over time
|
||||
/// </summary>
|
||||
[Id(3)]
|
||||
public Dictionary<DateTime, decimal> WalletBalances { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Current status of the bot (Running, Stopped, etc.)
|
||||
/// </summary>
|
||||
[Id(4)]
|
||||
public BotStatus Status { get; set; } = BotStatus.Down;
|
||||
|
||||
/// <summary>
|
||||
/// When the bot was started
|
||||
/// </summary>
|
||||
[Id(5)]
|
||||
public DateTime StartupTime { get; set; } = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// When the bot was created
|
||||
/// </summary>
|
||||
[Id(6)]
|
||||
public DateTime CreateDate { get; set; } = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// The user who owns this bot
|
||||
/// </summary>
|
||||
[Id(7)]
|
||||
public User User { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Bot execution counter
|
||||
/// </summary>
|
||||
[Id(8)]
|
||||
public long ExecutionCount { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Bot identifier/name
|
||||
/// </summary>
|
||||
[Id(9)]
|
||||
public string Identifier { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Bot display name
|
||||
/// </summary>
|
||||
[Id(10)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Preload start date for candles
|
||||
/// </summary>
|
||||
[Id(11)]
|
||||
public DateTime PreloadSince { get; set; } = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Number of preloaded candles
|
||||
/// </summary>
|
||||
[Id(12)]
|
||||
public int PreloadedCandlesCount { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Timer interval for bot execution
|
||||
/// </summary>
|
||||
[Id(13)]
|
||||
public int Interval { get; set; } = 60000; // Default 1 minute
|
||||
|
||||
/// <summary>
|
||||
/// Maximum number of signals to keep in memory
|
||||
/// </summary>
|
||||
[Id(14)]
|
||||
public int MaxSignals { get; set; } = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates if the bot has been initialized
|
||||
/// </summary>
|
||||
[Id(15)]
|
||||
public bool IsInitialized { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Last time the bot state was persisted
|
||||
/// </summary>
|
||||
[Id(16)]
|
||||
public DateTime LastBackupTime { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
@@ -776,7 +776,7 @@ public class TradingBotChromosome : ChromosomeBase
|
||||
UseForPositionSizing = false,
|
||||
UseForSignalFiltering = false,
|
||||
UseForDynamicStopLoss = false,
|
||||
Scenario = scenario,
|
||||
Scenario = LightScenario.FromScenario(scenario),
|
||||
MoneyManagement = mm,
|
||||
RiskManagement = new RiskManagement
|
||||
{
|
||||
@@ -915,7 +915,7 @@ public class TradingBotFitness : IFitness
|
||||
var currentGeneration = _geneticAlgorithm?.GenerationsNumber ?? 0;
|
||||
|
||||
// Run backtest using scoped service to avoid DbContext concurrency issues
|
||||
var backtest = ServiceScopeHelpers.WithScopedService<IBacktester, Backtest>(
|
||||
var lightBacktest = ServiceScopeHelpers.WithScopedService<IBacktester, LightBacktest>(
|
||||
_serviceScopeFactory,
|
||||
backtester => backtester.RunTradingBotBacktest(
|
||||
config,
|
||||
@@ -933,7 +933,7 @@ public class TradingBotFitness : IFitness
|
||||
).Result;
|
||||
|
||||
// Calculate multi-objective fitness based on backtest results
|
||||
var fitness = CalculateFitness(backtest, config);
|
||||
var fitness = CalculateFitness(lightBacktest, config);
|
||||
|
||||
return fitness;
|
||||
}
|
||||
@@ -945,13 +945,13 @@ public class TradingBotFitness : IFitness
|
||||
}
|
||||
}
|
||||
|
||||
private double CalculateFitness(Backtest backtest, TradingBotConfig config)
|
||||
private double CalculateFitness(LightBacktest lightBacktest, TradingBotConfig config)
|
||||
{
|
||||
if (backtest == null || backtest.Statistics == null)
|
||||
if (lightBacktest == null)
|
||||
return 0.1;
|
||||
|
||||
// Calculate base fitness from backtest score
|
||||
var baseFitness = backtest.Score;
|
||||
var baseFitness = lightBacktest.Score;
|
||||
|
||||
// Return base fitness (no penalty for now)
|
||||
return baseFitness;
|
||||
|
||||
@@ -4,6 +4,7 @@ using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Bots;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Scenarios;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Domain.Workflows;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -18,20 +19,21 @@ namespace Managing.Application.ManageBot
|
||||
private readonly IExchangeService _exchangeService;
|
||||
private readonly IMessengerService _messengerService;
|
||||
private readonly IAccountService _accountService;
|
||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||
private readonly ILogger<TradingBotBase> _tradingBotLogger;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IMoneyManagementService _moneyManagementService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly IBackupBotService _backupBotService;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly IGrainFactory _grainFactory;
|
||||
|
||||
private ConcurrentDictionary<string, BotTaskWrapper> _botTasks =
|
||||
new ConcurrentDictionary<string, BotTaskWrapper>();
|
||||
|
||||
public BotService(IBotRepository botRepository, IExchangeService exchangeService,
|
||||
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBot> tradingBotLogger,
|
||||
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBotBase> tradingBotLogger,
|
||||
ITradingService tradingService, IMoneyManagementService moneyManagementService, IUserService userService,
|
||||
IBackupBotService backupBotService, IServiceScopeFactory scopeFactory)
|
||||
IBackupBotService backupBotService, IServiceScopeFactory scopeFactory, IGrainFactory grainFactory)
|
||||
{
|
||||
_botRepository = botRepository;
|
||||
_exchangeService = exchangeService;
|
||||
@@ -43,26 +45,26 @@ namespace Managing.Application.ManageBot
|
||||
_userService = userService;
|
||||
_backupBotService = backupBotService;
|
||||
_scopeFactory = scopeFactory;
|
||||
_grainFactory = grainFactory;
|
||||
}
|
||||
|
||||
public class BotTaskWrapper
|
||||
{
|
||||
public Task Task { get; private set; }
|
||||
public Type BotType { get; private set; }
|
||||
public object BotInstance { get; private set; } // Add this line
|
||||
public object BotInstance { get; private set; }
|
||||
|
||||
public BotTaskWrapper(Task task, Type botType, object botInstance) // Update constructor
|
||||
public BotTaskWrapper(Task task, Type botType, object botInstance)
|
||||
{
|
||||
Task = task;
|
||||
BotType = botType;
|
||||
BotInstance = botInstance; // Set the bot instance
|
||||
BotInstance = botInstance;
|
||||
}
|
||||
}
|
||||
|
||||
public void AddSimpleBotToCache(IBot bot)
|
||||
{
|
||||
var botTask =
|
||||
new BotTaskWrapper(Task.Run(() => bot.Start()), bot.GetType(), bot); // Pass bot as the instance
|
||||
var botTask = new BotTaskWrapper(Task.Run(() => bot.Start()), bot.GetType(), bot);
|
||||
_botTasks.AddOrUpdate(bot.Identifier, botTask, (key, existingVal) => botTask);
|
||||
}
|
||||
|
||||
@@ -72,24 +74,34 @@ namespace Managing.Application.ManageBot
|
||||
_botTasks.AddOrUpdate(bot.Identifier, botTask, (key, existingVal) => botTask);
|
||||
}
|
||||
|
||||
|
||||
private async Task InitBot(ITradingBot bot, BotBackup backupBot)
|
||||
{
|
||||
var user = await _userService.GetUser(backupBot.User.Name);
|
||||
bot.User = user;
|
||||
// Config is already set correctly from backup data, so we only need to restore signals, positions, etc.
|
||||
bot.LoadBackup(backupBot);
|
||||
try
|
||||
{
|
||||
var user = await _userService.GetUser(backupBot.User.Name);
|
||||
bot.User = user;
|
||||
|
||||
// Load backup data into the bot
|
||||
bot.LoadBackup(backupBot);
|
||||
|
||||
// Only start the bot if the backup status is Up
|
||||
if (backupBot.LastStatus == BotStatus.Up)
|
||||
{
|
||||
// Start the bot asynchronously without waiting for completion
|
||||
_ = Task.Run(() => bot.Start());
|
||||
// Only start the bot if the backup status is Up
|
||||
if (backupBot.LastStatus == BotStatus.Up)
|
||||
{
|
||||
// Start the bot asynchronously without waiting for completion
|
||||
_ = Task.Run(() => bot.Start());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Keep the bot in Down status if it was originally Down
|
||||
bot.Stop();
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Keep the bot in Down status if it was originally Down
|
||||
_tradingBotLogger.LogError(ex, "Error initializing bot {Identifier} from backup", backupBot.Identifier);
|
||||
// Ensure the bot is stopped if initialization fails
|
||||
bot.Stop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +149,7 @@ namespace Managing.Application.ManageBot
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(scalpingConfig.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
scalpingConfig.Scenario = scenario;
|
||||
scalpingConfig.Scenario = LightScenario.FromScenario(scenario);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -155,6 +167,10 @@ namespace Managing.Application.ManageBot
|
||||
// Ensure critical properties are set correctly for restored bots
|
||||
scalpingConfig.IsForBacktest = false;
|
||||
|
||||
// IMPORTANT: Save the backup to database BEFORE creating the Orleans grain
|
||||
// This ensures the backup exists when the grain tries to serialize it
|
||||
await SaveOrUpdateBotBackup(backupBot.User, backupBot.Identifier, backupBot.LastStatus, backupBot.Data);
|
||||
|
||||
bot = await CreateTradingBot(scalpingConfig);
|
||||
botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
|
||||
|
||||
@@ -206,7 +222,7 @@ namespace Managing.Application.ManageBot
|
||||
if (botWrapper.BotInstance is IBot bot)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
bot.Stop()); // Assuming Stop is an asynchronous process wrapped in Task.Run for synchronous methods
|
||||
bot.Stop());
|
||||
|
||||
var stopMessage = $"🛑 **Bot Stopped**\n\n" +
|
||||
$"🎯 **Agent:** {bot.User.AgentName}\n" +
|
||||
@@ -231,7 +247,7 @@ namespace Managing.Application.ManageBot
|
||||
if (botWrapper.BotInstance is IBot bot)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
bot.Stop()); // Assuming Stop is an asynchronous process wrapped in Task.Run for synchronous methods
|
||||
bot.Stop());
|
||||
|
||||
var deleteMessage = $"🗑️ **Bot Deleted**\n\n" +
|
||||
$"🎯 **Agent:** {bot.User.AgentName}\n" +
|
||||
@@ -306,7 +322,7 @@ namespace Managing.Application.ManageBot
|
||||
public async Task<bool> UpdateBotConfiguration(string identifier, TradingBotConfig newConfig)
|
||||
{
|
||||
if (_botTasks.TryGetValue(identifier, out var botTaskWrapper) &&
|
||||
botTaskWrapper.BotInstance is TradingBot tradingBot)
|
||||
botTaskWrapper.BotInstance is TradingBotBase tradingBot)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
if (newConfig.Scenario == null && !string.IsNullOrEmpty(newConfig.ScenarioName))
|
||||
@@ -314,7 +330,7 @@ namespace Managing.Application.ManageBot
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(newConfig.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
newConfig.Scenario = scenario;
|
||||
newConfig.Scenario = LightScenario.FromScenario(scenario);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -370,7 +386,6 @@ namespace Managing.Application.ManageBot
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public async Task<ITradingBot> CreateTradingBot(TradingBotConfig config)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
@@ -379,7 +394,7 @@ namespace Managing.Application.ManageBot
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
config.Scenario = LightScenario.FromScenario(scenario);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -392,7 +407,15 @@ namespace Managing.Application.ManageBot
|
||||
throw new ArgumentException("Scenario object must be provided or ScenarioName must be valid");
|
||||
}
|
||||
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
// For now, use TradingBot for both live trading and backtesting
|
||||
// TODO: Implement Orleans grain for live trading when ready
|
||||
if (!config.IsForBacktest)
|
||||
{
|
||||
// Ensure critical properties are set correctly for live trading
|
||||
config.IsForBacktest = false;
|
||||
}
|
||||
|
||||
return new TradingBotBase(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
|
||||
public async Task<ITradingBot> CreateBacktestTradingBot(TradingBotConfig config)
|
||||
@@ -403,7 +426,7 @@ namespace Managing.Application.ManageBot
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
config.Scenario = LightScenario.FromScenario(scenario);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -417,109 +440,7 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
|
||||
config.IsForBacktest = true;
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
|
||||
public async Task<ITradingBot> CreateScalpingBot(TradingBotConfig config)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
if (config.Scenario == null && !string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Scenario '{config.ScenarioName}' not found in database");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.Scenario == null)
|
||||
{
|
||||
throw new ArgumentException("Scenario object must be provided or ScenarioName must be valid");
|
||||
}
|
||||
|
||||
config.FlipPosition = false;
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
|
||||
public async Task<ITradingBot> CreateBacktestScalpingBot(TradingBotConfig config)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
if (config.Scenario == null && !string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Scenario '{config.ScenarioName}' not found in database");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.Scenario == null)
|
||||
{
|
||||
throw new ArgumentException("Scenario object must be provided or ScenarioName must be valid");
|
||||
}
|
||||
|
||||
config.IsForBacktest = true;
|
||||
config.FlipPosition = false;
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
|
||||
public async Task<ITradingBot> CreateFlippingBot(TradingBotConfig config)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
if (config.Scenario == null && !string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Scenario '{config.ScenarioName}' not found in database");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.Scenario == null)
|
||||
{
|
||||
throw new ArgumentException("Scenario object must be provided or ScenarioName must be valid");
|
||||
}
|
||||
|
||||
config.FlipPosition = true;
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
|
||||
public async Task<ITradingBot> CreateBacktestFlippingBot(TradingBotConfig config)
|
||||
{
|
||||
// Ensure the scenario is properly loaded from database if needed
|
||||
if (config.Scenario == null && !string.IsNullOrEmpty(config.ScenarioName))
|
||||
{
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(config.ScenarioName);
|
||||
if (scenario != null)
|
||||
{
|
||||
config.Scenario = scenario;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Scenario '{config.ScenarioName}' not found in database");
|
||||
}
|
||||
}
|
||||
|
||||
if (config.Scenario == null)
|
||||
{
|
||||
throw new ArgumentException("Scenario object must be provided or ScenarioName must be valid");
|
||||
}
|
||||
|
||||
config.IsForBacktest = true;
|
||||
config.FlipPosition = true;
|
||||
return new TradingBot(_tradingBotLogger, _scopeFactory, config);
|
||||
return new TradingBotBase(_tradingBotLogger, _scopeFactory, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
|
||||
// Try to get the active bot multiple times to ensure it's properly started
|
||||
int attempts = 0;
|
||||
const int maxAttempts = 5;
|
||||
const int maxAttempts = 2;
|
||||
|
||||
while (attempts < maxAttempts)
|
||||
{
|
||||
@@ -58,7 +58,8 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
if (backupBot.LastStatus == BotStatus.Down)
|
||||
{
|
||||
result[activeBot.Identifier] = BotStatus.Down;
|
||||
_logger.LogInformation("Backup bot {Identifier} loaded but kept in Down status as it was originally Down.",
|
||||
_logger.LogInformation(
|
||||
"Backup bot {Identifier} loaded but kept in Down status as it was originally Down.",
|
||||
backupBot.Identifier);
|
||||
}
|
||||
else
|
||||
@@ -68,6 +69,7 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
_logger.LogInformation("Backup bot {Identifier} started successfully.",
|
||||
backupBot.Identifier);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,29 +7,31 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="MoneyManagements\Abstractions\**"/>
|
||||
<EmbeddedResource Remove="MoneyManagements\Abstractions\**"/>
|
||||
<None Remove="MoneyManagements\Abstractions\**"/>
|
||||
<Compile Remove="MoneyManagements\Abstractions\**" />
|
||||
<EmbeddedResource Remove="MoneyManagements\Abstractions\**" />
|
||||
<None Remove="MoneyManagements\Abstractions\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.9.1"/>
|
||||
<PackageReference Include="GeneticSharp" Version="3.1.4"/>
|
||||
<PackageReference Include="MediatR" Version="12.2.0"/>
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1"/>
|
||||
<PackageReference Include="Polly" Version="8.4.0"/>
|
||||
<PackageReference Include="Skender.Stock.Indicators" Version="2.5.0"/>
|
||||
<PackageReference Include="FluentValidation" Version="11.9.1" />
|
||||
<PackageReference Include="GeneticSharp" Version="3.1.4" />
|
||||
<PackageReference Include="MediatR" Version="12.2.0" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Orleans.Client" Version="9.2.1" />
|
||||
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="9.2.1" />
|
||||
<PackageReference Include="Polly" Version="8.4.0" />
|
||||
<PackageReference Include="Skender.Stock.Indicators" Version="2.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Managing.Application.Abstractions\Managing.Application.Abstractions.csproj"/>
|
||||
<ProjectReference Include="..\Managing.Common\Managing.Common.csproj"/>
|
||||
<ProjectReference Include="..\Managing.Domain\Managing.Domain.csproj"/>
|
||||
<ProjectReference Include="..\Managing.Infrastructure.Database\Managing.Infrastructure.Databases.csproj"/>
|
||||
<ProjectReference Include="..\Managing.Application.Abstractions\Managing.Application.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\Managing.Common\Managing.Common.csproj" />
|
||||
<ProjectReference Include="..\Managing.Domain\Managing.Domain.csproj" />
|
||||
<ProjectReference Include="..\Managing.Infrastructure.Database\Managing.Infrastructure.Databases.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -302,5 +302,16 @@ namespace Managing.Application.Scenarios
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Scenario> GetScenarioByNameAndUserAsync(string scenarioName, User user)
|
||||
{
|
||||
var scenario = await _tradingService.GetScenarioByNameAsync(scenarioName);
|
||||
if (scenario == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Scenario {scenarioName} not found for user {user.Name}");
|
||||
}
|
||||
|
||||
return scenario;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user