Add stats for kaigen
This commit is contained in:
@@ -16,6 +16,10 @@ namespace Managing.Domain.Bots
|
||||
public int Interval { get; set; }
|
||||
public BotStatus Status { get; set; }
|
||||
public User User { get; set; }
|
||||
/// <summary>
|
||||
/// The time when the bot was started
|
||||
/// </summary>
|
||||
public DateTime StartupTime { get; private set; }
|
||||
private CancellationTokenSource CancellationToken { get; set; }
|
||||
|
||||
public Bot(string name)
|
||||
@@ -26,11 +30,13 @@ namespace Managing.Domain.Bots
|
||||
CancellationToken = new CancellationTokenSource();
|
||||
ExecutionCount = 0;
|
||||
Interval = 3000;
|
||||
StartupTime = DateTime.MinValue; // Initialize with minimum value to indicate it hasn't been started yet
|
||||
}
|
||||
|
||||
public virtual void Start()
|
||||
{
|
||||
Status = BotStatus.Up;
|
||||
StartupTime = DateTime.UtcNow; // Record the startup time when the bot is started
|
||||
}
|
||||
|
||||
public async Task InitWorker(Func<Task> action)
|
||||
@@ -76,6 +82,7 @@ namespace Managing.Domain.Bots
|
||||
public void Restart()
|
||||
{
|
||||
Status = BotStatus.Up;
|
||||
StartupTime = DateTime.UtcNow; // Update the startup time when the bot is restarted
|
||||
}
|
||||
|
||||
public string GetStatus()
|
||||
@@ -87,6 +94,18 @@ namespace Managing.Domain.Bots
|
||||
{
|
||||
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 || StartupTime == DateTime.MinValue)
|
||||
return TimeSpan.Zero;
|
||||
|
||||
return DateTime.UtcNow - StartupTime;
|
||||
}
|
||||
|
||||
public abstract void SaveBackup();
|
||||
public abstract void LoadBackup(BotBackup backup);
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
void Restart();
|
||||
string GetStatus();
|
||||
string GetName();
|
||||
/// <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>
|
||||
TimeSpan GetRuntime();
|
||||
void SaveBackup();
|
||||
void LoadBackup(BotBackup backup);
|
||||
}
|
||||
|
||||
@@ -187,4 +187,140 @@ public static class TradingBox
|
||||
pnl.Realized = pnl.Realized * leverage;
|
||||
return pnl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the total volume traded across all positions
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <returns>The total volume traded in decimal</returns>
|
||||
public static decimal GetTotalVolumeTraded(List<Position> positions)
|
||||
{
|
||||
decimal totalVolume = 0;
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
// Add entry volume
|
||||
totalVolume += position.Open.Quantity * position.Open.Price;
|
||||
|
||||
// Add exit volumes from stop loss or take profits if they were executed
|
||||
if (position.StopLoss.Status == TradeStatus.Filled)
|
||||
{
|
||||
totalVolume += position.StopLoss.Quantity * position.StopLoss.Price;
|
||||
}
|
||||
|
||||
if (position.TakeProfit1.Status == TradeStatus.Filled)
|
||||
{
|
||||
totalVolume += position.TakeProfit1.Quantity * position.TakeProfit1.Price;
|
||||
}
|
||||
|
||||
if (position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled)
|
||||
{
|
||||
totalVolume += position.TakeProfit2.Quantity * position.TakeProfit2.Price;
|
||||
}
|
||||
}
|
||||
|
||||
return totalVolume;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the volume traded in the last 24 hours
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <returns>The volume traded in the last 24 hours in decimal</returns>
|
||||
public static decimal GetLast24HVolumeTraded(List<Position> positions)
|
||||
{
|
||||
decimal last24hVolume = 0;
|
||||
DateTime cutoff = DateTime.UtcNow.AddHours(-24);
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
// Check if any part of this position was traded in the last 24 hours
|
||||
|
||||
// Add entry volume if it was within the last 24 hours
|
||||
if (position.Open.Date >= cutoff)
|
||||
{
|
||||
last24hVolume += position.Open.Quantity * position.Open.Price;
|
||||
}
|
||||
|
||||
// Add exit volumes if they were executed within the last 24 hours
|
||||
if (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date >= cutoff)
|
||||
{
|
||||
last24hVolume += position.StopLoss.Quantity * position.StopLoss.Price;
|
||||
}
|
||||
|
||||
if (position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date >= cutoff)
|
||||
{
|
||||
last24hVolume += position.TakeProfit1.Quantity * position.TakeProfit1.Price;
|
||||
}
|
||||
|
||||
if (position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled &&
|
||||
position.TakeProfit2.Date >= cutoff)
|
||||
{
|
||||
last24hVolume += position.TakeProfit2.Quantity * position.TakeProfit2.Price;
|
||||
}
|
||||
}
|
||||
|
||||
return last24hVolume;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the win/loss counts from positions
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <returns>A tuple containing (wins, losses)</returns>
|
||||
public static (int Wins, int Losses) GetWinLossCount(List<Position> positions)
|
||||
{
|
||||
int wins = 0;
|
||||
int losses = 0;
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
// Only count finished positions
|
||||
if (position.IsFinished())
|
||||
{
|
||||
if (position.ProfitAndLoss != null && position.ProfitAndLoss.Realized > 0)
|
||||
{
|
||||
wins++;
|
||||
}
|
||||
else
|
||||
{
|
||||
losses++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (wins, losses);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the ROI for the last 24 hours
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <returns>The ROI for the last 24 hours as a percentage</returns>
|
||||
public static decimal GetLast24HROI(List<Position> positions)
|
||||
{
|
||||
decimal profitLast24h = 0;
|
||||
decimal investmentLast24h = 0;
|
||||
DateTime cutoff = DateTime.UtcNow.AddHours(-24);
|
||||
|
||||
foreach (var position in positions)
|
||||
{
|
||||
// Only count positions that were opened or closed within the last 24 hours
|
||||
if (position.IsFinished() &&
|
||||
(position.Open.Date >= cutoff ||
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date >= cutoff) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date >= cutoff) ||
|
||||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && position.TakeProfit2.Date >= cutoff)))
|
||||
{
|
||||
profitLast24h += position.ProfitAndLoss != null ? position.ProfitAndLoss.Realized : 0;
|
||||
investmentLast24h += position.Open.Quantity * position.Open.Price;
|
||||
}
|
||||
}
|
||||
|
||||
// Avoid division by zero
|
||||
if (investmentLast24h == 0)
|
||||
return 0;
|
||||
|
||||
return (profitLast24h / investmentLast24h) * 100;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user