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

@@ -4,7 +4,6 @@ using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Bots;
using Managing.Domain.Bots;
using Managing.Domain.MoneyManagements;
using Managing.Domain.Users;
using Managing.Domain.Workflows;
using Microsoft.Extensions.Logging;
@@ -121,39 +120,61 @@ namespace Managing.Application.ManageBot
switch (backupBot.BotType)
{
// case Enums.BotType.SimpleBot:
// bot = CreateSimpleBot(backupBot.Name,
// null); // Assuming null is an acceptable parameter for workflow
// botTask = Task.Run(() => ((IBot)bot).Start());
// break;
case BotType.ScalpingBot:
var scalpingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
var scalpingMoneyManagement =
_moneyManagementService.GetMoneyMangement(scalpingBotData.MoneyManagement.Name).Result;
bot = CreateScalpingBot(
scalpingBotData.AccountName,
scalpingMoneyManagement,
scalpingBotData.Name,
scalpingBotData.Ticker,
scalpingBotData.ScenarioName,
scalpingBotData.Timeframe,
scalpingBotData.IsForWatchingOnly,
scalpingBotData.BotTradingBalance);
// Create config from backup data
var scalpingConfig = new TradingBotConfig
{
AccountName = scalpingBotData.AccountName,
MoneyManagement = scalpingMoneyManagement,
Ticker = scalpingBotData.Ticker,
ScenarioName = scalpingBotData.ScenarioName,
Timeframe = scalpingBotData.Timeframe,
IsForWatchingOnly = scalpingBotData.IsForWatchingOnly,
BotTradingBalance = scalpingBotData.BotTradingBalance,
BotType = scalpingBotData.BotType,
Name = scalpingBotData.Name,
CooldownPeriod = scalpingBotData.CooldownPeriod,
MaxLossStreak = scalpingBotData.MaxLossStreak,
MaxPositionTimeHours = scalpingBotData.MaxPositionTimeHours == 0m ? null : scalpingBotData.MaxPositionTimeHours,
FlipOnlyWhenInProfit = scalpingBotData.FlipOnlyWhenInProfit,
IsForBacktest = false,
FlipPosition = false
};
bot = CreateScalpingBot(scalpingConfig);
botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
break;
case BotType.FlippingBot:
var flippingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
var flippingMoneyManagement =
_moneyManagementService.GetMoneyMangement(flippingBotData.MoneyManagement.Name).Result;
bot = CreateFlippingBot(
flippingBotData.AccountName,
flippingMoneyManagement,
flippingBotData.Name,
flippingBotData.Ticker,
flippingBotData.ScenarioName,
flippingBotData.Timeframe,
flippingBotData.IsForWatchingOnly,
flippingBotData.BotTradingBalance);
// Create config from backup data
var flippingConfig = new TradingBotConfig
{
AccountName = flippingBotData.AccountName,
MoneyManagement = flippingMoneyManagement,
Ticker = flippingBotData.Ticker,
ScenarioName = flippingBotData.ScenarioName,
Timeframe = flippingBotData.Timeframe,
IsForWatchingOnly = flippingBotData.IsForWatchingOnly,
BotTradingBalance = flippingBotData.BotTradingBalance,
BotType = flippingBotData.BotType,
Name = flippingBotData.Name,
CooldownPeriod = flippingBotData.CooldownPeriod,
MaxLossStreak = flippingBotData.MaxLossStreak,
MaxPositionTimeHours = flippingBotData.MaxPositionTimeHours == 0m ? null : flippingBotData.MaxPositionTimeHours,
FlipOnlyWhenInProfit = flippingBotData.FlipOnlyWhenInProfit,
IsForBacktest = false,
FlipPosition = true
};
bot = CreateFlippingBot(flippingConfig);
botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
break;
}
@@ -168,7 +189,8 @@ namespace Managing.Application.ManageBot
private void InitBot(ITradingBot bot, BotBackup backupBot)
{
var user = _userService.GetUser(backupBot.User.Name);
backupBot.User = user;
bot.User = user;
// Config is already set correctly from backup data, so we only need to restore signals, positions, etc.
bot.LoadBackup(backupBot);
bot.Start();
}
@@ -241,113 +263,6 @@ namespace Managing.Application.ManageBot
}
}
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly,
decimal initialTradingBalance)
{
var config = new TradingBotConfig
{
AccountName = accountName,
MoneyManagement = moneyManagement,
Ticker = ticker,
ScenarioName = scenario,
Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance,
BotType = BotType.ScalpingBot,
Name = name
};
return new ScalpingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
this,
config);
}
public ITradingBot CreateBacktestScalpingBot(string accountName, MoneyManagement moneyManagement,
Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly,
decimal initialTradingBalance)
{
var config = new TradingBotConfig
{
AccountName = accountName,
MoneyManagement = moneyManagement,
Ticker = ticker,
ScenarioName = scenario,
Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance,
BotType = BotType.ScalpingBot,
IsForBacktest = true
};
return new ScalpingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
this,
config);
}
public ITradingBot CreateFlippingBot(string accountName, MoneyManagement moneyManagement, string name,
Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly,
decimal initialTradingBalance)
{
var config = new TradingBotConfig
{
AccountName = accountName,
MoneyManagement = moneyManagement,
Ticker = ticker,
ScenarioName = scenario,
Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance,
BotType = BotType.FlippingBot
};
return new FlippingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
this,
config);
}
public ITradingBot CreateBacktestFlippingBot(string accountName, MoneyManagement moneyManagement,
Ticker ticker, string scenario, Timeframe interval, bool isForWatchingOnly,
decimal initialTradingBalance)
{
var config = new TradingBotConfig
{
AccountName = accountName,
MoneyManagement = moneyManagement,
Ticker = ticker,
ScenarioName = scenario,
Timeframe = interval,
IsForWatchingOnly = isForWatchingOnly,
BotTradingBalance = initialTradingBalance,
BotType = BotType.FlippingBot,
IsForBacktest = true
};
return new FlippingBot(
_exchangeService,
_tradingBotLogger,
_tradingService,
_accountService,
_messengerService,
this,
config);
}
public ITradingBot CreateScalpingBot(TradingBotConfig config)
{
return new ScalpingBot(

View File

@@ -0,0 +1,20 @@
using Managing.Domain.Bots;
using MediatR;
namespace Managing.Application.ManageBot.Commands
{
/// <summary>
/// Command to update the configuration of a running trading bot
/// </summary>
public class UpdateBotConfigCommand : IRequest<string>
{
public string Identifier { get; }
public TradingBotConfig NewConfig { get; }
public UpdateBotConfigCommand(string identifier, TradingBotConfig newConfig)
{
Identifier = identifier;
NewConfig = newConfig;
}
}
}

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
}
}
}
}

