From 52c11e30c4db9754c45195e6649fc17e2a542aa2 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Mon, 17 Nov 2025 23:53:53 +0700 Subject: [PATCH] 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. --- src/Managing.Application/Bots/TradingBotBase.cs | 11 +++++++---- .../performance-benchmarks-two-scenarios.csv | 2 ++ src/Managing.Workers.Tests/performance-benchmarks.csv | 2 ++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Managing.Application/Bots/TradingBotBase.cs b/src/Managing.Application/Bots/TradingBotBase.cs index 389586c4..187a84cd 100644 --- a/src/Managing.Application/Bots/TradingBotBase.cs +++ b/src/Managing.Application/Bots/TradingBotBase.cs @@ -40,6 +40,7 @@ public class TradingBotBase : ITradingBot public Dictionary Signals { get; set; } public Dictionary Positions { get; set; } public Dictionary 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(); Positions = new Dictionary(); WalletBalances = new Dictionary(); + _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( diff --git a/src/Managing.Workers.Tests/performance-benchmarks-two-scenarios.csv b/src/Managing.Workers.Tests/performance-benchmarks-two-scenarios.csv index 38e89d5e..3b40261c 100644 --- a/src/Managing.Workers.Tests/performance-benchmarks-two-scenarios.csv +++ b/src/Managing.Workers.Tests/performance-benchmarks-two-scenarios.csv @@ -22,3 +22,5 @@ DateTime,TestName,CandlesCount,ExecutionTimeSeconds,ProcessingRateCandlesPerSec, 2025-11-15T06:50:04Z,Telemetry_ETH_RSI_EMACROSS,5760,4.84,1190.4,29.01,19.10,35.17,0.0,0,0.0,0.0,0.0,0.0,-35450.45,20,-49.76,0.00,e814eb74,dev,development 2025-11-15T07:11:55Z,Telemetry_ETH_RSI_EMACROSS,5760,5.44,1059.4,28.81,18.07,33.80,0.0,0,0.0,0.0,0.0,0.0,-35450.45,20,-49.76,0.00,bed25e72,dev,development 2025-11-15T07:22:05Z,Telemetry_ETH_RSI_EMACROSS,5760,10.71,537.9,28.81,18.06,33.84,0.0,0,0.0,0.0,0.0,0.0,-35450.45,20,-49.76,0.00,49a693b4,dev,development +2025-11-17T16:35:10Z,Telemetry_ETH_RSI_EMACROSS,5760,5.88,979.2,28.79,17.97,33.77,0.0,0,0.0,0.0,0.0,0.0,-35450.45,20,-49.76,0.00,091f617e,dev,development +2025-11-17T16:49:22Z,Telemetry_ETH_RSI_EMACROSS,5760,4.61,1249.2,28.80,17.29,33.78,0.0,0,0.0,0.0,0.0,0.0,-35450.45,20,-49.76,0.00,091f617e,dev,development diff --git a/src/Managing.Workers.Tests/performance-benchmarks.csv b/src/Managing.Workers.Tests/performance-benchmarks.csv index 2b1bc897..01a07a47 100644 --- a/src/Managing.Workers.Tests/performance-benchmarks.csv +++ b/src/Managing.Workers.Tests/performance-benchmarks.csv @@ -67,3 +67,5 @@ DateTime,TestName,CandlesCount,ExecutionTimeSeconds,ProcessingRateCandlesPerSec, 2025-11-15T06:50:04Z,Telemetry_ETH_RSI,5760,4.47,1286.2,28.81,20.58,34.89,3324.75,0,0.0,965.71,0.00,0.17,-30689.97,24,-51.70,0.00,e814eb74,dev,development 2025-11-15T07:11:55Z,Telemetry_ETH_RSI,5760,3.365,1707.1,29.06,20.43,36.29,2872.29,0,0.0,371.33,0.00,0.06,-30689.97,24,-51.70,0.00,bed25e72,dev,development 2025-11-15T07:22:05Z,Telemetry_ETH_RSI,5760,7.49,766.2,28.80,20.86,34.90,5992.19,0,0.0,916.71,0.00,0.16,-30689.97,24,-51.70,0.00,49a693b4,dev,development +2025-11-17T16:35:10Z,Telemetry_ETH_RSI,5760,4.18,1373.1,29.03,20.63,36.17,3521.98,0,0.0,486.12,0.00,0.08,-30689.97,24,-51.70,0.00,091f617e,dev,development +2025-11-17T16:49:22Z,Telemetry_ETH_RSI,5760,2.885,1990.6,29.02,20.35,35.08,2530.49,0,0.0,226.92,0.00,0.04,-30689.97,24,-51.70,0.00,091f617e,dev,development