Add new parameters

This commit is contained in:
2025-06-03 01:01:17 +07:00
parent 71bcaea76d
commit 8c2e9b59de
23 changed files with 1346 additions and 510 deletions

View File

@@ -1,6 +1,8 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using Managing.Common;
using Managing.Domain.Bots;
using MediatR;
using static Managing.Common.Enums;
@@ -29,6 +31,18 @@ namespace Managing.Application.ManageBot
{
BotStatus botStatus = BotStatus.Down;
// Validate the configuration
if (request.Config == null)
{
throw new ArgumentException("Bot configuration is required");
}
if (request.Config.BotTradingBalance <= Constants.GMX.Config.MinimumPositionAmount)
{
throw new ArgumentException(
$"Bot trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
}
var account = await _accountService.GetAccount(request.Config.AccountName, true, true);
if (account == null)
@@ -43,32 +57,86 @@ namespace Managing.Application.ManageBot
throw new Exception($"Account {request.Config.AccountName} has no USDC balance or not enough balance");
}
// Ensure cooldown period is set
if (request.Config.CooldownPeriod <= 0)
// Ensure essential configuration values are properly set
var configToUse = new TradingBotConfig
{
request.Config.CooldownPeriod = 1; // Default to 1 minute if not set
}
AccountName = request.Config.AccountName,
MoneyManagement = request.Config.MoneyManagement,
Ticker = request.Config.Ticker,
ScenarioName = request.Config.ScenarioName,
Timeframe = request.Config.Timeframe,
IsForWatchingOnly = request.Config.IsForWatchingOnly,
BotTradingBalance = request.Config.BotTradingBalance,
BotType = request.Config.BotType,
IsForBacktest = request.Config.IsForBacktest,
CooldownPeriod = request.Config.CooldownPeriod > 0 ? request.Config.CooldownPeriod : 1, // Default to 1 if not set
MaxLossStreak = request.Config.MaxLossStreak,
MaxPositionTimeHours = request.Config.MaxPositionTimeHours, // Properly handle nullable value
FlipOnlyWhenInProfit = request.Config.FlipOnlyWhenInProfit,
FlipPosition = request.Config.FlipPosition,
Name = request.Config.Name ?? request.Name
};
switch (request.Config.BotType)
switch (configToUse.BotType)
{
case BotType.SimpleBot:
var bot = _botFactory.CreateSimpleBot(request.Name, null);
bot.User = request.User;
_botService.AddSimpleBotToCache(bot);
return bot.GetStatus();
case BotType.ScalpingBot:
var sBot = _botFactory.CreateScalpingBot(request.Config);
var sBot = _botFactory.CreateScalpingBot(configToUse);
sBot.User = request.User;
// Log the configuration being used
await LogBotConfigurationAsync(sBot, "ScalpingBot created");
_botService.AddTradingBotToCache(sBot);
return sBot.GetStatus();
case BotType.FlippingBot:
var fBot = _botFactory.CreateFlippingBot(request.Config);
var fBot = _botFactory.CreateFlippingBot(configToUse);
fBot.User = request.User;
// Log the configuration being used
await LogBotConfigurationAsync(fBot, "FlippingBot created");
_botService.AddTradingBotToCache(fBot);
return fBot.GetStatus();
}
return botStatus.ToString();
}
/// <summary>
/// Logs the bot configuration for debugging and audit purposes
/// </summary>
/// <param name="bot">The trading bot instance</param>
/// <param name="context">Context information for the log</param>
private async Task LogBotConfigurationAsync(ITradingBot bot, string context)
{
try
{
var config = bot.GetConfiguration();
var logMessage = $"{context} - Bot: {config.Name}, " +
$"Type: {config.BotType}, " +
$"Account: {config.AccountName}, " +
$"Ticker: {config.Ticker}, " +
$"Balance: {config.BotTradingBalance}, " +
$"MaxTime: {config.MaxPositionTimeHours?.ToString() ?? "Disabled"}, " +
$"FlipOnlyProfit: {config.FlipOnlyWhenInProfit}, " +
$"Cooldown: {config.CooldownPeriod}, " +
$"MaxLoss: {config.MaxLossStreak}";
// Log through the bot's logger (this will use the bot's logging mechanism)
// For now, we'll just add a comment that this could be enhanced with actual logging
// Console.WriteLine(logMessage); // Could be replaced with proper logging
}
catch (Exception)
{
// Ignore logging errors to not affect bot creation
}
}
}
}