Adapt spot for recovery

This commit is contained in:
2025-12-05 15:24:51 +07:00
parent 78edd850fe
commit 15d8b38d8b
4 changed files with 46 additions and 82 deletions

View File

@@ -168,7 +168,7 @@ public abstract class TradingBotBase : ITradingBot
public async Task LoadAccount()
{
if (Config.TradingType == TradingType.BacktestFutures) return;
if (TradingBox.IsBacktestTrading(Config.TradingType)) return;
await ServiceScopeHelpers.WithScopedService<IAccountService>(_scopeFactory, async accountService =>
{
var account = await accountService.GetAccountByAccountName(Config.AccountName, false, false);
@@ -182,7 +182,7 @@ public abstract class TradingBotBase : ITradingBot
/// </summary>
public async Task VerifyAndUpdateBalance()
{
if (Config.TradingType == TradingType.BacktestFutures) return;
if (TradingBox.IsBacktestTrading(Config.TradingType)) return;
if (Account == null)
{
Logger.LogWarning("Cannot verify balance: Account is null");
@@ -367,12 +367,8 @@ public abstract class TradingBotBase : ITradingBot
return;
// First, process all existing positions that are not finished
// Optimized: Inline the filter to avoid LINQ overhead
foreach (var position in Positions.Values)
foreach (var position in Positions.Values.Where(p => !p.IsFinished()))
{
if (position.IsFinished())
continue;
var signalForPosition = Signals[position.SignalIdentifier];
if (signalForPosition == null)
{
@@ -436,7 +432,7 @@ public abstract class TradingBotBase : ITradingBot
protected void UpdateWalletBalances()
{
var date = Config.TradingType == TradingType.BacktestFutures
var date = TradingBox.IsBacktestTrading(Config.TradingType)
? LastCandle?.Date ?? DateTime.UtcNow
: DateTime.UtcNow;
@@ -485,13 +481,13 @@ public abstract class TradingBotBase : ITradingBot
Candle lastCandle = null;
await ServiceScopeHelpers.WithScopedService<IExchangeService>(_scopeFactory, async exchangeService =>
{
lastCandle = Config.TradingType == TradingType.BacktestFutures
lastCandle = TradingBox.IsBacktestTrading(Config.TradingType)
? LastCandle
: await exchangeService.GetCandle(Account, Config.Ticker,
DateTime.UtcNow);
});
var currentTime = Config.TradingType == TradingType.BacktestFutures ? lastCandle.Date : DateTime.UtcNow;
var currentTime = TradingBox.IsBacktestTrading(Config.TradingType) ? lastCandle.Date : DateTime.UtcNow;
var currentPnl = positionForSignal.ProfitAndLoss?.Net ?? 0;
var pnlPercentage = TradingBox.CalculatePnLPercentage(currentPnl, positionForSignal.Open.Price,
positionForSignal.Open.Quantity);
@@ -1020,7 +1016,7 @@ public abstract class TradingBotBase : ITradingBot
signal.Date,
Account.User,
Config.BotTradingBalance,
Config.TradingType == TradingType.BacktestFutures,
TradingBox.IsBacktestTrading(Config.TradingType),
lastPrice,
signalIdentifier: signal.Identifier,
initiatorIdentifier: Identifier,
@@ -1245,7 +1241,7 @@ public abstract class TradingBotBase : ITradingBot
// Update the last position closing time for cooldown period tracking
// Only update if position was actually filled
LastPositionClosingTime = Config.TradingType == TradingType.BacktestFutures
LastPositionClosingTime = TradingBox.IsBacktestTrading(Config.TradingType)
? currentCandle.Date
: DateTime.UtcNow;
}
@@ -1498,7 +1494,7 @@ public abstract class TradingBotBase : ITradingBot
signal,
currentPrice,
Config,
Config.TradingType == TradingType.BacktestFutures);
TradingBox.IsBacktestTrading(Config.TradingType));
if (signalValidationResult.Confidence == Confidence.None ||
signalValidationResult.Confidence == Confidence.Low ||
@@ -1859,7 +1855,7 @@ public abstract class TradingBotBase : ITradingBot
// Calculate cooldown end time based on last position closing time
var cooldownEndTime =
TradingBox.CalculateCooldownEndTime(LastPositionClosingTime.Value, Config.Timeframe, Config.CooldownPeriod);
var isInCooldown = (Config.TradingType == TradingType.BacktestFutures ? LastCandle.Date : DateTime.UtcNow) <
var isInCooldown = (TradingBox.IsBacktestTrading(Config.TradingType) ? LastCandle.Date : DateTime.UtcNow) <
cooldownEndTime;
if (isInCooldown)
@@ -1917,7 +1913,7 @@ public abstract class TradingBotBase : ITradingBot
private async Task NotifyAgentAndPlatformGrainAsync(NotificationEventType eventType,
Position position)
{
if (Config.TradingType == TradingType.BacktestFutures)
if (TradingBox.IsBacktestTrading(Config.TradingType))
{
return; // Skip notifications for backtest
}
@@ -2064,7 +2060,7 @@ public abstract class TradingBotBase : ITradingBot
protected virtual async Task LogInformationAsync(string message)
{
if (Config.TradingType == TradingType.BacktestFutures)
if (TradingBox.IsBacktestTrading(Config.TradingType))
return;
Logger.LogInformation(message);
@@ -2081,7 +2077,7 @@ public abstract class TradingBotBase : ITradingBot
protected virtual async Task LogWarningAsync(string message)
{
if (Config.TradingType == TradingType.BacktestFutures)
if (TradingBox.IsBacktestTrading(Config.TradingType))
return;
message = $"[{Config.Name}] {message}";
@@ -2098,7 +2094,7 @@ public abstract class TradingBotBase : ITradingBot
protected virtual async Task LogDebugAsync(string message)
{
if (Config.TradingType == TradingType.BacktestFutures)
if (TradingBox.IsBacktestTrading(Config.TradingType))
return;
Logger.LogDebug(message);