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:
Oda
2025-08-04 23:07:06 +02:00
committed by GitHub
parent cd378587aa
commit 082ae8714b
215 changed files with 9562 additions and 14028 deletions

View File

@@ -3,123 +3,22 @@ using static Managing.Common.Enums;
namespace Managing.Domain.Bots
{
/// <summary>
/// A bot define what code should be run.
/// To run a code you have to herit from this class and implement the Run() method
/// </summary>
public abstract class Bot : IBot
public class Bot
{
public int ExecutionCount;
public string Identifier { get; set; }
public string Name { get; set; }
public int Interval { get; set; }
public BotStatus Status { get; set; }
public User User { get; set; }
public Guid Identifier { get; set; }
public string Name { get; set; }
public Ticker Ticker { get; set; }
public BotStatus Status { get; set; }
public DateTime StartupTime { get; set; }
public DateTime CreateDate { get; set; }
/// <summary>
/// The time when the bot was first started (creation date)
/// </summary>
public DateTime StartupTime { get; protected set; }
/// <summary>
/// The time when the bot was created
/// </summary>
public DateTime CreateDate { get; protected set; }
private CancellationTokenSource CancellationToken { get; set; }
public Bot(string name)
{
Identifier = $"{name}-{DateTime.Now:yyyyMMdd-hhmm}-{Guid.NewGuid()}";
Name = name;
Status = BotStatus.Down;
CancellationToken = new CancellationTokenSource();
ExecutionCount = 0;
Interval = 3000;
CreateDate = DateTime.UtcNow; // Set the creation time when the bot is instantiated
StartupTime = DateTime.UtcNow; // Set the startup time to creation date initially
}
public virtual void Start()
{
Status = BotStatus.Up;
// StartupTime remains unchanged on first start (it's already set to creation date)
}
public async Task InitWorker(Func<Task> action)
{
try
{
await Task.Run(async () =>
{
while (Status == BotStatus.Up && !CancellationToken.IsCancellationRequested)
{
try
{
await action();
ExecutionCount++;
if (CancellationToken.IsCancellationRequested)
break;
}
catch (TaskCanceledException) when (CancellationToken.IsCancellationRequested)
{
// Graceful shutdown when cancellation is requested
break;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
Console.WriteLine(ex.Message);
}
finally
{
await Task.Delay(Interval, CancellationToken.Token);
}
}
}, CancellationToken.Token);
}
catch (TaskCanceledException ex)
{
Console.WriteLine($"Bot was cancelled: {ex.Message}");
}
}
public void Stop()
{
Status = BotStatus.Down;
_ = Task.Run(async () => await SaveBackup());
// CancellationToken.Cancel();
}
public void Restart()
{
Status = BotStatus.Up;
StartupTime = DateTime.UtcNow; // Update the startup time when the bot is restarted
}
public string GetStatus()
{
return Status.ToString();
}
public string GetName()
{
return Name;
}
/// <summary>
/// Gets the total runtime of the bot since it was started
/// </summary>
/// <returns>TimeSpan representing the runtime, or TimeSpan.Zero if the bot is not running</returns>
public TimeSpan GetRuntime()
{
if (Status != BotStatus.Up)
return TimeSpan.Zero;
return DateTime.UtcNow - StartupTime;
}
public abstract Task SaveBackup();
public abstract void LoadBackup(BotBackup backup);
public int TradeWins { get; set; }
public int TradeLosses { get; set; }
public decimal Pnl { get; set; }
public decimal Roi { get; set; }
public decimal Volume { get; set; }
public decimal Fees { get; set; }
}
}