Fix realized pnl on backtest save + add tests (not all passing)

This commit is contained in:
2025-11-14 02:38:15 +07:00
parent 1f7d914625
commit 460a7bd559
34 changed files with 6012 additions and 500 deletions

View File

@@ -339,15 +339,16 @@ public class BacktestExecutor
// Start result calculation timing
var resultCalculationStart = Stopwatch.GetTimestamp();
// Calculate final results (using existing optimized methods)
var netPnl = tradingBot.GetProfitAndLoss(); // This returns Net PnL (after fees)
var winRate = tradingBot.GetWinRate();
var stats = TradingHelpers.GetStatistics(tradingBot.WalletBalances);
// Calculate final results using static methods from TradingBox
var realizedPnl = TradingBox.GetTotalRealizedPnL(tradingBot.Positions); // PnL before fees
var netPnl = TradingBox.GetTotalNetPnL(tradingBot.Positions); // PnL after fees
var winRate = TradingBox.GetWinRate(tradingBot.Positions);
var stats = TradingBox.GetStatistics(tradingBot.WalletBalances);
var growthPercentage =
TradingHelpers.GetGrowthFromInitalBalance(tradingBot.WalletBalances.FirstOrDefault().Value, netPnl);
var hodlPercentage = TradingHelpers.GetHodlPercentage(candles.First(), candles.Last());
TradingBox.GetGrowthFromInitalBalance(tradingBot.WalletBalances.FirstOrDefault().Value, netPnl);
var hodlPercentage = TradingBox.GetHodlPercentage(candles.First(), candles.Last());
var fees = tradingBot.GetTotalFees();
var fees = TradingBox.GetTotalFees(tradingBot.Positions);
var scoringParams = new BacktestScoringParams(
sharpeRatio: (double)stats.SharpeRatio,
growthPercentage: (double)growthPercentage,
@@ -383,7 +384,7 @@ public class BacktestExecutor
var result = new Backtest(config, tradingBot.Positions, tradingBot.Signals,
withCandles ? candles : new HashSet<Candle>())
{
FinalPnl = netPnl, // Net PnL (after fees)
FinalPnl = realizedPnl, // Realized PnL before fees
WinRate = winRate,
GrowthPercentage = growthPercentage,
HodlPercentage = hodlPercentage,
@@ -398,7 +399,7 @@ public class BacktestExecutor
StartDate = candles.FirstOrDefault()!.OpenTime,
EndDate = candles.LastOrDefault()!.OpenTime,
InitialBalance = initialBalance,
NetPnl = netPnl, // Already net of fees
NetPnl = netPnl, // Net PnL after fees
};
if (save && user != null)