Add data + fix positions
This commit is contained in:
@@ -323,4 +323,173 @@ public static class TradingBox
|
||||
|
||||
return (profitLast24h / investmentLast24h) * 100;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates profit and loss for positions within a specific time range
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <param name="timeFilter">Time filter to apply (24H, 3D, 1W, 1M, 1Y, Total)</param>
|
||||
/// <returns>The PnL for positions in the specified range</returns>
|
||||
public static decimal GetPnLInTimeRange(List<Position> positions, string timeFilter)
|
||||
{
|
||||
// If Total, just return the total PnL
|
||||
if (timeFilter == "Total")
|
||||
{
|
||||
return positions
|
||||
.Where(p => p.IsFinished() && p.ProfitAndLoss != null)
|
||||
.Sum(p => p.ProfitAndLoss.Realized);
|
||||
}
|
||||
|
||||
// Convert time filter to a DateTime
|
||||
DateTime cutoffDate = DateTime.UtcNow;
|
||||
|
||||
switch (timeFilter)
|
||||
{
|
||||
case "24H":
|
||||
cutoffDate = DateTime.UtcNow.AddHours(-24);
|
||||
break;
|
||||
case "3D":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-3);
|
||||
break;
|
||||
case "1W":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-7);
|
||||
break;
|
||||
case "1M":
|
||||
cutoffDate = DateTime.UtcNow.AddMonths(-1);
|
||||
break;
|
||||
case "1Y":
|
||||
cutoffDate = DateTime.UtcNow.AddYears(-1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Include positions that were closed within the time range
|
||||
return positions
|
||||
.Where(p => p.IsFinished() && p.ProfitAndLoss != null &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
|
||||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
|
||||
(p.TakeProfit2 != null && p.TakeProfit2.Status == TradeStatus.Filled && p.TakeProfit2.Date >= cutoffDate)))
|
||||
.Sum(p => p.ProfitAndLoss.Realized);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates ROI for positions within a specific time range
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <param name="timeFilter">Time filter to apply (24H, 3D, 1W, 1M, 1Y, Total)</param>
|
||||
/// <returns>The ROI as a percentage for positions in the specified range</returns>
|
||||
public static decimal GetROIInTimeRange(List<Position> positions, string timeFilter)
|
||||
{
|
||||
// If no positions, return 0
|
||||
if (!positions.Any())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Convert time filter to a DateTime
|
||||
DateTime cutoffDate = DateTime.UtcNow;
|
||||
|
||||
if (timeFilter != "Total")
|
||||
{
|
||||
switch (timeFilter)
|
||||
{
|
||||
case "24H":
|
||||
cutoffDate = DateTime.UtcNow.AddHours(-24);
|
||||
break;
|
||||
case "3D":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-3);
|
||||
break;
|
||||
case "1W":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-7);
|
||||
break;
|
||||
case "1M":
|
||||
cutoffDate = DateTime.UtcNow.AddMonths(-1);
|
||||
break;
|
||||
case "1Y":
|
||||
cutoffDate = DateTime.UtcNow.AddYears(-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter positions in the time range
|
||||
var filteredPositions = timeFilter == "Total"
|
||||
? positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null)
|
||||
: positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
|
||||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
|
||||
(p.TakeProfit2 != null && p.TakeProfit2.Status == TradeStatus.Filled && p.TakeProfit2.Date >= cutoffDate)));
|
||||
|
||||
// Calculate investment and profit
|
||||
decimal totalInvestment = filteredPositions.Sum(p => p.Open.Quantity * p.Open.Price);
|
||||
decimal totalProfit = filteredPositions.Sum(p => p.ProfitAndLoss.Realized);
|
||||
|
||||
// Calculate ROI
|
||||
if (totalInvestment == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (totalProfit / totalInvestment) * 100;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the win/loss counts from positions in a specific time range
|
||||
/// </summary>
|
||||
/// <param name="positions">List of positions to analyze</param>
|
||||
/// <param name="timeFilter">Time filter to apply (24H, 3D, 1W, 1M, 1Y, Total)</param>
|
||||
/// <returns>A tuple containing (wins, losses)</returns>
|
||||
public static (int Wins, int Losses) GetWinLossCountInTimeRange(List<Position> positions, string timeFilter)
|
||||
{
|
||||
// Convert time filter to a DateTime
|
||||
DateTime cutoffDate = DateTime.UtcNow;
|
||||
|
||||
if (timeFilter != "Total")
|
||||
{
|
||||
switch (timeFilter)
|
||||
{
|
||||
case "24H":
|
||||
cutoffDate = DateTime.UtcNow.AddHours(-24);
|
||||
break;
|
||||
case "3D":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-3);
|
||||
break;
|
||||
case "1W":
|
||||
cutoffDate = DateTime.UtcNow.AddDays(-7);
|
||||
break;
|
||||
case "1M":
|
||||
cutoffDate = DateTime.UtcNow.AddMonths(-1);
|
||||
break;
|
||||
case "1Y":
|
||||
cutoffDate = DateTime.UtcNow.AddYears(-1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Filter positions in the time range
|
||||
var filteredPositions = timeFilter == "Total"
|
||||
? positions.Where(p => p.IsFinished())
|
||||
: positions.Where(p => p.IsFinished() &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
|
||||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
|
||||
(p.TakeProfit2 != null && p.TakeProfit2.Status == TradeStatus.Filled && p.TakeProfit2.Date >= cutoffDate)));
|
||||
|
||||
int wins = 0;
|
||||
int losses = 0;
|
||||
|
||||
foreach (var position in filteredPositions)
|
||||
{
|
||||
if (position.ProfitAndLoss != null && position.ProfitAndLoss.Realized > 0)
|
||||
{
|
||||
wins++;
|
||||
}
|
||||
else
|
||||
{
|
||||
losses++;
|
||||
}
|
||||
}
|
||||
|
||||
return (wins, losses);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user