Adapt spot for recovery
This commit is contained in:
@@ -233,37 +233,6 @@ public class SpotBot : TradingBotBase, ITradingBot
|
||||
});
|
||||
}
|
||||
|
||||
protected override async Task VerifyAndUpdateBalanceAsync()
|
||||
{
|
||||
// Live trading: verify real USDC balance for spot trading
|
||||
if (Config.TradingType == TradingType.BacktestSpot) return;
|
||||
|
||||
try
|
||||
{
|
||||
var actualBalance = await ServiceScopeHelpers.WithScopedService<IExchangeService, decimal>(_scopeFactory,
|
||||
async exchangeService =>
|
||||
{
|
||||
var balances = await exchangeService.GetBalances(Account);
|
||||
var usdcBalance = balances.FirstOrDefault(b => b.TokenName?.ToUpper() == "USDC");
|
||||
return usdcBalance?.Amount ?? 0;
|
||||
});
|
||||
|
||||
if (actualBalance < Config.BotTradingBalance)
|
||||
{
|
||||
Logger.LogWarning(
|
||||
"Actual USDC balance ({ActualBalance:F2}) is less than configured balance ({ConfiguredBalance:F2}). Updating configuration.",
|
||||
actualBalance, Config.BotTradingBalance);
|
||||
|
||||
var newConfig = Config;
|
||||
newConfig.BotTradingBalance = actualBalance;
|
||||
await UpdateConfiguration(newConfig);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error verifying and updating balance");
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task SynchronizeWithBrokerPositions(Position internalPosition, Position positionForSignal,
|
||||
List<Position> brokerPositions)
|
||||
@@ -282,7 +251,31 @@ public class SpotBot : TradingBotBase, ITradingBot
|
||||
|
||||
if (tokenBalance != null && tokenBalance.Amount > 0)
|
||||
{
|
||||
// Token balance exists - verify position is filled
|
||||
// Verify that the token balance matches the position amount with 0.1% tolerance
|
||||
var positionQuantity = internalPosition.Open.Quantity;
|
||||
var tokenBalanceAmount = tokenBalance.Amount;
|
||||
|
||||
if (positionQuantity > 0)
|
||||
{
|
||||
var tolerance = positionQuantity * 0.001m; // 0.1% tolerance
|
||||
var difference = Math.Abs(tokenBalanceAmount - positionQuantity);
|
||||
|
||||
if (difference > tolerance)
|
||||
{
|
||||
await LogWarningAsync(
|
||||
$"⚠️ Token Balance Mismatch - Position Verification Failed\n" +
|
||||
$"Position: `{internalPosition.Identifier}`\n" +
|
||||
$"Position Quantity: `{positionQuantity:F5}`\n" +
|
||||
$"Token Balance: `{tokenBalanceAmount:F5}`\n" +
|
||||
$"Difference: `{difference:F5}`\n" +
|
||||
$"Tolerance (0.1%): `{tolerance:F5}`\n" +
|
||||
$"Token balance does not match position amount within tolerance\n" +
|
||||
$"Skipping position synchronization");
|
||||
return; // Skip processing if amounts don't match
|
||||
}
|
||||
}
|
||||
|
||||
// Token balance exists and matches position - verify position is filled
|
||||
var previousPositionStatus = internalPosition.Status;
|
||||
|
||||
// Position found on broker (token balance exists), means the position is filled
|
||||
|
||||
Reference in New Issue
Block a user