Refactor BacktestExecutor to use net PnL calculations consistently across methods. Updated variable names for clarity and ensured final results reflect net profit after fees. Minor adjustment in TradingBotBase to directly access net PnL from position profit and loss.
This commit is contained in:
@@ -335,11 +335,11 @@ public class BacktestExecutor
|
|||||||
var resultCalculationStart = Stopwatch.GetTimestamp();
|
var resultCalculationStart = Stopwatch.GetTimestamp();
|
||||||
|
|
||||||
// Calculate final results (using existing optimized methods)
|
// Calculate final results (using existing optimized methods)
|
||||||
var finalPnl = tradingBot.GetProfitAndLoss();
|
var netPnl = tradingBot.GetProfitAndLoss(); // This returns Net PnL (after fees)
|
||||||
var winRate = tradingBot.GetWinRate();
|
var winRate = tradingBot.GetWinRate();
|
||||||
var stats = TradingHelpers.GetStatistics(tradingBot.WalletBalances);
|
var stats = TradingHelpers.GetStatistics(tradingBot.WalletBalances);
|
||||||
var growthPercentage =
|
var growthPercentage =
|
||||||
TradingHelpers.GetGrowthFromInitalBalance(tradingBot.WalletBalances.FirstOrDefault().Value, finalPnl);
|
TradingHelpers.GetGrowthFromInitalBalance(tradingBot.WalletBalances.FirstOrDefault().Value, netPnl);
|
||||||
var hodlPercentage = TradingHelpers.GetHodlPercentage(candles.First(), candles.Last());
|
var hodlPercentage = TradingHelpers.GetHodlPercentage(candles.First(), candles.Last());
|
||||||
|
|
||||||
var fees = tradingBot.GetTotalFees();
|
var fees = tradingBot.GetTotalFees();
|
||||||
@@ -348,7 +348,7 @@ public class BacktestExecutor
|
|||||||
growthPercentage: (double)growthPercentage,
|
growthPercentage: (double)growthPercentage,
|
||||||
hodlPercentage: (double)hodlPercentage,
|
hodlPercentage: (double)hodlPercentage,
|
||||||
winRate: winRate,
|
winRate: winRate,
|
||||||
totalPnL: (double)finalPnl,
|
totalPnL: (double)netPnl,
|
||||||
fees: (double)fees,
|
fees: (double)fees,
|
||||||
tradeCount: tradingBot.Positions.Count,
|
tradeCount: tradingBot.Positions.Count,
|
||||||
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime,
|
maxDrawdownRecoveryTime: stats.MaxDrawdownRecoveryTime,
|
||||||
@@ -378,7 +378,7 @@ public class BacktestExecutor
|
|||||||
var result = new Backtest(config, tradingBot.Positions, tradingBot.Signals,
|
var result = new Backtest(config, tradingBot.Positions, tradingBot.Signals,
|
||||||
withCandles ? candles : new HashSet<Candle>())
|
withCandles ? candles : new HashSet<Candle>())
|
||||||
{
|
{
|
||||||
FinalPnl = finalPnl,
|
FinalPnl = netPnl, // Net PnL (after fees)
|
||||||
WinRate = winRate,
|
WinRate = winRate,
|
||||||
GrowthPercentage = growthPercentage,
|
GrowthPercentage = growthPercentage,
|
||||||
HodlPercentage = hodlPercentage,
|
HodlPercentage = hodlPercentage,
|
||||||
@@ -393,7 +393,7 @@ public class BacktestExecutor
|
|||||||
StartDate = candles.FirstOrDefault()!.OpenTime,
|
StartDate = candles.FirstOrDefault()!.OpenTime,
|
||||||
EndDate = candles.LastOrDefault()!.OpenTime,
|
EndDate = candles.LastOrDefault()!.OpenTime,
|
||||||
InitialBalance = initialBalance,
|
InitialBalance = initialBalance,
|
||||||
NetPnl = finalPnl - fees,
|
NetPnl = netPnl, // Already net of fees
|
||||||
};
|
};
|
||||||
|
|
||||||
if (save && user != null)
|
if (save && user != null)
|
||||||
|
|||||||
@@ -2132,7 +2132,7 @@ public class TradingBotBase : ITradingBot
|
|||||||
{
|
{
|
||||||
if (position.IsValidForMetrics() && position.ProfitAndLoss != null)
|
if (position.IsValidForMetrics() && position.ProfitAndLoss != null)
|
||||||
{
|
{
|
||||||
netPnl += position.GetPnLBeforeFees();
|
netPnl += position.ProfitAndLoss.Net;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export { default as PositionsModal } from './PositionsModal'
|
export { default as PositionsModal } from './PositionsModal'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user