Add stats for kaigen
This commit is contained in:
@@ -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