Refactor TradingBotBase to manage current balance more effectively. Introduced _currentBalance field to track balance updates during trading operations. Updated wallet balance logic to utilize _currentBalance for consistency. Added new entries to performance benchmark CSV files for recent test runs.

This commit is contained in:
2025-11-17 23:53:53 +07:00
parent 091f617e37
commit 52c11e30c4
3 changed files with 11 additions and 4 deletions

View File

@@ -40,6 +40,7 @@ public class TradingBotBase : ITradingBot
public Dictionary<string, LightSignal> Signals { get; set; }
public Dictionary<Guid, Position> Positions { get; set; }
public Dictionary<DateTime, decimal> WalletBalances { get; set; }
private decimal _currentBalance;
public DateTime PreloadSince { get; set; }
public int PreloadedCandlesCount { get; set; }
public long ExecutionCount { get; set; } = 0;
@@ -61,6 +62,7 @@ public class TradingBotBase : ITradingBot
Signals = new Dictionary<string, LightSignal>();
Positions = new Dictionary<Guid, Position>();
WalletBalances = new Dictionary<DateTime, decimal>();
_currentBalance = config.BotTradingBalance;
PreloadSince = CandleHelpers.GetBotPreloadSinceFromTimeframe(config.Timeframe);
}
@@ -457,18 +459,17 @@ public class TradingBotBase : ITradingBot
private void UpdateWalletBalances()
{
var date = DateTime.UtcNow;
var date = Config.IsForBacktest ? LastCandle?.Date ?? DateTime.UtcNow : DateTime.UtcNow;
if (WalletBalances.Count == 0)
{
WalletBalances[date] = Config.BotTradingBalance;
WalletBalances[date] = _currentBalance;
return;
}
if (!WalletBalances.ContainsKey(date))
{
var previousBalance = WalletBalances.First().Value;
WalletBalances[date] = previousBalance + TradingBox.GetTotalNetPnL(Positions);
WalletBalances[date] = _currentBalance;
}
}
@@ -2009,6 +2010,8 @@ public class TradingBotBase : ITradingBot
if (position.ProfitAndLoss != null)
{
// Update the current balance when position closes
_currentBalance += position.ProfitAndLoss.Net;
Config.BotTradingBalance += position.ProfitAndLoss.Net;
await LogDebug(