Fix realized pnl on backtest save + add tests (not all passing)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user