Fixes for bots running (#22)

* Fixes for bots running

* Up botmanager

* Add cooldown

* Refact can open position

* Add cooldown Period and MaxLossStreak

* Add agentName

* Add env variable for botManager

* Always enable Botmanager

* Fix bot handle

* Fix get positions

* Add Ticker url

* Dont start stopped bot

* fix
This commit is contained in:
Oda
2025-05-09 17:40:31 +02:00
committed by GitHub
parent a8eb0aaf02
commit 7c38c27b4a
54 changed files with 5164 additions and 641 deletions

View File

@@ -1,6 +1,6 @@
using Managing.Api.Models.Requests;
using Managing.Application.Abstractions;
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.Bots;
using Managing.Application.Hubs;
using Managing.Application.ManageBot.Commands;
using Managing.Common;
@@ -56,10 +56,10 @@ public class BotController : BaseController
/// <summary>
/// Checks if the current authenticated user owns the account associated with the specified bot or account name
/// </summary>
/// <param name="botName">The name of the bot to check</param>
/// <param name="identifier">The identifier of the bot to check</param>
/// <param name="accountName">Optional account name to check when creating a new bot</param>
/// <returns>True if the user owns the account, False otherwise</returns>
private async Task<bool> UserOwnsBotAccount(string botName, string accountName = null)
private async Task<bool> UserOwnsBotAccount(string identifier, string accountName = null)
{
try
{
@@ -78,12 +78,12 @@ public class BotController : BaseController
// For existing bots, check if the user owns the bot's account
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Name == botName);
var bot = activeBots.FirstOrDefault(b => b.Identifier == identifier);
if (bot == null)
return true; // Bot doesn't exist yet, so no ownership conflict
var botAccountService = HttpContext.RequestServices.GetRequiredService<IAccountService>();
var botAccount = await botAccountService.GetAccount(bot.AccountName, true, false);
var botAccount = await botAccountService.GetAccount(bot.Config.AccountName, true, false);
// Compare the user names
return botAccount != null && botAccount.User != null && botAccount.User.Name == user.Name;
}
@@ -106,7 +106,7 @@ public class BotController : BaseController
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(request.BotName, request.AccountName))
if (!await UserOwnsBotAccount(request.Identifier, request.AccountName))
{
return Forbid("You don't have permission to start this bot");
}
@@ -131,9 +131,19 @@ public class BotController : BaseController
$"Initial trading balance must be greater than {Constants.GMX.Config.MinimumPositionAmount}");
}
var result = await _mediator.Send(new StartBotCommand(request.BotType, request.BotName, request.Ticker,
request.Scenario, request.Timeframe, request.AccountName, request.MoneyManagementName, user,
request.IsForWatchOnly, request.InitialTradingBalance));
var config = new TradingBotConfig
{
AccountName = request.AccountName,
MoneyManagement = moneyManagement,
Ticker = request.Ticker,
ScenarioName = request.Scenario,
Timeframe = request.Timeframe,
IsForWatchingOnly = request.IsForWatchOnly,
BotTradingBalance = request.InitialTradingBalance,
BotType = request.BotType
};
var result = await _mediator.Send(new StartBotCommand(config, request.Identifier, user));
await NotifyBotSubscriberAsync();
return Ok(result);
@@ -149,22 +159,22 @@ public class BotController : BaseController
/// Stops a bot specified by type and name.
/// </summary>
/// <param name="botType">The type of the bot to stop.</param>
/// <param name="botName">The name of the bot to stop.</param>
/// <param name="identifier">The identifier of the bot to stop.</param>
/// <returns>A string indicating the result of the stop operation.</returns>
[HttpGet]
[Route("Stop")]
public async Task<ActionResult<string>> Stop(BotType botType, string botName)
public async Task<ActionResult<string>> Stop(BotType botType, string identifier)
{
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(botName))
if (!await UserOwnsBotAccount(identifier))
{
return Forbid("You don't have permission to stop this bot");
}
var result = await _mediator.Send(new StopBotCommand(botType, botName));
_logger.LogInformation($"{botType} type called {botName} is now {result}");
var result = await _mediator.Send(new StopBotCommand(botType, identifier));
_logger.LogInformation($"{botType} type with identifier {identifier} is now {result}");
await NotifyBotSubscriberAsync();
@@ -180,21 +190,21 @@ public class BotController : BaseController
/// <summary>
/// Deletes a bot specified by name.
/// </summary>
/// <param name="botName">The name of the bot to delete.</param>
/// <param name="identifier">The identifier of the bot to delete.</param>
/// <returns>A boolean indicating the result of the delete operation.</returns>
[HttpDelete]
[Route("Delete")]
public async Task<ActionResult<bool>> Delete(string botName)
public async Task<ActionResult<bool>> Delete(string identifier)
{
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(botName))
if (!await UserOwnsBotAccount(identifier))
{
return Forbid("You don't have permission to delete this bot");
}
var result = await _botService.DeleteBot(botName);
var result = await _botService.DeleteBot(identifier);
await NotifyBotSubscriberAsync();
return Ok(result);
}
@@ -235,9 +245,9 @@ public class BotController : BaseController
foreach (var bot in userBots)
{
await _mediator.Send(new StopBotCommand(bot.BotType, bot.Name));
await _mediator.Send(new StopBotCommand(bot.BotType, bot.Identifier));
await _hubContext.Clients.All.SendAsync("SendNotification",
$"Bot {bot.Name} paused by {user.Name}.", "Info");
$"Bot {bot.Identifier} paused by {user.Name}.", "Info");
}
await NotifyBotSubscriberAsync();
@@ -254,22 +264,22 @@ public class BotController : BaseController
/// Restarts a bot specified by type and name.
/// </summary>
/// <param name="botType">The type of the bot to restart.</param>
/// <param name="botName">The name of the bot to restart.</param>
/// <param name="identifier">The identifier of the bot to restart.</param>
/// <returns>A string indicating the result of the restart operation.</returns>
[HttpGet]
[Route("Restart")]
public async Task<ActionResult<string>> Restart(BotType botType, string botName)
public async Task<ActionResult<string>> Restart(BotType botType, string identifier)
{
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(botName))
if (!await UserOwnsBotAccount(identifier))
{
return Forbid("You don't have permission to restart this bot");
}
var result = await _mediator.Send(new RestartBotCommand(botType, botName));
_logger.LogInformation($"{botType} type called {botName} is now {result}");
var result = await _mediator.Send(new RestartBotCommand(botType, identifier));
_logger.LogInformation($"{botType} type with identifier {identifier} is now {result}");
await NotifyBotSubscriberAsync();
@@ -314,15 +324,15 @@ public class BotController : BaseController
{
// We can't directly restart a bot with just BotType and Name
// Instead, stop the bot and then retrieve the backup to start it again
await _mediator.Send(new StopBotCommand(bot.BotType, bot.Name));
await _mediator.Send(new StopBotCommand(bot.BotType, bot.Identifier));
// Get the saved bot backup
var backup = _botService.GetBotBackup(bot.Name);
var backup = _botService.GetBotBackup(bot.Identifier);
if (backup != null)
{
_botService.StartBotFromBackup(backup);
await _hubContext.Clients.All.SendAsync("SendNotification",
$"Bot {bot.Name} restarted by {user.Name}.", "Info");
$"Bot {bot.Identifier} restarted by {user.Name}.", "Info");
}
}
@@ -339,22 +349,22 @@ public class BotController : BaseController
/// <summary>
/// Toggles the watching status of a bot specified by name.
/// </summary>
/// <param name="botName">The name of the bot to toggle watching status.</param>
/// <param name="identifier">The identifier of the bot to toggle watching status.</param>
/// <returns>A string indicating the new watching status of the bot.</returns>
[HttpGet]
[Route("ToggleIsForWatching")]
public async Task<ActionResult<string>> ToggleIsForWatching(string botName)
public async Task<ActionResult<string>> ToggleIsForWatching(string identifier)
{
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(botName))
if (!await UserOwnsBotAccount(identifier))
{
return Forbid("You don't have permission to modify this bot");
}
var result = await _mediator.Send(new ToggleIsForWatchingCommand(botName));
_logger.LogInformation($"{botName} bot is now {result}");
var result = await _mediator.Send(new ToggleIsForWatchingCommand(identifier));
_logger.LogInformation($"Bot with identifier {identifier} is now {result}");
await NotifyBotSubscriberAsync();
@@ -397,13 +407,15 @@ public class BotController : BaseController
Candles = item.Candles.DistinctBy(c => c.Date).ToList(),
WinRate = item.GetWinRate(),
ProfitAndLoss = item.GetProfitAndLoss(),
Timeframe = item.Timeframe,
Ticker = item.Ticker,
Scenario = item.ScenarioName,
IsForWatchingOnly = item.IsForWatchingOnly,
BotType = item.BotType,
AccountName = item.AccountName,
MoneyManagement = item.MoneyManagement
Timeframe = item.Config.Timeframe,
Ticker = item.Config.Ticker,
Scenario = item.Config.ScenarioName,
IsForWatchingOnly = item.Config.IsForWatchingOnly,
BotType = item.Config.BotType,
AccountName = item.Config.AccountName,
MoneyManagement = item.Config.MoneyManagement,
Identifier = item.Identifier,
AgentName = item.User.AgentName
});
}
@@ -431,22 +443,22 @@ public class BotController : BaseController
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(request.BotName))
if (!await UserOwnsBotAccount(request.Identifier))
{
return Forbid("You don't have permission to open positions for this bot");
}
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Name == request.BotName) as ApplicationTradingBot;
var bot = activeBots.FirstOrDefault(b => b.Identifier == request.Identifier) as ApplicationTradingBot;
if (bot == null)
{
return NotFound($"Bot {request.BotName} not found or is not a trading bot");
return NotFound($"Bot with identifier {request.Identifier} not found or is not a trading bot");
}
if (bot.GetStatus() != BotStatus.Up.ToString())
{
return BadRequest($"Bot {request.BotName} is not running");
return BadRequest($"Bot with identifier {request.Identifier} is not running");
}
var position = await bot.OpenPositionManually(
@@ -475,29 +487,30 @@ public class BotController : BaseController
try
{
// Check if user owns the account
if (!await UserOwnsBotAccount(request.BotName))
if (!await UserOwnsBotAccount(request.Identifier))
{
return Forbid("You don't have permission to close positions for this bot");
}
var activeBots = _botService.GetActiveBots();
var bot = activeBots.FirstOrDefault(b => b.Name == request.BotName) as ApplicationTradingBot;
var bot = activeBots.FirstOrDefault(b => b.Identifier == request.Identifier) as ApplicationTradingBot;
if (bot == null)
{
return NotFound($"Bot {request.BotName} not found or is not a trading bot");
return NotFound($"Bot with identifier {request.Identifier} not found or is not a trading bot");
}
if (bot.GetStatus() != BotStatus.Up.ToString())
{
return BadRequest($"Bot {request.BotName} is not running");
return BadRequest($"Bot with identifier {request.Identifier} is not running");
}
// Find the position to close
var position = bot.Positions.FirstOrDefault(p => p.Identifier == request.PositionId);
if (position == null)
{
return NotFound($"Position with ID {request.PositionId} not found for bot {request.BotName}");
return NotFound(
$"Position with ID {request.PositionId} not found for bot with identifier {request.Identifier}");
}
// Find the signal associated with this position
@@ -528,18 +541,85 @@ public class BotController : BaseController
}
}
/// <summary>
/// Request model for opening a position manually
/// </summary>
public class OpenPositionManuallyRequest
{
/// <summary>
/// The identifier of the bot
/// </summary>
public string Identifier { get; set; }
/// <summary>
/// The direction of the position
/// </summary>
public TradeDirection Direction { get; set; }
}
/// <summary>
/// Request model for closing a position
/// </summary>
public class ClosePositionRequest
{
/// <summary>
/// The name of the bot
/// The identifier of the bot
/// </summary>
public string BotName { get; set; }
public string Identifier { get; set; }
/// <summary>
/// The ID of the position to close
/// </summary>
public string PositionId { get; set; }
}
/// <summary>
/// Request model for starting a bot
/// </summary>
public class StartBotRequest
{
/// <summary>
/// The type of bot to start
/// </summary>
public BotType BotType { get; set; }
/// <summary>
/// The identifier of the bot
/// </summary>
public string Identifier { get; set; }
/// <summary>
/// The ticker to trade
/// </summary>
public Ticker Ticker { get; set; }
/// <summary>
/// The scenario to use
/// </summary>
public string Scenario { get; set; }
/// <summary>
/// The timeframe to use
/// </summary>
public Timeframe Timeframe { get; set; }
/// <summary>
/// The account name to use
/// </summary>
public string AccountName { get; set; }
/// <summary>
/// The money management name to use
/// </summary>
public string MoneyManagementName { get; set; }
/// <summary>
/// Whether the bot is for watching only
/// </summary>
public bool IsForWatchOnly { get; set; }
/// <summary>
/// The initial trading balance
/// </summary>
public decimal InitialTradingBalance { get; set; }
}