Trading bot grain (#33)
* Trading bot Grain * Fix a bit more of the trading bot * Advance on the tradingbot grain * Fix build * Fix db script * Fix user login * Fix a bit backtest * Fix cooldown and backtest * start fixing bot start * Fix startup * Setup local db * Fix build and update candles and scenario * Add bot registry * Add reminder * Updateing the grains * fix bootstraping * Save stats on tick * Save bot data every tick * Fix serialization * fix save bot stats * Fix get candles * use dict instead of list for position * Switch hashset to dict * Fix a bit * Fix bot launch and bot view * add migrations * Remove the tolist * Add agent grain * Save agent summary * clean * Add save bot * Update get bots * Add get bots * Fix stop/restart * fix Update config * Update scanner table on new backtest saved * Fix backtestRowDetails.tsx * Fix agentIndex * Update agentIndex * Fix more things * Update user cache * Fix * Fix account load/start/restart/run
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.ManageBot.Commands;
|
||||
using Managing.Domain.Bots;
|
||||
using Managing.Domain.Statistics;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -46,20 +46,21 @@ public class BalanceTrackingWorker : BaseWorker<BalanceTrackingWorker>
|
||||
_logger.LogInformation("Starting balance tracking...");
|
||||
|
||||
// Get all active bots
|
||||
var bots = await _mediator.Send(new GetActiveBotsCommand());
|
||||
var bots = await _mediator.Send(new GetBotsByStatusCommand(BotStatus.Up));
|
||||
|
||||
if (bots.Count == 0)
|
||||
var botCount = bots.Count();
|
||||
if (botCount == 0)
|
||||
{
|
||||
_logger.LogWarning("No active bots found. Skipping balance tracking.");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation($"Found {bots.Count} active bots. Proceeding with balance tracking.");
|
||||
_logger.LogInformation($"Found {botCount} active bots. Proceeding with balance tracking.");
|
||||
await TrackBalances(bots);
|
||||
_logger.LogInformation("Completed balance tracking");
|
||||
}
|
||||
|
||||
private async Task TrackBalances(List<ITradingBot> bots)
|
||||
private async Task TrackBalances(IEnumerable<Bot> bots)
|
||||
{
|
||||
// Group bots by agent/user
|
||||
var botsByAgent = bots
|
||||
@@ -98,9 +99,9 @@ public class BalanceTrackingWorker : BaseWorker<BalanceTrackingWorker>
|
||||
// Calculate total allocated balance for all bots
|
||||
foreach (var bot in agentBots)
|
||||
{
|
||||
totalBotAllocatedBalance += bot.Config.BotTradingBalance;
|
||||
totalBotAllocatedBalance += bot.Volume;
|
||||
_logger.LogInformation(
|
||||
$"Bot {bot.Name} allocated balance: {bot.Config.BotTradingBalance} USD");
|
||||
$"Bot {bot.Name} allocated balance: {bot.Volume} USD");
|
||||
}
|
||||
|
||||
// Get account balances for this agent (only once per agent)
|
||||
@@ -140,20 +141,7 @@ public class BalanceTrackingWorker : BaseWorker<BalanceTrackingWorker>
|
||||
// Process all bots in a single iteration
|
||||
foreach (var bot in agentBots)
|
||||
{
|
||||
// Get wallet balance
|
||||
var latestBotBalance = bot.WalletBalances
|
||||
.OrderByDescending(x => x.Key)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (latestBotBalance.Key != default)
|
||||
{
|
||||
botsAllocationUsdValue += latestBotBalance.Value;
|
||||
_logger.LogInformation(
|
||||
$"Bot {bot.Name} wallet balance: {latestBotBalance.Value} USD at {latestBotBalance.Key}");
|
||||
}
|
||||
|
||||
// Calculate PnL
|
||||
totalPnL += bot.GetProfitAndLoss();
|
||||
totalPnL += bot.Pnl;
|
||||
}
|
||||
|
||||
totalAgentValue = totalAccountUsdValue + botsAllocationUsdValue;
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using Managing.Application.ManageBot;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Workers;
|
||||
|
||||
public class BotManagerWorker(
|
||||
ILogger<BotManagerWorker> logger,
|
||||
IServiceProvider serviceProvider,
|
||||
IMediator mediadior)
|
||||
: BaseWorker<BotManagerWorker>(WorkerType.BotManager,
|
||||
logger,
|
||||
TimeSpan.FromMinutes(5),
|
||||
serviceProvider)
|
||||
{
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
var loadBackupBotCommand = new LoadBackupBotCommand();
|
||||
await mediadior.Send(loadBackupBotCommand, cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user