Add exception for webhook + add gracefull time before market decrease
This commit is contained in:
@@ -142,3 +142,4 @@ namespace Managing.Api.HealthChecks
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ public class TradingBotBase : ITradingBot
|
||||
public readonly ILogger<TradingBotBase> Logger;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private const int NEW_POSITION_GRACE_SECONDS = 45; // grace window before evaluating missing orders
|
||||
private const int CLOSE_POSITION_GRACE_MS = 20000; // grace window before closing position to allow broker processing (20 seconds)
|
||||
|
||||
public TradingBotConfig Config { get; set; }
|
||||
public Account Account { get; set; }
|
||||
@@ -1324,6 +1325,13 @@ public class TradingBotBase : ITradingBot
|
||||
isForBacktest: Config.IsForBacktest);
|
||||
try
|
||||
{
|
||||
// Grace period: give the broker time to process any ongoing close operations
|
||||
// Using ConfigureAwait(false) to ensure non-blocking operation
|
||||
if (!Config.IsForBacktest)
|
||||
{
|
||||
await Task.Delay(CLOSE_POSITION_GRACE_MS).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Position closedPosition = null;
|
||||
await ServiceScopeHelpers.WithScopedServices<IExchangeService, IAccountService, ITradingService>(
|
||||
_scopeFactory, async (exchangeService, accountService, tradingService) =>
|
||||
|
||||
@@ -22,11 +22,11 @@ public class WebhookService : IWebhookService
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_n8nWebhookUrl = _configuration["N8n:WebhookUrl"] ?? string.Empty;
|
||||
|
||||
|
||||
// Configure basic authentication if credentials are provided
|
||||
var username = _configuration["N8n:Username"];
|
||||
var password = _configuration["N8n:Password"];
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
|
||||
{
|
||||
var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}"));
|
||||
@@ -63,12 +63,12 @@ public class WebhookService : IWebhookService
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Failed to send webhook notification. Status: {response.StatusCode}");
|
||||
throw new Exception($"Failed to send webhook message. Status: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error sending webhook notification");
|
||||
SentrySdk.CaptureException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,12 +100,12 @@ public class WebhookService : IWebhookService
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning($"Failed to send webhook message. Status: {response.StatusCode}");
|
||||
throw new Exception($"Failed to send webhook message. Status: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error sending webhook message");
|
||||
SentrySdk.CaptureException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user