View File

@@ -0,0 +1,60 @@
using Managing.Application.Abstractions;
using Managing.Application.ManageBot.Commands;
using MediatR;
namespace Managing.Application.ManageBot
{
/// <summary>
/// Handler for updating trading bot configurations
/// </summary>
public class UpdateBotConfigCommandHandler : IRequestHandler<UpdateBotConfigCommand, string>
{
private readonly IBotService _botService;
public UpdateBotConfigCommandHandler(IBotService botService)
{
_botService = botService;
}
public async Task<string> Handle(UpdateBotConfigCommand request, CancellationToken cancellationToken)
{
try
{
if (string.IsNullOrEmpty(request.Identifier))
{
throw new ArgumentException("Bot identifier is required");
}
if (request.NewConfig == null)
{
throw new ArgumentException("New configuration is required");
}
// Get the bot from active bots
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Identifier == request.Identifier);
if (bot == null)
{
return $"Bot with identifier {request.Identifier} not found or is not running";
}
// Update the bot configuration
var updateResult = await bot.UpdateConfiguration(request.NewConfig);
if (updateResult)
{
return $"Bot configuration updated successfully for {request.Identifier}";
}
else
{
return $"Failed to update bot configuration for {request.Identifier}";
}
}
catch (Exception ex)
{
return $"Error updating bot configuration: {ex.Message}";
}
}
}
}