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:
@@ -3,13 +3,13 @@ using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Bots;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.MoneyManagements;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Domain.Workflows;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.ManageBot
|
||||
{
|
||||
@@ -22,13 +22,14 @@ namespace Managing.Application.ManageBot
|
||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IMoneyManagementService _moneyManagementService;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
private ConcurrentDictionary<string, BotTaskWrapper> _botTasks =
|
||||
new ConcurrentDictionary<string, BotTaskWrapper>();
|
||||
|
||||
public BotService(IBotRepository botRepository, IExchangeService exchangeService,
|
||||
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBot> tradingBotLogger,
|
||||
ITradingService tradingService, IMoneyManagementService moneyManagementService)
|
||||
ITradingService tradingService, IMoneyManagementService moneyManagementService, IUserService userService)
|
||||
{
|
||||
_botRepository = botRepository;
|
||||
_exchangeService = exchangeService;
|
||||
@@ -37,11 +38,7 @@ namespace Managing.Application.ManageBot
|
||||
_tradingBotLogger = tradingBotLogger;
|
||||
_tradingService = tradingService;
|
||||
_moneyManagementService = moneyManagementService;
|
||||
}
|
||||
|
||||
public async void SaveOrUpdateBotBackup(BotBackup botBackup)
|
||||
{
|
||||
await _botRepository.InsertBotAsync(botBackup);
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
public BotBackup GetBotBackup(string identifier)
|
||||
@@ -49,12 +46,13 @@ namespace Managing.Application.ManageBot
|
||||
return _botRepository.GetBots().FirstOrDefault(b => b.Identifier == identifier);
|
||||
}
|
||||
|
||||
public void SaveOrUpdateBotBackup(User user, string identifier, Enums.BotType botType, string data)
|
||||
public void SaveOrUpdateBotBackup(User user, string identifier, BotType botType, BotStatus status, string data)
|
||||
{
|
||||
var backup = GetBotBackup(identifier);
|
||||
|
||||
if (backup != null)
|
||||
{
|
||||
backup.LastStatus = status;
|
||||
backup.Data = data;
|
||||
_botRepository.UpdateBackupBot(backup);
|
||||
}
|
||||
@@ -62,6 +60,7 @@ namespace Managing.Application.ManageBot
|
||||
{
|
||||
var botBackup = new BotBackup
|
||||
{
|
||||
LastStatus = status,
|
||||
User = user,
|
||||
Identifier = identifier,
|
||||
BotType = botType,
|
||||
@@ -127,7 +126,7 @@ namespace Managing.Application.ManageBot
|
||||
// null); // Assuming null is an acceptable parameter for workflow
|
||||
// botTask = Task.Run(() => ((IBot)bot).Start());
|
||||
// break;
|
||||
case Enums.BotType.ScalpingBot:
|
||||
case BotType.ScalpingBot:
|
||||
var scalpingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||
var scalpingMoneyManagement =
|
||||
_moneyManagementService.GetMoneyMangement(scalpingBotData.MoneyManagement.Name).Result;
|
||||
@@ -142,7 +141,7 @@ namespace Managing.Application.ManageBot
|
||||
scalpingBotData.BotTradingBalance);
|
||||
botTask = Task.Run(() => InitBot((ITradingBot)bot, backupBot));
|
||||
break;
|
||||
case Enums.BotType.FlippingBot:
|
||||
case BotType.FlippingBot:
|
||||
var flippingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||
var flippingMoneyManagement =
|
||||
_moneyManagementService.GetMoneyMangement(flippingBotData.MoneyManagement.Name).Result;
|
||||
@@ -166,9 +165,12 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
}
|
||||
|
||||
private static Action InitBot(ITradingBot bot, BotBackup backupBot)
|
||||
private Action InitBot(ITradingBot bot, BotBackup backupBot)
|
||||
{
|
||||
bot.Start();
|
||||
|
||||
var user = _userService.GetUser(backupBot.User.Name);
|
||||
backupBot.User = user;
|
||||
bot.LoadBackup(backupBot);
|
||||
return () => { };
|
||||
}
|
||||
@@ -178,9 +180,9 @@ namespace Managing.Application.ManageBot
|
||||
return new SimpleBot(botName, _tradingBotLogger, workflow, this);
|
||||
}
|
||||
|
||||
public async Task<string> StopBot(string botName)
|
||||
public async Task<string> StopBot(string identifier)
|
||||
{
|
||||
if (_botTasks.TryGetValue(botName, out var botWrapper))
|
||||
if (_botTasks.TryGetValue(identifier, out var botWrapper))
|
||||
{
|
||||
if (botWrapper.BotInstance is IBot bot)
|
||||
{
|
||||
@@ -190,12 +192,12 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
}
|
||||
|
||||
return Enums.BotStatus.Down.ToString();
|
||||
return BotStatus.Down.ToString();
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteBot(string botName)
|
||||
public async Task<bool> DeleteBot(string identifier)
|
||||
{
|
||||
if (_botTasks.TryRemove(botName, out var botWrapper))
|
||||
if (_botTasks.TryRemove(identifier, out var botWrapper))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -205,7 +207,7 @@ namespace Managing.Application.ManageBot
|
||||
bot.Stop()); // Assuming Stop is an asynchronous process wrapped in Task.Run for synchronous methods
|
||||
}
|
||||
|
||||
await _botRepository.DeleteBotBackup(botName);
|
||||
await _botRepository.DeleteBotBackup(identifier);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -218,9 +220,9 @@ namespace Managing.Application.ManageBot
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task<string> RestartBot(string botName)
|
||||
public Task<string> RestartBot(string identifier)
|
||||
{
|
||||
if (_botTasks.TryGetValue(botName, out var botWrapper))
|
||||
if (_botTasks.TryGetValue(identifier, out var botWrapper))
|
||||
{
|
||||
if (botWrapper.BotInstance is IBot bot)
|
||||
{
|
||||
@@ -229,17 +231,17 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(Enums.BotStatus.Down.ToString());
|
||||
return Task.FromResult(BotStatus.Down.ToString());
|
||||
}
|
||||
|
||||
public void DeleteBotBackup(string backupBotName)
|
||||
public void DeleteBotBackup(string identifier)
|
||||
{
|
||||
_botRepository.DeleteBotBackup(backupBotName);
|
||||
_botRepository.DeleteBotBackup(identifier);
|
||||
}
|
||||
|
||||
public void ToggleIsForWatchingOnly(string botName)
|
||||
public void ToggleIsForWatchingOnly(string identifier)
|
||||
{
|
||||
if (_botTasks.TryGetValue(botName, out var botTaskWrapper) &&
|
||||
if (_botTasks.TryGetValue(identifier, out var botTaskWrapper) &&
|
||||
botTaskWrapper.BotInstance is ITradingBot tradingBot)
|
||||
{
|
||||
tradingBot.ToggleIsForWatchOnly().Wait();
|
||||
@@ -247,89 +249,159 @@ namespace Managing.Application.ManageBot
|
||||
}
|
||||
|
||||
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly,
|
||||
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
|
||||
};
|
||||
|
||||
return new ScalpingBot(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
name,
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
initialTradingBalance,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestScalpingBot(string accountName, MoneyManagement moneyManagement,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly,
|
||||
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(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
"BacktestBot",
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
initialTradingBalance,
|
||||
isForBacktest: true,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateFlippingBot(string accountName, MoneyManagement moneyManagement, string name,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly,
|
||||
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(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
name,
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
initialTradingBalance,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestFlippingBot(string accountName, MoneyManagement moneyManagement,
|
||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly,
|
||||
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(
|
||||
accountName,
|
||||
moneyManagement,
|
||||
"BacktestBot",
|
||||
scenario,
|
||||
_exchangeService,
|
||||
ticker,
|
||||
_tradingService,
|
||||
_tradingBotLogger,
|
||||
interval,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
initialTradingBalance,
|
||||
isForBacktest: true,
|
||||
isForWatchingOnly: isForWatchingOnly);
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateScalpingBot(TradingBotConfig config)
|
||||
{
|
||||
return new ScalpingBot(
|
||||
_exchangeService,
|
||||
_tradingBotLogger,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestScalpingBot(TradingBotConfig config)
|
||||
{
|
||||
config.IsForBacktest = true;
|
||||
return new ScalpingBot(
|
||||
_exchangeService,
|
||||
_tradingBotLogger,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateFlippingBot(TradingBotConfig config)
|
||||
{
|
||||
return new FlippingBot(
|
||||
_exchangeService,
|
||||
_tradingBotLogger,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
config);
|
||||
}
|
||||
|
||||
public ITradingBot CreateBacktestFlippingBot(TradingBotConfig config)
|
||||
{
|
||||
config.IsForBacktest = true;
|
||||
return new FlippingBot(
|
||||
_exchangeService,
|
||||
_tradingBotLogger,
|
||||
_tradingService,
|
||||
_accountService,
|
||||
_messengerService,
|
||||
this,
|
||||
config);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,20 @@
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Application.Bots;
|
||||
using Managing.Domain.Users;
|
||||
using MediatR;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.ManageBot.Commands
|
||||
{
|
||||
public class StartBotCommand : IRequest<string>
|
||||
{
|
||||
public string Name { get; }
|
||||
public BotType BotType { get; }
|
||||
public Ticker Ticker { get; internal set; }
|
||||
public Timeframe Timeframe { get; internal set; }
|
||||
public bool IsForWatchingOnly { get; internal set; }
|
||||
public string Scenario { get; internal set; }
|
||||
public string AccountName { get; internal set; }
|
||||
public string MoneyManagementName { get; internal set; }
|
||||
public User User { get; internal set; }
|
||||
public decimal InitialTradingBalance { get; internal set; }
|
||||
public TradingBotConfig Config { get; }
|
||||
public User User { get; }
|
||||
|
||||
public StartBotCommand(BotType botType,
|
||||
string name,
|
||||
Ticker ticker,
|
||||
string scenario,
|
||||
Timeframe timeframe,
|
||||
string accountName,
|
||||
string moneyManagementName,
|
||||
User user,
|
||||
bool isForWatchingOnly = false,
|
||||
decimal initialTradingBalance = 0)
|
||||
public StartBotCommand(TradingBotConfig config, string name, User user)
|
||||
{
|
||||
BotType = botType;
|
||||
Config = config;
|
||||
Name = name;
|
||||
Scenario = scenario;
|
||||
Ticker = ticker;
|
||||
Timeframe = timeframe;
|
||||
IsForWatchingOnly = isForWatchingOnly;
|
||||
AccountName = accountName;
|
||||
MoneyManagementName = moneyManagementName;
|
||||
User = user;
|
||||
InitialTradingBalance = initialTradingBalance > 0 ? initialTradingBalance :
|
||||
throw new ArgumentException("Initial trading balance must be greater than zero", nameof(initialTradingBalance));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Managing.Application.ManageBot
|
||||
{
|
||||
var allActiveBots = _botService.GetActiveBots();
|
||||
var userBots = allActiveBots
|
||||
.Where(bot => bot.User != null && bot.User.Name == request.UserName)
|
||||
.Where(bot => bot.User != null && bot.User.AgentName == request.UserName)
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult(userBots);
|
||||
|
||||
@@ -19,15 +19,14 @@ namespace Managing.Application.ManageBot
|
||||
public Task<ITradingBot> Handle(GetUserStrategyCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var allActiveBots = _botService.GetActiveBots();
|
||||
|
||||
|
||||
// Find the specific strategy that matches both user and strategy name
|
||||
var strategy = allActiveBots
|
||||
.FirstOrDefault(bot =>
|
||||
bot.User != null &&
|
||||
bot.User.Name == request.AgentName &&
|
||||
bot.Name == request.StrategyName);
|
||||
.FirstOrDefault(bot =>
|
||||
bot.User.AgentName == request.AgentName &&
|
||||
bot.Identifier == request.StrategyName);
|
||||
|
||||
return Task.FromResult(strategy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,13 @@ public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand,
|
||||
{
|
||||
try
|
||||
{
|
||||
if (backupBot.LastStatus == BotStatus.Down)
|
||||
{
|
||||
_logger.LogInformation("Skipping backup bot {Identifier} as it is marked as Down.",
|
||||
backupBot.Identifier);
|
||||
continue;
|
||||
}
|
||||
|
||||
var activeBot = _botService.GetActiveBots().FirstOrDefault(b => b.Identifier == backupBot.Identifier);
|
||||
|
||||
if (activeBot == null)
|
||||
|
||||
@@ -29,38 +29,38 @@ namespace Managing.Application.ManageBot
|
||||
{
|
||||
BotStatus botStatus = BotStatus.Down;
|
||||
|
||||
var account = await _accountService.GetAccount(request.AccountName, true, true);
|
||||
var account = await _accountService.GetAccount(request.Config.AccountName, true, true);
|
||||
|
||||
if (account == null)
|
||||
{
|
||||
throw new Exception($"Account {request.AccountName} not found");
|
||||
throw new Exception($"Account {request.Config.AccountName} not found");
|
||||
}
|
||||
|
||||
var usdcBalance = account.Balances.FirstOrDefault(b => b.TokenName == Ticker.USDC.ToString());
|
||||
|
||||
if (usdcBalance == null || usdcBalance.Value < request.InitialTradingBalance)
|
||||
if (usdcBalance == null || usdcBalance.Value < request.Config.BotTradingBalance)
|
||||
{
|
||||
throw new Exception($"Account {request.AccountName} has no USDC balance or not enough balance");
|
||||
throw new Exception($"Account {request.Config.AccountName} has no USDC balance or not enough balance");
|
||||
}
|
||||
|
||||
var moneyManagement =
|
||||
await _moneyManagementService.GetMoneyMangement(request.User, request.MoneyManagementName);
|
||||
switch (request.BotType)
|
||||
// Ensure cooldown period is set
|
||||
if (request.Config.CooldownPeriod <= 0)
|
||||
{
|
||||
request.Config.CooldownPeriod = 1; // Default to 1 minute if not set
|
||||
}
|
||||
|
||||
switch (request.Config.BotType)
|
||||
{
|
||||
case BotType.SimpleBot:
|
||||
var bot = _botFactory.CreateSimpleBot(request.Name, null);
|
||||
_botService.AddSimpleBotToCache(bot);
|
||||
return bot.GetStatus();
|
||||
case BotType.ScalpingBot:
|
||||
var sBot = _botFactory.CreateScalpingBot(request.AccountName, moneyManagement, request.Name,
|
||||
request.Ticker, request.Scenario, request.Timeframe, request.IsForWatchingOnly,
|
||||
request.InitialTradingBalance);
|
||||
var sBot = _botFactory.CreateScalpingBot(request.Config);
|
||||
_botService.AddTradingBotToCache(sBot);
|
||||
return sBot.GetStatus();
|
||||
case BotType.FlippingBot:
|
||||
var fBot = _botFactory.CreateFlippingBot(request.AccountName, moneyManagement, request.Name,
|
||||
request.Ticker, request.Scenario, request.Timeframe, request.IsForWatchingOnly,
|
||||
request.InitialTradingBalance);
|
||||
var fBot = _botFactory.CreateFlippingBot(request.Config);
|
||||
_botService.AddTradingBotToCache(fBot);
|
||||
return fBot.GetStatus();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Managing.Application.ManageBot
|
||||
{
|
||||
_botService.ToggleIsForWatchingOnly(request.Name);
|
||||
var bot = _botService.GetActiveBots().FirstOrDefault(b => b.Name == request.Name);
|
||||
return Task.FromResult(bot?.IsForWatchingOnly.ToString());
|
||||
return Task.FromResult(bot?.Config.IsForWatchingOnly.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user